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

import cn.hutool.core.util.IdUtil;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSWrit;
import com.chinaweal.aiccs.aiccs.abnormal.service.CreatePdfService;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.auditing.entity.TSOpinion;
import com.chinaweal.aiccs.aiccs.auditing.service.TSOpinionService;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.service.TSTaskListService;
import com.chinaweal.aiccs.aiccs.inspect.service.TSWorkNoService;
import com.chinaweal.aiccs.aiccs.punishment.entity.CaseCreInformation;
import com.chinaweal.aiccs.aiccs.punishment.entity.CasePubNbaseinfo;
import com.chinaweal.aiccs.aiccs.punishment.entity.TBizRemPunishment;
import com.chinaweal.aiccs.aiccs.punishment.mapper.TBizRemPunishmentMapper;
import com.chinaweal.aiccs.aiccs.punishment.service.ICaseCreInformationService;
import com.chinaweal.aiccs.aiccs.punishment.service.ICasePubNbaseinfoService;
import com.chinaweal.aiccs.aiccs.punishment.service.ITBizRemPunishmentService;
import com.chinaweal.aiccs.aiccs.writs.entity.Tswrittemplate;
import com.chinaweal.aiccs.common.constant.BaseDataConstant;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.*;
import com.chinaweal.aiccs.crgs.punish.entity.TbIlcPunInfoQuery;
import com.chinaweal.aiccs.crgs.punish.service.TbIlcPunInfoQueryService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

import static com.chinaweal.aiccs.common.util.WorkNoUtil.WORKNOTYPE_65;

/**
 * <p>
 * 行政处罚修复业务表 服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2023-04-10
 */
@Service
public class TBizRemPunishmentServiceImpl extends BaseServiceImpl<TBizRemPunishmentMapper, TBizRemPunishment> implements ITBizRemPunishmentService {

    @Resource
    private EBaseinfoService eBaseInfoService;
    @Resource
    private TSTaskListService taskListService;
    @Resource
    private TbIlcPunInfoQueryService tbIlcPunInfoQueryService;
    @Resource
    private ICaseCreInformationService caseCreInformationService;
    @Resource
    private ICasePubNbaseinfoService casePubNbaseinfoService;
    @Autowired
    private TSWorkNoService workNoService;
    @Autowired
    private CreatePdfService createPdfService;
    @Autowired
    private TSWritService tsWritService;
    @Autowired
    private TSOpinionService tsOpinionService;
    @Resource
    private com.chinaweal.aiccs.aiccs.punishment.service.IQgCasePubNbaseinfoService qgCasePubNbaseinfoService;
    @Resource
    private com.chinaweal.aiccs.aiccs.punishment.service.ICrECaseService crECaseService;

    @Override
    public TBizRemPunishment saveTBizRemPunishment(TBizRemPunishment tBizRemPunishment) {
        save(tBizRemPunishment);
        return tBizRemPunishment;
    }

    @Override
    public void updateTBizRemPunishment(TBizRemPunishment tBizRemPunishment) {
        updateById(tBizRemPunishment);
    }

