package org.springblade.app.controller;

import lombok.RequiredArgsConstructor;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.mp.support.BladePage;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
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.system.expand.feign.IConfClient;
import org.springframework.util.Assert;
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 java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
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 IConfClient confClient;

	@PostMapping(value = "/estateRepairList")
	public R<BladePage<RepairVO>> getEstateRepairList(@RequestBody RepairDTO repairDTO){
		R<BladePage<RepairVO>> page = this.repairClient.page(repairDTO);
		return page;
	}

	@PostMapping(value = "/addEstateRepair")
	public R addEstateRepair(@RequestBody RepairDTO repairDTO){
		Assert.hasText(repairDTO.getReportor(), "上报人姓名必传");
		Assert.hasText(repairDTO.getReportorPhone(), "上报人联系电话必传");
		Assert.notNull(repairDTO.getResidentialId(), "上报小区必传");
		repairDTO.setRepairNo(generateRepairNO());
		Residential residential = residentialClient.getById(repairDTO.getResidentialId());
		Assert.notNull(residential, "小区不存在");
		repairDTO.setTenantId(residential.getTenantId());
		if(StringUtil.isNotBlank(repairDTO.getPic())){
			List<String> picList = Func.toStrList(repairDTO.getPic());
			List<String> savePicList = new ArrayList<>();
			for(String pic : picList){
				savePicList.add(confClient.removePrefix(pic));
			}
			repairDTO.setPic(StringUtil.join(savePicList));
		}
		boolean submit = this.repairClient.submit(repairDTO);
		return R.status(submit);
	}

	@PostMapping(value = "/estateRepairDetail")
	public R<RepairVO> estateDetail(@RequestBody RepairDTO repairDTO){
		Repair searchRepair = new Repair();
		searchRepair.setId(repairDTO.getId());
		RepairVO repairVO = this.repairClient.detail(searchRepair);
		return R.data(repairVO);
	}



	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();
	}


}
