package org.springblade.swzdr.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.AsynTaskHelper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.swzdr.excel.RyInfoExcel;
import org.springblade.swzdr.mapper.RyInfoMapper;
import org.springblade.swzdr.pojo.entity.*;
import org.springblade.swzdr.pojo.vo.RyInfoVO;
import org.springblade.swzdr.pojo.vo.SwzdrDjVO;
import org.springblade.swzdr.pojo.vo.XfxxDjVO;
import org.springblade.swzdr.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 人员_基本信息 服务实现类
 *
 * @author BladeX
 * @since 2024-09-12
 */
@Slf4j
@Service
@AllArgsConstructor
public class RyInfoServiceImpl extends BaseServiceImpl<RyInfoMapper, RyInfoEntity> implements IRyInfoService {

	private final ExecutorService executor = Executors.newFixedThreadPool(5);

	private final IRyCarService carService;

	private final IRyFwService fwService;

	private final IRyGzdwService gzdwService;

	private final IRyLxdhService lxdhService;

	private final IRyShgxService shgxService;

	private final IRyXnsfService xnsfService;

	private final IRyYhkService yhkService;

	private final IRyXfxxService xfxxService;

	private final IRyXflsService xflsService;

	private final IXfqtService xfqtService;

	private final IRyXfqtService ryXfqtService;

	@Override
	public IPage<RyInfoVO> selectRyInfoPage(IPage<RyInfoVO> page, Map<String, String> params) {
		return page.setRecords(baseMapper.selectRyInfoPage(page, params));
	}

	@Override
	public List<RyInfoExcel> exportRyInfo(Wrapper<RyInfoEntity> queryWrapper) {
		List<RyInfoExcel> ryInfoList = baseMapper.exportRyInfo(queryWrapper);
		//ryInfoList.forEach(ryInfo -> {
		//	ryInfo.setTypeName(DictCache.getValue(DictEnum.YES_NO, RyInfo.getType()));
		//});
		return ryInfoList;
	}

