package com.malk.mc.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.malk.core.McProject;
import com.malk.mc.service.McYdService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.server.dingtalk.DDConf;
import com.malk.server.dingtalk.DDR;
import com.malk.server.dingtalk.DDR_New;
import com.malk.service.aliwork.YDClient;
import com.malk.service.aliwork.YDService;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Contacts;
import com.malk.service.dingtalk.DDClient_Notice;
import com.malk.service.dingtalk.DDClient_Workflow;
import com.malk.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class McYdServiceImpl implements McYdService {
    @Autowired
    private YDClient ydClient;
    @Autowired
    private DDClient ddClient;
    @Autowired
    private DDConf ddConf;
    @Autowired
    private YDConf ydConf;
    @Autowired
    private DDClient_Workflow ddClientWorkflow;

    @Autowired
    private DDClient_Notice ddClientNotice;

    @Autowired
    private DDClient_Contacts ddClient_contacts;
    @Override
    @Async
    public void updateFormDataVersion(Map map) {
        try {
            String pid = Objects.nonNull(map.get("pid")) ? map.get("pid").toString() : "";
            String formInstId = Objects.nonNull(map.get("formInstId")) ? map.get("formInstId").toString() : "";
            String formUuid = Objects.nonNull(map.get("formUuid")) ? map.get("formUuid").toString() : "";

            if (Strings.isNotBlank(pid)){
                MDC.put("MDC_KEY_PID",pid);
                if (Strings.isNotBlank(formInstId) && Strings.isBlank(formUuid)){
                    ydClient.operateData(YDParam.builder()
                            .formInstId(formInstId)
                            .useLatestVersion(true)
                            .updateFormDataJson("{}").build(), YDConf.FORM_OPERATION.update);
                }else if (Strings.isBlank(formInstId) && Strings.isNotBlank(formUuid)){
                    int i = 1;
                    int pageSize = 10;
                    DDR_New ddrNew = null;
                    do {
                        int i1 =1 ;
                        boolean flag1 = false;
                        while (i1<=5 && !flag1) {
                            try {
                                ddrNew = ydClient.queryData(YDParam.builder()
                                        .formUuid(formUuid)
                                        .pageNumber(i)
                                        .pageSize(pageSize)
                                        .build(), YDConf.FORM_QUERY.retrieve_search_form_id);

                                List<String> formInstIdList = (List<String>) ddrNew.getData();

                                //noExecuteExpression字段不传或传false  传true会导致实例版本不更新或滞后更新
                                ydClient.operateData(YDParam.builder()
                                        .formInstanceIdList(formInstIdList)
                                        .formUuid(formUuid)
                                        .useLatestFormSchemaVersion(true)
                                        .updateFormDataJson("{}")
                                        .noExecuteExpression(false)
                                        .asynchronousExecution(false)
                                        .build(),YDConf.FORM_OPERATION.multi_update);

                                i++;
                                flag1 = true;
                            }catch (Exception e){
                                log.error("更新表单实例版本失败:{}, 重试次数:{}",e.getMessage(),i1);
                                try {
                                    Thread.sleep(3000);
                                } catch (InterruptedException ex) {
                                    throw new RuntimeException(ex);
                                }
                                i1++;
                            }
                        }
                        if (i1 > 5){
                            throw new RuntimeException("更新表单实例版本失败，超过最大重试次数！");
                        }

                    }while (ddrNew.getTotalCount() > ddrNew.getPageNumber() * pageSize);
                }

                //发送工作通知给当前登陆人
                if (Objects.nonNull(map.get("userId"))){
                    //获取当前时间 精确到秒
                    String time = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
                    sendDdMsg(ddClient.getAccessToken(),ddConf.getAgentId(),map.get("userId").toString(),"表单实例版本已更新  时间：" + time);
                }
            }
        }catch (Exception e){
            //发送工作通知
            if (Objects.nonNull(map.get("userId"))){
                String time = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");//获取当前时间 精确到秒
                sendDdMsg(ddClient.getAccessToken(),ddConf.getAgentId(),map.get("userId").toString(),"更新表单实例版本异常：" + e.getMessage() + "  时间：" + time);
            }

            throw new RuntimeException(e);
        }

    }

    //发送工作通知
    public void sendDdMsg(String access_token,Number agent_id, String userid_list, String content){
        if (Objects.nonNull(userid_list)){
            Map param2 = new HashMap();
            param2.put("access_token",access_token);
            Map body3 = new HashMap();
            body3.put("agent_id",agent_id);
            body3.put("userid_list",userid_list);
            Map msg = new HashMap();
            msg.put("msgtype","text");
            //获取当前时间 精确到秒
            String time = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
            msg.put("text", UtilMap.map("content",content));
            body3.put("msg",msg);
            UtilHttp.doPost("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2",null,param2,body3);
        }
    }

    @Override
    public McR initiateApproval(Map map) {
        String pid = getString(map.get("pid"));
        String formUuid = getString(map.get("formUuid"));
        String processCode = getString(map.get("processCode"));
        String userId = getString(map.get("userId"));
        Map formData = (Map) map.get("formData");
        Map processData = new HashMap();
        if (Objects.nonNull(map.get("processData"))){
            processData = (Map) map.get("processData");
        }

        if (Strings.isNotBlank(pid)){
            //发起审批
            String[] yida = McProject.getYida(pid);
            Map<String,Object> body = new HashMap<>();
            if (!PublicUtil.isNull(pid) && yida != null && yida.length >= 2) {
                body.put("appType",yida[0]);
                body.put("systemToken",yida[1]);
            }
            body.put("userId",userId);
            body.put("formUuid",formUuid);
            body.put("processCode",processCode);
            body.put("formDataJson", JSONObject.toJSONString(formData));
            if (Objects.nonNull(processData) && processData.size() > 0){
                body.put("processData", JSONObject.toJSONString(processData));
            }

            UtilHttp.doPost("https://api.dingtalk.com/v1.0/yida/processes/instances/start", ddClient.initTokenHeader(), null, body, DDR_New.class);
            return McR.success();
        }
        return McR.errorParam("参数缺失！");
    }

    @Override
    public McR convertSubTableNumber(Map map) {
        String formInstId = getString(map.get("formInstId"));
        String tableField = getString(map.get("tableField"));
        String numberFieldCode = getString(map.get("numFileCodes"));
        String textFieldCode = getString(map.get("textFieldCodes"));

        if (Strings.isNotBlank(formInstId)){
            DDR_New ddrNew = ydClient.queryData(YDParam.builder()
                    .formInstId(formInstId)
                    .build(), YDConf.FORM_QUERY.retrieve_id);
            Map formData = ddrNew.getFormData();
            Map updateMap=new HashMap();
            if (!PublicUtil.isNull(tableField)&&tableField.startsWith("tableField_")) {
                List<Map<String, ?>> table = UtilMap.getList( formData,(tableField));
                for (Map item : table) {
                    String[] numberFieldCodes=numberFieldCode.split(",");
                    String[] textFieldCodes=textFieldCode.split(",");
                    for (int i = 0; i < numberFieldCodes.length; i++) {
                        String numstr=UtilMap.getNumberStr(item,numberFieldCodes[i]);
                        if(PublicUtil.isNull(numstr)||numstr.equals("0")){
                            updateMap.put(textFieldCodes[i],"0.00");
                            continue;
                        }
                        BigDecimal number = NumberUtil.toBigDecimal(numstr);
                        // 创建一个 DecimalFormat 实例，指定千位分隔符模式
                        DecimalFormat df = new DecimalFormat("#,###.00");
                        // 格式化 double 数值
                        String formattedText = df.format(number);
                        item.put(textFieldCodes[i],formattedText);
                    }
                }
                updateMap.put(tableField,getTableData(table)); // 添加一层过滤
            }

            if(!PublicUtil.isNull(map,"mainNumKeys","mainTextKeys")){
                String[] mainNumKeys=UtilMap.getString(map,"mainNumKeys").split(",");
                String[] mainTextKeys=UtilMap.getString(map,"mainTextKeys").split(",");
                for (int i = 0; i < mainNumKeys.length; i++) {
                    String numstr=UtilMap.getNumberStr(formData,(mainNumKeys[i]));
                    if(PublicUtil.isNull(numstr)||numstr.equals("0")){
                        updateMap.put(mainTextKeys[i],"0.00");
                        continue;
                    }
                    BigDecimal number = NumberUtil.toBigDecimal(numstr);
                    // 创建一个 DecimalFormat 实例，指定千位分隔符模式
                    DecimalFormat df = new DecimalFormat("#,###.00");
                    // 格式化 double 数值
                    String formattedText = df.format(number);
                    updateMap.put(mainTextKeys[i],formattedText);
                }
            }

            ydClient.operateData(YDParam.builder()
                    .formInstId(formInstId)
                    .useLatestVersion(true)
                    .updateFormDataJson(JSONObject.toJSONString(updateMap))
                    .build(), YDConf.FORM_OPERATION.update);
        }

        return McR.success();
    }

    @Override
    public McR saveApproverToForm(Map map) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String key = UtilMap.getString(map,"key");
        String formInstId = UtilMap.getString(map,"formInstId");
        JSONArray jsonArray=(JSONArray) ydClient.queryData(YDParam.builder().processInstanceId(formInstId).build(),YDConf.FORM_QUERY.retrieve_approval_record).getResult();
        List<String> userIds = new ArrayList<>();
        extractOperatorIds(jsonArray,userIds);
        ydClient.operateData(YDParam.builder().updateFormDataJson(JSONObject.toJSONString(UtilMap.map(key, userIds))).formInstId(formInstId).build(), YDConf.FORM_OPERATION.update);
        return McR.success();
    }

    @Override
    public McR multiLocationOutbound(Map map) {

        String formInstId = UtilMap.getString(map,"formInstId");//实例id(出库单)
        Map d = ydClient.queryData(YDParam.builder().formInstId(formInstId)
                .formUuid("FORM-3208DFE5463549A9A57FB94358A75E80PV5F")
                .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                .userId("332051151139376769").build(),YDConf.FORM_QUERY.retrieve_id).getFormData();
        System.out.println("出库单数据："+d);
        List<Map> mapList = (List<Map>)d.get("tableField_m8mo8506");
        System.out.println("出库单明细数据："+mapList);
        ArrayList<Object> entries = new ArrayList<>();
        for(Map map1 : mapList){
            String LBJId = map1.get("textField_ma1usp6o").toString();//零部件的实例id
            entries.add(LBJId);
            System.out.println("物料名称："+map1.get("textField_m99kztjo").toString());
            BigDecimal InventoryCount = BigDecimal.valueOf(0);//库存数量
            BigDecimal OutboundQuantity = BigDecimal.valueOf(0);//
            if(!map1.get("numberField_m9qdwvu1_value").toString().isEmpty()){
                InventoryCount = new BigDecimal( map1.get("numberField_m9qdwvu1").toString());
            }
            BigDecimal numberFieldM9qdwvu1 = InventoryCount.subtract( new BigDecimal( map1.get("numberField_m8mo8508").toString()));//存库数量 - 出库数量
            System.out.println("出库后数量："+numberFieldM9qdwvu1);

            Map d1 = ydClient.queryData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .userId("332051151139376769").build(),YDConf.FORM_QUERY.retrieve_id).getFormData();
            System.out.println(map1.get("textField_m99kztjo").toString()+"的数据明细："+ d1.get("tableField_m96gje14").toString());

            String s = d1.get("tableField_m96gje14").toString();
            String warehouse = JSON.parseArray(s).getJSONObject(0).get("selectField_m88f1qi8").toString();//所属仓库
            String WarehouseLocation = JSON.parseArray(s).getJSONObject(0).get("textField_m8mnxgql").toString();//所属库位
            String SafetyValue = JSON.parseArray(s).getJSONObject(0).get("numberField_m8873fn4").toString();//安全库存值

            ArrayList<Map<String,String>> maps_YD = new ArrayList<>();
            Map sss=new HashMap<>();
            sss.put("selectField_m88f1qi8",warehouse);//所属仓库
            sss.put("textField_m8mnxgql",WarehouseLocation);//所属库位
            sss.put("numberField_m8873fn4",SafetyValue);//安全库存值
            sss.put("numberField_m9c8n9go",numberFieldM9qdwvu1);//仓库数量
            maps_YD.add(sss);

            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("tableField_m96gje14",maps_YD)))
                    .userId("332051151139376769").build(),YDConf.FORM_OPERATION.update);
            BigDecimal InventoryQuantity = new BigDecimal( d1.get("numberField_m8obn6wk").toString());//库存数量
            BigDecimal FreezeQuantity = null;
            if(!d1.get("numberField_m8obn6wl_value").toString().isEmpty()){
                FreezeQuantity = new BigDecimal( d1.get("numberField_m8obn6wl").toString());//冻结数量
            }else {
                FreezeQuantity = BigDecimal.valueOf(0);
            }
            System.out.println("--------------------零部件子表数据修改完毕！---------------------");
            BigDecimal FreezeQuantity_AAA = FreezeQuantity.add( new BigDecimal( map1.get("numberField_m8mo8508").toString()));//冻结数量（新）
            BigDecimal AvailableQuantity_AAA = numberFieldM9qdwvu1;//可用存库数量（新）
            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_m8obn6wl, numberField_m8gz97nk",FreezeQuantity_AAA,AvailableQuantity_AAA)))
                    .userId("332051151139376769")
                    .build(),YDConf.FORM_OPERATION.update);
            System.out.println("----------------------零部件主表数据修改完毕！------------------");
        }

        return McR.success();
    }

    @Override
    public McR WarehouseStorage(Map map) {
        String formInstId = UtilMap.getString(map,"formInstId");//实例id(入库单)
        Map d = ydClient.queryData(YDParam.builder().formInstId(formInstId)
                .formUuid("FORM-6DED8EB4CF0446DB8841C7798E106F2886OH")
                .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                .userId("332051151139376769").build(),YDConf.FORM_QUERY.retrieve_id).getFormData();
        System.out.println("入库单数据："+d);
        List<Map> mapList = (List<Map>)d.get("tableField_m8mseubi");
        System.out.println("入库单明细数据："+mapList);
        ArrayList<Object> entries = new ArrayList<>();
        for(Map map1 : mapList){
            String LBJId = map1.get("textField_ma3jhnpr").toString();//零部件的实例id
            entries.add(LBJId);
            System.out.println("物料名称："+map1.get("textField_m9b1vd2w").toString());

            BigDecimal numberFieldM9qdwvu1 = new BigDecimal( map1.get("numberField_ma3jwnr3").toString() ).add(new BigDecimal( map1.get("numberField_m8mseubm").toString()));//存库数量 + 入库数量
            System.out.println("入库后数量："+numberFieldM9qdwvu1);

            Map d1 = ydClient.queryData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .userId("332051151139376769").build(),YDConf.FORM_QUERY.retrieve_id).getFormData();
            System.out.println(map1.get("textField_m9b1vd2w").toString()+"的数据明细："+ d1.get("tableField_m96gje14").toString());

            String s = d1.get("tableField_m96gje14").toString();
            String warehouse = JSON.parseArray(s).getJSONObject(0).get("selectField_m88f1qi8").toString();//所属仓库
            String WarehouseLocation = JSON.parseArray(s).getJSONObject(0).get("textField_m8mnxgql").toString();//所属库位
            String SafetyValue = JSON.parseArray(s).getJSONObject(0).get("numberField_m8873fn4").toString();//安全库存值

            ArrayList<Map<String,String>> maps_YD = new ArrayList<>();
            Map sss=new HashMap<>();
            sss.put("selectField_m88f1qi8",warehouse);//所属仓库
            sss.put("textField_m8mnxgql",WarehouseLocation);//所属库位
            sss.put("numberField_m8873fn4",SafetyValue);//安全库存值
            sss.put("numberField_m9c8n9go",numberFieldM9qdwvu1);//仓库数量
            maps_YD.add(sss);

            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("tableField_m96gje14",maps_YD)))
                    .userId("332051151139376769").build(),YDConf.FORM_OPERATION.update);
            BigDecimal InventoryQuantity = null;
            if(!d1.get("numberField_m8gz97nk").toString().isEmpty()){
                InventoryQuantity = new BigDecimal( d1.get("numberField_m8gz97nk").toString());//可用库存数量
            }else {
                InventoryQuantity = BigDecimal.valueOf(0);
            }
            BigDecimal VirtualQuantity = null;
            if(!d1.get("numberField_m8obn6wq_value").toString().isEmpty()){
                VirtualQuantity = new BigDecimal( d1.get("numberField_m8obn6wq").toString());//虚拟数量
            }else {
                VirtualQuantity = BigDecimal.valueOf(0);
            }

            System.out.println("--------------------零部件子表数据修改完毕！---------------------");

            BigDecimal VirtualQuantity_AAA = VirtualQuantity.add( new BigDecimal( map1.get("numberField_m8mseubm").toString()));//虚拟数量（新）
            BigDecimal AvailableQuantity_AAA = InventoryQuantity .add (new BigDecimal( map1.get("numberField_m8mseubm").toString()));//可用存库数量（新）
            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_m8obn6wq, numberField_m8gz97nk",VirtualQuantity_AAA,AvailableQuantity_AAA)))
                    .userId("332051151139376769")
                    .build(),YDConf.FORM_OPERATION.update);
            System.out.println("----------------------零部件主表数据修改完毕！------------------");
        }
        return McR.success();
    }

    @Override
    public McR ProductReturn(Map map) {
        String formInstId = UtilMap.getString(map,"formInstId");//实例id(退货单)
        Map data = ydClient.queryData(YDParam.builder().formInstId(formInstId)
                .formUuid("FORM-E40A23253F2F43FEAF0E40F274A67B34OQC9")
                .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
        System.out.println("退货单："+data);
        List<Map> mapList = (List<Map>)data.get("tableField_m99koj01");
        System.out.println("退货单明细数据："+mapList);
        ArrayList<Object> entries = new ArrayList<>();
        for (Map map1 : mapList){
            String LBJId = map1.get("textField_maki4tqo").toString();//零部件的实例id
            System.out.println("物料名称："+map1.get("textField_m99koj02").toString());//物料名称
            System.out.println("所属库位："+map1.get("textField_maki4tqp").toString());//产品库位
            System.out.println("退货数量："+map1.get("numberField_ma0e13js").toString());//退货数量


            Map data1 = ydClient.queryData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
            System.out.println(map1.get("textField_m99koj02")+"明细表数据："+data1.get("tableField_m96gje14"));//零部件明细
            String s = data1.get("tableField_m96gje14").toString();
            String warehouse = JSON.parseArray(s).getJSONObject(0).get("selectField_m88f1qi8").toString();//所属仓库
            String WarehouseLocation = JSON.parseArray(s).getJSONObject(0).get("textField_m8mnxgql").toString();//所属库位
            String SafetyValue = JSON.parseArray(s).getJSONObject(0).get("numberField_m8873fn4").toString();//安全库存值
            BigDecimal numberFieldM9c8n9go = new BigDecimal(JSON.parseArray(s).getJSONObject(0).get("numberField_m9c8n9go").toString());//仓库数量

            BigDecimal numm = new BigDecimal(map1.get("numberField_ma0e13js").toString()).add(numberFieldM9c8n9go); //退货后的库存数量 =  退货数量 + 仓库数量（子）

            ArrayList<Map<String,String>> maps_YD = new ArrayList<>();
            Map sss=new HashMap<>();
            sss.put("selectField_m88f1qi8",warehouse);//所属仓库
            sss.put("textField_m8mnxgql",WarehouseLocation);//所属库位
            sss.put("numberField_m8873fn4",SafetyValue);//安全库存值
            sss.put("numberField_m9c8n9go",numm);//仓库数量
            maps_YD.add(sss);

            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("tableField_m96gje14",maps_YD)))
                    .userId("332051151139376769").build(),YDConf.FORM_OPERATION.update);

            BigDecimal InventoryQuantity = null;
            if(!data1.get("numberField_m8gz97nk_value").toString().isEmpty()){
                InventoryQuantity = new BigDecimal( data1.get("numberField_m8gz97nk").toString());//可用库存数量
            }else {
                InventoryQuantity = BigDecimal.valueOf(0);
            }

            BigDecimal FreezeQuantity = null;
            if(!data1.get("numberField_m8obn6wl_value").toString().isEmpty()){
                FreezeQuantity = new BigDecimal( data1.get("numberField_m8obn6wl").toString());//冻结数量
            }else {
                FreezeQuantity = BigDecimal.valueOf(0);
            }
            System.out.println("---------------------------------------零部件子表数据修改完毕--------------------------------");

            BigDecimal FreezeQuantity_AAA = FreezeQuantity.subtract( new BigDecimal( map1.get("numberField_ma0e13js").toString()));//冻结数量（新） = 冻结数量 - 退货数量
            BigDecimal AvailableQuantity_AAA = InventoryQuantity.add(new BigDecimal( map1.get("numberField_ma0e13js").toString()));//可用存库数量（新） = 可用库存数量 + 退货数量
            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_m8obn6wl, numberField_m8gz97nk",FreezeQuantity_AAA,AvailableQuantity_AAA)))
                    .userId("332051151139376769")
                    .build(),YDConf.FORM_OPERATION.update);
            System.out.println("----------------------零部件主表数据修改完毕！------------------");

        }
        return McR.success();
    }

    @Override
    public McR allocateTransfer(Map map) {
        String formInstId = UtilMap.getString(map,"formInstId");//实例id(调拨流程)
        Map data = ydClient.queryData(YDParam.builder().formInstId(formInstId)
                .formUuid("FORM-C6360F0CA28543E8BA99A1DDCF90964FSJEM")
                .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
        System.out.println("调拨单："+data);
        List<Map> mapList = (List<Map>)data.get("tableField_m9grj2ih");
        System.out.println("调拨明细数据："+mapList);
        ArrayList<Object> entries = new ArrayList<>();
        for (Map map1 : mapList){
            //TODO:1、调出零部件处理
            String LBJId = map1.get("textField_mawbt28t").toString();//（调出）零部件的实例id
            System.out.println("调出的所属库位："+map1.get("textField_m9grj2il").toString());//产品库位
            System.out.println("调出的所属仓库："+map1.get("textField_m9grj2ik").toString());//产品仓库
            System.out.println("调拨前数量："+map1.get("numberField_m9gs7k82").toString());//调拨前数量


            Map data1 = ydClient.queryData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

            String s = data1.get("tableField_m96gje14").toString();
            String warehouse = JSON.parseArray(s).getJSONObject(0).get("selectField_m88f1qi8").toString();//所属仓库
            String WarehouseLocation = JSON.parseArray(s).getJSONObject(0).get("textField_m8mnxgql").toString();//所属库位
            String SafetyValue = JSON.parseArray(s).getJSONObject(0).get("numberField_m8873fn4").toString();//安全库存值
            BigDecimal numberFieldM9c8n9go = new BigDecimal(JSON.parseArray(s).getJSONObject(0).get("numberField_m9c8n9go").toString());//仓库数量

            BigDecimal numm = numberFieldM9c8n9go.subtract(new BigDecimal(map1.get("numberField_m9grj2ij").toString())); //调拨后的库存数量 =  仓库数量 - 调拨数量

            ArrayList<Map<String,String>> maps_YD = new ArrayList<>();
            Map sss=new HashMap<>();
            sss.put("selectField_m88f1qi8",warehouse);//所属仓库
            sss.put("textField_m8mnxgql",WarehouseLocation);//所属库位
            sss.put("numberField_m8873fn4",SafetyValue);//安全库存值
            sss.put("numberField_m9c8n9go",numm);//仓库数量
            maps_YD.add(sss);

            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("tableField_m96gje14",maps_YD)))
                    .userId("332051151139376769").build(),YDConf.FORM_OPERATION.update);

            //TODO:2、调入零部件处理
            String LBJId2 = map1.get("textField_maxendgs").toString();//（调入）零部件的实例id
            System.out.println("调入的所属库位："+map1.get("textField_m9grj2il").toString());//产品库位
            System.out.println("调入的所属仓库："+map1.get("textField_m9grj2ik").toString());//产品仓库
            System.out.println("调拨前数量："+map1.get("numberField_maz574d0").toString());//调拨前数量

            Map data2 = ydClient.queryData(YDParam.builder().formInstId(LBJId2)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

            String s1 = data2.get("tableField_m96gje14").toString();
            String warehouse1 = JSON.parseArray(s1).getJSONObject(0).get("selectField_m88f1qi8").toString();//所属仓库
            String WarehouseLocation1 = JSON.parseArray(s1).getJSONObject(0).get("textField_m8mnxgql").toString();//所属库位
            String SafetyValue1 = JSON.parseArray(s1).getJSONObject(0).get("numberField_m8873fn4").toString();//安全库存值
            BigDecimal numberFieldM9c8n9go1 = new BigDecimal(JSON.parseArray(s1).getJSONObject(0).get("numberField_m9c8n9go").toString());//仓库数量

            BigDecimal numm1 = numberFieldM9c8n9go1.add(new BigDecimal(map1.get("numberField_m9grj2ij").toString())); //调拨后的库存数量 =  仓库数量 + 调拨数量

            ArrayList<Map<String,String>> maps_YD1 = new ArrayList<>();
            Map sss1=new HashMap<>();
            sss1.put("selectField_m88f1qi8",warehouse1);//所属仓库
            sss1.put("textField_m8mnxgql",WarehouseLocation1);//所属库位
            sss1.put("numberField_m8873fn4",SafetyValue1);//安全库存值
            sss1.put("numberField_m9c8n9go",numm1);//仓库数量
            maps_YD1.add(sss1);

            ydClient.operateData(YDParam.builder().formInstId(LBJId2)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("tableField_m96gje14",maps_YD1)))
                    .userId("332051151139376769").build(),YDConf.FORM_OPERATION.update);


            BigDecimal AvailableInventoryQuantity_AAA = null;
            BigDecimal InventoryQuantity_AAA = null;
            if(!data1.get("numberField_m8gz97nk_value").toString().isEmpty()){
                AvailableInventoryQuantity_AAA = new BigDecimal( data1.get("numberField_m8gz97nk").toString());//可用库存数量
            }else {
                AvailableInventoryQuantity_AAA = BigDecimal.valueOf(0);
            }
            if(!data1.get("numberField_m8obn6wk_value").toString().isEmpty()){
                InventoryQuantity_AAA = new BigDecimal(data1.get("numberField_m8obn6wk").toString());//存库数量
            }else {
                InventoryQuantity_AAA = BigDecimal.valueOf(0);
            }

            BigDecimal AvailableInventoryQuantity_BBB = null;
            BigDecimal InventoryQuantity_BBB = null;
            if(!data2.get("numberField_m8gz97nk_value").toString().isEmpty()){
                AvailableInventoryQuantity_BBB = new BigDecimal( data2.get("numberField_m8gz97nk").toString());//可用库存数量
            }else {
                AvailableInventoryQuantity_BBB = BigDecimal.valueOf(0);
            }
            if(!data2.get("numberField_m8obn6wk_value").toString().isEmpty()){
                InventoryQuantity_BBB = new BigDecimal(data2.get("numberField_m8obn6wk").toString());//可用库存数量
            }else {
                InventoryQuantity_BBB = BigDecimal.valueOf(0);
            }


            System.out.println("---------------------------------------零部件子表数据修改完毕--------------------------------");

