package com.chinaweal.aiccs.aiccs.force.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Gzaiccode;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSWrit;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.business.entity.XrOpinion;
import com.chinaweal.aiccs.aiccs.business.entity.XrTaskList;
import com.chinaweal.aiccs.aiccs.business.entity.dto.ApprovalFormDto;
import com.chinaweal.aiccs.aiccs.business.service.IXrOpinionService;
import com.chinaweal.aiccs.aiccs.business.service.IXrTaskListService;
import com.chinaweal.aiccs.aiccs.force.entity.XrForceDeregister;
import com.chinaweal.aiccs.aiccs.force.entity.XrForceDeregisterRes;
import com.chinaweal.aiccs.aiccs.force.entity.XrForceDeregisterSpe;
import com.chinaweal.aiccs.aiccs.force.mapper.XrForceDeregisterMapper;
import com.chinaweal.aiccs.aiccs.force.service.IXrForceDeregisterResService;
import com.chinaweal.aiccs.aiccs.force.service.IXrForceDeregisterService;
import com.chinaweal.aiccs.aiccs.force.service.IXrForceDeregisterSpeService;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.aiccs.writs.entity.Tswrittemplate;
import com.chinaweal.aiccs.aiccs.writs.service.TswrittemplateService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.common.util.WorkNoUtil;
import com.chinaweal.aicorg.model.AICUser;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 拟强制注销公告表 服务实现类
 *
 * @author zhouxy
 * @since 2025/9/28
 */
@Service
public class XrForceDeregisterServiceImpl extends ServiceImpl<XrForceDeregisterMapper, XrForceDeregister> implements IXrForceDeregisterService {

    @Resource
    @Lazy
    private IXrForceDeregisterSpeService xrForceDeregisterSpeService;
    @Resource
    private IXrForceDeregisterResService xrForceDeregisterResService;
    @Resource
    private IXrTaskListService iXrTaskListService;
    @Resource
    private IXrOpinionService opinionService;
    @Resource
    private TswrittemplateService tswrittemplateService;
    @Resource
    private TRBaseCodeService trBaseCodeService;
    @Resource
    private TSWritService tsWritService;
    @Resource
    private IGzaiccodeService iGzaiccodeService;

    @Override
    public ApprovalFormDto getApprovalFormByBizSeqNo(String bizSeqNo, String forceSpeId) {
        ApprovalFormDto approvalFormDto = new ApprovalFormDto();
        XrForceDeregisterSpe xrForceDeregisterSpe = xrForceDeregisterSpeService.getById(forceSpeId);
        String forceType = xrForceDeregisterSpe.getForceType();
        XrForceDeregister force = this.getById(xrForceDeregisterSpe.getForceId());
        XrForceDeregisterRes forceRes = xrForceDeregisterResService.getById(xrForceDeregisterSpe.getForceResId());

        LambdaQueryWrapper<XrTaskList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XrTaskList::getBizSeqNo, bizSeqNo);
        XrTaskList one = iXrTaskListService.getOne(queryWrapper);
        List<XrOpinion> data = opinionService.list(new LambdaQueryWrapper<XrOpinion>()
                .eq(XrOpinion::getTaskListId,one.getTaskLikeId()).orderByDesc(XrOpinion::getUpdateTime));
        BeanUtils.copyProperties(xrForceDeregisterSpe, approvalFormDto);
        approvalFormDto.setAreaName(force.getJudauthCn());
        approvalFormDto.setUniscId(xrForceDeregisterSpe.getUniscId());
        approvalFormDto.setOpinion(data);
        //事实和理由
        approvalFormDto.setLostCreditExplain(forceRes.getForceRraCn());

