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

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

public class productFormSaveAfter extends ExecuteListener {
    private final String[] excludesArray = new String[]{"ID", "ORGID", "BINDID", "CREATEDATE", "CREATEUSER", "UPDATEDATE", "UPDATEUSER", "PROCESSDEFID", "ISEND"};

    /**
     * 产品保存后，创建关联的服务
     * @param processExecutionContext
     * @throws Exception
     */
    @Override
    public void execute(ProcessExecutionContext processExecutionContext) throws Exception {
        // 获取流程实例ID
        String processId = processExecutionContext.getProcessInstance().getId();
        // 产品信息
        RowMap rowMap = DBSql.getMap("select * from BO_EU_DNIVT_PRODUCT where BINDID =?", new Object[]{processId});
        String product_id = rowMap.getString("ID");
        String CREATE_SERVICE = rowMap.getString("CREATE_SERVICE");

        String SERVICE_ID = rowMap.getString("SERVICE_ID");
        if (StringUtils.isBlank(SERVICE_ID))
            SERVICE_ID = DBSql.getString("select ID from BO_EU_DNIVT_SERVICE where PRODUCT_ID =?", new Object[]{rowMap.getString("ID")});

        if (CREATE_SERVICE.equals("1") || CREATE_SERVICE.equals("是")) {
            if (StringUtils.isNotBlank(SERVICE_ID)) {
                BO serviceBO = SDK.getBOAPI().get("BO_EU_DNIVT_SERVICE", SERVICE_ID);
                for (String exclude : excludesArray) {
                    rowMap.remove(exclude);
                }
                serviceBO.setAll(rowMap);
                serviceBO.set("CLOSED", 0);
                SDK.getBOAPI().update("BO_EU_DNIVT_SERVICE", serviceBO);

            } else {
                BO serviceBO = new BO();
                serviceBO.setAll(rowMap);
                serviceBO.set("PRODUCT_ID", rowMap.getString("ID"));
                serviceBO.set("PRODUCT_NAME", rowMap.getString("NAME"));
                serviceBO.remove("ID");
                serviceBO.remove("BINDID");
                ProcessInstance processInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d42c36c8e3f6443d81d310d96a28e256", processExecutionContext.getUserContext().getUID(), "产品-创建服务");
                serviceBO.setBindId(processInstance.getId());
                SDK.getBOAPI().create("BO_EU_DNIVT_SERVICE", serviceBO, processInstance, processExecutionContext.getUserContext());
                SERVICE_ID = serviceBO.getId();
            }
        } else {
            if (StringUtils.isNotBlank(SERVICE_ID)) {
                BO serviceBO = SDK.getBOAPI().get("BO_EU_DNIVT_SERVICE", SERVICE_ID);
                serviceBO.set("CLOSED", 1);
                SDK.getBOAPI().update("BO_EU_DNIVT_SERVICE", serviceBO);
                SERVICE_ID = null;
            }
        }

        DBSql.update("update BO_EU_DNIVT_PRODUCT set SERVICE_ID =? where ID =?", new Object[]{SERVICE_ID, product_id});
        if (StringUtils.isNotBlank(SERVICE_ID))
            DBSql.update("update BO_EU_DNIVT_SERVICE set PRODUCT_ID=? where ID=?", new Object[]{product_id, SERVICE_ID});

    }
}