//            BigDecimal FreezeQuantity_AAA = FreezeQuantity.subtract( new BigDecimal( map1.get("numberField_ma0e13js").toString()));//冻结数量（新） = 冻结数量 - 退货数量
            //TODO:3、调出物料的可用库存
            BigDecimal AvailableQuantity_AAA = AvailableInventoryQuantity_AAA.subtract(new BigDecimal( map1.get("numberField_m9grj2ij").toString()));//可用存库数量（新） = 可用库存数量 - 调拨数量
            BigDecimal Quantity_AAA = InventoryQuantity_AAA.subtract(new BigDecimal(map1.get("numberField_m9grj2ij").toString()));//存库数量（新） = 存库数量 - 调拨数量
            //TODO：4、调入物料的可用库存
            BigDecimal AvailableQuantity_BBB = AvailableInventoryQuantity_BBB.add(new BigDecimal( map1.get("numberField_m9grj2ij").toString()));//可用存库数量（新） = 可用库存数量 + 调拨数量
            BigDecimal Quantity_BBB = InventoryQuantity_BBB.add(new BigDecimal(map1.get("numberField_m9grj2ij").toString()));
            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_m8gz97nk, numberField_m8obn6wk",AvailableQuantity_AAA,Quantity_AAA)))
                    .userId("332051151139376769")
                    .build(),YDConf.FORM_OPERATION.update);

            ydClient.operateData(YDParam.builder().formInstId(LBJId2)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_m8gz97nk, numberField_m8obn6wk",AvailableQuantity_BBB,Quantity_BBB)))
                    .userId("332051151139376769")
                    .build(),YDConf.FORM_OPERATION.update);
            System.out.println("----------------------零部件主表数据修改完毕！------------------");

        }
        return McR.success();
    }

    @Override
    public McR goodsInandOut(Map map) {
        String formInstId = UtilMap.getString(map,"formInstId");//实例id(进货退货流程)
        Map data = ydClient.queryData(YDParam.builder().formInstId(formInstId)
                .formUuid("FORM-0D92610ED4AA4B81988CA190EE9A119EX37F")
                .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
        System.out.println("进货退货流程："+data);
        List<Map> mapList = (List<Map>)data.get("tableField_ml07z7bh");
        System.out.println("退货单明细数据："+mapList);
        ArrayList<Object> entries = new ArrayList<>();
        for (Map map1 : mapList){
            String LBJId = map1.get("textField_ml09d5ys").toString();//零部件的实例id
            System.out.println("物料名称："+map1.get("textField_ml07z7bi").toString());//物料名称
            System.out.println("所属库位："+map1.get("textField_ml09riy7").toString());//产品库位
            System.out.println("退货数量："+map1.get("numberField_ml07z7bp").toString());//退货数量


            Map data1 = ydClient.queryData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .userId("332051151139376769").build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
            System.out.println(map1.get("textField_ml07z7bi")+"明细表数据："+data1.get("tableField_m96gje14"));//零部件明细
            String s = data1.get("tableField_m96gje14").toString();
            String warehouse = JSON.parseArray(s).getJSONObject(0).get("selectField_m88f1qi8").toString();//所属仓库
            String WarehouseLocation = JSON.parseArray(s).getJSONObject(0).get("textField_m8mnxgql").toString();//所属库位
            String SafetyValue = JSON.parseArray(s).getJSONObject(0).get("numberField_m8873fn4").toString();//安全库存值
            BigDecimal numberFieldM9c8n9go = new BigDecimal(JSON.parseArray(s).getJSONObject(0).get("numberField_m9c8n9go").toString());//仓库数量

//             new BigDecimal(map1.get("numberField_ma0e13js").toString()).add(numberFieldM9c8n9go);
            BigDecimal numm = numberFieldM9c8n9go.subtract(new BigDecimal(map1.get("numberField_ml07z7bp").toString()));//退货后的库存数量 =   仓库数量（子） - 退货数量

            ArrayList<Map<String,String>> maps_YD = new ArrayList<>();
            Map sss=new HashMap<>();
            sss.put("selectField_m88f1qi8",warehouse);//所属仓库
            sss.put("textField_m8mnxgql",WarehouseLocation);//所属库位
            sss.put("numberField_m8873fn4",SafetyValue);//安全库存值
            sss.put("numberField_m9c8n9go",numm);//仓库数量
            maps_YD.add(sss);

            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("tableField_m96gje14",maps_YD)))
                    .userId("332051151139376769").build(),YDConf.FORM_OPERATION.update);

            BigDecimal InventoryQuantity = null;
            if(!data1.get("numberField_m8gz97nk_value").toString().isEmpty()){
                InventoryQuantity = new BigDecimal( data1.get("numberField_m8gz97nk").toString());//可用库存数量
            }else {
                InventoryQuantity = BigDecimal.valueOf(0);
            }

            BigDecimal VirtualQuantity1 = null;
            if(!data1.get("numberField_m8obn6wl_value").toString().isEmpty()){
                VirtualQuantity1 = new BigDecimal( data1.get("numberField_m8obn6wl").toString());//虚拟数量
            }else {
                VirtualQuantity1 = BigDecimal.valueOf(0);
            }
            System.out.println("---------------------------------------零部件子表数据修改完毕--------------------------------");

            BigDecimal VirtualQuantity1_AAA = VirtualQuantity1.subtract( new BigDecimal( map1.get("numberField_ml07z7bp").toString()));//虚拟数量（新） = 虚拟数量 - 退货数量
            BigDecimal AvailableQuantity_AAA = InventoryQuantity.subtract(new BigDecimal( map1.get("numberField_ml07z7bp").toString()));//可用存库数量（新） = 可用库存数量 - 退货数量
            ydClient.operateData(YDParam.builder().formInstId(LBJId)
                    .formUuid("FORM-2353BE632D97434DAD55AEBD0866C44DVAT5")
                    .appType("APP_GNQ1RGK68X4JNZUEU8RQ")
                    .systemToken("XH866P81QDOTQ0218TGZM9WCNXOF24WIP488M7A5")
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("numberField_m8obn6wq, numberField_m8gz97nk",VirtualQuantity1_AAA,AvailableQuantity_AAA)))
                    .userId("332051151139376769")
                    .build(),YDConf.FORM_OPERATION.update);
            System.out.println("----------------------零部件主表数据修改完毕！------------------");

        }
        return McR.success();

    }

    @Override
    public McR shareRecord(Map data) {
        String pid = getString(data.get("pid"));
        if (Strings.isNotBlank(pid)){
            //发起审批
            String[] dd = McProject.getDd(pid);
            String appKey = "";
            String appSecret = "";
            String agentId = "";
            if (!PublicUtil.isNull(pid) && dd != null && dd.length >= 3) {
                appKey = dd[0];
                appSecret = dd[1];
                agentId = dd[2];
            }
            String[] yida = McProject.getYida(pid);
            String appType = "";
            String systemToken = "";
            if (!PublicUtil.isNull(pid) && dd != null && dd.length >= 2) {
                appType = yida[0];
                systemToken = yida[1];
            }

            // 执行分享逻辑 [ 拼接后的权限组 ]
            McException.assertParamException_Null(data, "compId, userIds, userId, atUserId, isNotice");
            //开始抓取宜搭对应的子表数据
            String processInstanceId = data.get("processInstanceId").toString();
            List<String> atUserId = (List<String>) data.get("atUserId");

            Map map = new HashMap();
            map.put(UtilMap.getString(data, "compId"),UtilMap.getList(data, "userIds"));

            ydClient.operateData(YDParam.builder()
                    .appType(appType)
                    .systemToken(systemToken)
                    .formInstId(processInstanceId)
                    .updateFormDataJson(JSON.toJSONString(map))
                    .build(), YDConf.FORM_OPERATION.update);

            String userIds = String.valueOf(data.get("atUserId"));
            String[] split = userIds.split(",");
            ArrayList<String> names = new ArrayList<>();
            for(Object auserId : split){
                DDR_New ddrNew = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/v2/user/get", null, ddClient.initTokenParams(), UtilMap.map("userid", auserId), DDR_New.class);
                Object result = ddrNew.getResult();
                if (result != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(result));
                    String name = jsonObject.getString("name");
                    names.add(name);
                }
            }
            List<Map> searchCondition = new ArrayList<>();
            searchCondition.addAll(Arrays.asList(
                    YDConf.searchCondition_TextFiled(data.get("processKey").toString(), data.get("processInstanceId").toString(),"eq")
            ));
            String title = UtilMap.getString(data, "content").isEmpty() || !ObjectUtil.isNotNull(UtilMap.getString(data, "content")) ? "单据分享" : UtilMap.getString(data, "content");
            ydClient.operateData(YDParam.builder()
                    .appType(appType)
                    .systemToken(systemToken)
                    .searchCondition(JSON.toJSONString(searchCondition))
                    .formUuid(data.get("formId").toString())
                    .formDataJson(JSON.toJSONString(UtilMap.map(data.get("processKey").toString()+", "+data.get("empKey").toString()+", "+data.get("titleKey").toString(),
                            data.get("processInstanceId").toString(),UtilMap.getList(data, "userIds"),title)))
                    .useLatestVersion(true)
                    .build(), YDConf.FORM_OPERATION.upsert);//upsert_v2
            if (UtilMap.getBoolean(data, "isNotice")) {
//                data.put("content", names.stream().collect(Collectors.joining(",")) + UtilMap.getString(data, "content"));
                String appUrl = data.get("appUrl").toString();
                String pcUrl = data.get("pcUrl").toString();
                if ( !ObjectUtil.isNotNull(UtilMap.getString(data,"sendType")) || UtilMap.getString(data,"sendType").isEmpty() || "工作通知".equals(UtilMap.getString(data,"sendType"))){
                    Map<String, String> contentMap = new HashMap<>();
                    contentMap.put("title",title);
                    contentMap.put("text",title);
                    contentMap.put("picUrl",pcUrl+"/processDetail"+"?procInsId="+processInstanceId);
                    contentMap.put("messageUrl",pcUrl+"/processDetail"+"?procInsId="+processInstanceId);

                    Map<String, Object> msg = new HashMap<>();
                    msg.put("msgtype","link");
                    msg.put("link",contentMap);
                    sendNotification(ddClient.getAccessToken(appKey,appSecret),atUserId,null,false,msg,agentId);

                } else if ("钉钉代办".equals(UtilMap.getString(data, "sendType"))) {
                    /*查询钉钉人员*/
                    ddClientWorkflow.createTBTask(ddClient.getAccessToken(appKey,appSecret),UtilMap.getString(data, "userId"),"流程分享："+UtilMap.getString(data, "content"),UtilMap.getString(data, "content"),
                            UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().plusDays(7).atTime(LocalTime.MAX)), atUserId,null
                            ,UtilMap.map("appUrl, pcUrl",appUrl+"/processDetail"+"?procInsId="+processInstanceId,pcUrl+"/processDetail"+"?procInsId="+processInstanceId),false,30,UtilMap.map("dingNotify","1"));
                }
            }
            return McR.success();
        }
        return McR.errorParam("参数缺失！");
    }

    @Override
    public McR approveTask(Map map) {
        String outResult = UtilMap.getString(map, "outResult");
        String userId = UtilMap.getString(map, "userId");

        String processInstanceId = UtilMap.getString(map, "processInstanceId");
        String activityId = UtilMap.getString(map, "activityId");
        long taskId = 0;

        //获取审批记录
        List<Map> result = (List<Map>) ydClient.queryData(YDParam.builder()
                .processInstanceId(processInstanceId)
                .build(),YDConf.FORM_QUERY.retrieve_approval_record).getResult();
        for (Map approveInfo : result) {
            if (activityId.equals(UtilMap.getString(approveInfo, "activityId"))){
                //获取taskId
                taskId = UtilMap.getLong(approveInfo, "taskId");
                break;
            }
        }

        //审批
        Map body = new HashMap();
        body.put("outResult",outResult);//AGREE：同意 DISAGREE：拒绝。
        body.put("appType",ydConf.getAppType());
        body.put("systemToken",ydConf.getSystemToken());
        body.put("remark",outResult);//审批意见
        body.put("processInstanceId",processInstanceId);//审批实例id
        body.put("userId",userId);//用户userid
        body.put("taskId",taskId);//任务id

        UtilHttp.doPost("https://api.dingtalk.com/v1.0/yida/tasks/execute", ddClient.initTokenHeader(), null,body);

        return McR.success();
    }

    @Override
    public McR syncYdAuth(Map map) {
        String matrixId = UtilMap.getString(map, "matrixId");//矩阵id
        String corpId = UtilMap.getString(map, "corpId");//corpId
        String userId = UtilMap.getString(map, "userId");//userId
        String corpToken = UtilMap.getString(map, "corpToken");//corpToken
        String formInstId = UtilMap.getString(map, "formInstId");//实例id
        String[] conditionColumns = UtilMap.getString(map, "conditionColumns").split(",");//矩阵表条件列（名称）
        String[] conditionColumnValues = UtilMap.getString(map, "conditionColumnValues").split(",");;//矩阵表条件列值
        String[] resultColumns = UtilMap.getString(map, "resultColumns").split(",");;//矩阵表角色列（名称）
        String[] resultColumnFields = UtilMap.getString(map, "resultColumnFields").split(",");;//矩阵表角色列值（表单成员组件唯一标识）

        String[] conditionColumnIds = new String[conditionColumns.length];//矩阵表条件列（id）
        String[] resultColumnIds = new String[resultColumns.length];//矩阵表角色列（id）
        List<List<String>> resultColumnValues = new ArrayList<>();//矩阵表角色列值

        //md5加密corpId,userId,corpToken
        String token = DigestUtils.md5Hex(corpId + userId + corpToken).toUpperCase();

        //查询矩阵明细
        Map params = new HashMap();

        int pageSize = 100;
        int pageNumber = 0;

        params.put("pageSize",pageSize);
        params.put("matrixId",matrixId);
        params.put("corpId",corpId);
        params.put("userId",userId);
        params.put("token",token);

        List<Map> data = new ArrayList<>();

        String rowId = "";

        int totalCount = 0;

        //查询矩阵所有明细
        do {
            pageNumber++;
            params.put("pageNumber",pageNumber);
            DDR_New ddrNew = (DDR_New) UtilHttp.doGet("https://api.dingtalk.com/v2.0/yida/forms/resources/matrices", ddClient.initTokenHeader(), params, DDR_New.class);

            Map result = (Map) ddrNew.getResult();

            Map matrixData = UtilMap.getMap(result, "matrixData");

            data.addAll(UtilMap.getList(matrixData, "data"));

            totalCount = UtilMap.getInt(matrixData, "totalCount");

            //查询列id
            if (pageNumber == 1){
                Map matrixTable = UtilMap.getMap(result, "matrixTable");

                List<Map> conditionColumnList = UtilMap.getList(matrixTable, "conditionColumn");
                List<Map> resultColumnList = UtilMap.getList(matrixTable, "resultColumn");

                for (int i = 0; i < conditionColumns.length; i++) {
                    int finalI = i;
                    Map conditionColumn = conditionColumnList.stream().filter(item -> UtilMap.getString(item, "name").equals(conditionColumns[finalI])).findFirst().orElse(null);
                    conditionColumnIds[i] = UtilMap.getString(conditionColumn, "columnId");
                }

                for (int i = 0; i < resultColumns.length; i++) {
                    int finalI = i;
                    Map resultColumn = resultColumnList.stream().filter(item -> UtilMap.getString(item, "name").equals(resultColumns[finalI])).findFirst().orElse(null);
                    resultColumnIds[i] = UtilMap.getString(resultColumn, "columnId");
                }
            }
        }while (pageNumber * pageSize < totalCount);

        //查询实例详情获取矩阵表角色列值
        Map formData = ydClient.queryData(YDParam.builder().formInstId(formInstId).build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

        for (int i = 0; i < resultColumnFields.length; i++) {
            List<String> employees = UtilMap.getList(formData, resultColumnFields[i] + "_id");

            resultColumnValues.add(employees);
        }

        boolean flag = false;
        //查询该矩阵条件是否存在
        for (Map datum : data) {
            for (int i = 0; i < conditionColumnIds.length; i++) {
                if (conditionColumnValues[i].equals(UtilMap.getString(datum, conditionColumnIds[i]))){
                    flag = true;
                }else {
                    flag = false;
                }
            }

            if (flag){
                rowId = UtilMap.getString(datum, "rowId");
                break;
            }
        }

        //更新矩阵明细
        Map matrixData = new HashMap();
        if (Strings.isBlank(rowId)){
            matrixData.put("isNew",1);//新增
        }else {
            matrixData.put("rowId",rowId);//更新
        }
        for (int i = 0; i < conditionColumnIds.length; i++) {
            matrixData.put(conditionColumnIds[i],conditionColumnValues[i]);
        }
        for (int i = 0; i < resultColumnIds.length; i++) {
            matrixData.put(resultColumnIds[i],resultColumnValues.get(i));
        }

        Map body = params;
        body.put("dataJson",JSONObject.toJSONString(Arrays.asList(matrixData)));

        UtilHttp.doPost("https://api.dingtalk.com/v2.0/yida/forms/resources/matrices/upsert", ddClient.initTokenHeader(), null,body, DDR_New.class);

        return McR.success();
    }

    @Override
    public McR delYdAuth(Map map) {
        String matrixId = UtilMap.getString(map, "matrixId");//矩阵id
        String corpId = UtilMap.getString(map, "corpId");//corpId
        String userId = UtilMap.getString(map, "userId");//userId
        String corpToken = UtilMap.getString(map, "corpToken");//corpToken
        String[] conditionColumns = UtilMap.getString(map, "conditionColumns").split(",");//矩阵表条件列（名称）
        String[] conditionColumnValues = UtilMap.getString(map, "conditionColumnValues").split(",");;//矩阵表条件列值

        String[] conditionColumnIds = new String[conditionColumns.length];//矩阵表条件列（id）

        //md5加密corpId,userId,corpToken
        String token = DigestUtils.md5Hex(corpId + userId + corpToken).toUpperCase();

        //查询矩阵明细
        Map params = new HashMap();

        int pageSize = 100;
        int pageNumber = 0;

        params.put("pageSize",pageSize);
        params.put("matrixId",matrixId);
        params.put("corpId",corpId);
        params.put("userId",userId);
        params.put("token",token);

        List<Map> data = new ArrayList<>();

        String rowId = "";

        int totalCount = 0;

        //查询矩阵所有明细
        do {
            pageNumber++;
            params.put("pageNumber",pageNumber);
            DDR_New ddrNew = (DDR_New) UtilHttp.doGet("https://api.dingtalk.com/v2.0/yida/forms/resources/matrices", ddClient.initTokenHeader(), params, DDR_New.class);

            Map result = (Map) ddrNew.getResult();

            Map matrixData = UtilMap.getMap(result, "matrixData");

            data.addAll(UtilMap.getList(matrixData, "data"));

            totalCount = UtilMap.getInt(matrixData, "totalCount");

            //查询列id
            if (pageNumber == 1){
                Map matrixTable = UtilMap.getMap(result, "matrixTable");

                List<Map> conditionColumnList = UtilMap.getList(matrixTable, "conditionColumn");

                for (int i = 0; i < conditionColumns.length; i++) {
                    int finalI = i;
                    Map conditionColumn = conditionColumnList.stream().filter(item -> UtilMap.getString(item, "name").equals(conditionColumns[finalI])).findFirst().orElse(null);
                    conditionColumnIds[i] = UtilMap.getString(conditionColumn, "columnId");
                }
            }
        }while (pageNumber * pageSize < totalCount);

        boolean flag = false;
        //查询该矩阵条件是否存在
        for (Map datum : data) {
            for (int i = 0; i < conditionColumnIds.length; i++) {
                if (conditionColumnValues[i].equals(UtilMap.getString(datum, conditionColumnIds[i]))){
                    flag = true;
                }else {
                    flag = false;
                }
            }

            if (flag){
                rowId = UtilMap.getString(datum, "rowId");
                break;
            }
        }

        //删除矩阵明细
        if (Strings.isBlank(rowId)){
            return McR.errorUnknown("该矩阵条件不存在");
        }else {
            Map body = params;
            body.put("rowIds",rowId);

            UtilHttp.doPost("https://api.dingtalk.com/v2.0/yida/forms/resources/matrices/remove", ddClient.initTokenHeader(), null,body, DDR_New.class);
        }

        return McR.success();
    }

    @Override
    public void getCheckInData(JSONObject eventJson) {
        MDC.put("MDC_KEY_PID","1000");

        String baseId = "YMyQA2dXW7LkEz9LcDgZZDzPVzlwrZgb";//AI表格ID
        String sheetIdOrName = "签到数据";//数据表ID或数据表名称

        int retryCount = 0;
        int maxRetries = 5;

        long timeStamp = roundToNearestThousand(Long.parseLong(eventJson.getString("timeStamp")));//签到时间戳
        String staffId = eventJson.getString("StaffId");//签到人userId
        String eventId = eventJson.getString("eventId");//签到事件id

        while (retryCount <= maxRetries) {
            try {
                //获取签到数据
                Map body = new HashMap();
                body.put("cursor",0);
                body.put("size",100);
                body.put("start_time",timeStamp);
                body.put("end_time",timeStamp);
                body.put("userid_list",staffId);

                //查询用户详情
                DDR_New ddrNew1 = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/v2/user/get", null, ddClient.initTokenParams(), UtilMap.map("userid", staffId), DDR_New.class);
                Map result1 = (Map) ddrNew1.getResult();
                String userName = UtilMap.getString(result1, "name");
                String unionid = UtilMap.getString(result1, "unionid");

                //查询用户考勤数据
                DDR_New ddrNew = (DDR_New) UtilHttp.doPost("https://oapi.dingtalk.com/topapi/checkin/record/get", null, ddClient.initTokenParams(), body, DDR_New.class);
                Map result = (Map) ddrNew.getResult();
                List list = UtilMap.getList(result, "page_list");

                if (!list.isEmpty()){
                    Map fields = new HashMap();
                    Map checkInData = (Map) list.get(0);
                    Long checkinTime = UtilMap.getLong(checkInData, "checkin_time");
                    String detailPlace = UtilMap.getString(checkInData, "detail_place");
                    String remark = UtilMap.getString(checkInData,"remark");
                    Map user = new HashMap();
                    user.put("unionId",unionid);
                    fields.put("9XFXz5d",Arrays.asList(user));//签到人
                    fields.put("mKUEya0",checkinTime);//签到时间
                    fields.put("mHe1U1b",detailPlace);//签到地点
                    fields.put("CngGPET",remark);//备注

                    Map records = new HashMap();

                    Map fieldsMap = new HashMap();
                    fieldsMap.put("fields",fields);

                    records.put("records",Arrays.asList(fieldsMap));

                    Map param = new HashMap();
                    param.put("operatorId",unionid);

                    UtilHttp.doPost("https://api.dingtalk.com/v1.0/notable/bases/"+baseId+"/sheets/"+sheetIdOrName+"/records",ddClient.initTokenHeader(),param,records);
                }
                break;
            } catch (Exception e) {
                if (retryCount < maxRetries){
                    retryCount++;
                    log.info("eventId：{}，第{}次重试，异常信息：{}",eventId, retryCount, e.getMessage());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();// 如果线程在等待期间被中断，恢复中断状态
                        throw new RuntimeException("Request interrupted", ie);// 抛出运行时异常，终止重试
                    }
                    continue;
                }
                throw new RuntimeException("eventId：" + eventId + "，超出最大重试次数，异常信息：" + e.getMessage());
            }
        }
    }

    public String sendNotification(String access_token, List<String> userid_list, List<String> dept_id_list, boolean to_all_user, Map msg, String agent_id) {
        Map body = UtilMap.map("agent_id, to_all_user, msg", new Object[]{agent_id, to_all_user, msg});
        if (UtilList.isNotEmpty(userid_list)) {
            body.put("userid_list", String.join(",", userid_list));
        }
        if (UtilList.isNotEmpty(dept_id_list)) {
            body.put("dept_id_list", String.join(",", dept_id_list));
        }

        DDR ddr = DDR.doPost("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2", (Map)null, DDConf.initTokenParams(access_token), body);
        return ddr.getTask_id();
    }

    public long roundToNearestThousand(long timestamp) {
        // 将时间戳除以1000，得到秒级部分（带毫秒的小数部分）
        double seconds = timestamp / 1000.0;

        // 对秒级部分的个位数进行四舍五入（即对毫秒的千位四舍五入）
        long roundedSeconds = Math.round(seconds);

        // 将四舍五入后的秒级部分转换回毫秒时间戳
        return roundedSeconds * 1000;
    }

    @Async
    @Override
    public void batchUpdateSpecifiedQuantityData(Map map) {
        try {
            String pid = UtilMap.getString(map, "pid");
            MDC.put("MDC_KEY_PID",pid);

            String formInstId = UtilMap.getString(map, "formInstId");//当前表实例id

            //查询当前表formData
            Map formData = ydClient.queryData(YDParam.builder()
                    .formInstanceId(formInstId)
                    .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

            //目标表筛选字段唯一标识
            String[] searchFields = UtilMap.getString(map, "searchFieldStr").split(",");
            //目标表筛选字段值
            String[] searchFieldValues = UtilMap.getString(map, "searchFieldValueStr").split(",");

            Map searchCondition = new HashMap();

            if (searchFields.length == searchFieldValues.length){
                for (int i = 0; i < searchFields.length; i++) {
                    searchCondition.put(searchFields[i],searchFieldValues[i]);
                }
            }else {
                log.info("筛选字段和筛选值数量不一致");
            }

            int quantity = UtilMap.getInt(map, "quantity");//指定更新数量
            if (quantity > 100){
                log.info("更新数量不能大于100");
            }

            String formUuid = UtilMap.getString(map, "formUuid");//目标表uuid

            List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder()
                    .searchCondition(JSONObject.toJSONString(searchCondition))
                    .formUuid(formUuid)
                    .pageSize(quantity)
                    .build(), YDConf.FORM_QUERY.retrieve_list).getData();



            //目标表更新字段唯一标识
            String[] fieldStrs = UtilMap.getString(map, "fieldStr").split(",");
            //当前表字段唯一标识
            String[] currentFieldStrs = UtilMap.getString(map, "currentFieldStrs").split(",");

            Map updateFormData = new HashMap();

            if (fieldStrs.length == currentFieldStrs.length){
                for (int i = 0; i < fieldStrs.length; i++) {
                    updateFormData.put(fieldStrs[i],YDConf.getDataByCompId(formData,currentFieldStrs[i]));
                }
            }else {
                log.info("更新字段和当前表字段数量不一致");
            }

            for (Map datum : data) {
                String formInstanceId = UtilMap.getString(datum, "formInstanceId");

                ydClient.operateData(YDParam.builder()
                        .formInstanceId(formInstanceId)
                        .updateFormDataJson(JSONObject.toJSONString(updateFormData))
                        .build(), YDConf.FORM_OPERATION.update);
            }
            log.info("批量更新成功");
        }catch (Exception e){
            log.info("更新数据失败：{}",e.getMessage());
        }
    }


    private static void extractOperatorIds(JSONArray array, List<String> result) {
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;

            // 提取当前层级的 operatorUserId 或 operator
            String userId = obj.getString("operatorUserId");
            if (userId == null) userId = obj.getString("operator"); // 处理 domainList 中的字段
            if (userId != null) result.add(userId);

            // 递归处理 domainList
            JSONArray domainList = obj.getJSONArray("domainList");
            if (domainList != null && !domainList.isEmpty()) {
                extractOperatorIds(domainList, result);
            }
        }
    }

    private String getString(Object obj){
        return obj == null ? "" : obj.toString();
    }

    private List<Map> getTableData(List<Map<String, ?>> details){
        List<Map> table = new ArrayList<>();
        for (Map detail : details) {
            Map row = UtilMap.empty();
            for (String subId : details.get(0).keySet()) {
                // ppExt: 组件会返回带有_id/_value字段, 关联表单 & 成员组件赋值为原字段 [关联表单仅仅会返回带Id字段, 成员组件不带Id返回姓名过滤]
                if (subId.endsWith("_value") || (subId.startsWith("employeeField_") && !subId.endsWith("_id"))) {
                    continue;
                }
                if (subId.endsWith("_id")) {
                    if (subId.startsWith("employeeField_") || subId.startsWith("associationFormField_")) {
                        subId = subId.replace("_id", "");
                    } else {
                        continue;
                    }
                }
                row.put(subId, YDConf.getDataByCompId(detail, subId));
            }

            table.add(row);
        }
        return table;
    }
}
