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

import com.actionsoft.bpms.bo.design.cache.BOCache;
import com.actionsoft.bpms.bo.design.model.BOModel;
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.htmlframework.HtmlPageTemplate;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Controller
public class udfController {
    /**
     * 获取UDF数据
     * @param userContext
     * @param cateId
     * @param objectId
     * @return
     * @throws IOException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_udf")
    public String udf(UserContext userContext, String cateId, String objectId) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", userContext.getUID());
        map.put("sid", userContext.getSessionId());
        map.put("cateId", cateId);
        map.put("objectId", objectId);

        String ORGID = userContext.getCompanyModel().getId();

        List<RowMap> udfList = DBSql.getMaps("SELECT * FROM BO_EU_DNSYS_UDF_FIELD WHERE COL_NAME NOT LIKE 'NETWORK_%' AND CATE_ID=? AND ORGID=? AND IS_ACTIVE=1 ORDER BY SORT_ORDER", new Object[]{cateId, ORGID});

        List<String> cols = udfList.stream().map(rowMap -> rowMap.getString("COL_NAME")).collect(Collectors.toList());

        RowMap cateGeneral = DBSql.getMap("select * from bo_eu_dnd_general where is_active=1 and closed=0 and general_table_id=32 AND ORGID=? AND oid=?", new Object[]{ORGID, cateId});

        map.put("formTitle", cateGeneral.getString("NAME") + "自定义字段");

        String tableName = cateGeneral.getString("REMARK");
        tableName = tableName.toUpperCase();
        BOModel boModel = BOCache.getInstance().getModelByEntityName(tableName);

        RowMap object = null;//已存储数据
        if (StringUtils.isNotBlank(objectId) && cols != null && cols.isEmpty() == false && StringUtils.isNotBlank(objectId)) {
            String sql = "SELECT " + String.join(",", cols) + " FROM " + boModel.getName() + " WHERE ID=?";
            object = DBSql.getMap(sql, new Object[]{objectId});
        }

        List<Map<String, Object>> colFields = new ArrayList<>();

        for (RowMap udf : udfList) {
            Map<String, Object> field = new HashMap<>();
            String COL_NAME = udf.getString("COL_NAME");

            field.put("name", COL_NAME);
            if (object != null) {
                field.put("value", object.getString(COL_NAME));//对象中存储
            } else {
                field.put("value", udf.getString("DEFAULT_VALUE"));//默认值
            }

            String DATA_TYPE_ID = udf.getString("DATA_TYPE_ID");//控件类型

            field.put("type", DATA_TYPE_ID);
            if (DATA_TYPE_ID.equals("530")) //列表，需要获取列表数据
            {
                String sql = udf.getString("SQL_CONFIG");//查询配置

                if (sql.contains("@")) sql = SDK.getRuleAPI().executeAtScript(sql, userContext);


                if (sql.contains("@")) sql = SDK.getRuleAPI().executeAtScript(sql, userContext);
                if (sql.contains("$ID")) sql = sql.replace("$ID", objectId);

                if (sql.contains("$")) {

                    //匹配字符串中 所有 $大写字母小写字母下划线数字 的字符串
                    Pattern pattern = Pattern.compile("\\$([A-Za-z0-9_]+)");
                    Matcher matcher = pattern.matcher(sql);
                    while (matcher.find()) {
                        String group = matcher.group(1);
                        sql = sql.replace("$" + group, object.getString(group.toUpperCase()));
                    }
                }

                List<RowMap> list = DBSql.getMaps(sql);

                field.put("list", list);
            }

            field.put("comment", udf.getString("COL_COMMENT"));//字段中文名
            field.put("readonly", udf.getString("IS_PROTECTED"));//只读
            field.put("required", udf.getString("IS_REQUIRED"));//必填

            colFields.add(field);
        }

        map.put("colFields", JSON.toJSONString(colFields));

        return HtmlPageTemplate.merge("UDF.html", map);
    }

    /**
     * 保存UDF数据
     * @param userContext
     * @param cateId
     * @param objectId
     * @param data
     * @return
     * @throws IOException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_udf_save")
    public ResponseObject udfSave(UserContext userContext, String cateId, String objectId, String data) throws IOException {
        System.out.println("com.awspaas.user.apps.donenow_sys_udf_save");
        System.out.println("cateId=" + cateId + " objectId=" + objectId);
        System.out.println(data);
        if (StringUtils.isBlank(data) || data.equals("{}")) return ResponseObject.newOkResponse();

        JSONObject json = JSON.parseObject(data);

        String ORGID = userContext.getCompanyModel().getId();
        RowMap cateGeneral = DBSql.getMap("select * from bo_eu_dnd_general where is_active=1 and closed=0 and general_table_id=32 AND ORGID=? AND oid=?", new Object[]{ORGID, cateId});
        String tableName = cateGeneral.getString("REMARK");
        List<RowMap> udfList = DBSql.getMaps("SELECT * FROM BO_EU_DNSYS_UDF_FIELD WHERE  COL_NAME NOT LIKE 'NETWORK_%' AND CATE_ID=? AND ORGID=? AND IS_ACTIVE=1 ORDER BY SORT_ORDER", new Object[]{cateId, ORGID});
        List<String> cols = udfList.stream().map(rowMap -> rowMap.getString("COL_NAME")).collect(Collectors.toList());
        String set = "";
        List<String> values = new ArrayList<>();
        for (String key : json.keySet()) {
            String value = json.getString(key);
            if (cols.contains(key)) {
                set = set + key + "=?,";
                values.add(value);
            }
        }
        values.add(objectId);
        //转换成 数组
        if (StringUtils.isBlank(set)) return ResponseObject.newOkResponse();

        String sql = "update " + tableName + " set " + set.substring(0, set.length() - 1) + " where id=?";
        System.out.println("sql===" + sql);
        int cnt = DBSql.update(sql, values.toArray());
        if (tableName.toUpperCase().equals("BO_EU_DNCTT_CONTRACT") && cnt == 0) {
            sql = "update BO_EU_DNCTT_CONTRACT_APPLY set " + set.substring(0, set.length() - 1) + " where id=?";
            cnt = DBSql.update(sql, values.toArray());
        }

        return ResponseObject.newOkResponse();

    }


    /**
     * 信息流组件上的自定义字段
     * @param userContext
     * @param boDefName
     * @return
     * @throws IOException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_network_udf")
    public ResponseObject networkUdf(UserContext userContext, String boDefName, String boId) throws IOException {
        String ORGID = userContext.getCompanyModel().getId();
        RowMap cateGeneral = DBSql.getMap("select * from bo_eu_dnd_general where is_active=1 and closed=0 and general_table_id=32 AND ORGID=? AND REMARK=?", new Object[]{ORGID, boDefName});
        if (cateGeneral == null) return ResponseObject.newOkResponse("未配置");
        String cateId = cateGeneral.getString("OID");
        List<RowMap> udfList = DBSql.getMaps("SELECT * FROM BO_EU_DNSYS_UDF_FIELD WHERE  COL_NAME LIKE 'NETWORK_%' AND CATE_ID=? AND ORGID=? AND IS_ACTIVE=1 ORDER BY SORT_ORDER", new Object[]{cateId, ORGID});

        String newInputDateTitle = null;
        String newSelectedOptionTitle = null;
        List<RowMap> newSelectedOptions = new ArrayList<>();

        String tableName = cateGeneral.getString("REMARK");
        tableName = tableName.toUpperCase();
        BOModel boModel = BOCache.getInstance().getModelByEntityName(tableName);
        RowMap rowMap = DBSql.getMap("select * from " + boModel.getName() + " where ID=?", new Object[]{boId});

        for (RowMap udf : udfList) {
            String COL_NAME = udf.getString("COL_NAME");
            if (COL_NAME.equals("NETWORK_DATE"))//日期
            {
                newInputDateTitle = udf.getString("COL_COMMENT");
            } else if (COL_NAME.equals("NETWORK_SELECT")) //下拉框
            {
                newSelectedOptionTitle = udf.getString("COL_COMMENT");
                String sql = udf.getString("SQL_CONFIG");//查询配置  label  value
                if (sql.contains("@")) sql = SDK.getRuleAPI().executeAtScript(sql, userContext);
                if (sql.contains("$ID")) sql = sql.replace("$ID", boId);

                if (sql.contains("$")) {

                    //匹配字符串中 所有 $大写字母小写字母下划线数字 的字符串
                    Pattern pattern = Pattern.compile("\\$([A-Za-z0-9_]+)");
                    Matcher matcher = pattern.matcher(sql);
                    while (matcher.find()) {
                        String group = matcher.group(1);
                        sql = sql.replace("$" + group, rowMap.getString(group.toUpperCase()));
                    }
                }

                newSelectedOptions = DBSql.getMaps(sql);
            } else {
                continue;
            }
        }
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("newInputDateTitle", newInputDateTitle);
        resultData.put("newSelectedOptionTitle", newSelectedOptionTitle);
        resultData.put("newSelectedOptions", newSelectedOptions);
        ResponseObject responseObject = ResponseObject.newOkResponse();
        responseObject.setData(resultData);
        return responseObject;
    }


    /**
     * 信息流组件上的自定义字段
     * @param userContext
     * @param boDefName
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_network_udfList")
    public ResponseObject networkUdfList(UserContext userContext, String boDefName, String boId) {

        String ORGID = userContext.getCompanyModel().getId();
        RowMap cateGeneral = DBSql.getMap("select * from bo_eu_dnd_general where is_active=1 and closed=0 and general_table_id=32 AND ORGID=? AND REMARK=?", new Object[]{ORGID, boDefName});
        if (cateGeneral == null) return ResponseObject.newOkResponse("未配置");
        String cateId = cateGeneral.getString("OID");

        List<RowMap> udfList = DBSql.getMaps("SELECT * FROM BO_EU_DNSYS_UDF_FIELD WHERE  COL_NAME LIKE 'NETWORK_%' AND CATE_ID=? AND ORGID=? AND IS_ACTIVE=1 ORDER BY SORT_ORDER", new Object[]{cateId, ORGID});


        List<RowMap> dataTypeList = DBSql.getMaps("SELECT OID,NAME FROM bo_eu_dnd_general WHERE GENERAL_TABLE_ID=33 AND ORGID=?", new Object[]{ORGID});
        Map<String, String> dataTypeMap = new HashMap<>();
        for (RowMap rowMap : dataTypeList) {
            dataTypeMap.put(rowMap.getString("OID"), rowMap.getString("NAME"));
        }

        String tableName = cateGeneral.getString("REMARK");
        tableName = tableName.toUpperCase();
        BOModel boModel = BOCache.getInstance().getModelByEntityName(tableName);
        RowMap rowMap = DBSql.getMap("select * from " + boModel.getName() + " where ID=?", new Object[]{boId});

        List<Map<String, Object>> colFields = new ArrayList<>();
        for (RowMap udf : udfList) {
            Map<String, Object> field = new HashMap<>();
            String COL_NAME = udf.getString("COL_NAME");

            field.put("name", COL_NAME);
            field.put("value", udf.getString("DEFAULT_VALUE"));//默认值
            String DATA_TYPE_ID = udf.getString("DATA_TYPE_ID");//控件类型
            field.put("type", dataTypeMap.get(DATA_TYPE_ID));
            if (DATA_TYPE_ID.equals("530")) //列表，需要获取列表数据
            {
                String sql = udf.getString("SQL_CONFIG");//查询配置

                if (sql.contains("@")) sql = SDK.getRuleAPI().executeAtScript(sql, userContext);

                if (sql.contains("@")) sql = SDK.getRuleAPI().executeAtScript(sql, userContext);
                if (sql.contains("$ID")) sql = sql.replace("$ID", boId);
                if (sql.contains("$")) {
                    //匹配字符串中 所有 $大写字母小写字母下划线数字 的字符串
                    Pattern pattern = Pattern.compile("\\$([A-Za-z0-9_]+)");
                    Matcher matcher = pattern.matcher(sql);
                    while (matcher.find()) {
                        String group = matcher.group(1);
                        // 新增 rowMap 非空判断
                        if (rowMap == null) {
                            // 可日志记录错误，或给默认值（如空字符串）
                            sql = sql.replace("$" + group, "");
                            continue;
                        }
                        // 若 rowMap 非空，再调用 getString
                        String replacement = rowMap.getString(group.toUpperCase());
                        sql = sql.replace("$" + group, replacement != null ? replacement : "");
                    }
                }

                if (StringUtils.isNotBlank(sql) && sql.toLowerCase().startsWith("select")) {
                    List<RowMap> list = DBSql.getMaps(sql);
                    field.put("list", list);
                }
            }

            field.put("comment", udf.getString("COL_COMMENT"));//字段中文名
            field.put("readonly", udf.getString("IS_PROTECTED"));//只读
            field.put("required", udf.getString("IS_REQUIRED"));//必填

            colFields.add(field);
        }

        ResponseObject responseObject = ResponseObject.newOkResponse();
        responseObject.setData(colFields);
        return responseObject;
    }


    private static final String[] OBJECT_COL_NAME = new String[]{"ACCOUNT_ID", "CONTACT_ID", "CONTRACT_ID", "OPPORTUNITY_ID", "TICKET_ID", "TASK_ID", "SALES_ORDER_ID"};
    private final String[] excludesArray = new String[]{"ID", "ORGID", "BINDID", "CREATEDATE", "CREATEUSER", "UPDATEDATE", "UPDATEUSER"};

    /**
     * 信息流组件上的自定义字段保存
     * @param userContext
     * @param boDefName
     * @param boId
     * @param data
     * @return
     * @throws IOException
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_NetworkSave")
    public ResponseObject NetworkSave(UserContext userContext, String boDefName, String boId, String streamId, String commentType, String data) {
        System.out.println("boDefName=" + boDefName + " boId=" + boId);
        System.out.println("streamId=" + streamId);
        System.out.println("commentType=" + commentType);

        BOModel boModel = BOCache.getInstance().getModelByEntityName(boDefName);
        if (boModel == null)
            return ResponseObject.newOkResponse("未配置");
        String tableName = boModel.getName();
        RowMap boMap = DBSql.getMap("select * from " + tableName + " where ID=?", new Object[]{boId});
        if (boMap == null)
            return ResponseObject.newOkResponse("未找到对象");


        JSONObject json = JSON.parseObject(data);

        //String ORGID = userContext.getCompanyModel().getId();
        BO bo = new BO();
        bo.set("BOID", boId);//对象ID
        bo.set("STREAM_ID", streamId);
        bo.set("COMMENT_TYPE", commentType);
        bo.set("BODEFNAME", tableName);//对象数据表名

        for (String key : json.keySet()) {
            String fieldName = key.toUpperCase();
            if (fieldName.startsWith("NETWORK_")) bo.set(fieldName, json.get(key));
        }

        /*
        bo.set("DATE_TITLE", json.getString("DATE_TITLE"));
        bo.set("DATE_VALUE", json.getString("DATE_VALUE"));
        bo.set("SELECT_TITLE", json.getString("SELECT_TITLE"));
        bo.set("SELECT_VALUE", json.getString("SELECT_VALUE"));
        bo.set("SELECT_TEXT", json.getString("SELECT_TEXT"));
        */