    @Override
    public IPage<TBizRemPunishment> listTBizRemPunishment(BaseQuery<TBizRemPunishment> query) {
        TBizRemPunishment entity = query.getEntity(TBizRemPunishment.class);
        Page<TBizRemPunishment> page = query.getPage();

        LambdaQueryWrapper<TBizRemPunishment> lambdaQueryWrapper = Wrappers.lambdaQuery();

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public Map<String, String> startTask(TBizRemPunishment tBizRemPunishment) {
        AICUser loginUser = SSOUtil.getUser();
        Map<String, String> resultMap = new HashMap<>();
        String entName = "";

        // 先查本省表
        TbIlcPunInfoQuery tbIlcPunInfoQuery = tbIlcPunInfoQueryService.findById(tBizRemPunishment.getPunishmentId());
        if (tbIlcPunInfoQuery != null) {
            // 本省案件
            tBizRemPunishment.setSourceType("本省");
            EBaseinfo eBaseInfo = eBaseInfoService.lambdaQuery().eq(EBaseinfo::getUniscid, tbIlcPunInfoQuery.getUniscid()).one();
            tBizRemPunishment.setPripid(eBaseInfo.getPripid());
            entName = eBaseInfo.getEntname();
        } else {
            // 查外省表
            com.chinaweal.aiccs.aiccs.punishment.entity.QgCasePubNbaseinfo qgCasePubNbaseinfo =
                    qgCasePubNbaseinfoService.findById(tBizRemPunishment.getPunishmentId());
            if (qgCasePubNbaseinfo != null) {
                // 外省案件
                tBizRemPunishment.setSourceType("外省");
                tBizRemPunishment.setPripid(qgCasePubNbaseinfo.getPripid());
                entName = qgCasePubNbaseinfo.getEntname();
            } else {
                throw new RuntimeException("未找到对应的行政处罚记录");
            }
        }

        String bizSeq = IdUtil.simpleUUID();
        tBizRemPunishment.setId(bizSeq);
        tBizRemPunishment.setUserId(loginUser.getPrimaryKey());
        tBizRemPunishment.setStatus("0");
        tBizRemPunishment.setLinkType("1");
        tBizRemPunishment.setBizSeq(bizSeq);
        this.save(tBizRemPunishment);

        // 启动工作流
        TSTaskList tsTaskList = new TSTaskList();
        tsTaskList.setBizseqid(bizSeq);
        tsTaskList.setBustype(BaseDataConstant.BUSINESS_TYPE_REM_PUNISH);
        tsTaskList.setBusname(entName);
        //新待办 设置待办信息
        tsTaskList = HandleWorkFlowUtil.createTaskList(tsTaskList, loginUser);
        //开始工作流
        taskListService.startWorkFlow(tsTaskList, loginUser, FlowUtil.REM_PUNISH_WORKFLOW);

        // 返回结果
        resultMap.put("taskId", tsTaskList.getTasklistid());
        resultMap.put("bizSeq", bizSeq);
        resultMap.put("workflowId", tsTaskList.getWorkflowid());
        return resultMap;
    }

    @Override
    public void submitTask(TBizRemPunishment tBizRemPunishment, TSTaskList tsTaskList, Tswrittemplate tswrittemplate) {
        AICUser loginUser = SSOUtil.getUser();
        //当前环节
        String linkType = tBizRemPunishment.getLinkType();
        String isAccept = tBizRemPunishment.getIsAccept();
        String isAgreeRepair = tBizRemPunishment.getIsAgreeRepair();
        String nextNodeID = tBizRemPunishment.getNextNodeId();
        String nextPerformerids = StringUtils.isEmpty(tBizRemPunishment.getNextPerformerids()) ?
                loginUser.getPrimaryKey() : tBizRemPunishment.getNextPerformerids();    //如果没有下一步操作人则存当前用户
        String nextPerformerName = StringUtils.isEmpty(tBizRemPunishment.getNextPerformerName()) ?
                loginUser.getName() : tBizRemPunishment.getNextPerformerName();

        //封装意见表
        String opinionContent = tBizRemPunishment.getOpinion();
        TSOpinion opinion = createOpinion(opinionContent, tsTaskList, loginUser);

        //外网申请的且不受理的情况下，意见值给到回退建议
        if(StringUtils.equals(tsTaskList.getOrigin(),"1")
                && StringUtils.equals(isAccept,"0")){
            tBizRemPunishment.setBackProposal(opinionContent);
        }

        //不予受理的情况直接可以结束流程
        if(StringUtils.equals(isAccept,"0")){
            tBizRemPunishment.setStatus("3");
            nextNodeID = "disagreeRemPunish";
            tBizRemPunishment.setLinkType("3"); //设置下一环节
            opinion.setOpiniontype("0")
                    .setIsagree("2");
        } else {        //予以受理的情况下再看下一节点
            //根据当前下一步节点判断
            if("handleRemPunish".equals(nextNodeID)){  //下一步是审核
                tBizRemPunishment.setLinkType("0"); //设置下一环节
                opinion.setOpiniontype("0")
                        .setIsagree("1");
            } else if("approveRemPunish".equals(nextNodeID)){  //下一步是审批
                tBizRemPunishment.setLinkType("2"); //设置下一环节
                opinion.setOpiniontype("1")
                        .setIsagree("1");
            } else if("endRemPunish".equals(nextNodeID)){  //下一步是结束流程
                opinion.setOpiniontype("2");
                //最后一步才设置业务状态为通过或者不通过
                if(StringUtils.equals(isAgreeRepair,"0")){     //不予修复
                    //文书模板数据转为文书正式数据
                    this.handleTsWrit(tBizRemPunishment, tswrittemplate);
                    tBizRemPunishment.setStatus("3"); //设置业务状态为不通过
                    opinion.setIsagree("0");
                } else {     //准予修复
                    //文书模板数据转为文书正式数据
                    this.handleTsWrit(tBizRemPunishment, tswrittemplate);
                    //根据数据来源判断处理方式
                    if ("外省".equals(tBizRemPunishment.getSourceType())) {
                        // 外省：更新QG表 + 插入CR_E_CASE
                        this.insertCrECase(tBizRemPunishment);
                    } else {
                        // 本省：插入本地修复信息
                        this.insertCaseCreInformation(tBizRemPunishment);
                    }
                    tBizRemPunishment.setStatus("2"); //设置业务状态为通过
                    opinion.setIsagree("1");
                }
                tBizRemPunishment.setLinkType("3"); //设置下一环节为结束
            } else if("returnRemPunish".equals(nextNodeID)){  //下一步是回退上一步
                opinion.setIsagree("2");
                if(StringUtils.equals(linkType,"0")){   //审核回退到受理
                    tBizRemPunishment.setLinkType("1"); //设置下一环节
                    opinion.setOpiniontype("1");
                } else if(StringUtils.equals(linkType,"2")){   //审批回退到审核
                    tBizRemPunishment.setLinkType("0"); //设置下一环节
                    opinion.setOpiniontype("2");
                } else {    //无法回退
                    throw new RuntimeException("无法回退到上一步");
                }
                //通过意见表找出上一个节点的人
                TSOpinion tsOpinion = tsOpinionService.getOne(new LambdaQueryWrapper<TSOpinion>()
                        .orderByDesc(TSOpinion::getHandledate) // 按 handledate 降序排列
                        .last("LIMIT 1"));
                if(tsOpinion != null){
                    //取上一个节点的人
                    nextPerformerids  = tsOpinion.getHandlerid();
                    nextPerformerName = tsOpinion.getHandler();
                }
            } else {    //流程无法找到
                throw new RuntimeException("流程无法找到");
            }
        }
        //修改业务表
        this.updateById(tBizRemPunishment);

        // 流程推进
        taskListService.updateWorkFlow(loginUser, opinion, tsTaskList,
                nextNodeID, new String[]{ nextPerformerids }, new String[]{ nextPerformerName });
    }

    /**
     * 行政处罚信用修复信息插入数据
     * @param tBizRemPunishment
     */
    private void insertCaseCreInformation(TBizRemPunishment tBizRemPunishment) {
        CaseCreInformation caseCreInformation = new CaseCreInformation();
        EBaseinfo eBaseinfo = eBaseInfoService.getOneByPripid(tBizRemPunishment.getPripid());
        //查询行政处罚基本信息
        CasePubNbaseinfo casePubNbaseinfo = casePubNbaseinfoService.getById(tBizRemPunishment.getPunishmentId());

        caseCreInformation.setCasecreid(IdUtil.simpleUUID())
                .setPripid(tBizRemPunishment.getPripid())
                .setEntname(eBaseinfo.getEntname())
                .setUniscid(eBaseinfo.getUniscid())
                .setCaseid(casePubNbaseinfo.getCaseid());
        // 将LocalDateTime转换为Date
        LocalDateTime localDateTimeRepairDate = tBizRemPunishment.getRepairDate();
        Date repairDate = Date.from(localDateTimeRepairDate.atZone(ZoneId.systemDefault()).toInstant());
        LocalDate localDate = tBizRemPunishment.getApplyDate();
        Date applyDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        caseCreInformation.setRepairdate(applyDate)
                .setRepauth(tBizRemPunishment.getRepairOrg())
                .setRepauthCn(tBizRemPunishment.getRepairOrgCn())
                .setRepdate(repairDate)
                .setDatasource("wA")
                .setRepairno(tBizRemPunishment.getRepairNo());

        //插入数据
        caseCreInformationService.save(caseCreInformation);
    }

    @Override
    public List<TBizRemPunishment> selectbizRembizseq(String bizseqid) {
        return this.list(new LambdaQueryWrapper<TBizRemPunishment>()
                .eq(TBizRemPunishment::getBizSeq, bizseqid));
    }

    @Override
    public void submitTaskN(TBizRemPunishment tBizRemPunishment, TSTaskList tsTaskList, Tswrittemplate tswrittemplate,
                            com.chinaweal.aiccs.aiccs.punishment.service.IQgCasePubNbaseinfoService qgCasePubNbaseinfoService,
                            com.chinaweal.aiccs.aiccs.punishment.service.ICrECaseService crECaseService) {
        AICUser loginUser = SSOUtil.getUser();
        //当前环节
        String linkType = tBizRemPunishment.getLinkType();
        String isAccept = tBizRemPunishment.getIsAccept();
        String isAgreeRepair = tBizRemPunishment.getIsAgreeRepair();
        String nextNodeID = tBizRemPunishment.getNextNodeId();
        String nextPerformerids = StringUtils.isEmpty(tBizRemPunishment.getNextPerformerids()) ?
                loginUser.getPrimaryKey() : tBizRemPunishment.getNextPerformerids();    //如果没有下一步操作人则存当前用户
        String nextPerformerName = StringUtils.isEmpty(tBizRemPunishment.getNextPerformerName()) ?
                loginUser.getName() : tBizRemPunishment.getNextPerformerName();

        //封装意见表
        String opinionContent = tBizRemPunishment.getOpinion();
        TSOpinion opinion = createOpinion(opinionContent, tsTaskList, loginUser);

        //外网申请的且不受理的情况下，意见值给到回退建议
        if(StringUtils.equals(tsTaskList.getOrigin(),"1")
                && StringUtils.equals(isAccept,"0")){
            tBizRemPunishment.setBackProposal(opinionContent);
        }

        //不予受理的情况直接可以结束流程
        if(StringUtils.equals(isAccept,"0")){
            tBizRemPunishment.setStatus("3");
            nextNodeID = "disagreeRemPunish";
            tBizRemPunishment.setLinkType("3"); //设置下一环节
            opinion.setOpiniontype("0")
                    .setIsagree("2");
        } else {        //予以受理的情况下再看下一节点
            //根据当前下一步节点判断
            if("handleRemPunish".equals(nextNodeID)){  //下一步是审核
                tBizRemPunishment.setLinkType("0"); //设置下一环节
                opinion.setOpiniontype("0")
                        .setIsagree("1");
            } else if("approveRemPunish".equals(nextNodeID)){  //下一步是审批
                tBizRemPunishment.setLinkType("2"); //设置下一环节
                opinion.setOpiniontype("1")
                        .setIsagree("1");
            } else if("endRemPunish".equals(nextNodeID)){  //下一步是结束流程
                opinion.setOpiniontype("2");
                //最后一步才设置业务状态为通过或者不通过
                if(StringUtils.equals(isAgreeRepair,"0")){     //不予修复
                    //文书模板数据转为文书正式数据
                    this.handleTsWrit(tBizRemPunishment, tswrittemplate);
                    tBizRemPunishment.setStatus("3"); //设置业务状态为不通过
                    opinion.setIsagree("0");
                } else {     //准予修复
                    //文书模板数据转为文书正式数据
                    this.handleTsWrit(tBizRemPunishment, tswrittemplate);
                    //外省行政处罚修复：修改跨省表 + 插入跨省修复案例表
                    this.insertCrECase(tBizRemPunishment);
                    tBizRemPunishment.setStatus("2"); //设置业务状态为通过
                    opinion.setIsagree("1");
                }
                tBizRemPunishment.setLinkType("3"); //设置下一环节为结束
            } else if("returnRemPunish".equals(nextNodeID)){  //下一步是回退上一步
                opinion.setIsagree("2");
                if(StringUtils.equals(linkType,"0")){   //审核回退到受理
                    tBizRemPunishment.setLinkType("1"); //设置下一环节
                    opinion.setOpiniontype("1");
                } else if(StringUtils.equals(linkType,"2")){   //审批回退到审核
                    tBizRemPunishment.setLinkType("0"); //设置下一环节
                    opinion.setOpiniontype("2");
                } else {    //无法回退
                    throw new RuntimeException("无法回退到上一步");
                }
                //通过意见表找出上一个节点的人
                TSOpinion tsOpinion = tsOpinionService.getOne(new LambdaQueryWrapper<TSOpinion>()
                        .orderByDesc(TSOpinion::getHandledate) // 按 handledate 降序排列
                        .last("LIMIT 1"));
                if(tsOpinion != null){
                    //取上一个节点的人
                    nextPerformerids  = tsOpinion.getHandlerid();
                    nextPerformerName = tsOpinion.getHandler();
                }
            } else {    //流程无法找到
                throw new RuntimeException("流程无法找到");
            }
        }
        //修改业务表
        this.updateById(tBizRemPunishment);

        // 流程推进
        taskListService.updateWorkFlow(loginUser, opinion, tsTaskList,
                nextNodeID, new String[]{ nextPerformerids }, new String[]{ nextPerformerName });
    }

    /**
     * 外省行政处罚修复 - 插入跨省修复案例表并更新跨省处罚表
     * @param tBizRemPunishment 业务信息
     */
    private void insertCrECase(TBizRemPunishment tBizRemPunishment) {
        // 1. 更新跨省处罚表：opflag='D', pubdeadline=审批日期, S_EXT_DATETIME=审批日期
        LocalDateTime now = LocalDateTime.now();
        qgCasePubNbaseinfoService.updateForRepair(tBizRemPunishment.getPunishmentId(), now.toLocalDate());

        // 2. 获取跨省处罚信息用于插入CR_E_CASE
        com.chinaweal.aiccs.aiccs.punishment.entity.QgCasePubNbaseinfo qgCasePubNbaseinfo = 
                qgCasePubNbaseinfoService.findById(tBizRemPunishment.getPunishmentId());

        // 3. 插入跨省修复案例表
        com.chinaweal.aiccs.aiccs.punishment.entity.CrECase crECase = new com.chinaweal.aiccs.aiccs.punishment.entity.CrECase();
        crECase.setCasecreid(IdUtil.simpleUUID())
                .setPripid(tBizRemPunishment.getPripid())
                .setEntname(qgCasePubNbaseinfo != null ? qgCasePubNbaseinfo.getEntname() : "")
                .setUniscid(qgCasePubNbaseinfo != null ? qgCasePubNbaseinfo.getUniscid() : "")
                .setCaseid(tBizRemPunishment.getPunishmentId());

        // 修复日期
        LocalDateTime localDateTimeRepairDate = tBizRemPunishment.getRepairDate();
        LocalDate repairDate = localDateTimeRepairDate.toLocalDate();
        LocalDate applyDate = tBizRemPunishment.getApplyDate();

        crECase.setRepairdate(applyDate)
                .setRepauth(tBizRemPunishment.getRepairOrg())
                .setRepauthcn(tBizRemPunishment.getRepairOrgCn())
                .setRepdate(repairDate)
                .setRepstate("1")  // 修复状态：1-已修复
                .setUploadtime(now)
                .setEntrytype("0")  // 录入方式：0-页面
                .setPenauth(qgCasePubNbaseinfo != null ? qgCasePubNbaseinfo.getPenAuth() : "")
                .setPenauthcn(qgCasePubNbaseinfo != null ? qgCasePubNbaseinfo.getPenAuthCn() : "")
                .setPendecno(qgCasePubNbaseinfo != null ? qgCasePubNbaseinfo.getPenDecNo() : "")
//                .setRepairno(tBizRemPunishment.getRepairNo())
                .setSExtDatetime(now);

        crECaseService.save(crECase);
    }

    private TBizRemPunishment handleTsWrit(TBizRemPunishment tBizRemPunishment, Tswrittemplate tswrittemplate){
        TSWrit tsWritModel = new TSWrit();
        String tswritId = UUID.randomUUID().toString();
        tsWritModel.setWritsid(tswritId);
        LambdaQueryWrapper<Tswrittemplate> wrapper = new LambdaQueryWrapper<>();
        String writsType = tswrittemplate.getWritsType();
        String bizSeq = tBizRemPunishment.getBizSeq();
        wrapper.eq(Tswrittemplate::getBizseq, bizSeq)
                .eq(Tswrittemplate::getIsTemplate, "0")
                .eq(Tswrittemplate::getWritsType, writsType);
        tsWritModel.setBizseq(bizSeq);

        AICUser loginUser = SSOUtil.getUser();
        //获取用户所在部门辖区码值
        if (StringUtils.isNotEmpty(tBizRemPunishment.getRepairOrg())) {
            tsWritModel.setAreaCode(tBizRemPunishment.getRepairOrg().substring(0, 6));
        } else {
            tBizRemPunishment.setRepairOrg(loginUser.getRegionID().substring(0, 6));
            tsWritModel.setAreaCode(loginUser.getRegionID().substring(0, 6));
        }

        String workNoTypeSearch = WORKNOTYPE_65;  //予以修复和不予修复的文书号都用同样的
        tsWritModel.setWritsno(workNoService.getOperatingWrirsno(workNoTypeSearch, tsWritModel.getAreaCode())); //当前用户的regionID
        tsWritModel.setWritstype(writsType);
        tsWritModel.setPripid(tBizRemPunishment.getPripid());
        tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(writsType));
        tsWritModel.setCreatetime(LocalDateTime.now());
        tsWritModel.setLauptime(LocalDateTime.now());
        if (tswrittemplate != null) {
            tsWritModel.setContent(tswrittemplate.getContent());
        }

        // 生成决定书附件
        HashMap<String, Object> map = new HashMap<>();
        String cityName = Constant.NM_REGION_CODE.get(tsWritModel.getAreaCode().substring(0, 4) + "00");
        String locName = Constant.NM_LOC_REGION_CODE.get(tsWritModel.getAreaCode());
        String areaName = cityName + locName;
        areaName = StringUtils.equals(tsWritModel.getAreaCode(),"150000") ? "内蒙古自治区" : areaName;
        map.put("areaName", areaName);
        map.put("writsNo", tsWritModel.getWritsno());
        map.put("htmlContent", tsWritModel.getContent());
        String approveDate = DateUtils.localDateFormat(LocalDate.now(), "yyyy年MM月dd日");
        map.put("approveDate", approveDate);

        //生成PDF
        String path = "";
        if ("0".equals(tBizRemPunishment.getIsAgreeRepair())) {
            path = "static" + File.separator + "template" + File.separator + "noRepairDec.docx";
        } else if ("1".equals(tBizRemPunishment.getIsAgreeRepair())) {
            path = "static" + File.separator + "template" + File.separator + "repairDec.docx";
        }
        String pdfPath = createPdfService.createPdfWithHtmlTemplate(bizSeq, path, map);
        if (StringUtils.isNotBlank(pdfPath)) {
            tsWritModel.setPath(pdfPath);
        }
        tsWritService.save(tsWritModel);

        //将文书号赋值到业务表
        tBizRemPunishment.setRepairNo(tsWritModel.getWritsno());
        return tBizRemPunishment;
    }

    private TSOpinion createOpinion(String opinioncontent,TSTaskList taskListModel, AICUser aicUser) {
        TSOpinion tsOpinion = new TSOpinion();
        // 初始化意见对象的属性
        tsOpinion.setOpinionid(IdUtil.simpleUUID());
        tsOpinion.setBizseq(taskListModel.getBizseqid());
        tsOpinion.setWorkflowid(taskListModel.getTasklistid());
        tsOpinion.setHandlerid(aicUser.getPrimaryKey());
        tsOpinion.setHandler(aicUser.getName());
        tsOpinion.setOpinioncontent(opinioncontent);
        tsOpinion.setHandledate(LocalDateTime.now());
        tsOpinion.setAreaCode(aicUser.getRegionID().substring(0, 6));
        tsOpinion.setLauptime(LocalDateTime.now());
        return tsOpinion;
    }

}