	@Override
	public boolean swzdrDj(SwzdrDjVO swzdrDjVO) {
		// 保存车辆信息
		if (null != swzdrDjVO.getCarList() && !swzdrDjVO.getCarList().isEmpty()) {
			for (RyCarEntity carEntity : swzdrDjVO.getCarList()) {
				carEntity.setUserId(swzdrDjVO.getId());
			}
			carService.saveOrUpdateBatch(swzdrDjVO.getCarList());
		}
		// 保存房屋信息
		if (null != swzdrDjVO.getFwList() && !swzdrDjVO.getFwList().isEmpty()) {
			for (RyFwEntity fwEntity : swzdrDjVO.getFwList()) {
				fwEntity.setUserId(swzdrDjVO.getId());
			}
			fwService.saveOrUpdateBatch(swzdrDjVO.getFwList());
		}
		// 保存工作单位信息
		if (null != swzdrDjVO.getGzdwList() && !swzdrDjVO.getGzdwList().isEmpty()) {
			for (RyGzdwEntity gzdwEntity : swzdrDjVO.getGzdwList()) {
				gzdwEntity.setUserId(swzdrDjVO.getId());
			}
			gzdwService.saveOrUpdateBatch(swzdrDjVO.getGzdwList());
		}
		// 保存联系电话信息
		if (null != swzdrDjVO.getLxdhList() && !swzdrDjVO.getLxdhList().isEmpty()) {
			for (RyLxdhEntity lxdhEntity : swzdrDjVO.getLxdhList()) {
				lxdhEntity.setUserId(swzdrDjVO.getId());
			}
			lxdhService.saveOrUpdateBatch(swzdrDjVO.getLxdhList());
		}
		// 保存社会关系信息
		if (null != swzdrDjVO.getShgxList() && !swzdrDjVO.getShgxList().isEmpty()) {
			for (RyShgxEntity shgxEntity : swzdrDjVO.getShgxList()) {
				shgxEntity.setUserId(swzdrDjVO.getId());
			}
			shgxService.saveOrUpdateBatch(swzdrDjVO.getShgxList());
		}
		// 保存虚拟身份信息
		if (null != swzdrDjVO.getXnsfList() && !swzdrDjVO.getXnsfList().isEmpty()) {
			for (RyXnsfEntity xnsfEntity : swzdrDjVO.getXnsfList()) {
				xnsfEntity.setUserId(swzdrDjVO.getId());
			}
			xnsfService.saveOrUpdateBatch(swzdrDjVO.getXnsfList());
		}
		// 保存银行卡信息
		if (null != swzdrDjVO.getYhkList() && !swzdrDjVO.getYhkList().isEmpty()) {
			for (RyYhkEntity yhkEntity : swzdrDjVO.getYhkList()) {
				yhkEntity.setUserId(swzdrDjVO.getId());
			}
			yhkService.saveOrUpdateBatch(swzdrDjVO.getYhkList());
		}
		// 保存信访信息
		if (null != swzdrDjVO.getXfxxList() && !swzdrDjVO.getXfxxList().isEmpty()) {
			for (XfxxDjVO xfxxDjVO : swzdrDjVO.getXfxxList()) {
				RyXfxxEntity xfxxEntity = new RyXfxxEntity();
				BeanUtils.copyProperties(xfxxDjVO, xfxxEntity);
				xfxxEntity.setUserId(swzdrDjVO.getId());
				xfxxService.saveOrUpdate(xfxxEntity);
				// 保存信访历史
				if (null != xfxxDjVO.getXflsList() && !xfxxDjVO.getXflsList().isEmpty()) {
					for (RyXflsEntity xflsEntity : xfxxDjVO.getXflsList()) {
						xflsEntity.setXfId(xfxxEntity.getId());
						xflsService.saveOrUpdate(xflsEntity);
					}
				}
			}
		}
		// 保存信访群体信息
		if (null != swzdrDjVO.getXfqtList() && !swzdrDjVO.getXfqtList().isEmpty()) {
			// 信访群体暂时无法和信访信息关联，暂时不做
		}
		// 保存人员-信访群体关联信息
		if (null != swzdrDjVO.getRyXfqtList() && !swzdrDjVO.getRyXfqtList().isEmpty()) {
			// 信访群体暂时无法和信访信息关联，暂时不做
		}
		return true;
	}