        if(forceType.equals("1")){
            HashMap<String, Object> map = new HashMap<>();
            //拟强制注销直接显示拟强制注销公告
            map.put("areaName", force.getJudauthCn());
            map.put("content", force.getNoticeContent());
            map.put("writTime", force.getCreateTime());
            approvalFormDto.setRepairMap(map);
        } else if(forceType.equals("0")){
            HashMap<String, Object> decisionMap = new HashMap<>();
            HashMap<String, Object> forceMap = new HashMap<>();
            //强制注销决定书内容
            TSWrit writ = tsWritService.getOne(new LambdaQueryWrapper<TSWrit>()
                    .eq(TSWrit::getWritstype, WorkNoUtil.WORKNOTYPE_72)
                    .eq(TSWrit::getBizseq, bizSeqNo)
                    .eq(TSWrit::getPripid, xrForceDeregisterSpe.getPripId()));
            if (writ != null) {
                //公告内容
                decisionMap.put("writsNo", writ.getWritsno());
                decisionMap.put("content", writ.getContent());
                decisionMap.put("writsName", writ.getWritsname());
                decisionMap.put("areaName", force.getJudauthCn());
                decisionMap.put("writTime", writ.getCreatetime());
                approvalFormDto.setIncAbnDecisionMap(decisionMap);
            }

            //强制注销决定送达公告内容
            forceMap.put("content", xrForceDeregisterSpe.getForceNoticeContent());
            forceMap.put("areaName", force.getJudauthCn());
            forceMap.put("writTime", force.getNoticeTo());
            approvalFormDto.setRemAbnDecisionMap(forceMap);
        }
        return approvalFormDto;
    }

    @Override
    public String getDecisionContent(AICUser user, XrForceDeregisterSpe xrForceDeregisterSpe) {
        //查找对应的模板
        Tswrittemplate templateDB = tswrittemplateService.getOne(new LambdaQueryWrapper<Tswrittemplate>()
                .eq(Tswrittemplate::getWritsType, WorkNoUtil.WORKNOTYPE_72)
                .eq(Tswrittemplate::getIsTemplate, "1"));
        LambdaQueryWrapper<Gzaiccode> lqw = Wrappers.lambdaQuery();
        //查地市名称
        lqw.eq(Gzaiccode::getScountrycode, xrForceDeregisterSpe.getRegOrg().substring(0, 4) + "00");
        Gzaiccode gzaiccode = iGzaiccodeService.getOne(lqw);
        String city = gzaiccode.getScountryextcode();

        String content = templateDB.getContent();
        return content
                .replace("对上述强制注销决定", "\n        对上述强制注销决定")
                .replace("你（单位）如不服本决定", "\n        你（单位）如不服本决定")
                .replace("{{government}}", StringUtils.isNotEmpty(city) ? city + "市场监督管理局或" + city + "人民政府" : "   ")
                .replace("{{court}}", StringUtils.isNotEmpty(city) ? city + "人民法院" : "   ");
    }

    @Override
    public String getForceNoticeContent(AICUser user, XrForceDeregisterSpe xrForceDeregisterSpe) {
        //查找对应的模板
        Tswrittemplate templateDB = tswrittemplateService.getOne(new LambdaQueryWrapper<Tswrittemplate>()
                .eq(Tswrittemplate::getWritsType, WorkNoUtil.WORKNOTYPE_73)
                .eq(Tswrittemplate::getIsTemplate, "1"));
        //查对应的拟强制注销登记法定事由
        String forceRraCn =  "";
        if(StringUtils.isNotEmpty(xrForceDeregisterSpe.getForceRra())){
            TRBaseCode xrbasecode = trBaseCodeService.getOne(new LambdaQueryWrapper<TRBaseCode>()
                    .eq(TRBaseCode::getCodeid, "C13042")
                    .eq(TRBaseCode::getCode, xrForceDeregisterSpe.getForceRra()));
            forceRraCn = xrbasecode.getName();
        }

        List<XrForceDeregisterSpe> speList = xrForceDeregisterSpeService.list(new LambdaQueryWrapper<XrForceDeregisterSpe>()
                .eq(XrForceDeregisterSpe::getBizSeqNo, xrForceDeregisterSpe.getBizSeqNo()));
        //处理公司名称字段
        //取第一家公司的名称
        String entName = speList.get(0).getEntName();
        if(speList.size() > 1) {
            //取公司共计数量
            entName = entName + "等" + speList.size() + "户公司（另附附件）";
        }

        String content = templateDB.getContent();
        return content
                .replace("{{entName}}", entName)
                .replace("{{forceRraCn}}", forceRraCn)
                .replace("对上述强制注销决定", "\n        对上述强制注销决定")
                .replace("公告期内有异议的", "\n        公告期内有异议的")
                .replace("{{regOrgCn}}", xrForceDeregisterSpe.getRegOrgCn())
                .replace("联系人：", "\n        联系人：");
    }

}