        bo.set("CONTENT", json.getString("CONTENT"));

        String processDefId = "obj_c3fa1b927eb64261ba4d49d297d80788";
        ProcessInstance contractInstance = SDK.getProcessAPI().createBOProcessInstance(processDefId, userContext.getUID(), "自定义信息流");

        SDK.getBOAPI().create("BO_EU_DNSYS_NETWORK", bo, contractInstance, userContext);


        //客户沟通  BO_EU_DNCOM_ACTIVITY  obj_5c7fe5e4cfa047eab692230ecb985143
        /*
        1) 最新跟进信息同步到相关表   bo_eu_dncom_activity---------张耀
        a) 客户：客户跟进、联系人跟进、商机跟进、合同跟进、项目跟进、工单跟进，都要同步到客户上
        b) 联系人：联系人跟进、商机跟进、合同跟进、项目跟进、工单跟进，都要同步到相关联系人上
        c) 合同：合同跟进
        d) ……
        */

        BO activityBo = new BO();
        for (String objectColName : OBJECT_COL_NAME) {
            if (boMap.containsKey(objectColName)) {
                activityBo.set(objectColName, boMap.get(objectColName));
            }
        }

        activityBo.set("OBJECT_ID", boId);

        switch (tableName) {
            case "BO_EU_DNCRM_ACCOUNT"://客户
                activityBo.set("ACCOUNT_ID", boId);
                activityBo.set("OBJECT_TYPE_ID", 735);
                activityBo.set("CATE_ID", "31");
                break;
            case "BO_EU_DNCRM_CONTACT"://联系人
                activityBo.set("CONTACT_ID", boId);
                activityBo.set("OBJECT_TYPE_ID", 736);
                break;
            case "BO_EU_DNCTT_CONTRACT"://合同
                activityBo.set("CONTRACT_ID", boId);
                activityBo.set("OBJECT_TYPE_ID", 739);
                activityBo.set("CATE_ID", "1498");//活动分类 合同
                break;
            case "BO_EU_DNCRM_OPPORTUNITY"://商机
                activityBo.set("OPPORTUNITY_ID", boId);
                activityBo.set("OBJECT_TYPE_ID", 737);
                break;
            default:
                break;
        }

