package org.springblade.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.tool.api.R;
import org.springblade.estate.dto.RepairDTO;
import org.springblade.estate.entity.Repair;
import org.springblade.estate.feign.IRepairClient;
import org.springblade.estate.vo.RepairVO;
import org.springblade.person.entity.HouseUser;
import org.springblade.person.feign.IHouseUserClient;
import org.springblade.person.vo.HouseUserVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created By lidexi in 2021/5/10
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/app/estateRepair")
public class EstateRepairController {

	private final IRepairClient repairClient;
	private final IResidentialClient residentialClient;
	private final IHouseUserClient houseUserClient;

	@PostMapping(value = "/estateRepairList")
	public Map<String,Object> getEstateRepairList(@RequestBody RepairDTO estateRepairDto){
		Map<String,Object> map = new HashMap<>();
//        EstateRepairDto
		R<Page<Repair>> page = this.repairClient.page(estateRepairDto);
		if(page != null &&page.getData()!=null){
			map.put("estateRepairList",page.getData());
		}
		return map;
	}

	@PostMapping(value = "/addEstateRepair")
	public Map<String,Object> addEstateRepair(@RequestBody RepairDTO estateRepairDto){
		Map<String,Object> map = new HashMap<>();
		estateRepairDto.setRepairNo(generateRepairNO());
		Residential searchResidential = new Residential();
		Residential residentialById = residentialClient.detail(searchResidential);
		if(residentialById != null){
			estateRepairDto.setTenantId(residentialById.getTenantId());
		}else{
			map.put("add_result", "小区不存在");
		}
		HouseUser searchHouseUser = new HouseUser();
		HouseUserVO houseUserVO = houseUserClient.detail(searchHouseUser);
		if(houseUserVO != null){
			estateRepairDto.setReportor(houseUserVO.getName());
			estateRepairDto.setReportorPhone(houseUserVO.getPhone());
		}
		//处理图片
		Object pic = map.get("pic");
		if(pic != null){

		}
		boolean submit = this.repairClient.submit(estateRepairDto);
		if(submit){
			map.put("add_result",submit);
		}else{
			map.put("add_result", "后台程序异常");
		}
		return map;
	}

	@PostMapping(value = "/estateRepairDetail")
	public Map<String,Object> estateDetail(@RequestBody Repair estateRepair1){
		Map<String,Object> map = new HashMap<>();
		Repair searchRepair = new Repair();
		searchRepair.setId(estateRepair1.getId());
		RepairVO detail = this.repairClient.detail(searchRepair);
		if(detail != null){

			map.put("estateRepairDetail",detail);
		}else{
			map.put("estateRepairDetail", "报修记录不存在");
		}

		return map;
	}



	private static final AtomicInteger SEQ = new AtomicInteger();
	private static final DateTimeFormatter DF_FMT_PREFIX = DateTimeFormatter.ofPattern("yyMMddHHmmssSS");
	private static ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");

	public static String generateRepairNO(){
		LocalDateTime dateTime = LocalDateTime.now(ZONE_ID);
		if(SEQ.intValue()>9990){
			SEQ.getAndSet(1000);
		}

		return dateTime.format(DF_FMT_PREFIX)+SEQ.getAndIncrement();
	}


}
