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

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.annual.entity.AnnualDissentInfo;
import com.chinaweal.aiccs.aiccs.annual.entity.BizAnnualDissentInfo;
import com.chinaweal.aiccs.aiccs.annual.entity.EPubObjection;
import com.chinaweal.aiccs.aiccs.annual.mapper.BizAnnualDissentInfoMapper;
import com.chinaweal.aiccs.aiccs.annual.service.AnnualDissentInfoService;
import com.chinaweal.aiccs.aiccs.annual.service.BizAnnualDissentInfoService;
import com.chinaweal.aiccs.aiccs.annual.service.EPubObjectionService;
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.common.constant.Constant;
import com.chinaweal.aiccs.common.util.ConstantsUtil;
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.org.service.AicorgService;
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.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 年报公示异议反馈业务记录信息 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-04-13
 */
@Service
public class BizAnnualDissentInfoServiceImpl extends BaseServiceImpl<BizAnnualDissentInfoMapper, BizAnnualDissentInfo> implements BizAnnualDissentInfoService {

    @Autowired
    private TSTaskListService taskListService;
    @Autowired
    private AnnualDissentInfoService annualDissentInfoService;
    @Autowired
    private EPubObjectionService ePubObjectionService;
    @Autowired
    private AicorgService aicorgService;
    @Autowired
    private IGzaiccodeService gzaiccodeService;

    @Override
    public TSTaskList saveAnnualDissentAccept(AICUser curUser, TSTaskList taskList, BizAnnualDissentInfo... infos) {
        if (StringUtils.isBlank(taskList.getTasklistid())) {
            String code = aicorgService.getRegionCodeByUser(curUser);
            for (BizAnnualDissentInfo info : infos) {
                info.setCreatorId(curUser.getPrimaryKey()); // 流程启动人(主办人)
                info.setState(Constant.TRUE_1_STR); // 流程启动把状态设置为 "受理中"
                info.setUpdateTime(LocalDateTime.now());
                info.setUnit(code);
                String decOrgName = gzaiccodeService.getNameByCode(info.getUnit());
                info.setUnitCn(decOrgName);
                saveOrUpdate(info);
            }
            String definedname = FlowUtil.ANNUAL_DISSENT_WORKFLOW;//年报公示信息异议流程模板
            //新待办
            taskList = HandleWorkFlowUtil.createTaskList(taskList, curUser);
            //开始工作流
            taskListService.startWorkFlow(taskList, curUser, definedname);
        }
        return taskList;
    }

    @Override
    public TSTaskList nextProcess(AICUser user, TSTaskList taskList, TSOpinion opinionModel, String nextNodeID, String[] nextPerformerids, String[] nextPerformerNames, BizAnnualDissentInfo... infos) {
        try {
            String state = Constant.TRUE_1_STR;
            if(FlowUtil.ANNUAL_DISSENT_WORKFLOW_OVERANNUALDISSENT.equals(nextNodeID)){
                // 不通过
                state = Constant.FALSE_0_STR;
            }else if(FlowUtil.ANNUAL_DISSENT_WORKFLOW_ENDANNUALDISSENT.equals(nextNodeID)){
                // 通过
                state = "2";
            }
            List<String> infoIds = new ArrayList<>();
            for (BizAnnualDissentInfo each : infos) {
                if(StringUtils.isNotBlank(each.getInfoId())){
                    infoIds.add(each.getInfoId());
                }
                each.setState(state);
                if(StringUtils.isBlank(each.getUid())) throw new BusinessException("缺少业务信息主键");
            }
            if(CollectionUtils.isNotEmpty(infoIds)){
                annualDissentInfoService.update(new AnnualDissentInfo().setHandleStatus(state), new LambdaUpdateWrapper<AnnualDissentInfo>().in(AnnualDissentInfo::getUid, infoIds));
            }
            updateBatchById(Arrays.asList(infos));
            taskListService.updateWorkFlow(user, opinionModel, taskList, nextNodeID, nextPerformerids, nextPerformerNames);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("异常名录期限届满列入严重违法失信名单审核出错");
        }
        return taskList;
    }

    @Transactional
    @Override
    public TSTaskList saveAnnualProcess(AICUser user, TSTaskList taskListModel, BizAnnualDissentInfo bizAnnualDissentInfo, TSOpinion opinionModel, String nextNodeID, String[] nextPerformerids, String[] nextPerformerNames) {
        if (FlowUtil.ANNUAL_DISSENT_WORKFLOW_END.equals(nextNodeID)) { //审批结束
            EPubObjection ePubObjection = ePubObjectionService.getById(bizAnnualDissentInfo.getInfoId());
            ePubObjection.setDispose("2"); //以处理
            ePubObjection.setResult(bizAnnualDissentInfo.getFinalFeedback());
            ePubObjection.setAcceptUnit(bizAnnualDissentInfo.getUnit());
            ePubObjection.setAcceptUnitCn(bizAnnualDissentInfo.getUnitCn());
            ePubObjection.setResultTime(LocalDateTime.now());
            ePubObjectionService.updateById(ePubObjection);
            bizAnnualDissentInfo.setState("2"); //通过
            bizAnnualDissentInfo.setUpdateTime(LocalDateTime.now());
        }
        if (FlowUtil.ANNUAL_DISSENT_WORKFLOW_REVOKE.equals(nextNodeID)) { //撤销
//            EPubObjection ePubObjection = ePubObjectionService.getById(bizAnnualDissentInfo.getInfoId());
//            ePubObjection.setDispose("0"); //已提交
//            ePubObjection.setResult("");
//            ePubObjectionService.updateById(ePubObjection);
            bizAnnualDissentInfo.setState("3"); //撤销
            bizAnnualDissentInfo.setUpdateTime(LocalDateTime.now());
        }
        if (FlowUtil.ANNUAL_DISSENT_WORKFLOW_OVER.equals(nextNodeID)) { //不同意
            bizAnnualDissentInfo.setState("0"); //不同意
            bizAnnualDissentInfo.setUpdateTime(LocalDateTime.now());
        }
        this.updateById(bizAnnualDissentInfo);
        taskListService.updateWorkFlow(user,opinionModel,taskListModel,nextNodeID,nextPerformerids,nextPerformerNames);
        return taskListModel;
    }

    @Override
    public List<BizAnnualDissentInfo> queryByinfoId(String infoId) {
        return baseMapper.queryByinfoId(infoId);
    }

    @Override
    public List<BizAnnualDissentInfo> queryByBizseq(String bizseq) {
        return baseMapper.queryByBizseq(bizseq);
    }
}
