package org.springblade.modules.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.modules.entity.*;
import org.springblade.modules.feign.*;
import org.springblade.modules.service.BzdzService;
import org.springblade.modules.util.GsonUtils;
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.*;

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

	private BzdzService bzdzService;

	private SydwFeign sydwFeign;

	private RhzfFwFeign rhzfFwFeign;

	private RhzfFwLsFeign rhzfFwLsFeign;

	private RhzfFzFeign rhzfFzFeign;

	private RhzfFzLsFeign rhzfFzLsFeign;

	private RhzfRyLsFeign rhzfRyLsFeign;

	private RhzfCzrkFeign rhzfCzrkFeign;

	private RhzfJwrkFeign rhzfJwrkFeign;

	private RhzfJzrkFeign rhzfJzrkFeign;

	private RhzfWlrkFeign rhzfWlrkFeign;

	private RhzfZzrkFeign rhzfZzrkFeign;

	private RhzfFwRyFeign rhzfFwRyFeign;

	/**
	 * 楼栋列表
	 */
	@GetMapping("/getLdList-pt")
	public R getLdListPt(@RequestParam Map<String, Object> params) {
		params.put("gajgdm", AuthUtil.getDeptId());
		List<LdBzdzVO> ldList = bzdzService.getJzwList(params);
		return R.data(GsonUtils.toGsonStr(ldList));
	}

	/**
	 * 楼栋详情
	 * params ldphxlh
	 */
	@GetMapping("/getLdDetail")
	public R getLdDetail(@RequestParam Map<String, Object> params) {
		// 查有几个单元
		List<DyVO> dyList = bzdzService.getDyList(params);
		for (DyVO dyVO : dyList) {
			params.put("dyh", dyVO.getDyh());
			List<LcVO> lcList = bzdzService.getLcList(params);
			for (LcVO lcVO : lcList) {
				params.put("lch", lcVO.getLch());
				List<FjVO> fjList = bzdzService.getFjList(params);
				lcVO.setFjList(fjList);
			}
			dyVO.setLcList(lcList);
		}
		return R.data(dyList);
	}

	/**
	 * 标准地址详情
	 */
	@GetMapping("Detail")
	public R bzdzDetail(@RequestParam Map<String, Object> params) {
		BzdzEntity bzdz = bzdzService.bzdzDetail(params);
		if (bzdz != null) {
			List<FzLsVO> list = bzdzService.selectFzLs(bzdz.getFzid());
			bzdz.setFzls(list);
		}
		return R.data(bzdz);
	}

	/**
	 * 房主修改
	 */
	@PostMapping("fzUpdate")
	public R fzUpdate(@RequestBody BzdzEntity bzdzEntity) {
		FwFzEntity fz = bzdzService.selectByFzId(bzdzEntity.getFzid());
		if (fz != null) {
			FzLsEntity fzLsEntity = new FzLsEntity();
			BeanUtils.copyProperties(fz, fzLsEntity);
			fz.setCreateUser(AuthUtil.getUserId());
			fz.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			fz.setCreateTime(new Date());
			rhzfFzLsFeign.fzlsSave(fzLsEntity);
		}
		bzdzEntity.setUpdateUser(AuthUtil.getUserId());
		bzdzEntity.setJz(Integer.valueOf(bzdzEntity.getSfzcjz()));
		return R.data(bzdzService.fzUpdate(bzdzEntity));
	}

	/**
	 * 房屋修改
	 */
	@PostMapping("fwUpdate")
	public R fwUpdate(@RequestBody FwInfoEntity fwInfoEntity) {
		FwLsEntity fwLsEntity = new FwLsEntity();
		fwLsEntity.setDzbm(fwInfoEntity.getDzbm());
		fwLsEntity.setFwLb(fwInfoEntity.getFwLb());
		fwLsEntity.setFwYt(fwInfoEntity.getFwYt());
		fwLsEntity.setFwXz(fwInfoEntity.getFwXz());
		fwLsEntity.setFwSyyt(fwInfoEntity.getFwSyyt());
		fwLsEntity.setFwMj(fwInfoEntity.getFwMj());
		fwLsEntity.setFwJs(fwInfoEntity.getFwJs());
		fwLsEntity.setCreateUser(AuthUtil.getUserId());
		fwLsEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		rhzfFwLsFeign.fwlsSave(fwLsEntity);
		fwInfoEntity.setUpdateUser(AuthUtil.getUserId());
		fwInfoEntity.setUpdateTime(new Date());
		rhzfFwFeign.fwUpdateById(fwInfoEntity);
		return R.data("修改成功");
	}

	/**
	 * 实有人口详情
	 */
	@GetMapping("syrkDetail")
	public R syrkDetail(String dzbm) {
		List<SyjzryVO> list = bzdzService.syrkDetail(dzbm);
		for (SyjzryVO syjzryVO:list) {
			String updateDeptName = bzdzService.selectDept(syjzryVO.getUpdateDept());
			syjzryVO.setUpdateDeptName(updateDeptName);
		}
		return R.data(list);
	}

	/**
	 * 实有人口历史详情
	 */
	@GetMapping("syrkLsDetail")
	public R syrkLsDetail(String dzbm) {
		List<SyjzryVO> list = bzdzService.syrkLsDetail(dzbm);
		return R.data(list);
	}

	/**
	 * 标准地址实有房屋 新增
	 */
	@PostMapping("/saveFwAndFz")
	public R saveFwAndFz(@RequestBody RhzfSaveVO rhzfSaveVO) {
		// 先保存房屋信息
		FwInfoEntity fwInfo = new FwInfoEntity();
		BeanUtils.copyProperties(rhzfSaveVO, fwInfo);
		// 根据dzbm 查rhzf_fw_info是否已有数据
		FwInfoEntity oldRecord = bzdzService.getFwIsExist(fwInfo);
		if (oldRecord != null) {
			return R.fail("这个地址已有房屋");
		} else {
			fwInfo.setDzType(0);
			fwInfo.setCreateUser(AuthUtil.getUserId());
			fwInfo.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			rhzfFwFeign.fwSave(fwInfo);
			if (rhzfSaveVO.getFzInfo() != null){
				BzdzEntity bzdz = bzdzService.selectFw(fwInfo.getDzbm());
				rhzfSaveVO.getFzInfo().setFwId(bzdz.getId());
				rhzfSaveVO.getFzInfo().setType(0);
				rhzfSaveVO.getFzInfo().setCreateUser(AuthUtil.getUserId());
				rhzfSaveVO.getFzInfo().setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
				rhzfFzFeign.fzSave(rhzfSaveVO.getFzInfo());
			}
		}
//		if (null == oldRecord) {
//			fwInfo.setTimes(1);
//			fwInfo.setMjId(mjId);
//			fwInfoService.save(fwInfo);
//		} else {
//			// 先更新
//			fwInfo.setId(oldRecord.getId());
//			fwInfo.setTimes(oldRecord.getTimes() + 1);
//			fwInfoService.updateById(fwInfo);
//		}
		// 保存房屋操作记录
//		RhzfFwEntity fwLogEntity = new RhzfFwEntity();
//		BeanUtils.copyProperties(fwInfo, fwLogEntity);
//		fwLogEntity.setMjId(mjId);
//		fwLogService.save(fwLogEntity);
		// 保存房主信息
//		if (null == oldRecord) {
//			rhzfSaveVO.getFzInfo().setFwId(fwInfo.getId());
//			rhzfSaveVO.getFzInfo().setMjId(mjId);
//			rhzfFzFeign.fzSave(rhzfSaveVO.getFzInfo());
//		}
		// 保存房主操作记录
//		RhzfFzEntity fzLogEntity = new RhzfFzEntity();
//		BeanUtils.copyProperties(rhzfSaveVO.getFzInfo(), fzLogEntity);
//		fzLogEntity.setMjId(mjId);
//		fzLogService.save(fzLogEntity);
		return R.data(fwInfo);
	}

	/**
	 * 实有人口新增
	 */
	@PostMapping("/saveRy")
	public R saveRy(@RequestBody SyjzryVO syjzryVO) {
		Long mjId = syjzryVO.getMjId();
		BzdzEntity bzdz = bzdzService.selectFw(syjzryVO.getDzbm());
		if (bzdz == null) {
			return R.fail("请先创建实有房屋");
		}
		//if (null != rhzfSaveVO.getSyjzryList() && !rhzfSaveVO.getSyjzryList().isEmpty()) {
			//for (SyjzryVO syjzryVO : rhzfSaveVO.getSyjzryList()) {
				if (syjzryVO.getIsDeleted() != 1 && !syjzryVO.getRyType().equals("境外人口") && StringUtils.isBlank(syjzryVO.getRySfzh())) {
					return R.fail("请填写身份证号");
				}
				Long ryId = 0L;
				int type = 1; // 1=新增 2=修改 3=删除
				if (syjzryVO.getRyType().equals("常住人口")) {
					// 常住人口
					RyCzrkEntity czrkEntity = new RyCzrkEntity();
					BeanUtils.copyProperties(syjzryVO, czrkEntity);
					czrkEntity.setFwId(bzdz.getId());
					czrkEntity.setDzbm(syjzryVO.getDzbm());
					czrkEntity.setCreateUser(AuthUtil.getUserId());
					czrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
					czrkEntity.setUpdateUser(AuthUtil.getUserId());
					BzdzEntity czrk = bzdzService.selectCzrkBySfzh(czrkEntity.getRySfzh());
					if (czrk == null) {
						czrkEntity.setMjId(mjId);
						rhzfCzrkFeign.czrkSave(czrkEntity);
					} else if (!czrkEntity.getDzbm().equals(czrk.getDzbm())) {
						RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
						ryLs.setFwId(czrk.getFwId());
						ryLs.setRyId(czrk.getId());
						ryLs.setDzbm(czrk.getDzbm());
						ryLs.setRyType(syjzryVO.getRyType());
						ryLs.setRyXm(syjzryVO.getRyXm());
						ryLs.setRyXb(syjzryVO.getRyXb());
						ryLs.setRySfzh(syjzryVO.getRySfzh());
						ryLs.setRyDh(syjzryVO.getRyDh());
						ryLs.setRyTsLb(syjzryVO.getRyTsLb());
						ryLs.setZxyy(syjzryVO.getZxyy());
						ryLs.setZxrq(syjzryVO.getZxrq());
						ryLs.setRyXs(syjzryVO.getRyXs());
						ryLs.setRyMz(syjzryVO.getRyYwm());
						ryLs.setRyZjhm(syjzryVO.getRyZjhm());
						ryLs.setCreateUser(AuthUtil.getUserId());
						ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
						ryLs.setUpdateUser(AuthUtil.getUserId());
						ryLs.setIsDeleted(1);
						rhzfRyLsFeign.rylsSave(ryLs);
						czrkEntity.setId(czrk.getId());
						rhzfCzrkFeign.czrkUpdate(czrkEntity);
					}
					ryId = bzdzService.selectCzrkId(czrkEntity);
				} else if (syjzryVO.getRyType().equals("境外人口")) {
					// 境外人口
					RyJwrkEntity jwrkEntity = new RyJwrkEntity();
					BeanUtils.copyProperties(syjzryVO, jwrkEntity);
					jwrkEntity.setFwId(bzdz.getId());
					jwrkEntity.setRyMz(syjzryVO.getRyYwm());
					jwrkEntity.setDzbm(syjzryVO.getDzbm());
					jwrkEntity.setCreateUser(AuthUtil.getUserId());
					jwrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
					jwrkEntity.setUpdateUser(AuthUtil.getUserId());
					BzdzEntity jwrk = bzdzService.selectJwrkByZjhm(jwrkEntity.getRyZjhm());
					if (jwrk == null) {
						jwrkEntity.setMjId(mjId);
						rhzfJwrkFeign.jwrkSave(jwrkEntity);
					} else if (!jwrkEntity.getDzbm().equals(jwrk.getDzbm())) {
						RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
						ryLs.setFwId(jwrk.getFwId());
						ryLs.setRyId(jwrk.getId());
						ryLs.setDzbm(jwrk.getDzbm());
						ryLs.setRyType(syjzryVO.getRyType());
						ryLs.setRyXm(syjzryVO.getRyXm());
						ryLs.setRyXb(syjzryVO.getRyXb());
						ryLs.setRySfzh(syjzryVO.getRySfzh());
						ryLs.setRyDh(syjzryVO.getRyDh());
						ryLs.setRyTsLb(syjzryVO.getRyTsLb());
						ryLs.setZxyy(syjzryVO.getZxyy());
						ryLs.setZxrq(syjzryVO.getZxrq());
						ryLs.setRyXs(syjzryVO.getRyXs());
						ryLs.setRyMz(syjzryVO.getRyYwm());
						ryLs.setRyZjhm(syjzryVO.getRyZjhm());
						ryLs.setCreateUser(AuthUtil.getUserId());
						ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
						ryLs.setUpdateUser(AuthUtil.getUserId());
						ryLs.setIsDeleted(1);
						rhzfRyLsFeign.rylsSave(ryLs);
						jwrkEntity.setId(jwrk.getId());
						rhzfJwrkFeign.jwrkUpdate(jwrkEntity);
					}
					ryId = bzdzService.selectJwrkId(jwrkEntity);
				} else if (syjzryVO.getRyType().equals("寄住人口")) {
					// 寄住人口
					RyJzrkEntity jzrkEntity = new RyJzrkEntity();
					BeanUtils.copyProperties(syjzryVO, jzrkEntity);
					jzrkEntity.setFwId(bzdz.getId());
					jzrkEntity.setDzbm(syjzryVO.getDzbm());
					jzrkEntity.setCreateUser(AuthUtil.getUserId());
					jzrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
					jzrkEntity.setUpdateUser(AuthUtil.getUserId());
					BzdzEntity jzrk = bzdzService.selectJzrkBySfzh(jzrkEntity.getRySfzh());
					if (jzrk == null) {
						jzrkEntity.setMjId(mjId);
						rhzfJzrkFeign.jzrkSave(jzrkEntity);
					} else if (!jzrkEntity.getDzbm().equals(jzrk.getDzbm())) {
						RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
						ryLs.setFwId(jzrk.getFwId());
						ryLs.setRyId(jzrk.getId());
						ryLs.setDzbm(jzrk.getDzbm());
						ryLs.setRyType(syjzryVO.getRyType());
						ryLs.setRyXm(syjzryVO.getRyXm());
						ryLs.setRyXb(syjzryVO.getRyXb());
						ryLs.setRySfzh(syjzryVO.getRySfzh());
						ryLs.setRyDh(syjzryVO.getRyDh());
						ryLs.setRyTsLb(syjzryVO.getRyTsLb());
						ryLs.setZxyy(syjzryVO.getZxyy());
						ryLs.setZxrq(syjzryVO.getZxrq());
						ryLs.setRyXs(syjzryVO.getRyXs());
						ryLs.setRyMz(syjzryVO.getRyYwm());
						ryLs.setRyZjhm(syjzryVO.getRyZjhm());
						ryLs.setCreateUser(AuthUtil.getUserId());
						ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
						ryLs.setUpdateUser(AuthUtil.getUserId());
						ryLs.setIsDeleted(1);
						rhzfRyLsFeign.rylsSave(ryLs);
						jzrkEntity.setId(jzrk.getId());
						rhzfJzrkFeign.jzrkUpdate(jzrkEntity);
					}
					ryId = bzdzService.selectJzrkId(jzrkEntity);
				} else if (syjzryVO.getRyType().equals("未落人口")) {
					// 未落常口
					RyWlrkEntity wlrkEntity = new RyWlrkEntity();
					BeanUtils.copyProperties(syjzryVO, wlrkEntity);
					wlrkEntity.setFwId(bzdz.getId());
					wlrkEntity.setDzbm(syjzryVO.getDzbm());
					wlrkEntity.setCreateUser(AuthUtil.getUserId());
					wlrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
					wlrkEntity.setUpdateUser(AuthUtil.getUserId());
					BzdzEntity wlrk = bzdzService.selectWlrkBySfzh(wlrkEntity.getRySfzh());
					if (wlrk == null) {
						wlrkEntity.setMjId(mjId);
						rhzfWlrkFeign.wlrkSave(wlrkEntity);
					} else if (!wlrkEntity.getDzbm().equals(wlrk.getDzbm())) {
						RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
						ryLs.setFwId(wlrk.getFwId());
						ryLs.setRyId(wlrk.getId());
						ryLs.setDzbm(wlrk.getDzbm());
						ryLs.setRyType(syjzryVO.getRyType());
						ryLs.setRyXm(syjzryVO.getRyXm());
						ryLs.setRyXb(syjzryVO.getRyXb());
						ryLs.setRySfzh(syjzryVO.getRySfzh());
						ryLs.setRyDh(syjzryVO.getRyDh());
						ryLs.setRyTsLb(syjzryVO.getRyTsLb());
						ryLs.setZxyy(syjzryVO.getZxyy());
						ryLs.setZxrq(syjzryVO.getZxrq());
						ryLs.setRyXs(syjzryVO.getRyXs());
						ryLs.setRyMz(syjzryVO.getRyYwm());
						ryLs.setRyZjhm(syjzryVO.getRyZjhm());
						ryLs.setCreateUser(AuthUtil.getUserId());
						ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
						ryLs.setUpdateUser(AuthUtil.getUserId());
						ryLs.setIsDeleted(1);
						rhzfRyLsFeign.rylsSave(ryLs);
						wlrkEntity.setId(wlrk.getId());
						rhzfWlrkFeign.wlrkUpdate(wlrkEntity);
					}
					ryId = bzdzService.selectWlrkId(wlrkEntity);
				} else if (syjzryVO.getRyType().equals("暂住人口")) {
					// 暂住人口
					RyZzrkEntity zzrkEntity = new RyZzrkEntity();
					BeanUtils.copyProperties(syjzryVO, zzrkEntity);
					zzrkEntity.setFwId(bzdz.getId());
					zzrkEntity.setDzbm(syjzryVO.getDzbm());
					zzrkEntity.setCreateUser(AuthUtil.getUserId());
					zzrkEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
					zzrkEntity.setUpdateUser(AuthUtil.getUserId());
					BzdzEntity zzrk = bzdzService.selectZzrkBySfzh(zzrkEntity.getRySfzh());
					if (zzrk == null) {
						zzrkEntity.setMjId(mjId);
						rhzfZzrkFeign.zzrkSave(zzrkEntity);
					} else if (!zzrkEntity.getDzbm().equals(zzrk.getDzbm())) {
						RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
						ryLs.setFwId(zzrk.getFwId());
						ryLs.setRyId(zzrk.getId());
						ryLs.setDzbm(zzrk.getDzbm());
						ryLs.setRyType(syjzryVO.getRyType());
						ryLs.setRyXm(syjzryVO.getRyXm());
						ryLs.setRyXb(syjzryVO.getRyXb());
						ryLs.setRySfzh(syjzryVO.getRySfzh());
						ryLs.setRyDh(syjzryVO.getRyDh());
						ryLs.setRyTsLb(syjzryVO.getRyTsLb());
						ryLs.setZxyy(syjzryVO.getZxyy());
						ryLs.setZxrq(syjzryVO.getZxrq());
						ryLs.setRyXs(syjzryVO.getRyXs());
						ryLs.setRyMz(syjzryVO.getRyYwm());
						ryLs.setRyZjhm(syjzryVO.getRyZjhm());
						ryLs.setCreateUser(AuthUtil.getUserId());
						ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
						ryLs.setUpdateUser(AuthUtil.getUserId());
						ryLs.setIsDeleted(1);
						rhzfRyLsFeign.rylsSave(ryLs);
						zzrkEntity.setId(zzrk.getId());
						rhzfZzrkFeign.zzrkUpdate(zzrkEntity);
					}
					ryId = bzdzService.selectZzrkId(zzrkEntity);
				}
				// 新增人员 需要 保存房屋_人员关系
				if (type == 1) {
					FwRyEntity fwRyEntity = new FwRyEntity();
					fwRyEntity.setFwId(bzdz.getId());
					fwRyEntity.setRyId(ryId);
					if (syjzryVO.getRyType().equals("常住人口")) {
						syjzryVO.setRyType("czrk");
					} else if (syjzryVO.getRyType().equals("境外人口")) {
						syjzryVO.setRyType("jwrk");
					} else if (syjzryVO.getRyType().equals("寄住人口")) {
						syjzryVO.setRyType("jzrk");
					} else if (syjzryVO.getRyType().equals("未落人口")) {
						syjzryVO.setRyType("wlrk");
					} else if (syjzryVO.getRyType().equals("暂住人口")) {
						syjzryVO.setRyType("zzrk");
					}
					fwRyEntity.setRyType(syjzryVO.getRyType());
					fwRyEntity.setDzbm(syjzryVO.getDzbm());
					fwRyEntity.setMjId(mjId);
					fwRyEntity.setCreateUser(AuthUtil.getUserId());
					fwRyEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
					fwRyEntity.setUpdateUser(AuthUtil.getUserId());
					bzdzService.updateFwRy(fwRyEntity);
					rhzfFwRyFeign.fwrySave(fwRyEntity);
				}
			//}
		//}
		return R.success("操作成功");
	}

	/**
	 * 实有人口注销
	 */
	@PostMapping("syrkzx")
	public R syrkzx(@RequestBody SyrkzxVO syrkzxVO) {
		RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
		ryLs.setFwId(syrkzxVO.getFwId());
		ryLs.setRyId(syrkzxVO.getId());
		ryLs.setDzbm(syrkzxVO.getDzbm());
		ryLs.setRyType(syrkzxVO.getRyType());
		ryLs.setRyXm(syrkzxVO.getRyXm());
		ryLs.setRyXb(syrkzxVO.getRyXb());
		ryLs.setRySfzh(syrkzxVO.getRySfzh());
		ryLs.setRyDh(syrkzxVO.getRyLxdh());
		ryLs.setRyTsLb(syrkzxVO.getRyTsLb());
		ryLs.setZxyy(syrkzxVO.getZxyy());
		ryLs.setZxrq(syrkzxVO.getZxrq());
		ryLs.setRyXs(syrkzxVO.getRyXs());
		ryLs.setRyMz(syrkzxVO.getRyYwm());
		ryLs.setRyZjhm(syrkzxVO.getRyZjhm());
		ryLs.setCreateUser(AuthUtil.getUserId());
		ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		ryLs.setUpdateUser(AuthUtil.getUserId());
		ryLs.setIsDeleted(1);
		syrkzxVO.setUpdateUser(AuthUtil.getUserId());
		syrkzxVO.setUpdateTime(new Date());
		if (syrkzxVO.getRyType().equals("常住人口")) {
			bzdzService.czrkZx(syrkzxVO);
			rhzfRyLsFeign.rylsSave(ryLs);
			bzdzService.updateFwRyzxById(syrkzxVO);
		} else if (syrkzxVO.getRyType().equals("境外人口")) {
			bzdzService.jwrkZx(syrkzxVO);
			rhzfRyLsFeign.rylsSave(ryLs);
			bzdzService.updateFwRyzxById(syrkzxVO);
		} else if (syrkzxVO.getRyType().equals("寄住人口")) {
			bzdzService.jzrkZx(syrkzxVO);
			rhzfRyLsFeign.rylsSave(ryLs);
			bzdzService.updateFwRyzxById(syrkzxVO);
		} else if (syrkzxVO.getRyType().equals("未落人口")) {
			bzdzService.wlrkZx(syrkzxVO);
			rhzfRyLsFeign.rylsSave(ryLs);
			bzdzService.updateFwRyzxById(syrkzxVO);
		} else if (syrkzxVO.getRyType().equals("暂住人口")) {
			bzdzService.zzrkZx(syrkzxVO);
			rhzfRyLsFeign.rylsSave(ryLs);
			bzdzService.updateFwRyzxById(syrkzxVO);
		}
		return R.success("注销成功");
	}

	/**
	 * 实有单位新增
	 */
	@PostMapping("sydwSave")
	public R sydwSave(@RequestBody SydwEntity sydwEntity) {
		SydwEntity sydw = bzdzService.selectSydwByDzbm(sydwEntity.getDzbm());
		if (sydw == null) {
			UserVO user = bzdzService.djxx(AuthUtil.getUserId());
			sydwEntity.setCreateUser(AuthUtil.getUserId());
			sydwEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			sydwEntity.setDjrxm(AuthUtil.getUserName());
			sydwEntity.setDjrjh(AuthUtil.getUserAccount());
			sydwEntity.setDjrsfzh(user.getSfzh());
			sydwEntity.setSspcs(user.getSspcs());
			sydwEntity.setSssq(user.getSssq());
			sydwFeign.sydwSave(sydwEntity);
			for (CyryEntity cyryEntity:sydwEntity.getCyryList()) {
				cyryEntity.setCreateUser(AuthUtil.getUserId());
				cyryEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
				cyryEntity.setDwdzbm(sydwEntity.getDzbm());
				sydwFeign.cyrySave(cyryEntity);
			}
		} else {
			return R.fail("该地址已有实有单位");
		}
		return R.success("新增成功");
	}

	/**
	 * 实有单位详情
	 */
	@GetMapping("sydwDetail")
	public R sydwDetail(SydwEntity sydwEntity) {
		SydwEntity sydw = bzdzService.sydwDetail(sydwEntity);
		if (sydw != null) {
			if (sydw.getDjrsfzh() != null) {
				sydw.setDjrsfzh(AESUtil.decrypt(sydw.getDjrsfzh(), "2SexR1rfjZ8CD3M6"));
			}
			List<CyryEntity> cyry = bzdzService.selectCyryByDzbm(sydw.getTyshxydm());
			sydw.setCyryList(cyry);
			List<CyryEntity> cyryls = bzdzService.selectCyrylsByDzbm(sydw.getTyshxydm());
			if (cyryls.size() > 0) {
				sydw.setCyrylsList(cyryls);
			}
		}
		return R.data(sydw);
	}

	/**
	 * 房主注销
	 */
	@PostMapping("fzzx")
	public R fzzx(@RequestBody FwFzEntity fwFzEntity) {
		fwFzEntity.setCreateUser(AuthUtil.getUserId());
		fwFzEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		fwFzEntity.setCreateTime(new Date());
		fwFzEntity.setUpdateTime(new Date());
		fwFzEntity.setIsDeleted(1);
		bzdzService.fzzxlsSave(fwFzEntity);
		return R.data(bzdzService.fzzx(fwFzEntity));
	}

	/**
	 * 新增房主
	 */
	@PostMapping("saveFz")
	public R saveFz(@RequestBody FwFzEntity fwFzEntity) {
		fwFzEntity.setCreateUser(AuthUtil.getUserId());
		fwFzEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		return R.data(rhzfFzFeign.fzSave(fwFzEntity));
	}

	/**
	 * 实有单位注销
	 */
	@PostMapping("sydwzx")
	public R sydwzx(@RequestBody SydwEntity sydw) {
		bzdzService.updateSydwzx(sydw, AuthUtil.getUserId());
		bzdzService.updateCyryzx(sydw, AuthUtil.getUserId());
		return R.data("操作成功");
	}

	/**
	 * 实有单位历史记录
	 */
	@GetMapping("sydwLsjl")
	public R sydwLsjl(String dzbm) {
		List<SydwEntity> list = bzdzService.sydwLsjl(dzbm);
		for (SydwEntity sydw:list) {
			List<CyryEntity> cyry = bzdzService.cyryLsjl(sydw.getDzbm());
			sydw.setCyryList(cyry);
		}
		return R.data(list);
	}

	/**
	 * 根据身份证号查询信息
	 */
	@GetMapping("selectXxBySfzh")
	public R selectXxBySfzh(String fzSfzh) {
		FwFzEntity ryxx = bzdzService.selectXxBySfzh(fzSfzh);
		Dept dept = bzdzService.selectDeptById(AuthUtil.getDeptId());
		BzdzEntity dzpcs = bzdzService.selectRyxxBySfzh(fzSfzh);
		if (dzpcs != null && !dept.getPcsId().equals(dzpcs.getPcsjgdm()) && dept.getSjId().contains(dzpcs.getDqm())) {
			ryxx.setRyType("寄住人口");
		}
		if (dzpcs != null && !dept.getSjId().contains(dzpcs.getDqm())) {
			ryxx.setRyType("暂住人口");
		}
		if (dzpcs != null && dept.getPcsId().equals(dzpcs.getPcsjgdm())) {
			ryxx.setRyType("常住人口");
		}
		return R.data(ryxx);
	}

	/**
	 * 根据当前部门来获取实有房屋地址
	 */
	@GetMapping("selectDzByDeptId")
	public R selectDzByDeptId() {
		List<DwdzVO> list = new ArrayList<>();
		List<String> dzbm = bzdzService.selectFwInfo(AuthUtil.getDeptId());
		for (String dz:dzbm) {
			DwdzVO dwdzVO = bzdzService.selectDwDz(dz);
			list.add(dwdzVO);
		}
		return R.data(list);
	}

	/**
	 * 查询实有人口是否在其他的派出所
	 */
	@GetMapping("selectSyrkBySfzh")
	public R selectSyrkBySfzh(BzdzEntity bzdzEntity) {
		BzdzEntity bzdz = bzdzService.selectSyrkBySfzh(bzdzEntity, AuthUtil.getDeptId());
		if (bzdz == null) {
			return R.data(null);
		}
		if (bzdz.getCreateDept().toString().equals(AuthUtil.getDeptId())) {
			return R.data("你已在其他房屋录入此人口，是否将他迁入到此房屋");
		}
		BzdzEntity pcs = bzdzService.selectPcsByCreateDept(bzdz.getCreateDept());
		return R.data(pcs);
	}

	/**
	 * 实有人口在其他的派出所的时候，将其状态改变
	 */
	@PostMapping("updateSyrkStatus")
	public R updateSyrkStatus(@RequestBody BzdzEntity bzdzEntity) {
		bzdzEntity.setUpdateDept(AuthUtil.getDeptId());
		if (bzdzEntity.getRyType().equals("常住人口")) {
			// 常住人口
			bzdzService.czrkUpdate(bzdzEntity);
		} else if (bzdzEntity.getRyType().equals("境外人口")) {
			// 境外人口
			bzdzService.jwrkUpdate(bzdzEntity);
		} else if (bzdzEntity.getRyType().equals("寄住人口")) {
			// 寄住人口
			bzdzService.jzrkUpdate(bzdzEntity);
		} else if (bzdzEntity.getRyType().equals("未落人口")) {
			// 未落常口
			bzdzService.wlrkUpdate(bzdzEntity);
		} else if (bzdzEntity.getRyType().equals("暂住人口")) {
			// 暂住人口
			bzdzService.zzrkUpdate(bzdzEntity);
		}
		return R.data("操作成功");
	}

	/**
	 * 原派出所确认将实有人口迁入到新派出所
	 */
	@PostMapping("updatePcs")
	public R updatePcs(@RequestBody SyjzryVO syjzryVO) {
		RhzfRyLsEntity ryLs = new RhzfRyLsEntity();
		ryLs.setFwId(syjzryVO.getFwId());
		ryLs.setRyId(syjzryVO.getId());
		ryLs.setDzbm(syjzryVO.getDzbm());
		ryLs.setRyType(syjzryVO.getRyType());
		ryLs.setRyXm(syjzryVO.getRyXm());
		ryLs.setRyXb(syjzryVO.getRyXb());
		ryLs.setRySfzh(syjzryVO.getRySfzh());
		ryLs.setRyDh(syjzryVO.getRyDh());
		ryLs.setRyTsLb(syjzryVO.getRyTsLb());
		ryLs.setZxyy(syjzryVO.getZxyy());
		ryLs.setZxrq(syjzryVO.getZxrq());
		ryLs.setRyXs(syjzryVO.getRyXs());
		ryLs.setRyMz(syjzryVO.getRyYwm());
		ryLs.setRyZjhm(syjzryVO.getRyZjhm());
		ryLs.setCreateUser(AuthUtil.getUserId());
		ryLs.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		ryLs.setUpdateUser(AuthUtil.getUserId());
		ryLs.setIsDeleted(1);
		rhzfRyLsFeign.rylsSave(ryLs);
		FwRyEntity fwRyEntity = new FwRyEntity();
		fwRyEntity.setFwId(syjzryVO.getNewFwId());
		fwRyEntity.setRyId(syjzryVO.getId());
		fwRyEntity.setDzbm(syjzryVO.getNewDzbm());
		fwRyEntity.setCreateUser(AuthUtil.getUserId());
		fwRyEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		fwRyEntity.setUpdateUser(AuthUtil.getUserId());
		bzdzService.updateFwRy(fwRyEntity);
		if (syjzryVO.getRyType().equals("常住人口")) {
			// 常住人口
			fwRyEntity.setRyType("czrk");
			rhzfFwRyFeign.fwrySave(fwRyEntity);
			bzdzService.updatePcsCzrk(syjzryVO);
		} else if (syjzryVO.getRyType().equals("境外人口")) {
			// 境外人口
			fwRyEntity.setRyType("jwrk");
			rhzfFwRyFeign.fwrySave(fwRyEntity);
			bzdzService.updatePcsJwrk(syjzryVO);
		} else if (syjzryVO.getRyType().equals("寄住人口")) {
			// 寄住人口
			fwRyEntity.setRyType("jzrk");
			rhzfFwRyFeign.fwrySave(fwRyEntity);
			bzdzService.updatePcsJzrk(syjzryVO);
		} else if (syjzryVO.getRyType().equals("未落人口")) {
			// 未落常口
			fwRyEntity.setRyType("wlrk");
			rhzfFwRyFeign.fwrySave(fwRyEntity);
			bzdzService.updatePcsWlrk(syjzryVO);
		} else if (syjzryVO.getRyType().equals("暂住人口")) {
			// 暂住人口
			fwRyEntity.setRyType("zzrk");
			rhzfFwRyFeign.fwrySave(fwRyEntity);
			bzdzService.updatePcsZzrk(syjzryVO);
		}
		return R.data("操作成功");
	}
}
