package org.springblade.modules.sqjw.sydw.controller;

import lombok.AllArgsConstructor;
import org.springblade.common.utils.AESUtil;
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.modules.cdglinfos.service.IGlInfoService;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.sqjw.sydw.pojo.entity.CyryEntity;
import org.springblade.modules.sqjw.sydw.pojo.entity.SydwEntity;
import org.springblade.modules.sqjw.sydw.pojo.vo.SydwMenuVO;
import org.springblade.modules.sqjw.sydw.pojo.vo.UserVO;
import org.springblade.modules.sqjw.sydw.service.CyryService;
import org.springblade.modules.sqjw.sydw.service.SydwService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.DeptAllVO;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/sydw")
public class SydwController {

	private final SydwService sydwService;

	private final CyryService cyryService;

	private final ICsInfoService csInfoService;

	private final IUserService userService;

	private final IGlInfoService cdGlinfoService;


	/**
	 * 实有单位新增
	 */
	@PostMapping("/sydwSave")
	public R sydwSave(@RequestBody SydwEntity sydwEntity) {
		SydwEntity sydw = sydwService.selectSydwByDzbm(sydwEntity.getDzbm());
		if (sydw == null) {
			User user = userService.getById(AuthUtil.getUserId());
			DeptAllVO deptAll = csInfoService.getParentInfo(sydwEntity.getSssqBm());
			sydwEntity.setCreateUser(AuthUtil.getUserId());
			sydwEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
			sydwEntity.setDjrxm(AuthUtil.getUserName());
			sydwEntity.setDjrjh(AuthUtil.getUserAccount());
			sydwEntity.setDjrsfzh(user.getSfzh());
			sydwEntity.setSssq(deptAll.getSqMc());
			sydwEntity.setSspcsBm(deptAll.getPcsId());
			sydwEntity.setSspcs(deptAll.getPcsMc());
			sydwService.save(sydwEntity);
			if (null != sydwEntity.getCyryList() && sydwEntity.getCyryList().size() > 0) {
				for (CyryEntity cyryEntity : sydwEntity.getCyryList()) {
					cyryEntity.setCreateUser(AuthUtil.getUserId());
					cyryEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
					cyryEntity.setDwdzbm(sydwEntity.getDzbm());
					cyryEntity.setTyshxydm(sydwEntity.getTyshxydm());
					cyryService.save(cyryEntity);
				}
			}
		} else {
			return R.fail("该地址已有实有单位");
		}
		return R.success("新增成功");
	}

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

	/**
	 * 实有单位——菜单列表
	 */
	@GetMapping("/cdPage")
	public R cdPage(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		List<SydwMenuVO> sydwMenuList = cdGlinfoService.getSydwMenu("2", null);
		for (SydwMenuVO sydwMenu : sydwMenuList) {
			if (sydwMenu.getName().equals("娱乐服务场所")) {
				params.put("hylbDm", 1853611429036111902L);
				int num = sydwService.countByHylb(params);
				sydwMenu.setNum(num);
			} else if (sydwMenu.getName().equals("危险物品行业")) {
				params.put("hylbDm", 1853611429036111908L);
				int num = sydwService.countByHylb(params);
				sydwMenu.setNum(num);
			} else if (sydwMenu.getName().equals("新兴重点行业")) {
				params.put("hylbDm", 1853611429036111911L);
				int num = sydwService.countByHylb(params);
				sydwMenu.setNum(num);
			} else if (sydwMenu.getName().equals("九小场所管理")) {
				int num = csInfoService.countBySqBm(params);
				sydwMenu.setNum(num);
			} else if (sydwMenu.getName().equals("特种行业")) {
				params.put("hylbDm", 1853611429036111905L);
				int num = sydwService.countByHylb(params);
				sydwMenu.setNum(num);
			} else if (sydwMenu.getName().equals("非法集资信息采集")) {

			}
		}
		return R.data(sydwMenuList);
	}

