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;
import static com.awspaas.user.apps.donenow_ctt.service.contractService.isApproximatelyEqual;

public class commissionFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "佣金表单保存前的事件测试";
    }


    @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);
        String bindid = param.getProcessInstance().getId();

        LocalDate PERIOD_BEGIN_DATE = getLocalDate(formData.get("PERIOD_BEGIN_DATE"));//开始日期
        LocalDate PERIOD_END_DATE = getLocalDate(formData.get("PERIOD_END_DATE"));//结束日期
        String RULE_CATE = formData.getString("RULE_CATE");//佣金规则
        if (boName.equals("BO_EU_DNCTT_COMMISSION")) {
            BO orgData = SDK.getBOAPI().get(boName, formData.getId());
            if (orgData != null) {
                //已审批 不允许修改 开始日期和佣金金额
                String PERIOD_BEGIN_DATE_MIN = DBSql.getString("SELECT MIN(PERIOD_BEGIN_DATE) FROM BO_EU_DNCTT_COMMISSION_PERIOD WHERE BINDID = '" + bindid + "'");

                if (StringUtils.isNotBlank(PERIOD_BEGIN_DATE_MIN) && !PERIOD_BEGIN_DATE.equals(getLocalDate(PERIOD_BEGIN_DATE_MIN)) || !isApproximatelyEqual(orgData.get("COMMISSION_DOLLARS"), formData.get("COMMISSION_DOLLARS"))) {
                    //开始日期已修改

                    //已审批不允许修改
                    if (DBSql.getInt("select count(1) CNT from BO_EU_DNCTT_COMMISSION_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  BINDID=?", new Object[]{bindid}) > 0) {
                        throw new BPMNError("500", "已审批不允许修改开始日期和佣金金额");
                    }

                    if (DBSql.getInt("select count(1) CNT from BO_EU_DNCTT_COMMISSION_PERIOD_AGENT 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  BINDID=?", new Object[]{bindid}) > 0) {
                        throw new BPMNError("500", "已审批不允许修改开始日期和佣金金额");
                    }
                }
            }
        } else if (boName.equals("BO_EU_DNCTT_COMMISSION_ADJUST")) {
            //校验
            LocalDate EFFECTIVE_DATE = getLocalDate(formData.get("PERIOD_BEGIN_DATE"));//服务生效日期
            String EFFECTIVE_DATE_STR = EFFECTIVE_DATE.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

            if (DBSql.getInt("SELECT count(1) FROM BO_EU_DNCTT_COMMISSION_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 BINDID=? AND  PERIOD_END_DATE>=?", new Object[]{formData.get("COMMISSION_BINDID"), EFFECTIVE_DATE_STR}) > 0) {
                throw new BPMNError("500", EFFECTIVE_DATE_STR + "之后的佣金周期已被审批，则不能修改到当前日期");
            }

            if (DBSql.getInt("SELECT count(1) FROM BO_EU_DNCTT_COMMISSION_PERIOD_AGENT 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 BINDID=? AND  PERIOD_END_DATE>=?", new Object[]{formData.get("COMMISSION_BINDID"), EFFECTIVE_DATE_STR}) > 0) {
                throw new BPMNError("500", EFFECTIVE_DATE_STR + "之后的佣金周期已被审批，则不能修改到当前日期");
            }
        }
        dn.recordFormChanges.execute(param);
        return true;
    }

}
