package com.css.txw.mhzc.service.impl;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.util.object.BeanUtils;
import com.css.ggzc.framework.session.SessionUtils;
import com.css.ggzc.framework.xxzx.api.XxzxApi;
import com.css.ggzc.framework.xxzx.pojo.XxReqVO;
import com.css.txw.mhzc.mapper.TxwMhzcQyrzsqjlbMapper;
import com.css.txw.mhzc.mapper.TxwMhzcQyxxbMapper;
import com.css.txw.mhzc.pojo.QyrzReqDTO;
import com.css.txw.mhzc.pojo.YhxxbDTO;
import com.css.txw.mhzc.pojo.domain.TxwMhzcQyrzsqjlbDO;
import com.css.txw.mhzc.pojo.domain.TxwMhzcQyxxbDO;
import com.css.txw.mhzc.pojo.domain.TxwMhzcYhqygxbDO;
import com.css.txw.mhzc.pojo.vo.CompanySpReqVO;
import com.css.txw.mhzc.pojo.vo.CompanyVO;
import com.css.txw.mhzc.pojo.vo.RegisterCompanyReqVO;
import com.css.txw.mhzc.service.TxwMhzcQyrzsqjlbService;
import com.css.txw.mhzc.service.TxwMhzcQyxxbService;
import com.css.txw.mhzc.service.TxwMhzcYhqygxbService;
import com.css.txw.sso.api.yhxx.SwitchSessionApi;
import com.css.txw.sso.pojo.yhxx.SwitchCompanyResDTO;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author AirlH
 * @description 针对表【txw_mhzc_qyxxb(企业信息表)】的数据库操作Service实现
 * @createDate 2025-09-11 14:10:56
 */
