package org.springblade.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.entity.*;
import org.springblade.modules.feign.*;
import org.springblade.modules.service.SyryService;
import org.springblade.modules.utils.AESUtil;
import org.springblade.modules.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("api")
public class SyryController {

	private SyryService syryService;

	private RhzfCzrkFeign rhzfCzrkFeign;

	private RhzfJwrkFeign rhzfJwrkFeign;

	private RhzfJzrkFeign rhzfJzrkFeign;

	private RhzfWlrkFeign rhzfWlrkFeign;

	private RhzfZzrkFeign rhzfZzrkFeign;

	private RhzfFwRyFeign rhzfFwRyFeign;

	private RhzfRyLsFeign rhzfRyLsFeign;

	/**
	 * 房屋人员
	 */
	@GetMapping("/getFwry")
	public R getFwry(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		List<SyryEntity> list = syryService.getMphxlhByLdphxlh(params);
		if (list.size() > 0) {
			IPage<SyryEntity> page = new Page<>();
			List<SyryEntity> fwList = new ArrayList<>();
			for (SyryEntity fwInfoVO:list) {
				SyryEntity fw = syryService.selectFwInfo(params, fwInfoVO.getDzbm());
				if (fw != null) {
					fwList.add(fw);
				}
			}
			List<SyryEntity> subList = fwList.stream().skip((query.getCurrent() - 1) * query.getSize()).limit(query.getSize()).collect(Collectors.toList());
			page.setRecords(subList).setTotal(fwList.size()).setSize(query.getSize()).setCurrent(query.getCurrent());
			return R.data(page);
		}
		IPage<SyryEntity> rySameList = syryService.getFwry(Condition.getPage(query), params);
//		for (SyryEntity syry:rySameList.getRecords()) {
//			if (syry.getRyTsLb() != null && !syry.getRyTsLb().equals("")) {
//				String tsrqlb = "";
//				String[] split = syry.getRyTsLb().split(",");
//				for (int i = 0; i < split.length; i++) {
//					String s = split[i];
//					String tsrq = syryService.selectDictBiz(s);
//					tsrqlb += tsrq + ",";
//				}
//				if (tsrqlb.contains(",")){
//					int index = tsrqlb.lastIndexOf(",");
//					tsrqlb = tsrqlb.substring(0, index);
//				}
//				syry.setRyTsLb(tsrqlb);
//			}
//		}
		return R.data(rySameList);
	}

	/**
	 * 房屋人员详情
	 */
	@GetMapping("Detail")
	public R fwryDetail(SyryEntity syryEntity) {
		if (syryEntity.getRyType().equals("常住人口")) {
			CzrkVO czrkVO = syryService.fwryCzrkDetail(syryEntity);
			if (czrkVO.getSfzh() != (null)) {
				czrkVO.setSfzh(AESUtil.decrypt(czrkVO.getSfzh(), "2SexR1rfjZ8CD3M6"));
			}
			return R.data(czrkVO);
		} else if (syryEntity.getRyType().equals("境外人口")) {
			JwrkVO jwrkVO = syryService.fwryJwrkDetail(syryEntity);
			if (jwrkVO.getSfzh() != (null)) {
				jwrkVO.setSfzh(AESUtil.decrypt(jwrkVO.getSfzh(), "2SexR1rfjZ8CD3M6"));
			}
			return R.data(jwrkVO);
		} else if (syryEntity.getRyType().equals("寄住人口")) {
			JzrkVO jzrkVO = syryService.fwryJzrkDetail(syryEntity);
			if (jzrkVO.getSfzh() != (null)) {
				jzrkVO.setSfzh(AESUtil.decrypt(jzrkVO.getSfzh(), "2SexR1rfjZ8CD3M6"));
			}
			return R.data(jzrkVO);
		} else if (syryEntity.getRyType().equals("未落人口")) {
			WlrkVO wlrkVO = syryService.fwryWlrkDetail(syryEntity);
			if (wlrkVO.getSfzh() != (null)) {
				wlrkVO.setSfzh(AESUtil.decrypt(wlrkVO.getSfzh(), "2SexR1rfjZ8CD3M6"));
			}
			return R.data(wlrkVO);
		} else if (syryEntity.getRyType().equals("暂住人口")) {
			ZzrkVO zzrkVO = syryService.fwryZzrkDetail(syryEntity);
			if (zzrkVO.getSfzh() != (null)) {
				zzrkVO.setSfzh(AESUtil.decrypt(zzrkVO.getSfzh(), "2SexR1rfjZ8CD3M6"));
			}
			return R.data(zzrkVO);
		}
		return R.data("查看详情失败");
	}

	/**
	 * 登记信息
	 */
	@GetMapping("djxx")
	public R djxx() {
		return R.data(syryService.djxx(AuthUtil.getUserId()));
	}