        //活动类型
        /*
        if (json.containsKey("NETWORK_CATE_ID"))
            activityBo.set("CATE_ID", json.getString("NETWORK_CATE_ID"));
         */

        for (String key : json.keySet()) {
            String fieldName = key.toUpperCase();
            if (fieldName.startsWith("NETWORK_")) {
                fieldName = fieldName.replace("NETWORK_", "");
                activityBo.set(fieldName, json.get(key));
            }
        }

        activityBo.set("START_DATE", json.getString("DATE_VALUE"));
        activityBo.set("DESCRIPTION", json.getString("CONTENT"));

        activityBo.set("STREAM_ID", streamId);
        activityBo.set("COMMENT_TYPE", commentType);

        //父级 PARENT_ID
        String PARENT_ID = DBSql.getString("select ID from BO_EU_DNCOM_ACTIVITY where STREAM_ID=? and COMMENT_TYPE=0", new Object[]{streamId});
        activityBo.set("PARENT_ID", PARENT_ID);

        //完成待办
        if (json.containsKey("FINISH_DATE") && json.containsKey("CONTENT")) {
            activityBo.set("COMPLETE_TIME", json.getString("FINISH_DATE"));
            activityBo.set("COMPLETE_DESCRIPTION", json.getString("CONTENT"));
            activityBo.set("STATUS_ID", "828");//已完成
            if (StringUtils.isNotBlank(PARENT_ID))
                DBSql.update("update BO_EU_DNCOM_ACTIVITY set COMPLETE_TIME=?,COMPLETE_DESCRIPTION=?,STATUS_ID=? where ID=?", new Object[]{activityBo.get("COMPLETE_TIME"), activityBo.get("COMPLETE_DESCRIPTION"), "828", PARENT_ID});
        }