@Service
@Slf4j
public class TxwMhzcQyxxbServiceImpl extends ServiceImpl<TxwMhzcQyxxbMapper, TxwMhzcQyxxbDO>
		implements TxwMhzcQyxxbService {

	@Resource
	private TxwMhzcQyxxbMapper qyxxbMapper;

	@Resource
	private TxwMhzcQyrzsqjlbService qyrzsqjlbService;

	@Resource
	private TxwMhzcQyrzsqjlbMapper qyrzsqjlbMapper;

	@Resource
	private TxwMhzcYhqygxbService yhqygxbService;

	@Resource
	private SwitchSessionApi switchSessionApi;

	@Resource
	private XxzxApi xxzxApi;

	@Override
	public String register(RegisterCompanyReqVO reqDTO) {
		List<TxwMhzcQyxxbDO> byNsrsbh = qyxxbMapper.getByNsrsbh(reqDTO.getNsrsbh());
		if (!GyUtils.isNull(byNsrsbh) || byNsrsbh.size() > 0)
			throw new IllegalStateException("企业已认证");
		TxwMhzcQyrzsqjlbDO qyrzsqjlbDO = new TxwMhzcQyrzsqjlbDO();
		qyrzsqjlbDO.setUuid(IdUtil.fastSimpleUUID());
		qyrzsqjlbDO.setQymc(reqDTO.getQymc());
		qyrzsqjlbDO.setNsrsbh(reqDTO.getNsrsbh());
		qyrzsqjlbDO.setLrrq(new Date());
		qyrzsqjlbDO.setLrruuid(SessionUtils.getYhUuid());
		qyrzsqjlbDO.setSjhm1(SessionUtils.getSjhm());
		qyrzsqjlbDO.setSqsm(reqDTO.getSqsm());
		qyrzsqjlbDO.setYhUuid(SessionUtils.getYhUuid());
		qyrzsqjlbDO.setDlzh(SessionUtils.getDlzh());
		qyrzsqjlbDO.setZsxm1(SessionUtils.getZsxm());
		qyrzsqjlbMapper.insert(qyrzsqjlbDO);
		return "success";
	}

	@Transactional
	@Override
	public String qyRzSp(CompanySpReqVO reqVO) {
		LocalDateTime now = LocalDateTime.now();

		// 定义格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

		// 生成时间字符串
		String timeString = now.format(formatter);
		TxwMhzcQyrzsqjlbDO qyrzsqjlbDO = qyrzsqjlbService.getById(reqVO.getUuid());
		qyrzsqjlbDO.setShjg1(reqVO.getShjg1());
		qyrzsqjlbDO.setShyj1(reqVO.getShyj1());
		qyrzsqjlbDO.setShrsfid(SessionUtils.getYhUuid());
		qyrzsqjlbDO.setShsj(new Date());
		qyrzsqjlbService.updateById(qyrzsqjlbDO);
		if ("3".equals(reqVO.getShjg1())) {
			TxwMhzcQyxxbDO qyxxbDO = BeanUtils.toBean(qyrzsqjlbDO, TxwMhzcQyxxbDO.class);
			qyxxbDO.setQyuuid(IdUtil.fastSimpleUUID());
			qyxxbDO.setYxbz("Y");
			qyxxbDO.setLrrq(new Date());
			this.save(qyxxbDO);
			TxwMhzcYhqygxbDO yhqygxbDO = new TxwMhzcYhqygxbDO();
			yhqygxbDO.setQyuuid(qyxxbDO.getQyuuid());
			yhqygxbDO.setYhUuid(qyrzsqjlbDO.getLrruuid());
			yhqygxbDO.setUuid(IdUtil.fastSimpleUUID());
			yhqygxbService.save(yhqygxbDO);
			String lrruuid = qyrzsqjlbDO.getLrruuid();
			SwitchCompanyResDTO resDTO = new SwitchCompanyResDTO();
			resDTO.setYhuuid(lrruuid);
			resDTO.setQyuuid(qyxxbDO.getQyuuid());
			resDTO.setQymc(qyxxbDO.getQymc());
			resDTO.setNsrsbh(qyxxbDO.getNsrsbh());
			switchSessionApi.addCompany(resDTO);
			XxReqVO xxReqVO = new XxReqVO();
			Map<String, String> btMap = new HashMap<>();
			btMap.put("qymc", qyxxbDO.getQymc());
			Map<String, String> nrMap = new HashMap<>();
			nrMap.put("qymc", qyxxbDO.getQymc());
			nrMap.put("rq", timeString);
			xxReqVO.setMbuuid("mhzc_qyrz_sptg");
			xxReqVO.setXxbtMap(btMap);
			xxReqVO.setXxnrMap(nrMap);
			xxReqVO.setYhuuid(qyrzsqjlbDO.getLrruuid());
			xxzxApi.xtxxfb(xxReqVO);
		} else {
			XxReqVO xxReqVO = new XxReqVO();
			Map<String, String> btMap = new HashMap<>();
			btMap.put("qymc", qyrzsqjlbDO.getQymc());
			Map<String, String> nrMap = new HashMap<>();
			nrMap.put("qymc", qyrzsqjlbDO.getQymc());
			nrMap.put("rq", timeString);
			xxReqVO.setMbuuid("mhzc_qyrz_spsb");
			xxReqVO.setXxbtMap(btMap);
			xxReqVO.setXxnrMap(nrMap);
			xxReqVO.setYhuuid(qyrzsqjlbDO.getLrruuid());
			xxzxApi.xtxxfb(xxReqVO);
		}
		return "success";
	}

	@Override
	public YhxxbDTO intQyxxByDid(YhxxbDTO qyxx) {
		log.info("qyxx:{}",qyxx);
		if (GyUtils.isNull(qyxx)
				|| GyUtils.isNull(qyxx.getNsrsbh())
				|| GyUtils.isNull(qyxx.getQymc())
				||  GyUtils.isNull(qyxx.getYhUuid())) {
			return null;
		}
		String lrruuid = qyxx.getYhUuid();
		LocalDateTime now = LocalDateTime.now();
		// 定义格式
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		// 生成时间字符串
		String timeString = now.format(formatter);
		List<TxwMhzcQyxxbDO> qyxxList  = qyxxbMapper.getByNsrsbh(qyxx.getNsrsbh());
		TxwMhzcQyxxbDO qyxxbDO = null;
		boolean isNew = false;// 是否新增
		if (GyUtils.isNull(qyxxList)) {
			qyxxbDO = new TxwMhzcQyxxbDO();
			qyxxbDO.setQyuuid(IdUtil.fastSimpleUUID());
			qyxxbDO.setQymc(qyxx.getQymc());
			qyxxbDO.setNsrsbh(qyxx.getNsrsbh());
			qyxxbDO.setGxdtrzbz("N");
			qyxxbDO.setYwqdDm("did");
			qyxxbDO.setYxbz("Y");
			qyxxbDO.setLrrq(new Date());
			log.info("qyxxbDO:{}",qyxxbDO); 
			this.save(qyxxbDO);
			isNew = true;
		} 
		else {
			qyxxbDO = qyxxList.get(0);
		}
		if (isNew) {
			TxwMhzcYhqygxbDO yhqygxbDO = new TxwMhzcYhqygxbDO();
			yhqygxbDO.setQyuuid(qyxxbDO.getQyuuid());
			yhqygxbDO.setYhUuid(lrruuid);
			yhqygxbDO.setUuid(IdUtil.fastSimpleUUID());
			log.info("yhqygxbDO:{}",yhqygxbDO); 
			yhqygxbService.save(yhqygxbDO);
		}
		else {
			String qyuuid = qyxxbDO.getQyuuid();
			List<TxwMhzcYhqygxbDO> gxList = yhqygxbService.query(qyuuid,lrruuid);
			if (GyUtils.isNull(gxList)) {
				TxwMhzcYhqygxbDO yhqygxbDO = new TxwMhzcYhqygxbDO();
				yhqygxbDO.setQyuuid(qyxxbDO.getQyuuid());
				yhqygxbDO.setYhUuid(lrruuid);
				yhqygxbDO.setUuid(IdUtil.fastSimpleUUID());
				log.info("yhqygxbDO:{}",yhqygxbDO); 
				yhqygxbService.save(yhqygxbDO);
			}
		}
		
		// 切换token
		SwitchCompanyResDTO resDTO = new SwitchCompanyResDTO();
		resDTO.setYhuuid(lrruuid);
		resDTO.setQyuuid(qyxxbDO.getQyuuid());
		resDTO.setQymc(qyxxbDO.getQymc());
		resDTO.setNsrsbh(qyxxbDO.getNsrsbh());
		log.info("SwitchCompanyResDTO:{}",resDTO); 
		switchSessionApi.addCompany(resDTO);
//		XxReqVO xxReqVO = new XxReqVO();
//		Map<String, String> btMap = new HashMap<>();
//		btMap.put("qymc", qyxxbDO.getQymc());
//		Map<String, String> nrMap = new HashMap<>();
//		nrMap.put("qymc", qyxxbDO.getQymc());
//		nrMap.put("rq", timeString);
//		xxReqVO.setMbuuid("mhzc_qyrz_sptg");
//		xxReqVO.setXxbtMap(btMap);
//		xxReqVO.setXxnrMap(nrMap);
//		xxReqVO.setYhuuid(lrruuid);
//		log.info("xxReqVO:{}",xxReqVO); 
//		xxzxApi.xtxxfb(xxReqVO);
		return qyxx;
	}

	@Override
	public String qyrz(QyrzReqDTO reqDTO) {
		switch (reqDTO.getYwlxDm()) {
		case "01":
			qyxxbMapper.qyrz(reqDTO.getNsrsbh());
			break;
		case "02":
			qyxxbMapper.qyrz(reqDTO.getNsrsbh());
			break;
		}
		return "success";
	}

	@Override
	public List<CompanyVO> getQyxxListByYwlx(String ywlx) {
		List<TxwMhzcQyxxbDO> list = qyxxbMapper.getGxQy(ywlx);
		if (GyUtils.isNull(list))
			return null;
		return BeanUtils.toBean(list, CompanyVO.class);
	}

}
