package com.awspaas.user.apps.donenow_crm.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.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

public class InstalledProductFormBeforeSave extends InterruptListener {
    public String getDescription() {
        return "配置项保存前事件";
    }

    /**
     *配置项保存前事件
     * @param processExecutionContext
     * @return
     * @throws Exception
     */
    @Override
    public boolean execute(ProcessExecutionContext processExecutionContext) throws Exception {
        UserContext uc = processExecutionContext.getUserContext();
        String boId = processExecutionContext.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BOID);
        BO formData = processExecutionContext.getFormData();
        String boName = processExecutionContext.getParameterOfString("$BONAME");
        String bindId = processExecutionContext.getProcessInstance().getId();
        boolean isCreate = processExecutionContext.getParameterOfBoolean(ListenerConst.FORM_EVENT_PARAM_ISCREATE);
        String CONTRACT_COST_ID1 = formData.getString("CONTRACT_COST_ID");
        if (StringUtils.isNotBlank(boId)) {
            Object CONTRACT_COST_ID2 = SDK.getBOAPI().get(boName, boId, "CONTRACT_COST_ID");
            if (CONTRACT_COST_ID2 != null) {
                DBSql.update("update BO_EU_DNCTT_CONTRACT_COST set CREATE_CI=0 where ID=?", new Object[]{CONTRACT_COST_ID2});
            }
        }

        if (StringUtils.isNotBlank(CONTRACT_COST_ID1))
            DBSql.update("update BO_EU_DNCTT_CONTRACT_COST set CREATE_CI=1 where ID=?", new Object[]{CONTRACT_COST_ID1});

        //记录表单修改
        dn.recordFormChanges.execute(processExecutionContext);
        return true;
    }
}
