package com.malk.pake.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDResponse;
import com.aliyun.dingtalkyida_1_0.models.SearchFormDatasResponse;
import com.aliyun.dingtalkyida_1_0.models.StartInstanceResponse;
import com.aliyun.dingtalkyida_1_0.models.UpdateFormDataResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.malk.pake.service.DingOaService;
import com.malk.service.dingtalk.DDClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class DingOaServiceImpl implements DingOaService {

    @Autowired
    private DDClient ddClient;

    //宜搭接口调用
    public static com.aliyun.dingtalkyida_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkyida_1_0.Client(config);
    }


    //更新商机申报表单关联表单控件
    @Override
    public String updateShangjishenbao(String formInstanceId, String shenbaoName){

//        formInstanceId = "dcdd56ca-c305-44bd-97c6-b6d7befff366";
//        shenbaoName = "测试机会111";

        log.info("宜搭连接器获取商机申报表单实例ID：" + formInstanceId);
        log.info("宜搭连接器获取商机申报表单名称：" + shenbaoName);

        //======商机申报表单中的商机关联表单控件值======
        Map map = new HashMap();
        //表单类型：默认
        map.put("formType", "receipt");
        //表单ID
        map.put("formUuid", "FORM-2A666T61HPMCPMEX8BBVV6R1XFKI23ZTWECKLV");
        //表单实例ID
        map.put("instanceId", formInstanceId);
        //标题
        map.put("subTitle", "系统同步");
        //应用编码appType
        map.put("appType", "APP_UNSAR4O4Y7NBDUYXLIP4");
        //表单实例标题
        map.put("title", shenbaoName);
        System.out.println("项目关联表单控件值map：" + map);

        //转换成数组集合
        List list = new ArrayList();
        list.add(map);

        Map map1 = new HashMap();
        map1.put("associationFormField_lkdu45yi",list);
        String mapString = JSON.toJSONString(map1);
        System.out.println("项目关联表单控件值list文本json：" + mapString);

        JSONObject jsonObject = null;
        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders updateFormDataHeaders = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders();
            updateFormDataHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest updateFormDataRequest = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setFormInstanceId(formInstanceId)
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4")
                    .setUpdateFormDataJson(mapString);

            UpdateFormDataResponse updateFormDataResponse = client.updateFormDataWithOptions(updateFormDataRequest, updateFormDataHeaders, new RuntimeOptions());
            String s = JSON.toJSONString(updateFormDataResponse);
            jsonObject = JSON.parseObject(s);
            System.out.println("【宜搭更新】表单object对象：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }

        return "更新商机申报表单关联表单控件，执行结束。";
    }

    public String getShangjiDanganMingxi(String sjbh, String yxjd, Map mapNew) {

//        //测试商机【测试机会-muzhi】
//        String sjbh = "XM-20230912-00056";
//        //营销阶段
//        String yxjd = "客户分析";

        //封装查询条件
        Map mapAll = new HashMap();
        //商机编号
        mapAll.put("textField_lkchfcy2", sjbh);

        String mapAllStr = JSON.toJSONString(mapAll);
        System.out.println("封装修改的字段参数：" + mapAllStr);

        JSONObject jsonObject = null;

        int size = 100;
        int page = 1;


        //【帕科架构】
        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.SearchFormDatasHeaders searchFormDatasHeaders = new com.aliyun.dingtalkyida_1_0.models.SearchFormDatasHeaders();
            searchFormDatasHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.SearchFormDatasRequest searchFormDatasRequest = new com.aliyun.dingtalkyida_1_0.models.SearchFormDatasRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setSearchFieldJson(mapAllStr)
                    .setCurrentPage(page)
                    .setPageSize(size)
                    .setFormUuid("FORM-XN966G71JIOCJM8O9D2YVDJZNMVC2SYBNECKLE")
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4");

            SearchFormDatasResponse searchFormDatasResponse = client.searchFormDatasWithOptions(searchFormDatasRequest, searchFormDatasHeaders, new RuntimeOptions());
//            System.out.println(JSON.toJSONString(searchFormDatasResponse.getBody()));
            String s = JSON.toJSONString(searchFormDatasResponse.getBody());
            jsonObject = JSON.parseObject(s);
//            System.out.println("高级条件查询【商机档案】接口object对象：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }

        JSONArray data = jsonObject.getJSONArray("data");
//        System.out.println("返回的宜搭data：" + data);

        JSONObject formData = data.getJSONObject(0).getJSONObject("formData");
//        System.out.println("返回的宜搭formData：" + formData);

        String formInstanceId = data.getJSONObject(0).getString("formInstanceId");
        System.out.println("实例ID：" + formInstanceId);


        if (data.isEmpty()) {
            System.out.println("查询【商机档案】返回数据为空！");
        } else {
            String mapAllString = null;
            List list = new ArrayList<>();

            if (yxjd.equals("客户分析")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_lkpqn4jo");
                System.out.println("【客户分析】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_lkpqn4ke = jsonObject1.getString("dateField_lkpqn4ke");
                    //跟进日期
                    String dateField_lkpqn4jv = jsonObject1.getString("dateField_lkpqn4jv");
                    //拜访记录-关联表单
                    String associationFormField_lkpqn4kk_id = jsonObject1.getString("associationFormField_lkpqn4kk_id");
                    //当前工作进展
                    String textareaField_llz1k48y = jsonObject1.getString("textareaField_llz1k48y");
                    //当前卡点与关键问题
                    String textareaField_llz1k48z = jsonObject1.getString("textareaField_llz1k48z");
                    //下一步计划事项
                    String textareaField_llz1k490 = jsonObject1.getString("textareaField_llz1k490");
                    //所需资源
                    String textareaField_llz1k491 = jsonObject1.getString("textareaField_llz1k491");

                    map.put("dateField_lkpqn4ke", dateField_lkpqn4ke);
                    map.put("dateField_lkpqn4jv", dateField_lkpqn4jv);
                    map.put("associationFormField_lkpqn4kk_id", associationFormField_lkpqn4kk_id);
                    map.put("textareaField_llz1k48y", textareaField_llz1k48y);
                    map.put("textareaField_llz1k48z", textareaField_llz1k48z);
                    map.put("textareaField_llz1k490", textareaField_llz1k490);
                    map.put("textareaField_llz1k491", textareaField_llz1k491);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_lkpqn4jo", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("建立信任")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_lm0itw5e");
                System.out.println("【建立信任】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_lm0itw5b = jsonObject1.getString("dateField_lm0itw5b");
                    //跟进日期
                    String dateField_lm0itw57 = jsonObject1.getString("dateField_lm0itw57");
                    //拜访记录-关联表单
                    String associationFormField_lm0itw58_id = jsonObject1.getString("associationFormField_lm0itw58_id");
                    //当前工作进展
                    String textareaField_lm0itw59 = jsonObject1.getString("textareaField_lm0itw59");
                    //当前卡点与关键问题
                    String textareaField_lm0itw5a = jsonObject1.getString("textareaField_lm0itw5a");
                    //下一步计划事项
                    String textareaField_lm0itw5c = jsonObject1.getString("textareaField_lm0itw5c");
                    //所需资源
                    String textareaField_lm0itw5d = jsonObject1.getString("textareaField_lm0itw5d");

                    map.put("dateField_lm0itw5b", dateField_lm0itw5b);
                    map.put("dateField_lm0itw57", dateField_lm0itw57);
                    map.put("associationFormField_lm0itw58_id", associationFormField_lm0itw58_id);
                    map.put("textareaField_lm0itw59", textareaField_lm0itw59);
                    map.put("textareaField_lm0itw5a", textareaField_lm0itw5a);
                    map.put("textareaField_lm0itw5c", textareaField_lm0itw5c);
                    map.put("textareaField_lm0itw5d", textareaField_lm0itw5d);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_lm0itw5e", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("挖掘需求")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_llz3h77t");
                System.out.println("【挖掘需求】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_llz3h77q = jsonObject1.getString("dateField_llz3h77q");
                    //跟进日期
                    String dateField_llz3h77m = jsonObject1.getString("dateField_llz3h77m");
                    //拜访记录-关联表单
                    String associationFormField_llz3h77n_id = jsonObject1.getString("associationFormField_llz3h77n_id");
                    //当前工作进展
                    String textareaField_llz3h77o = jsonObject1.getString("textareaField_llz3h77o");
                    //当前卡点与关键问题
                    String textareaField_llz3h77p = jsonObject1.getString("textareaField_llz3h77p");
                    //下一步计划事项
                    String textareaField_llz3h77r = jsonObject1.getString("textareaField_llz3h77r");
                    //所需资源
                    String textareaField_llz3h77s = jsonObject1.getString("textareaField_llz3h77s");

                    map.put("dateField_llz3h77q", dateField_llz3h77q);
                    map.put("dateField_llz3h77m", dateField_llz3h77m);
                    map.put("associationFormField_llz3h77n_id", associationFormField_llz3h77n_id);
                    map.put("textareaField_llz3h77o", textareaField_llz3h77o);
                    map.put("textareaField_llz3h77p", textareaField_llz3h77p);
                    map.put("textareaField_llz3h77r", textareaField_llz3h77r);
                    map.put("textareaField_llz3h77s", textareaField_llz3h77s);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_llz3h77t", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("呈现价值")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_llz3h781");
                System.out.println("【呈现价值】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_llz3h77y = jsonObject1.getString("dateField_llz3h77y");
                    //跟进日期
                    String dateField_llz3h77u = jsonObject1.getString("dateField_llz3h77u");
                    //拜访记录-关联表单
                    String associationFormField_llz3h77v_id = jsonObject1.getString("associationFormField_llz3h77v_id");
                    //当前工作进展
                    String textareaField_llz3h77w = jsonObject1.getString("textareaField_llz3h77w");
                    //当前卡点与关键问题
                    String textareaField_llz3h77x = jsonObject1.getString("textareaField_llz3h77x");
                    //下一步计划事项
                    String textareaField_llz3h77z = jsonObject1.getString("textareaField_llz3h77z");
                    //所需资源
                    String textareaField_llz3h780 = jsonObject1.getString("textareaField_llz3h780");

                    map.put("dateField_llz3h77y", dateField_llz3h77y);
                    map.put("dateField_llz3h77u", dateField_llz3h77u);
                    map.put("associationFormField_llz3h77v_id", associationFormField_llz3h77v_id);
                    map.put("textareaField_llz3h77w", textareaField_llz3h77w);
                    map.put("textareaField_llz3h77x", textareaField_llz3h77x);
                    map.put("textareaField_llz3h77z", textareaField_llz3h77z);
                    map.put("textareaField_llz3h780", textareaField_llz3h780);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_llz3h781", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("赢取承诺")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_llz3h77l");
                System.out.println("【赢取承诺】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_llz3h77i = jsonObject1.getString("dateField_llz3h77i");
                    //跟进日期
                    String dateField_llz3h77e = jsonObject1.getString("dateField_llz3h77e");
                    //拜访记录-关联表单
                    String associationFormField_llz3h77f_id = jsonObject1.getString("associationFormField_llz3h77f_id");
                    //当前工作进展
                    String textareaField_llz3h77g = jsonObject1.getString("textareaField_llz3h77g");
                    //当前卡点与关键问题
                    String textareaField_llz3h77h = jsonObject1.getString("textareaField_llz3h77h");
                    //下一步计划事项
                    String textareaField_llz3h77j = jsonObject1.getString("textareaField_llz3h77j");
                    //所需资源
                    String textareaField_llz3h77k = jsonObject1.getString("textareaField_llz3h77k");

                    map.put("dateField_llz3h77i", dateField_llz3h77i);
                    map.put("dateField_llz3h77e", dateField_llz3h77e);
                    map.put("associationFormField_llz3h77f_id", associationFormField_llz3h77f_id);
                    map.put("textareaField_llz3h77g", textareaField_llz3h77g);
                    map.put("textareaField_llz3h77h", textareaField_llz3h77h);
                    map.put("textareaField_llz3h77j", textareaField_llz3h77j);
                    map.put("textareaField_llz3h77k", textareaField_llz3h77k);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_llz3h77l", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("回收账款")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_lm0itw5m");
                System.out.println("【回收账款】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_lm0itw5j = jsonObject1.getString("dateField_lm0itw5j");
                    //跟进日期
                    String dateField_lm0itw5f = jsonObject1.getString("dateField_lm0itw5f");
                    //拜访记录-关联表单
                    String associationFormField_lm0itw5g_id = jsonObject1.getString("associationFormField_lm0itw5g_id");
                    //当前工作进展
                    String textareaField_lm0itw5h = jsonObject1.getString("textareaField_lm0itw5h");
                    //当前卡点与关键问题
                    String textareaField_lm0itw5i = jsonObject1.getString("textareaField_lm0itw5i");
                    //下一步计划事项
                    String textareaField_lm0itw5k = jsonObject1.getString("textareaField_lm0itw5k");
                    //所需资源
                    String textareaField_lm0itw5l = jsonObject1.getString("textareaField_lm0itw5l");

                    map.put("dateField_lm0itw5j", dateField_lm0itw5j);
                    map.put("dateField_lm0itw5f", dateField_lm0itw5f);
                    map.put("associationFormField_lm0itw5g_id", associationFormField_lm0itw5g_id);
                    map.put("textareaField_lm0itw5h", textareaField_lm0itw5h);
                    map.put("textareaField_lm0itw5i", textareaField_lm0itw5i);
                    map.put("textareaField_lm0itw5k", textareaField_lm0itw5k);
                    map.put("textareaField_lm0itw5l", textareaField_lm0itw5l);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_lm0itw5m", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("商机暂停")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_lm0itw5u");
                System.out.println("【商机暂停】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_lm0itw5r = jsonObject1.getString("dateField_lm0itw5r");
                    //跟进日期
                    String dateField_lm0itw5n = jsonObject1.getString("dateField_lm0itw5n");
                    //拜访记录-关联表单
                    String associationFormField_lm0itw5o_id = jsonObject1.getString("associationFormField_lm0itw5o_id");
                    //当前工作进展
                    String textareaField_lm0itw5p = jsonObject1.getString("textareaField_lm0itw5p");
                    //当前卡点与关键问题
                    String textareaField_lm0itw5q = jsonObject1.getString("textareaField_lm0itw5q");
                    //下一步计划事项
                    String textareaField_lm0itw5s = jsonObject1.getString("textareaField_lm0itw5s");
                    //所需资源
                    String textareaField_lm0itw5t = jsonObject1.getString("textareaField_lm0itw5t");

                    map.put("dateField_lm0itw5r", dateField_lm0itw5r);
                    map.put("dateField_lm0itw5n", dateField_lm0itw5n);
                    map.put("associationFormField_lm0itw5o_id", associationFormField_lm0itw5o_id);
                    map.put("textareaField_lm0itw5p", textareaField_lm0itw5p);
                    map.put("textareaField_lm0itw5q", textareaField_lm0itw5q);
                    map.put("textareaField_lm0itw5s", textareaField_lm0itw5s);
                    map.put("textareaField_lm0itw5t", textareaField_lm0itw5t);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_lm0itw5u", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);

            } else if (yxjd.equals("商机取消") || yxjd.equals("商机丢失")) {
                JSONArray jsonArray = formData.getJSONArray("tableField_lm74i665");
                System.out.println("【商机取消/商机丢失】页签明细：" + jsonArray);

                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);


                    Map map = new HashMap<>();

                    //下一步计划日期
                    String dateField_lm74i662 = jsonObject1.getString("dateField_lm74i662");
                    //跟进日期
                    String dateField_lm74i65y = jsonObject1.getString("dateField_lm74i65y");
                    //拜访记录-关联表单
                    String associationFormField_lm74i65z_id = jsonObject1.getString("associationFormField_lm74i65z_id");
                    //当前工作进展
                    String textareaField_lm74i660 = jsonObject1.getString("textareaField_lm74i660");
                    //当前卡点与关键问题
                    String textareaField_lm74i661 = jsonObject1.getString("textareaField_lm74i661");
                    //下一步计划事项
                    String textareaField_lm74i663 = jsonObject1.getString("textareaField_lm74i663");
                    //所需资源
                    String textareaField_lm74i664 = jsonObject1.getString("textareaField_lm74i664");

                    map.put("dateField_lm74i662", dateField_lm74i662);
                    map.put("dateField_lm74i65y", dateField_lm74i65y);
                    map.put("associationFormField_lm74i65z_id", associationFormField_lm74i65z_id);
                    map.put("textareaField_lm74i660", textareaField_lm74i660);
                    map.put("textareaField_lm74i661", textareaField_lm74i661);
                    map.put("textareaField_lm74i663", textareaField_lm74i663);
                    map.put("textareaField_lm74i664", textareaField_lm74i664);

                    list.add(map);
                }

                //封装新增的一条页签明细
                list.add(mapNew);


                Map mapMingxi = new HashMap<>();
                mapMingxi.put("tableField_lm74i665", list);

                mapAllString = JSON.toJSONString(mapMingxi);
                System.out.println("封装最新页签明细：" + mapAllString);
            }

            //更新子表控件数据【全量更新】
            try {
                com.aliyun.dingtalkyida_1_0.Client client = createClient();
                com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders updateFormDataHeaders = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders();
                updateFormDataHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
                com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest updateFormDataRequest = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest()
                        .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                        .setFormInstanceId(formInstanceId)
                        .setUserId("yida_pub_account")
                        .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4")
                        .setUpdateFormDataJson(mapAllString);

                UpdateFormDataResponse updateFormDataResponse = client.updateFormDataWithOptions(updateFormDataRequest, updateFormDataHeaders, new RuntimeOptions());
//                    System.out.println(JSON.toJSONString(updateFormDataResponse));
            } catch (TeaException err) {
                if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                    // err 中含有 code 和 message 属性，可帮助开发定位问题
                }
            } catch (Exception _err) {
                TeaException err = new TeaException(_err.getMessage(), _err);
                if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                    // err 中含有 code 和 message 属性，可帮助开发定位问题
                }
            }

        }

        log.info("商机档案页签更新写入结束。");

        return "商机档案页签更新写入结束。";
    }

    //商机周报通过后同步创建商机跟进表单
    @Override
    public String createShangjigenjin(String formInstanceId,String user,String time){

//        formInstanceId = "b9ea6302-f55f-4506-8a52-15a8abd599de";

        log.info("宜搭连接器获取商机周报表单实例ID：" + formInstanceId);
        log.info("宜搭连接器获取商机周报表单录入人员：" + user);
        log.info("宜搭连接器获取商机周报表单录入时间：" + time);

        String userid = user.replace("\"","")
                .replaceAll("\\[", "")
                .replaceAll("\\]", "");
        System.out.println("处理后的userid：" + userid);

        //查询宜搭表单实例详情
        JSONObject jsonObject = null;
        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDHeaders getFormDataByIDHeaders = new com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDHeaders();
            getFormDataByIDHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDRequest getFormDataByIDRequest = new com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4");

            GetFormDataByIDResponse formDataByIDWithOptions = client.getFormDataByIDWithOptions(formInstanceId, getFormDataByIDRequest, getFormDataByIDHeaders, new RuntimeOptions());
            String jsonString = JSON.toJSONString(formDataByIDWithOptions.getBody());
            jsonObject = JSON.parseObject(jsonString);
//            System.out.println("查询宜搭商机周报表单实例详情：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }

        if (!jsonObject.isEmpty()){
            JSONObject jsonObject1 = jsonObject.getJSONObject("formData");
            JSONArray tableFieldLjgi73ld = jsonObject1.getJSONArray("tableField_ljgi73ld");
            for (int i = 0; i < tableFieldLjgi73ld.size(); i++) {
                int xxx = i + 1;
                log.info("开始循环商机周报表单明细第" + xxx + "遍");

                JSONObject jsonObject2 = tableFieldLjgi73ld.getJSONObject(i);

                Map map = new HashMap();
                //录入人员
                map.put("employeeField_lkchu9sl_id",userid);
                //录入时间
                map.put("dateField_lkchu9sm",time);

                //跟进日期
                String dateField_lkdgvkx8 = jsonObject2.getString("dateField_lkdgvkx8");
                System.out.println("跟进日期：" + dateField_lkdgvkx8);
                map.put("dateField_lkdgvkx8",dateField_lkdgvkx8);
                //选择商机档案-关联表单
                String associationFormField_lkdg30xt_id = jsonObject2.getString("associationFormField_lkdg30xt_id");
                String shangjidanganStr = associationFormField_lkdg30xt_id.replace("\\","")
                        .replaceAll("\\[", "")
                        .replaceAll("\\]", "");
                String shangjidanganSubstring = shangjidanganStr.substring(1, shangjidanganStr.length() - 1);
                JSONObject shangjidanganJsonObject = JSON.parseObject(shangjidanganSubstring);
                System.out.println("选择商机档案-关联表单（实例ID）：" + shangjidanganJsonObject);
                //关联表单控件封装格式示例
                //{
                //    "formType":"receipt",
                //    "formUuid":"FORM-XN966G71JIOCJM8O9D2YVDJZNMVC2SYBNECKLE",
                //    "instanceId":"FINST-DJ9665819MNCGMDZCPWMDB45TRKE358D1QGKLJEK5",
                //    "subTitle":"上海普益信息技术有限公司",
                //    "appType":"APP_UNSAR4O4Y7NBDUYXLIP4",
                //    "title":"商机A"
                //}
                //表单类型
                String formTypeSJ = shangjidanganJsonObject.getString("formType");
                //表单ID
                String formUuidSJ = shangjidanganJsonObject.getString("formUuid");
                //表单实例ID
                String instanceIdSJ = shangjidanganJsonObject.getString("instanceId");
                //标题
                String subTitleSJ = shangjidanganJsonObject.getString("subTitle");
                //应用编码
                String appTypeSJ = shangjidanganJsonObject.getString("appType");
                //表单实例标题
                String titleSJ = shangjidanganJsonObject.getString("title");

                Map shiliMapSJ = new HashMap();
                shiliMapSJ.put("formType",formTypeSJ);
                shiliMapSJ.put("formUuid",formUuidSJ);
                shiliMapSJ.put("instanceId",instanceIdSJ);
                shiliMapSJ.put("subTitle",subTitleSJ);
                shiliMapSJ.put("appType",appTypeSJ);
                shiliMapSJ.put("title",titleSJ);

                List shiliListSJ = new ArrayList();
                shiliListSJ.add(shiliMapSJ);
                System.out.println("封装选择商机档案-关联表单：" + shiliListSJ);

                map.put("associationFormField_lkdg30xt",shiliListSJ);
                //商机阶段变更
                String numberField_lkjcdtbn = jsonObject2.getString("numberField_lkjcdtbn");
                System.out.println("商机阶段是否变更：" + numberField_lkjcdtbn);
                map.put("numberField_lkjfz0cg",numberField_lkjcdtbn);
                //商机编号
                String textField_lkdszuo1 = jsonObject2.getString("textField_lkdszuo1");
                System.out.println("商机编号：" + textField_lkdszuo1);
                map.put("textField_lkdszuo1",textField_lkdszuo1);
                //商机是否正常
                String selectField_lkdr3fnv = jsonObject2.getString("selectField_lkdr3fnv");
                System.out.println("商机是否正常：" + selectField_lkdr3fnv);
                map.put("selectField_lkdr3fnv",selectField_lkdr3fnv);
                //原始营销阶段
                String selectField_lkdr3fnw = jsonObject2.getString("selectField_lkdr3fnw");
                System.out.println("原始营销阶段：" + selectField_lkdr3fnw);
                map.put("selectField_lkjfz0cb",selectField_lkdr3fnw);
                //客户编号
                String textField_lkchxv7e = jsonObject2.getString("textField_lkchxv7e");
                System.out.println("客户编号：" + textField_lkchxv7e);
                map.put("textField_lkchxv7e",textField_lkchxv7e);
                //客户名称
                String textField_lkchxv7f = jsonObject2.getString("textField_lkchxv7f");
                System.out.println("客户名称：" + textField_lkchxv7f);
                map.put("textField_lkchxv7f",textField_lkchxv7f);
                //当前营销阶段
                String selectField_lkdq225w = jsonObject2.getString("selectField_lkdq225w");
                System.out.println("当前营销阶段：" + selectField_lkdq225w);
                map.put("selectField_lkjfz0cc",selectField_lkdq225w);
                //当前商机是否正常
                String selectField_lkdscrr1 = jsonObject2.getString("selectField_lkdscrr1");
                System.out.println("当前商机是否正常：" + selectField_lkdscrr1);
                map.put("selectField_lkdscrr1",selectField_lkdscrr1);
                //客户拜访记录-关联表单 prd 3.28 客户组件ID修改
                //String associationFormField_lk6vtbpj_id = jsonObject2.getString("associationFormField_lk6vtbpj_id");
                String associationFormField_lk6vtbpj_id = jsonObject2.getString("associationFormField_lkdg30xt_id");
                System.out.println("客户拜访记录-关联表单：" + associationFormField_lk6vtbpj_id);

                List shiliListBF = new ArrayList();
                Map shiliMapBF = new HashMap();
                try {
                    String kehubaifangStr = associationFormField_lk6vtbpj_id.replace("\\", "")
                            .replaceAll("\\[", "")
                            .replaceAll("\\]", "");
                    String kehubaifangSubstring = kehubaifangStr.substring(1, kehubaifangStr.length() - 1);
                    JSONObject kehubaifangObject = JSON.parseObject(kehubaifangSubstring);
                    System.out.println("选择客户拜访记录-关联表单（实例ID）：" + kehubaifangObject);
                    //关联表单控件封装格式示例
                    //{
                    //    "formType":"receipt",
                    //    "formUuid":"FORM-XN966G71JIOCJM8O9D2YVDJZNMVC2SYBNECKLE",
                    //    "instanceId":"FINST-DJ9665819MNCGMDZCPWMDB45TRKE358D1QGKLJEK5",
                    //    "subTitle":"上海普益信息技术有限公司",
                    //    "appType":"APP_UNSAR4O4Y7NBDUYXLIP4",
                    //    "title":"商机A"
                    //}
                    //表单类型
                    String formTypeBF = kehubaifangObject.getString("formType");
                    //表单ID
                    String formUuidBF = kehubaifangObject.getString("formUuid");
                    //表单实例ID
                    String instanceIdBF = kehubaifangObject.getString("instanceId");
                    //标题
                    String subTitleBF = kehubaifangObject.getString("subTitle");
                    //应用编码
                    String appTypeBF = kehubaifangObject.getString("appType");
                    //表单实例标题
                    String titleBF = kehubaifangObject.getString("title");


                    shiliMapBF.put("formType", formTypeBF);
                    shiliMapBF.put("formUuid", formUuidBF);
                    shiliMapBF.put("instanceId", instanceIdBF);
                    shiliMapBF.put("subTitle", subTitleBF);
                    shiliMapBF.put("appType", appTypeBF);
                    shiliMapBF.put("title", titleBF);
                }catch (Exception e){
                    e.printStackTrace();
                }

                shiliListBF.add(shiliMapBF);
                System.out.println("封装选择客户拜访记录-关联表单：" + shiliListBF);

                map.put("associationFormField_lk6vtbpj",shiliListBF);
                //拜访记录流水号
                String textField_lkdgvkx9 = jsonObject2.getString("textField_lkdgvkx9");
                System.out.println("拜访记录流水号：" + textField_lkdgvkx9);
                map.put("textField_lkdgvkx9",textField_lkdgvkx9);

                //上周工作进展
                String textareaField_llxlmstq = jsonObject2.getString("textareaField_llxlmstq");
                System.out.println("上周工作进展：" + textareaField_llxlmstq);
                map.put("textareaField_llxlzceo",textareaField_llxlmstq);

                //当前工作进展
                String textField_ljgw88jo = jsonObject2.getString("textareaField_llxlmstm");
                System.out.println("当前工作进展：" + textField_ljgw88jo);
                map.put("textareaField_llxlzcem",textField_ljgw88jo);
                //当前卡点与关键任务
                String textField_ljgw88jp = jsonObject2.getString("textareaField_llxlmstn");
                System.out.println("当前卡点与关键任务：" + textField_ljgw88jp);
                map.put("textareaField_llxlzcen",textField_ljgw88jp);
                //下一步任务
                String textField_ljgi73lo = jsonObject2.getString("textareaField_llxlmsto");
                System.out.println("下一步任务：" + textField_ljgi73lo);
                map.put("textareaField_lmeudgqu",textField_ljgi73lo);
                //所需资源
                String textField_ljgi73ls = jsonObject2.getString("textareaField_llxlmstp");
                System.out.println("所需资源：" + textField_ljgi73ls);
                map.put("textareaField_lleusw03",textField_ljgi73ls);
                //预计实行时间
                String dateField_ljgi73lq = jsonObject2.getString("dateField_ljgi73lq");
                System.out.println("预计实行时间：" + dateField_ljgi73lq);
                map.put("dateField_ljgi73lq",dateField_ljgi73lq);
                //附件
                String attachmentField_lkdhkxm0 = jsonObject2.getString("attachmentField_lkdhkxm0");
                System.out.println("附件：" + attachmentField_lkdhkxm0);
//                map.put("attachmentField_lkdhc264",attachmentField_lkdhkxm0);

                String jsonMapString = JSON.toJSONString(map);

                /**
                 * 封装最新的商机档案页签明细
                 * */
                Map mapNew = new HashMap<>();

                //TODO2 待完善：拜访记录-关联表单  需要【统一】进行关联组件的解析，然后封装成指定格式

                if (selectField_lkdq225w.equals("客户分析")) {
                    //下一步计划日期
                    mapNew.put("dateField_lkpqn4ke", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_lkpqn4jv", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_lkpqn4kk_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_llz1k48y", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_llz1k48z", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_llz1k490", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_llz1k491", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("建立信任")) {
                    //下一步计划日期
                    mapNew.put("dateField_lm0itw5b", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_lm0itw57", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_lm0itw58_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_lm0itw59", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_lm0itw5a", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_lm0itw5c", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_lm0itw5d", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("挖掘需求")) {
                    //下一步计划日期
                    mapNew.put("dateField_llz3h77q", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_llz3h77m", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_llz3h77n_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_llz3h77o", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_llz3h77p", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_llz3h77r", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_llz3h77s", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("呈现价值")) {
                    //下一步计划日期
                    mapNew.put("dateField_llz3h77y", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_llz3h77u", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_llz3h77v_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_llz3h77w", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_llz3h77x", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_llz3h77z", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_llz3h780", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("赢取承诺")) {
                    //下一步计划日期
                    mapNew.put("dateField_llz3h77i", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_llz3h77e", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_llz3h77f_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_llz3h77g", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_llz3h77h", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_llz3h77j", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_llz3h77k", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("回收账款")) {
                    //下一步计划日期
                    mapNew.put("dateField_lm0itw5j", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_lm0itw5f", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_lm0itw5g_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_lm0itw5h", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_lm0itw5i", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_lm0itw5k", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_lm0itw5l", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("商机暂停")) {
                    //下一步计划日期
                    mapNew.put("dateField_lm0itw5r", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_lm0itw5n", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_lm0itw5o_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_lm0itw5p", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_lm0itw5q", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_lm0itw5s", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_lm0itw5t", textField_ljgi73ls);

                } else if (selectField_lkdq225w.equals("商机取消") || selectField_lkdq225w.equals("商机丢失")) {
                    //下一步计划日期
                    mapNew.put("dateField_lm74i662", dateField_ljgi73lq);
                    //跟进日期
                    mapNew.put("dateField_lm74i65y", dateField_lkdgvkx8);
                    //拜访记录-关联表单
                    mapNew.put("associationFormField_lm74i65z_id", associationFormField_lk6vtbpj_id);
                    //当前工作进展
                    mapNew.put("textareaField_lm74i660", textField_ljgw88jo);
                    //当前卡点与关键问题
                    mapNew.put("textareaField_lm74i661", textField_ljgw88jp);
                    //下一步计划事项
                    mapNew.put("textareaField_lm74i663", textField_ljgi73lo);
                    //所需资源
                    mapNew.put("textareaField_lm74i664", textField_ljgi73ls);

                }

                //商机编号【测试】：XM-20230912-00056
                /**
                 * 调用写入商机档案页签明细的方法
                 * */
                getShangjiDanganMingxi(textField_lkdszuo1,selectField_lkdq225w,mapNew);

                //发起宜搭流程表单
                try {
                    com.aliyun.dingtalkyida_1_0.Client client = createClient();
                    com.aliyun.dingtalkyida_1_0.models.StartInstanceHeaders startInstanceHeaders = new com.aliyun.dingtalkyida_1_0.models.StartInstanceHeaders();
                    startInstanceHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
                    com.aliyun.dingtalkyida_1_0.models.StartInstanceRequest startInstanceRequest = new com.aliyun.dingtalkyida_1_0.models.StartInstanceRequest()
                            .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                            .setFormUuid("FORM-3J866L81SSMCLJDTD1DKJBY9KG5Q3YI3AHDKLG")
                            .setUserId(userid)
                            .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4")
                            .setProcessCode("TPROC--3J866L81SSMCLJDTD1DKJBY9KG5Q38K3AHDKLH")
                            .setFormDataJson(jsonMapString);

                    StartInstanceResponse startInstanceResponse = client.startInstanceWithOptions(startInstanceRequest, startInstanceHeaders, new RuntimeOptions());
                    String jsonString = JSON.toJSONString(startInstanceResponse.getBody());
                    JSONObject jsonObject11 = JSON.parseObject(jsonString);
                    System.out.println("发起宜搭流程表单：" + jsonObject11);
                } catch (TeaException err) {
                    if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                    }
                } catch (Exception _err) {
                    TeaException err = new TeaException(_err.getMessage(), _err);
                    if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                    }
                }
            }
        }

        return "商机周报通过后同步创建商机跟进表单，执行结束。";
    }

    //商机跟进表单通过后同步创建商机档案表的页签明细
    @Override
    public String createShangjiDangan(String formInstanceId){

//        formInstanceId = "5c7336ce-7387-4190-8a8e-f0c3d1393515";
        log.info("宜搭连接器获取商机跟进表单实例ID：" + formInstanceId);

        //查询宜搭表单实例详情
        JSONObject jsonObject = null;
        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDHeaders getFormDataByIDHeaders = new com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDHeaders();
            getFormDataByIDHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDRequest getFormDataByIDRequest = new com.aliyun.dingtalkyida_1_0.models.GetFormDataByIDRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4");

            GetFormDataByIDResponse formDataByIDWithOptions = client.getFormDataByIDWithOptions(formInstanceId, getFormDataByIDRequest, getFormDataByIDHeaders, new RuntimeOptions());
            String jsonString = JSON.toJSONString(formDataByIDWithOptions.getBody());
            jsonObject = JSON.parseObject(jsonString);
//            System.out.println("查询宜搭商机跟进记录表单实例详情：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }

        JSONObject jsonObject1 = jsonObject.getJSONObject("formData");
        System.out.println("formData：" + jsonObject1);

        //商机编号
        String textField_lkdszuo1 = jsonObject1.getString("textField_lkdszuo1");
        //当前营销阶段
        String selectField_lkjfz0cc = jsonObject1.getString("selectField_lkjfz0cc");

        //下一步计划日期
        String dateField_ljgi73lq = jsonObject1.getString("dateField_ljgi73lq");
        //跟进日期
        String dateField_lkdgvkx8 = jsonObject1.getString("dateField_lkdgvkx8");
        //拜访记录-关联表单
        String associationFormField_lk6vtbpj_id = jsonObject1.getString("associationFormField_lk6vtbpj_id");
        //当前工作进展
        String textareaField_llxlzcem = jsonObject1.getString("textareaField_llxlzcem");
        //当前卡点与关键问题
        String textareaField_llxlzcen = jsonObject1.getString("textareaField_llxlzcen");
        //下一步计划事项
        String textareaField_lmeudgqu = jsonObject1.getString("textareaField_lmeudgqu");
        //所需资源
        String textareaField_lleusw03 = jsonObject1.getString("textareaField_lleusw03");

        /**
         * 封装最新的商机档案页签明细
         * */
        Map mapNew = new HashMap<>();

        //TODO2 待完善：拜访记录-关联表单  需要【统一】进行关联组件的解析，然后封装成指定格式

        if (selectField_lkjfz0cc.equals("客户分析")) {
            //下一步计划日期
            mapNew.put("dateField_lkpqn4ke", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_lkpqn4jv", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_lkpqn4kk_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_llz1k48y", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_llz1k48z", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_llz1k490", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_llz1k491", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("建立信任")) {
            //下一步计划日期
            mapNew.put("dateField_lm0itw5b", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_lm0itw57", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_lm0itw58_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_lm0itw59", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_lm0itw5a", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_lm0itw5c", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_lm0itw5d", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("挖掘需求")) {
            //下一步计划日期
            mapNew.put("dateField_llz3h77q", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_llz3h77m", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_llz3h77n_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_llz3h77o", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_llz3h77p", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_llz3h77r", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_llz3h77s", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("呈现价值")) {
            //下一步计划日期
            mapNew.put("dateField_llz3h77y", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_llz3h77u", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_llz3h77v_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_llz3h77w", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_llz3h77x", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_llz3h77z", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_llz3h780", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("赢取承诺")) {
            //下一步计划日期
            mapNew.put("dateField_llz3h77i", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_llz3h77e", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_llz3h77f_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_llz3h77g", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_llz3h77h", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_llz3h77j", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_llz3h77k", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("回收账款")) {
            //下一步计划日期
            mapNew.put("dateField_lm0itw5j", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_lm0itw5f", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_lm0itw5g_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_lm0itw5h", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_lm0itw5i", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_lm0itw5k", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_lm0itw5l", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("商机暂停")) {
            //下一步计划日期
            mapNew.put("dateField_lm0itw5r", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_lm0itw5n", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_lm0itw5o_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_lm0itw5p", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_lm0itw5q", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_lm0itw5s", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_lm0itw5t", textareaField_lleusw03);

        } else if (selectField_lkjfz0cc.equals("商机取消") || selectField_lkjfz0cc.equals("商机丢失")) {
            //下一步计划日期
            mapNew.put("dateField_lm74i662", dateField_ljgi73lq);
            //跟进日期
            mapNew.put("dateField_lm74i65y", dateField_lkdgvkx8);
            //拜访记录-关联表单
            mapNew.put("associationFormField_lm74i65z_id", associationFormField_lk6vtbpj_id);
            //当前工作进展
            mapNew.put("textareaField_lm74i660", textareaField_llxlzcem);
            //当前卡点与关键问题
            mapNew.put("textareaField_lm74i661", textareaField_llxlzcen);
            //下一步计划事项
            mapNew.put("textareaField_lm74i663", textareaField_lmeudgqu);
            //所需资源
            mapNew.put("textareaField_lm74i664", textareaField_lleusw03);

        }

        //商机编号【测试】：XM-20230912-00056
        /**
         * 调用写入商机档案页签明细的方法
         * */
        getShangjiDanganMingxi(textField_lkdszuo1,selectField_lkjfz0cc,mapNew);

        return "商机跟进记录表单通过后同步创建商机档案表的页签明细";
    }

    //采购合同发起用印通过后同步更新合同用印表单关联采购合同表单控件
    @Override
    public String updateHetongyongyin(String caigouhetongliushuihao,String caigouhetongName){

//        caigouhetongliushuihao = "";
//        caigouhetongName = "";

        log.info("宜搭连接器获取采购合同发起用印表单的采购合同流水号：" + caigouhetongliushuihao);
        log.info("宜搭连接器获取采购合同发起用印表单的采购合同名称：" + caigouhetongName);


        //休眠5秒后，进行关联字段的查询，匹配到对应的采购合同表单实例ID，进行更新合同用印的采购合同关联表单控件
        try {
            log.info("休眠中，时长为：5秒...");
            Thread.sleep(5000);
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("休眠5秒结束，开始进行关联字段的查询，匹配到对应的采购合同表单实例ID，进行更新合同用印的采购合同关联表单控件===>");

        //查询采购合同用印申请档案表的实例ID
        //封装根据采购合同流水号作为查询条件
        Map mapAll = new HashMap();
        //查询采购合同流水号
        mapAll.put("textField_lmabq30w",caigouhetongliushuihao);

        String mapAllStr = JSON.toJSONString(mapAll);
        System.out.println("封装修改的字段参数：" + mapAllStr);

        JSONObject jsonObject = null;

        int size = 100;
        int page = 1;

        //【合同用印表】
        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.SearchFormDatasHeaders searchFormDatasHeaders = new com.aliyun.dingtalkyida_1_0.models.SearchFormDatasHeaders();
            searchFormDatasHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.SearchFormDatasRequest searchFormDatasRequest = new com.aliyun.dingtalkyida_1_0.models.SearchFormDatasRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setSearchFieldJson(mapAllStr)
                    .setCurrentPage(page)
                    .setPageSize(size)
                    .setFormUuid("FORM-4G8667A1V7TDM98J9DEV07KVU7ED2XD5BB0ML4")
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4");

            SearchFormDatasResponse searchFormDatasResponse = client.searchFormDatasWithOptions(searchFormDatasRequest, searchFormDatasHeaders, new RuntimeOptions());
//            System.out.println(JSON.toJSONString(searchFormDatasResponse.getBody()));
            String s = JSON.toJSONString(searchFormDatasResponse.getBody());
            jsonObject = JSON.parseObject(s);
//            System.out.println("钉钉高级条件查询【合同用印表】接口object对象：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }

        JSONArray data1 = jsonObject.getJSONArray("data");
//        System.out.println("返回的宜搭data：" + data1);

        //合同用印实例ID
        String hetongyongyinshiliID = data1.getJSONObject(0).getString("formInstanceId");
        System.out.println("合同用印实例ID：" + hetongyongyinshiliID);

        //【采购合同用印数据表】
        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.SearchFormDatasHeaders searchFormDatasHeaders = new com.aliyun.dingtalkyida_1_0.models.SearchFormDatasHeaders();
            searchFormDatasHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.SearchFormDatasRequest searchFormDatasRequest = new com.aliyun.dingtalkyida_1_0.models.SearchFormDatasRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setSearchFieldJson(mapAllStr)
                    .setCurrentPage(page)
                    .setPageSize(size)
                    .setFormUuid("FORM-4IA668916LVDD1E56XDKO8D2F15I3TC64F0ML0")
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4");

            SearchFormDatasResponse searchFormDatasResponse = client.searchFormDatasWithOptions(searchFormDatasRequest, searchFormDatasHeaders, new RuntimeOptions());
            System.out.println(JSON.toJSONString(searchFormDatasResponse.getBody()));
            String s = JSON.toJSONString(searchFormDatasResponse.getBody());
            jsonObject = JSON.parseObject(s);
            System.out.println("钉钉高级条件查询【采购合同用印数据表】接口object对象：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }

        JSONArray data = jsonObject.getJSONArray("data");
        System.out.println("返回的宜搭data：" + data);

        //采购合同实例ID
        String caigouhetongshiliID = data.getJSONObject(0).getString("formInstanceId");
        System.out.println("采购合同实例ID：" + caigouhetongshiliID);

        //======合同用印表单中的采购合同关联表单控件值======
        Map map = new HashMap();
        //表单类型：默认
        map.put("formType", "receipt");
        //表单ID
        map.put("formUuid", "FORM-4IA668916LVDD1E56XDKO8D2F15I3TC64F0ML0");
        //表单实例ID
        map.put("instanceId", caigouhetongshiliID);
        //标题
        map.put("subTitle", "系统同步");
        //应用编码appType
        map.put("appType", "APP_UNSAR4O4Y7NBDUYXLIP4");
        //表单实例标题
        map.put("title", caigouhetongName);
        System.out.println("采购合同关联表单控件值map：" + map);

        //转换成数组集合
        List list = new ArrayList();
        list.add(map);

        Map map1 = new HashMap();
        map1.put("associationFormField_llf5snhl",list);
        String mapString = JSON.toJSONString(map1);
        System.out.println("采购合同关联表单控件值list文本json：" + mapString);

        try {
            com.aliyun.dingtalkyida_1_0.Client client = createClient();
            com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders updateFormDataHeaders = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataHeaders();
            updateFormDataHeaders.xAcsDingtalkAccessToken = ddClient.getAccessToken();
            com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest updateFormDataRequest = new com.aliyun.dingtalkyida_1_0.models.UpdateFormDataRequest()
                    .setSystemToken("HP666C71JQMCDBUHBETIS992J3SW3XKUKBCKL53")
                    .setFormInstanceId(hetongyongyinshiliID)
                    .setUserId("yida_pub_account")
                    .setAppType("APP_UNSAR4O4Y7NBDUYXLIP4")
                    .setUpdateFormDataJson(mapString);

            UpdateFormDataResponse updateFormDataResponse = client.updateFormDataWithOptions(updateFormDataRequest, updateFormDataHeaders, new RuntimeOptions());
            String s = JSON.toJSONString(updateFormDataResponse);
            jsonObject = JSON.parseObject(s);
            System.out.println("【宜搭更新】表单object对象：" + jsonObject);
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }

        return "更新合同用印表单关联采购合同表单控件，执行结束。";
    }
}
