package com.awspaas.user.apps.donenow_ctt.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static com.awspaas.user.apps.donenow_ctt.service.contractService.getLocalDate;

public class contractFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "合同表单保存前的事件测试";
    }

    /**
     *数据验证
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(ProcessExecutionContext param) throws Exception {

        String boId = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BOID);
        String boName = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);
        BO formData = (BO) param.getParameter(ListenerConst.FORM_EVENT_PARAM_FORMDATA);

        //合同编辑
        if (boName.equals("BO_EU_DNCTT_CONTRACT") && param.getProcessInstance().getProcessDefId().equals("obj_4bac3fd23814406c8d224ef2094ac274")) {
            //数据库原始数据
            BO orgData = SDK.getBOAPI().get(boName, boId);
            if (orgData != null) {
                LocalDate ORG_START_DATE = getLocalDate(orgData.get("START_DATE"));//合同开始日期
                LocalDate ORG_END_DATE = getLocalDate(orgData.get("END_DATE"));//合同结束日期


                LocalDate START_DATE = getLocalDate(formData.get("START_DATE"));//合同开始日期
                LocalDate END_DATE = getLocalDate(formData.get("END_DATE"));//合同结束日期

                //修改了合同开始日期
                if (START_DATE != null && ORG_START_DATE != null && START_DATE.isEqual(ORG_START_DATE) == false) {
                    //已审批服务周期，则不能修改合同开始时间
                    if (DBSql.getInt("SELECT count(1) FROM BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD WHERE APPROVE_AND_POST_USER_ID IS NOT NULL AND LENGTH(APPROVE_AND_POST_USER_ID)>1 AND APPROVE_AND_POST_DATE IS NOT NULL AND CONTRACT_ID=? ", new Object[]{orgData.getId()}) > 0) {
                        throw new BPMNError("500", "已审批服务周期，则不能修改合同开始时间");
                    }
                }

                //修改了合同结束日期
                if (END_DATE != null && ORG_END_DATE != null && END_DATE.isEqual(ORG_END_DATE) == false) {

                    String END_DATE_STR = END_DATE.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                    String lastPeriodEndDate = DBSql.getString("SELECT MAX(PERIOD_END_DATE) FROM BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD WHERE APPROVE_AND_POST_USER_ID IS NOT NULL AND LENGTH(APPROVE_AND_POST_USER_ID)>1 AND APPROVE_AND_POST_DATE IS NOT NULL AND CONTRACT_ID=? ", new Object[]{orgData.getId()});

                    //已审批服务周期，则不能修改合同开始时间
                    if (StringUtils.isNotEmpty(lastPeriodEndDate) && lastPeriodEndDate.length() >= 10) {
                        lastPeriodEndDate = lastPeriodEndDate.substring(0, 10);

                        SDK.getLogAPI().consoleInfo("lastPeriodEndDate:" + lastPeriodEndDate);
                        SDK.getLogAPI().consoleInfo("END_DATE:" + END_DATE);

                        LocalDate lastPeriodEndDate1 = getLocalDate(lastPeriodEndDate);
                        if (lastPeriodEndDate1 != null && !END_DATE.isAfter(lastPeriodEndDate1)) {
                            String lastPeriodEndDate_STR = lastPeriodEndDate1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                            throw new BPMNError("500", lastPeriodEndDate_STR + "之后的服务周期已被审批，则合同结束日期不能修改到" + END_DATE_STR + "。");
                        }
                    }
                }
            }
        }

        dn.recordFormChanges.execute(param);

        return true;
    }
}