	/**
	 * 人员修改
	 */
	@PostMapping("updateRy")
	public R updateRy(@RequestBody SyjzryVO syjzryVO) {
		if (syjzryVO.getRyType().equals("常住人口")) {
			// 常住人口
			RyCzrkEntity czrkEntity = new RyCzrkEntity();
			BeanUtils.copyProperties(syjzryVO, czrkEntity);
			czrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			czrkEntity.setUpdateTime(new Date());
			rhzfCzrkFeign.czrkUpdate(czrkEntity);
		} else if (syjzryVO.getRyType().equals("境外人口")) {
			// 境外人口
			RyJwrkEntity jwrkEntity = new RyJwrkEntity();
			BeanUtils.copyProperties(syjzryVO, jwrkEntity);
			jwrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			rhzfJwrkFeign.jwrkUpdate(jwrkEntity);
		} else if (syjzryVO.getRyType().equals("寄住人口")) {
			// 寄住人口
			RyJzrkEntity jzrkEntity = new RyJzrkEntity();
			BeanUtils.copyProperties(syjzryVO, jzrkEntity);
			jzrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			rhzfJzrkFeign.jzrkUpdate(jzrkEntity);
		} else if (syjzryVO.getRyType().equals("未落人口")) {
			// 未落常口
			RyWlrkEntity wlrkEntity = new RyWlrkEntity();
			BeanUtils.copyProperties(syjzryVO, wlrkEntity);
			wlrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			rhzfWlrkFeign.wlrkUpdate(wlrkEntity);
		} else if (syjzryVO.getRyType().equals("暂住人口")) {
			// 暂住人口
			RyZzrkEntity zzrkEntity = new RyZzrkEntity();
			BeanUtils.copyProperties(syjzryVO, zzrkEntity);
			zzrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			rhzfZzrkFeign.zzrkUpdate(zzrkEntity);
		}
		syryService.updateFwId(syjzryVO.getFwId(), syjzryVO.getDzbm(), syjzryVO.getRyType(), syjzryVO.getId());
		RhzfRyLsEntity rhzfRyLs = new RhzfRyLsEntity();
		rhzfRyLs.setFwId(syjzryVO.getFwId());
		rhzfRyLs.setRyId(syjzryVO.getId());
		rhzfRyLs.setRyType(syjzryVO.getRyType());
		rhzfRyLs.setDzbm(syjzryVO.getDzbm());
		rhzfRyLs.setRyXm(syjzryVO.getRyXm());
		rhzfRyLs.setRyXb(syjzryVO.getRyXb());
		rhzfRyLs.setRySfzh(syjzryVO.getRySfzh());
		rhzfRyLs.setRyDh(syjzryVO.getRyDh());
		rhzfRyLs.setRyTsLb(syjzryVO.getRyTsLb());
		rhzfRyLs.setZxrq(syjzryVO.getZxrq());
		rhzfRyLs.setCreateUser(AuthUtil.getUserId());
		rhzfRyLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		rhzfRyLs.setUpdateUser(AuthUtil.getUserId());
		rhzfRyLsFeign.rylsSave(rhzfRyLs);
		return R.data("修改成功");
	}

	/**
	 * 从业人员详细信息
	 */
	@GetMapping("cyryXx")
	public R cyryXx(String gmsfhm) {
		CzrkVO czrk = syryService.cyryCzrkDetail(gmsfhm);
		if (czrk != null) {
			return R.data(czrk);
		}
		JwrkVO jwrk = syryService.cyryJwrkDetail(gmsfhm);
		if (jwrk != null) {
			return R.data(jwrk);
		}
		JzrkVO jzrk = syryService.cyryJzrkDetail(gmsfhm);
		if (jzrk != null) {
			return R.data(jzrk);
		}
		WlrkVO wlrk = syryService.cyryWlrkDetail(gmsfhm);
		if (wlrk != null) {
			return R.data(wlrk);
		}
		ZzrkVO zzrk = syryService.cyryZzrkDetail(gmsfhm);
		if (zzrk != null) {
			return R.data(zzrk);
		}
		CyryEntity cyry = syryService.cyryDetail(gmsfhm);
		if (cyry != null) {
			return R.data(cyry);
		}
		return R.data("暂无详细数据");
	}

	/**
	 * 历史从业人员详细信息
	 */
	@GetMapping("cyrylsXx")
	public R cyrylsXx(String gmsfhm, String dzbm) {
		CzrkVO czrk = syryService.cyrylsCzrkDetail(gmsfhm, dzbm);
		if (czrk != null) {
			return R.data(czrk);
		}
		JwrkVO jwrk = syryService.cyrylsJwrkDetail(gmsfhm, dzbm);
		if (jwrk != null) {
			return R.data(jwrk);
		}
		JzrkVO jzrk = syryService.cyrylsJzrkDetail(gmsfhm, dzbm);
		if (jzrk != null) {
			return R.data(jzrk);
		}
		WlrkVO wlrk = syryService.cyrylsWlrkDetail(gmsfhm, dzbm);
		if (wlrk != null) {
			return R.data(wlrk);
		}
		ZzrkVO zzrk = syryService.cyrylsZzrkDetail(gmsfhm, dzbm);
		if (zzrk != null) {
			return R.data(zzrk);
		}
		return R.data("暂无详细数据");
	}
}
