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.ExecuteListener;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class crmLocationFormSaveAfter extends ExecuteListener {
    public String getDescription() {
        return "表单保存后，数据处理事件";
    }

    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 {
        String processId = processExecutionContext.getProcessInstance().getId();
        RowMap rowMap = DBSql.getMap("select * from BO_EU_DNCRM_LOCATION_TEMP where BINDID =?", processId);

        String locationId = rowMap.getString("LOCATION_ID");
        if (locationId != null && !"".equals(locationId)) {
            BO locationBO = SDK.getBOAPI().get("BO_EU_DNCRM_LOCATION", locationId);
            if (rowMap != null) {
                for (String key : excludesArray) {
                    rowMap.remove(key);
                }
            }
            locationBO.setAll(rowMap);
            SDK.getBOAPI().update("BO_EU_DNCRM_LOCATION", locationBO);
        } else {
            String accountBindid = DBSql.getString("select BINDID from BO_EU_DNCRM_ACCOUNT where ID=?", new Object[]{rowMap.getString("ACCOUNT_ID")});
            BO locationBO = new BO();
            for (String key : excludesArray) {
                rowMap.remove(key);
            }
            locationBO.setAll(rowMap);
            locationBO.set("BINDID", accountBindid);
            SDK.getBOAPI().create("BO_EU_DNCRM_LOCATION", locationBO, accountBindid, processExecutionContext.getUserContext().getUID());
            DBSql.update("update BO_EU_DNCRM_LOCATION_TEMP set LOCATION_ID=? where BINDID=?", new Object[]{locationBO.getId(), processId});
        }
    }
}