        String activityDrocessDefId = "obj_5c7fe5e4cfa047eab692230ecb985143";
        ProcessInstance activityInstance = SDK.getProcessAPI().createBOProcessInstance(activityDrocessDefId, userContext.getUID(), "客户沟通-跟进信息");

        SDK.getBOAPI().create("BO_EU_DNCOM_ACTIVITY", activityBo, activityInstance, userContext);


        return ResponseObject.newOkResponse("保存成功");
    }

    /**
     * 判断当前记录是不是待办
     * @param userContext
     * @param boDefName
     * @param boId
     * @param streamId
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_IsNetworkTodo")
    public ResponseObject IsNetworkTodo(UserContext userContext, String boDefName, String boId, String streamId) {
        ResponseObject responseObject = ResponseObject.newOkResponse();
        Map<String, Object> map = new HashMap<>();
        map.put("streamId", streamId);
        int isNetworkTodo = DBSql.getInt("select count(1) CNT from BO_EU_DNCOM_ACTIVITY where STREAM_ID=? and CATE_ID=30", "CNT", map);
        responseObject.setData(isNetworkTodo > 0);
        return responseObject;
    }

    /**
     * 信息流查看权限
     * @param userContext
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_network_perm")
    public ResponseObject networkPerm(UserContext userContext) {
        List<String> permissions = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("fd55ab6b-0298-4c94-b26c-a9182d04fa7c", "ACCOUNT");//客户备注-查询
        map.put("567a36ca-c44e-4f9d-a3cc-54c1752ba4dd", "CONTACT");//联系人备注-查询
        map.put("738af9c9-c744-4bd4-9be0-13e409cedd0a", "CONTRACT");//合同备注-查询


        map.put("6b277817-a32a-46db-8a94-11970a81d1e1", "SALESORDER");//销售订单备注-查询
        map.put("784690e1-b925-4bbe-ae21-ba9f11520ea2", "BUSINESS");//商机备注-查询
        map.put("857c1cd4-e70e-4c49-8a99-5f4ce4e0f982", "TASK");//工单 备注-查询
        map.put("f8e1f132-95c4-46c1-8f1e-bcf13902bb72", "ADDRESS");//客户地址备注-查询


        for (String permissionId : map.keySet()) {
            boolean flag = SDK.getPermAPI().havingUserInPermission(userContext.getUID(), permissionId);
            if (flag) permissions.add(map.get(permissionId));
        }

        ResponseObject responseObject = ResponseObject.newOkResponse();
        if (!permissions.isEmpty()) responseObject.setData(StringUtils.join(permissions, ","));
        return responseObject;
    }

    /**
     * 获取联系人
     * @param userContext
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_network_contacts")
    public ResponseObject networkContacts(UserContext userContext) {
        String sql = "SELECT USERID AS value,USERNAME as label FROM orguser a join VIEW_EU_SYS_DEPTPATH b  on a.DEPARTMENTID=b.id WHERE  closed=0  and INSTR(b.deptpath,(select id from orgdepartment where DEPARTMENTNO='gsbm' and closed=0 and companyid='" + userContext.getCompanyModel().getId() + "'))>0 ";
        ResponseObject responseObject = ResponseObject.newOkResponse();
        responseObject.setData(DBSql.getMaps(sql));
        return responseObject;
    }


    /**
     * 获取信息流信息
     * @param userContext
     * @param streamId
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_sys_getstreamInfo")
    public ResponseObject getstreamInfo(UserContext userContext, String streamId) {
        ResponseObject responseObject = ResponseObject.newOkResponse();

        //获取活动相关信息
        RowMap rowMap = DBSql.getMap("select * from BO_EU_DNCOM_ACTIVITY where STREAM_ID=? ", new Object[]{streamId});
        if (rowMap == null)
            return responseObject;

        String OBJECT_TYPE_ID = rowMap.getString("OBJECT_TYPE_ID");
        Map<String, String> resultMap = new HashMap<>();

        String boId = null;

        switch (OBJECT_TYPE_ID) {
            case "735"://客户
            {
                boId = rowMap.getString("ACCOUNT_ID");
                RowMap account = DBSql.getMap("select BINDID,NAME,NO from BO_EU_DNCRM_ACCOUNT WHERE ID=?", new Object[]{boId});
                resultMap.put("NAME", account.getString("NAME"));
                resultMap.put("NO", account.getString("NO"));
                resultMap.put("TITLE", "客户");
                String processUrl = SDK.getFormAPI().getFormURL(SDK.getPortalAPI().getPortalUrl(), userContext.getSessionId(), account.getString("BINDID"), null, 1, null, null, null, true);
                resultMap.put("URL", processUrl);
            }


            break;
            case "736"://联系人
            {
                boId = rowMap.getString("CONTACT_ID");
                RowMap contact = DBSql.getMap("select BINDID,NAME from BO_EU_DNCRM_CONTACT WHERE ID=?", new Object[]{boId});
                resultMap.put("NAME", contact.getString("NAME"));
                resultMap.put("TITLE", "联系人");
                String processUrl = SDK.getFormAPI().getFormURL(SDK.getPortalAPI().getPortalUrl(), userContext.getSessionId(), contact.getString("BINDID"), null, 1, null, null, null, true);
                resultMap.put("URL", processUrl);
            }

            break;
            case "739"://合同
            {
                boId = rowMap.getString("CONTRACT_ID");
                RowMap contract = DBSql.getMap("select BINDID,NAME,NO from BO_EU_DNCTT_CONTRACT WHERE ID=?", new Object[]{boId});
                resultMap.put("NAME", contract.getString("NAME"));
                resultMap.put("NO", contract.getString("NO"));
                resultMap.put("TITLE", "合同");

                String processUrl = SDK.getFormAPI().getFormURL(SDK.getPortalAPI().getPortalUrl(), userContext.getSessionId(), contract.getString("BINDID"), null, 1, null, null, null, true);
                resultMap.put("URL", processUrl);
            }


            break;
            case "737"://商机
                boId = rowMap.getString("OPPORTUNITY_ID");


                break;
            default:
                break;
        }

        responseObject.setData(resultMap);
        return responseObject;
    }

}
