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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSWrit;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Toincludeabn;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.auditing.entity.TSOpinion;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.service.TSTaskListService;
import com.chinaweal.aiccs.aiccs.revoke.entity.BizRevEnt;
import com.chinaweal.aiccs.aiccs.revoke.entity.Revokelist;
import com.chinaweal.aiccs.aiccs.revoke.entity.Tsbizrevcancel;
import com.chinaweal.aiccs.aiccs.revoke.entity.Tsbizrevlist;
import com.chinaweal.aiccs.aiccs.revoke.mapper.TsbizrevcancelMapper;
import com.chinaweal.aiccs.aiccs.revoke.service.BizRevEntService;
import com.chinaweal.aiccs.aiccs.revoke.service.RevokelistService;
import com.chinaweal.aiccs.aiccs.revoke.service.TsbizrevcancelService;
import com.chinaweal.aiccs.aiccs.revoke.service.TsbizrevlistService;
import com.chinaweal.aiccs.common.util.FlowUtil;
import com.chinaweal.aiccs.common.util.HandleWorkFlowUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.common.util.WorkNoUtil;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2021-08-27
 */
@Service
public class TsbizrevcancelServiceImpl extends BaseServiceImpl<TsbizrevcancelMapper, Tsbizrevcancel> implements TsbizrevcancelService {

    @Autowired
    private TSTaskListService taskListService;
    @Autowired
    private TSWritService tsWritService;
    @Autowired
    private RevokelistService revokelistService;
    @Autowired
    private TsbizrevlistService tsbizrevlistService;
    @Autowired
    private BizRevEntService bizRevEntService;

    @Override
    public TSTaskList startBizRevCancelAccept(AICUser user, TSTaskList taskListModel, Tsbizrevcancel tsbizrevcancelModel) {
        if (StringUtils.isBlank(taskListModel.getTasklistid())) {
            tsbizrevcancelModel.setHandlerid(user.getPrimaryKey());
            tsbizrevcancelModel.setHandlername(user.getName());
            saveOrUpdate(tsbizrevcancelModel);
            taskListModel = HandleWorkFlowUtil.createTaskList(taskListModel, user); //新生成待办
            String definedname = FlowUtil.REVOKE_CANCEL_WORKFLOW;//批量吊销-撤销复议诉讼
            taskListService.startWorkFlow(taskListModel, user, definedname);
        }
        return taskListModel;
    }

    @Override
    public TSTaskList saveRevokeCancelProcess(AICUser user, TSTaskList taskListModel, Tsbizrevcancel tsbizrevcancelModel, TSOpinion opinionModel, String nextNodeID, String[] nextPerformerids, String[] nextPerformerNames) {
        try {
            //下一步为审核
            if (FlowUtil.REVOKECANCELWORKFLOW_HANDLEREVCANCEL.equals(nextNodeID)) {
                tsbizrevcancelModel.setStatus("1"); // 审批流程中把状态设置成 "审批中"
                saveOrUpdate(tsbizrevcancelModel);
            }

            if (FlowUtil.REVOKECANCELWORKFLOW_ENDREVCANCEL.equals(nextNodeID)) {//完结时
                TSWrit tsWritModel = new TSWrit();
                tsWritModel.setWritsid(UUID.randomUUID().toString());
                String writstype = "";
                String handletype = tsbizrevcancelModel.getHandletype();
                String status = "";
                LocalDateTime now = LocalDateTime.now();
                if("1".equals(handletype)){
                    writstype = WorkNoUtil.WORKNOTYPE_34;
                    status = "2";
                }else if("2".equals(handletype)){
                    writstype = WorkNoUtil.WORKNOTYPE_35;
                    status = "3";
                }else if("3".equals(handletype)){
                    writstype = WorkNoUtil.WORKNOTYPE_36;
                    status = "4";
                }
                //插入文书表
                tsWritModel.setWritstype(writstype);
                tsWritModel.setWritsname(WorkNoUtil.getWorkNoTypeName(writstype));
                tsWritModel.setWritsno(tsbizrevcancelModel.getCancelwritsno());
                tsWritModel.setBizseq(tsbizrevcancelModel.getBizseq());
                tsWritModel.setPripid(tsbizrevcancelModel.getPripid());
                tsWritModel.setCreatetime(now);
                tsWritService.save(tsWritModel);

                // 更新批吊记录
                Revokelist revokelist = new Revokelist();
                revokelist.setRevlistid(tsbizrevcancelModel.getRevlistid());
                revokelist.setHandlerid(tsbizrevcancelModel.getHandlerid());
                revokelist.setHandlername(tsbizrevcancelModel.getHandlername());
                revokelist.setHandlerdate(tsbizrevcancelModel.getHandlerdate());
                revokelist.setCancelwritsid(tsWritModel.getWritsid());
                revokelist.setState(status);
                revokelist.setLauptime(now);
                revokelistService.updateById(revokelist);
                // 更新主体公示状态
//                Tsbizrevlist tsbizrevlist = new Tsbizrevlist();
//                tsbizrevlist.setRevlistid(tsbizrevcancelModel.getRevlistid());
//                tsbizrevlist.setIsDecisionPublish("2");//撤销公示
//                tsbizrevlist.setLauptime(now);
//                tsbizrevlistService.updateById(tsbizrevlist);
                BizRevEnt revEntDB = bizRevEntService.getById(tsbizrevcancelModel.getRevlistid());
                revEntDB.setIsdecisionpublish(status);//撤销、复议、诉讼
                revEntDB.setLauptime(now);
                bizRevEntService.updateById(revEntDB);
                //更新文书表的更新时间，视图时间才会更新
                TSWrit tsWritParam = new TSWrit();
                tsWritParam.setLauptime(LocalDateTime.now());
                tsWritService.update(tsWritParam, new LambdaQueryWrapper<TSWrit>().in(TSWrit::getWritstype, WorkNoUtil.WORKNOTYPE_32,WorkNoUtil.WORKNOTYPE_33)
                        .eq(TSWrit::getBizseq, revEntDB.getBizseq())
                        .eq(TSWrit::getPripid, tsbizrevcancelModel.getPripid()));

                tsbizrevcancelModel.setStatus("2"); // 审批流程中把状态设置成 "审批通过"
                tsbizrevcancelModel.setApprovaldate(now.toLocalDate());
                tsbizrevcancelModel.setLauptime(now);
                updateById(tsbizrevcancelModel);
            }
            //  审批选择不同意> 结束流程
            if (FlowUtil.REVOKECANCELWORKFLOW_ENDREVCANCEL.equals(nextNodeID)) {
                tsbizrevcancelModel.setApprovaldate(LocalDate.now());//核准日期
                tsbizrevcancelModel.setStatus("3");// 9 受理中、1 审批中、2 已通过、3 不通过
                updateById(tsbizrevcancelModel);
            }
            taskListService.updateWorkFlow(user, opinionModel, taskListModel, nextNodeID, nextPerformerids, nextPerformerNames);
        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("批吊撤销复议诉讼审核出错");
        }
        return null;
    }
}
