package com.zcloud.flow.xgf.GuFen;

import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeSwitchComponent;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.xgf.XgfFlowsMapper;
import com.zcloud.mapper.datasource.xgf.XgfUserDetailsMapper;
import com.zcloud.mapper.datasource.xgf.XgfUserMapper;
import com.zcloud.service.xgf.XgfUserService;
import com.zcloud.util.DateUtil;
import com.zcloud.flow.xgf.util.XgfFlowDto;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;

@LiteflowComponent("GuFenCharge")
public class GuFenCharge extends NodeSwitchComponent {

    @Resource
    private XgfFlowsMapper xgfFlowsMapper;

    @Resource
    private XgfUserService xgfUserService;

    @Resource
    private XgfUserMapper xgfUserMapper;

    @Resource
    private XgfUserDetailsMapper xgfUserDetailsMapper;

    @Override
    public boolean isAccess() {
        XgfFlowDto info = this.getRequestData();

        // 如果指针没有数据,赋值后自动结束
        if (StringUtils.isBlank(info.getIterator())) {
            PageData condition = new PageData();
            condition.put("FLOWS_ID", info.getFLOWS_ID());
            PageData flows = xgfFlowsMapper.findById(condition);
            info.setIterator("0");
            flows.put("APPOINT_ZERO_CORP_TYPE", "0");
            flows.put("APPOINT_ZERO_DEPARTMENT_ID", info.getAPPOINT_DEPARTMENT_ID());
            flows.put("APPOINT_ZERO_DEPARTMENT_NAME", info.getAPPOINT_DEPARTMENT_NAME());
            flows.put("APPOINT_ZERO_USER_ID", info.getAPPOINT_USER_ID());
            flows.put("APPOINT_ZERO_USER_NAME", info.getAPPOINT_USER_NAME());
            flows.put("FLOWS_STEP", 0);
            xgfFlowsMapper.edit(flows);
            return false;
        }
        return super.isAccess();
    }

    @Override
    public String processSwitch() throws Exception {
        System.out.println(getName() + "节点");

        XgfFlowDto info = this.getRequestData();
        PageData condition = new PageData();

        condition.put("FLOWS_ID", info.getFLOWS_ID());
        PageData flows = xgfFlowsMapper.findById(condition);

        condition.clear();
        condition.put("XGF_USER_ID", flows.getString("FLOWS_ID"));
        PageData entity = xgfUserMapper.findById(condition);

        // 如果不是当前流程,进入下一流程判断
        if (!"1".equals(info.getIterator())) {
            // 根据是否有委托书判断是否走委托流程
            if (entity.get("ATTORNEY") != null && StringUtils.isNotBlank(entity.getString("ATTORNEY"))) {
                return "WeiTuo";
            }
            return "JiTuanSupervise";
        }

        flows.put("APPOINT_ZERO_CORP_ID", "1");
        flows.put("APPOINT_ZERO_CORP_NAME", "秦港股份有限公司");
        flows.put("APPOINT_ZERO_TIME", DateUtil.getTime());
        flows.put("APPOINT_ZERO_STATUS", info.getSTATUS());
        flows.put("APPOINT_ZERO_OPINION", info.getOPINION());
        flows.put("OPINION", info.getOPINION());
        if ("0".equals(info.getSTATUS())) {
            // 打回至相关方端
            xgfUserService.repulse(flows);
            return "";
        } else {
            xgfFlowsMapper.edit(flows);
            entity.put("CHECK_STATUS", 1);
            entity.put("VALID_FLAG", "2");
            entity.put("CHECK_STEP", Integer.parseInt(entity.get("CHECK_STEP").toString()) + 1);
            // created by liu jun 2024-03-01 港务局开会后,将入场告知培训的有效期设置权限给发包单位
            if (StringUtils.isNotBlank(info.getLIMIT_END_TIME())) {
                entity.put("LIMIT_END_TIME", info.getLIMIT_END_TIME());
            }
            // created by liu jun 2024-02-04 如果有委托书则保存委托书
            if (StringUtils.isNotBlank(info.getATTORNEY())) {
                condition.clear();
                condition.put("XGF_USER_DETAILS_ID", flows.getString("FLOWS_ID"));
                PageData userInfo = xgfUserDetailsMapper.findById(condition);
                userInfo.put("ATTORNEY", info.getAPPOINT_ANNEX());
                xgfUserDetailsMapper.edit(userInfo);
            }
            xgfUserMapper.edit(entity);
            // 将指针清空
            info.setIterator("");
        }

        // 保存操作记录
        xgfUserService.saveLog(info, info.getSTATUS(), "0");

        if (entity.get("ATTORNEY") != null && StringUtils.isNotBlank(entity.getString("ATTORNEY"))) {
            return "GuFenWeiTuo";
        }
        return "GuFenSupervise";
    }
}