	@Override
	public SwzdrDjVO getByUserId(RyInfoEntity ryInfoEntity) throws Exception {
		Long id = ryInfoEntity.getId();
		SwzdrDjVO swzdrDjVO = new SwzdrDjVO();
		BeanUtils.copyProperties(ryInfoEntity, swzdrDjVO);

		Long beginTime = System.currentTimeMillis();
		log.info("开始人员信息查询，开始时间：" + beginTime);

		// 根据userId查询车辆信息
		QueryWrapper<RyCarEntity> carQueryWrapper = new QueryWrapper<>();
		carQueryWrapper.eq("is_deleted", 0);
		carQueryWrapper.eq("user_id", id);
		Callable<List<RyCarEntity>> carCall = () -> carService.list(carQueryWrapper);
		// 根据userId查询房屋信息
		QueryWrapper<RyFwEntity> fwQueryWrapper = new QueryWrapper<>();
		fwQueryWrapper.eq("is_deleted", 0);
		fwQueryWrapper.eq("user_id", id);
		Callable<List<RyFwEntity>> fwCall = () -> fwService.list(fwQueryWrapper);
		// 根据userId查询工作单位信息
		QueryWrapper<RyGzdwEntity> gzdwQueryWrapper = new QueryWrapper<>();
		gzdwQueryWrapper.eq("is_deleted", 0);
		gzdwQueryWrapper.eq("user_id", id);
		Callable<List<RyGzdwEntity>> gzdwCall = () -> gzdwService.list(gzdwQueryWrapper);
		// 根据userId查询电话信息
		QueryWrapper<RyLxdhEntity> lxdhQueryWrapper = new QueryWrapper<>();
		lxdhQueryWrapper.eq("is_deleted", 0);
		lxdhQueryWrapper.eq("user_id", id);
		Callable<List<RyLxdhEntity>> lxdhCall = () -> lxdhService.list(lxdhQueryWrapper);
		// 根据userId查询社会关系信息
		QueryWrapper<RyShgxEntity> shgxQueryWrapper = new QueryWrapper<>();
		shgxQueryWrapper.eq("is_deleted", 0);
		shgxQueryWrapper.eq("user_id", id);
		Callable<List<RyShgxEntity>> shgxCall = () -> shgxService.list(shgxQueryWrapper);
		// 根据userId查询虚拟身份信息
		QueryWrapper<RyXnsfEntity> xnsfQueryWrapper = new QueryWrapper<>();
		xnsfQueryWrapper.eq("is_deleted", 0);
		xnsfQueryWrapper.eq("user_id", id);
		Callable<List<RyXnsfEntity>> xnsfCall = () -> xnsfService.list(xnsfQueryWrapper);
		// 根据userId查询银行卡信息
		QueryWrapper<RyYhkEntity> yhkQueryWrapper = new QueryWrapper<>();
		yhkQueryWrapper.eq("is_deleted", 0);
		yhkQueryWrapper.eq("user_id", id);
		Callable<List<RyYhkEntity>> yhkCall = () -> yhkService.list(yhkQueryWrapper);
		// 根据userId查询信访信息
		Callable<List<XfxxDjVO>> xfxxCall = () -> xfxxService.getByUserId(id);
		// 创建异步任务·
		AsynTaskHelper taskCallors = new AsynTaskHelper().setExecutorService(executor)
			.addTask("car", carCall).addTask("fw", fwCall).addTask("gzdw", gzdwCall)
			.addTask("lxdh", lxdhCall).addTask("shgx", shgxCall).addTask("xnsf", xnsfCall)
			.addTask("yhk", yhkCall).addTask("xfxx", xfxxCall).submit();

		List<RyCarEntity> carList = null;
		List<RyFwEntity> fwList = null;
		List<RyGzdwEntity> gzdwList = null;
		List<RyLxdhEntity> lxdhList = null;
		List<RyShgxEntity> shgxList = null;
		List<RyXnsfEntity> xnsfList = null;
		List<RyYhkEntity> yhkList = null;
		List<XfxxDjVO> xfxxList = null;

		long runTime;

		do {
			Map map = taskCallors.getResult();
			carList = (List<RyCarEntity>) map.get("car");
			fwList = (List<RyFwEntity>) map.get("fw");
			gzdwList = (List<RyGzdwEntity>) map.get("gzdw");
			lxdhList = (List<RyLxdhEntity>) map.get("lxdh");
			shgxList = (List<RyShgxEntity>) map.get("shgx");
			xnsfList = (List<RyXnsfEntity>) map.get("xnsf");
			yhkList = (List<RyYhkEntity>) map.get("yhk");
			xfxxList = (List<XfxxDjVO>) map.get("xfxx");
			runTime = System.currentTimeMillis() - beginTime;
		} while ((null == carList || null == fwList || null == gzdwList || null == lxdhList || null == shgxList
			|| null == xnsfList || null == yhkList || null == xfxxList) && runTime < 3000);
		log.info("结束分析结果查询，总耗时：" + (System.currentTimeMillis() - beginTime));

		swzdrDjVO.setCarList(carList);
		swzdrDjVO.setFwList(fwList);
		swzdrDjVO.setGzdwList(gzdwList);
		swzdrDjVO.setLxdhList(lxdhList);
		swzdrDjVO.setShgxList(shgxList);
		swzdrDjVO.setXnsfList(xnsfList);
		swzdrDjVO.setYhkList(yhkList);
		swzdrDjVO.setXfxxList(xfxxList);
		return swzdrDjVO;
	}

	@Override
	public List<Long> getSwzdrList() {
		return baseMapper.getSwzdrList();
	}
}
