package com.awspaas.user.apps.donenow_crm.controller;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;

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

    /**
     * 修改客户地址
     * @param uc
     * @param locationId
     * @return
     * @throws SQLException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_crm.addOrEdit_location")
    public ResponseObject addOrEditLocation(UserContext uc, String accountId, String locationId) throws SQLException {

        if (StringUtils.isBlank(accountId)) {
            accountId = DBSql.getString("select ID from bo_eu_dncrm_account where closed=0 and orgid=? and type_id=9002", new Object[]{uc.getCompanyModel().getId()});
        }

        if (StringUtils.isBlank(locationId) && StringUtils.isBlank(accountId))
            return ResponseObject.newErrResponse("参数错误");
        String binidid = null;
        if (StringUtils.isNotBlank(locationId)) {
            binidid = DBSql.getString("select BINDID from BO_EU_DNCRM_LOCATION_TEMP where ID=?", new Object[]{locationId});
        }

        //新增
        if (StringUtils.isBlank(binidid)) {
            ProcessInstance processInstance = SDK.getProcessAPI().createBOProcessInstance("obj_4ec546053dc64086ab19a8565082f8f6", uc.getUID(), "");
            binidid = processInstance.getId();

            BO newBO = new BO();
            if (StringUtils.isNotBlank(locationId)) {
                RowMap rowMap = DBSql.getMap("select * from BO_EU_DNCRM_LOCATION where ID=?", new Object[]{locationId});
                if (rowMap != null) {
                    for (String key : excludesArray) {
                        rowMap.remove(key);
                    }
                }
                newBO.setAll(rowMap);
            }
            newBO.setBindId(processInstance.getId());
            if (StringUtils.isNotBlank(accountId)) {
                newBO.set("ACCOUNT_ID", accountId);
                newBO.set("ACCOUNT_NAME", DBSql.getString("select NAME from BO_EU_DNCRM_ACCOUNT where ID=?", new Object[]{accountId}));
            }
            SDK.getBOAPI().create("BO_EU_DNCRM_LOCATION_TEMP", newBO, processInstance, uc);

        }

        String url = SDK.getFormAPI().getFormURL("", uc.getSessionId(), binidid, "", 1, "", "", "");

        url = SDK.getPortalAPI().getPortalUrl() + "/r" + url.substring(1);

        ResponseObject responseObject = ResponseObject.newOkResponse();
        responseObject.setData(url);

        return responseObject;
    }


    /**
     * 获取地址详情
     * @param uc
     * @param locationId
     * @return
     * @throws SQLException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_crm.get_location")
    public ResponseObject getLocation(UserContext uc, String locationId) throws SQLException {
        RowMap rowMap = DBSql.getMap("select * from VIEW_EU_DNCRM_LOCATION where ID=?", new Object[]{locationId});
        if (rowMap == null) {
            rowMap = DBSql.getMap("select * from VIEW_EU_DNCRM_LOCATION where ID=(select LOCATION_ID from BO_EU_DNCRM_LOCATION_TEMP where ID=?)", new Object[]{locationId});
        }
        if (rowMap != null) {
            ResponseObject responseObject = ResponseObject.newOkResponse();
            responseObject.setData(rowMap);
            return responseObject;
        }
        return null;
    }

    @Mapping(value = "com.awspaas.user.apps.donenow_crm.get_account")
    public ResponseObject getAccount(UserContext uc, String accountId) throws SQLException {
        // 仅查询BO_EU_DNCRM_ACCOUNT单表
        RowMap rowMap = DBSql.getMap("select * from BO_EU_DNCRM_ACCOUNT where ID=?", new Object[]{accountId});

        if (rowMap != null) {
            ResponseObject responseObject = ResponseObject.newOkResponse();
            responseObject.setData(rowMap);
            return responseObject;
        }

        return null;
    }


}
