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.modules.entity.CyryEntity;
import org.springblade.modules.entity.SydwEntity;
import org.springblade.modules.service.CyryService;
import org.springblade.modules.service.SydwService;
import org.springblade.modules.utils.AESUtil;
import org.springblade.modules.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

	private SydwService sydwService;

	private CyryService cyryService;

	/**
	 * 实有单位列表
	 */
	@GetMapping("page")
	public R page(SydwEntity sydwEntity, Query query)  {
		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), sydwEntity));
	}

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

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

	/**
	 * 单位地址
	 */
	@GetMapping("dwDz")
	public R dwDz() {
		return R.data(sydwService.selectDwDz(AuthUtil.getDeptId()));
	}

	/**
	 * 实有单位修改
	 */
	@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());
		cyryEntity.setDjrjh(AuthUtil.getUserAccount());
		cyryEntity.setDjrxm(AuthUtil.getUserName());
		cyryEntity.setDjrsfzh(user.getSfzh());
		cyryEntity.setSjgxbs("0");
		cyryEntity.setCreateUser(AuthUtil.getUserId());
		cyryEntity.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
		CyryEntity cyry = cyryService.selectCyryBySfzh(cyryEntity.getGmsfhm());
		if (cyry != null) {
			return R.fail("此人员已有单位");
		}
		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));
	}
}