	/**
	 * 实有单位——单位列表
	 */
	@GetMapping("/page")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
//		List<SydwEntity> list = sydwService.getMphxlhByLdphxlh(sydwEntity.getLdphxlh());
//		if (list.size() > 0) {
//			IPage<SydwEntity> page = new Page<>();
//			List<SydwEntity> fwList = new ArrayList<>();
//			for (SydwEntity fwInfoVO : list) {
//				SydwEntity fw = sydwService.selectFwInfo(sydwEntity, fwInfoVO.getDzbm());
//				if (fw != null) {
//					fwList.add(fw);
//				}
//			}
//			List<SydwEntity> 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);
//		}
		return R.data(sydwService.selectSydwPage(Condition.getPage(query), params));
	}

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

	/**
	 * 从业人员详情
	 */
	@GetMapping("/cyryDetail")
	public R cyryDetail(CyryEntity cyryEntity) {
		return R.data(sydwService.cyryDetail(cyryEntity));
	}

	/**
	 * 历史从业人员列表
	 */
	@GetMapping("/cyryHistory")
	public R cyryHistory(@RequestParam Map<String, Object> params) {
		List<CyryEntity> cyryls = sydwService.selectCyrylsByDzbm(params.get("dzbm").toString());
		return R.data(cyryls);
	}

	/**
	 * 单位地址
	 */
	@GetMapping("/dwDz")
	public R dwDz(String dzbm) {
		String deptId;
		if (AuthUtil.getDeptId().contains(",")) {
			deptId = String.valueOf(sydwService.selectParentIdByDeptId(AuthUtil.getDeptId().split(",")[0]));
		} else {
			deptId = AuthUtil.getDeptId();
		}
		return R.data(sydwService.selectDwDz(deptId, dzbm));
	}

	/**
	 * 实有单位修改
	 */
	@PostMapping("/update")
	public R update(@RequestBody SydwEntity sydwEntity) {
		return R.data(sydwService.updateById(sydwEntity));
	}

	/**
	 * 从业人员新增
	 */
	@PostMapping("/saveCyry")
	public R saveCyry(@RequestBody CyryEntity cyryEntity) {
		UserVO user = sydwService.djxx(AuthUtil.getUserId());
		if (user != null) {
			cyryEntity.setDjrsfzh(user.getSfzh());
		}
		cyryEntity.setDjrjh(AuthUtil.getUserAccount());
		cyryEntity.setDjrxm(AuthUtil.getUserName());
		cyryEntity.setSjgxbs("0");
		cyryEntity.setCreateUser(AuthUtil.getUserId());
		cyryEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
		CyryEntity cyry = cyryService.selectCyryBySfzh(cyryEntity.getGmsfhm());
		if (cyry != null) {
			cyryService.updateCyry(cyryEntity);
		}
		return R.data(cyryService.save(cyryEntity));
	}

	/**
	 * 从业人员修改
	 */
	@PostMapping("/updateCyry")
	public R updateCyry(@RequestBody CyryEntity cyryEntity) {
		// 获取当前日期和时间
		LocalDateTime now = LocalDateTime.now();
		// 定义日期时间格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
		// 将当前日期时间格式化为14位字符串
		String formattedDate = now.format(formatter);
		cyryEntity.setSjgxbs("1");
		cyryEntity.setSjgxsj(formattedDate);
		cyryEntity.setUpdateUser(AuthUtil.getUserId());
		return R.data(cyryService.updateById(cyryEntity));
	}

	/**
	 * 从业人员注销
	 */
	@PostMapping("/cyryzx")
	public R cyryzx(@RequestBody CyryEntity cyryEntity) {
		cyryEntity.setUpdateUser(AuthUtil.getUserId());
		return R.data(cyryService.updatezxById(cyryEntity));
	}

	/**
	 * 根据社会统一信用代码查询从业人员
	 */
	@GetMapping("/selectCyryByXydm")
	public R selectCyryByXydm(String tyshxydm) {
		return R.data(cyryService.selectCyryByXydm(tyshxydm));
	}

	/**
	 * 查询从业人员是否在其他单位
	 */
	@GetMapping("/selectCyryBySfzh")
	public R selectCyryBySfzh(String gmsfhm) {
		CyryEntity cyry = cyryService.selectCyryBySfzh(gmsfhm);
		if (cyry != null) {
			return R.data("此人员已在" + cyry.getDwmc() + "登记，是否将他移入到本单位");
		}
		return R.data(true);
	}
}
