package com.malk.lilin.Service.impl;


import cn.hutool.crypto.SecureUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.malk.lilin.Service.LiLinService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.aliwork.YDSearch;
import com.malk.server.common.McR;
import com.malk.server.dingtalk.DDConf;
import com.malk.service.aliwork.YDClient;
import com.malk.service.aliwork.YDService;
import com.malk.utils.UtilHttp;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.DoubleAdder;
import java.util.stream.Collectors;

@Slf4j
@Service
public class LiLinServiceImpl implements LiLinService {

    @Autowired
    private YDClient ydClient;
    @Autowired
    private YDService ydService;
    @Autowired
    private YDConf ydConf;
    @Autowired
    private DDConf ddConf;
    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#0.00");


    @Override
    public McR lilinUpsert(List<String> rqqj, String bdbh) {
        List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-5F8B2B6418E14EF0967CC60DA25A7558K5L7")
                .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
                        new YDSearch("createTime", rqqj, "DateField", YDSearch.Type.DATE_FIELD, YDSearch.Operator.BETWEEN),
                        new YDSearch("textField_m9cdsn32", bdbh, "TextField", YDSearch.Type.TEXT_FIELD, YDSearch.Operator.EQ))))
                .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();

        List<Map> details = new ArrayList<>();
        List<String> details2 = new ArrayList<>();

        // 存储每个【订单编号】对应的是哪个项目（便于后续处理）
        Map<String, String> orderToProjectMap = new HashMap<>();
        Map<String, Map<String, BigDecimal>> projectAmountMap = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            for (Map map : list) {
                try {
                    Map detail = new HashMap<>();
                    Map formData = (Map) map.get("formData");
                    String zt = UtilMap.getString(formData, "textField_md6z2k32");

                    // 如果状态是“已同步”，跳过
                    if ("已同步".equals(zt)) {
                        continue;
                    }


                    String formInstanceId = UtilMap.getString(map, "formInstanceId");
                    details2.add(formInstanceId);

                    String ddbh = UtilMap.getString(formData, "textField_m9cdsn32"); // 订单编号
                    String xmbh = UtilMap.getString(formData, "textField_m9chwmaj"); // 项目编号
                    if (xmbh == null || xmbh.trim().isEmpty()) {
                        log.warn("明细数据缺少项目编号，跳过: formInstanceId={}", formInstanceId);
                        continue;
                    }

                    // 记录 订单 -> 项目 映射关系
                    if (ddbh != null && !ddbh.isEmpty()) {
                        orderToProjectMap.put(ddbh, xmbh);
                    }

                    // 获取金额字段（安全获取）
                    BigDecimal je = safeGetBigDecimal(formData, "numberField_m9b2rwwo"); // 含税小计
                    BigDecimal jews = safeGetBigDecimal(formData, "numberField_m9b2rwwp"); // 无税小计
                    BigDecimal yf = safeGetBigDecimal(formData, "numberField_majn78hg"); // 运费补贴

                    // 累加到项目金额 map
                    projectAmountMap.putIfAbsent(xmbh, new HashMap<>());
                    Map<String, BigDecimal> amounts = projectAmountMap.get(xmbh);

                    amounts.merge("numberField_m9b2rwwo", je, BigDecimal::add);
                    amounts.merge("numberField_m9b2rwwp", jews, BigDecimal::add);
                    amounts.merge("numberField_majn78hg", yf, BigDecimal::add);


                    // 处理关联产品字段
                    String dgcpRaw = String.valueOf(formData.get("associationFormField_m984zbpq_id"));
                    String dgcpString = StringEscapeUtils.unescapeJava(dgcpRaw.substring(1, dgcpRaw.length() - 1));
                    List<Map> dgcps = (List<Map>) JSONArray.parse(dgcpString);
                    detail.put("associationFormField_m9cdctdo", dgcps);

                    // 其他字段提取
                    detail.put("textField_m9cdctdq", safeGetString(formData, "textField_m8yanvkl")); // 型号
                    detail.put("numberField_m9cdctdx", safeGetString(formData, "numberField_m8yanvkx")); // 数量
                    detail.put("numberField_m9cdctdw", safeGetString(formData, "numberField_m8yanvkw")); // 销售单价含税
                    detail.put("selectField_manmqrif", safeGetString(formData, "selectField_m9me4py8")); // 销售税率
                    detail.put("numberField_m9cdctdy", safeGetString(formData, "numberField_m9b2rwwo")); // 金额小计
                    detail.put("numberField_majmvrdr", safeGetString(formData, "numberField_majn78hg")); // 运费补贴
                    detail.put("numberField_m9cdctdv", safeGetString(formData, "numberField_m8yanvkv")); // 销售单价未税
                    detail.put("numberField_m9cdctdz", safeGetString(formData, "numberField_m9b2rwwp")); // 金额未税小计
                    detail.put("textField_m96r764p", safeGetString(formData, "textField_m96r764p")); // 产品小类
                    detail.put("textField_m96r764o", safeGetString(formData, "textField_m96r764o")); // 产品大类
                    detail.put("textField_m9cdctdp", safeGetString(formData, "numberField_majn78hg")); // 品牌
                    detail.put("textField_m9cdctdr", safeGetString(formData, "textField_m8yanvks")); // 产品单位
                    detail.put("textField_m9cdctds", safeGetString(formData, "textField_m8yanvkt")); // 颜色
                    detail.put("selectField_m9cdcte0", safeGetString(formData, "selectField_m8yanvkn")); // 是否带采
                    detail.put("selectField_m9cdcte1", safeGetString(formData, "selectField_m8yanvko")); // 是否定制
                    detail.put("selectField_m9cdcte2", safeGetString(formData, "selectField_m8yanvkp")); // 是否明细
                    detail.put("textField_m9gewnyi", safeGetString(formData, "textField_m9gfqsow")); // 产品名称
                    detail.put("textField_m9gewnyj", safeGetString(formData, "textField_m9gfqsox")); // SKU码
                    detail.put("selectField_m9qrzktd", safeGetString(formData, "selectField_m9cdsn2v")); // 订单状态
                    detail.put("textField_m9qnz34u", safeGetString(formData, "textField_m9gfqsox")); // 订单明细编号
                    detail.put("numberField_m9gfg5mi", safeGetString(formData, "numberField_m9gfg5mi")); // 成本单价含税
                    detail.put("selectField_m9me4py8", safeGetString(formData, "selectField_m9me4py8")); // 成本税率
                    detail.put("numberField_m9gfg5mj", safeGetString(formData, "numberField_m9gfg5mj")); // 成本单价无税
                    detail.put("textField_maoynrp4", safeGetString(formData, "textField_maoynrp4")); // 供应商名称
                    detail.put("textField_maoynrp5", safeGetString(formData, "textField_maoynrp5")); // 供应商编号

                    details.add(detail);

                } catch (Exception e) {
                    log.error("处理子表单数据时发生异常", e);
                }
            }

            try {
                if (!projectAmountMap.isEmpty()) {
                    for (Map.Entry<String, Map<String, BigDecimal>> entry : projectAmountMap.entrySet()) {
                        String xmbh = entry.getKey();           // 项目编号
                        Map<String, BigDecimal> amounts = entry.getValue();

                        try {
                            // 查询项目主表单
                            List<Map> xmdata = (List<Map>) ydClient.queryData(
                                    YDParam.builder()
                                            .formUuid("FORM-FFA52F0206684931A0F18C00C2C937EAAVI5")
                                            .searchFieldJson(JSONObject.toJSONString(UtilMap.map("textField_m9awsc29", xmbh)))
                                            .build(),
                                    YDConf.FORM_QUERY.retrieve_list_all
                            ).getData();

                            if (xmdata != null && !xmdata.isEmpty()) {
                                Map xmmap = xmdata.get(0);
                                String xmformInstanceId = UtilMap.getString(xmmap, "formInstanceId");
                                Map xmFormData = (Map) xmmap.get("formData");

                                // 获取当前项目表单中的金额
                                BigDecimal existingXsddje = safeGetBigDecimal(xmFormData, "numberField_m982msp5"); // 订单金额含税
                                BigDecimal existingXsddwthj = safeGetBigDecimal(xmFormData, "numberField_m982msp7"); // 订单金额无税
                                BigDecimal existingXsddwsk = safeGetBigDecimal(xmFormData, "numberField_m9iarxwb"); // 未收款金额
                                BigDecimal existingXsddwkp = safeGetBigDecimal(xmFormData, "numberField_m9iarxwc"); // 未开票金额

                                // 获取本次要累加的金额
                                BigDecimal je = amounts.getOrDefault("numberField_m9b2rwwo", BigDecimal.ZERO);
                                BigDecimal jews = amounts.getOrDefault("numberField_m9b2rwwp", BigDecimal.ZERO);
                                BigDecimal yf = amounts.getOrDefault("numberField_majn78hg", BigDecimal.ZERO);

                                // 累加
                                existingXsddje = existingXsddje.add(je).add(yf);
                                existingXsddwthj = existingXsddwthj.add(jews);
                                existingXsddwsk = existingXsddwsk.add(je).add(yf); // 含税 + 运费
                                existingXsddwkp = existingXsddwkp.add(je).add(yf);        // 无税金额

                                // 构建更新数据
                                Map<String, Object> xmupdateMap = new HashMap<>();
                                xmupdateMap.put("numberField_m982msp5", existingXsddje.stripTrailingZeros().toPlainString());
                                xmupdateMap.put("numberField_m982msp7", existingXsddwthj.stripTrailingZeros().toPlainString());
                                xmupdateMap.put("numberField_m9iarxwb", existingXsddwsk.stripTrailingZeros().toPlainString());
                                xmupdateMap.put("numberField_m9iarxwc", existingXsddwkp.stripTrailingZeros().toPlainString());

                                // 执行更新
                                ydClient.operateData(
                                        YDParam.builder()
                                                .formInstanceId(xmformInstanceId)
                                                .updateFormDataJson(JSONObject.toJSONString(xmupdateMap))
                                                .useLatestVersion(true)
                                                .build(),
                                        YDConf.FORM_OPERATION.update
                                );

                                log.info("已更新项目表单，项目编号：{}，累加含税金额：{}", xmbh, je);

                            } else {
                                log.warn("未找到项目表单，项目编号：{}", xmbh);
                            }

                        } catch (Exception e) {
                            log.error("更新项目表单失败，项目编号：{}", xmbh, e);
                        }
                    }
                }
                for (String ddbh : orderToProjectMap.keySet()) {
                    List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder()
                            .formUuid("FORM-C9BC8F7A159E4281B707ADBE1628EB7FDACQ")
                            .searchFieldJson(JSONObject.toJSONString(UtilMap.map("textField_m9cdcten", ddbh)))
                            .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();

                    if (data == null || data.isEmpty()) {
                        return McR.errorParam("主表单不存在");
                    }

                    Map mainMap = data.get(0);
                    log.info("主表单数据: {}", mainMap);

                    String formInstanceId1 = UtilMap.getString(mainMap, "formInstanceId");
                    Map mainFormData = (Map) mainMap.get("formData");
                    List<Map> tableField = (List<Map>) mainFormData.get("tableField_m9cdcte4");
                    System.out.println("主表单数据：" + tableField);
                    if (tableField == null) {
                        tableField = new ArrayList<>();
                    }

                    tableField.addAll(details);
// 修改后的处理逻辑，合并相同SKU的数据
                    List<Map> distinctList = new ArrayList<>();
                    Map<String, Map> skuMap = new HashMap<>();

                    for (Map item : tableField) {
                        String sku = safeGetString(item, "textField_m9gewnyj");
                        if (skuMap.containsKey(sku)) {
                            // 如果SKU已存在，则累加数值字段
                            Map existingItem = skuMap.get(sku);

                            // 累加数量
                            double currentQty = getDoubleSafe(existingItem, "numberField_m9cdctdx");
                            double newQty = getDoubleSafe(item, "numberField_m9cdctdx");
                            existingItem.put("numberField_m9cdctdx", currentQty + newQty);

                            // 累加销售含税金额
                            double currentHsAmount = getDoubleSafe(existingItem, "numberField_m9cdctdy");
                            double newHsAmount = getDoubleSafe(item, "numberField_m9cdctdy");
                            existingItem.put("numberField_m9cdctdy", currentHsAmount + newHsAmount);

                            // 累加运费补贴
                            double currentYfbt = getDoubleSafe(existingItem, "numberField_majmvrdr");
                            double newYfbt = getDoubleSafe(item, "numberField_majmvrdr");
                            existingItem.put("numberField_majmvrdr", currentYfbt + newYfbt);

                            // 累加销售未税金额
                            double currentWsAmount = getDoubleSafe(existingItem, "numberField_m9cdctdz");
                            double newWsAmount = getDoubleSafe(item, "numberField_m9cdctdz");
                            existingItem.put("numberField_m9cdctdz", currentWsAmount + newWsAmount);

                            // 累加成本含税
                            double currentHsCost = getDoubleSafe(existingItem, "numberField_m9gfg5mi");
                            double newHsCost = getDoubleSafe(item, "numberField_m9gfg5mi");
                            existingItem.put("numberField_m9gfg5mi", currentHsCost + newHsCost);

                            // 累加成本无税
                            double currentWsCost = getDoubleSafe(existingItem, "numberField_m9gfg5mj");
                            double newWsCost = getDoubleSafe(item, "numberField_m9gfg5mj");
                            existingItem.put("numberField_m9gfg5mj", currentWsCost + newWsCost);

                            // 单价字段保持不变（销售单价含税/未税）
                        } else {
                            // 如果SKU不存在，则添加到map中
                            skuMap.put(sku, new HashMap(item));
                        }
                    }

// 将合并后的数据放入distinctList
                    distinctList.addAll(skuMap.values());

                    DoubleAdder totalZsl = new DoubleAdder();
                    DoubleAdder totalXsDj = new DoubleAdder();
                    DoubleAdder totalJexj = new DoubleAdder();
                    DoubleAdder totalYfzj = new DoubleAdder();

                    distinctList.forEach(item -> {
                        totalZsl.add(getDoubleSafe(item, "numberField_m9cdctdx"));
                        totalXsDj.add(getDoubleSafe(item, "numberField_m9cdctdy"));
                        totalJexj.add(getDoubleSafe(item, "numberField_m9cdctdz"));
                        totalYfzj.add(getDoubleSafe(item, "numberField_majmvrdr"));
                    });

                    Map<String, Object> updateMap = new HashMap<>();
                    updateMap.put("tableField_m9cdcte4", distinctList);
                    updateMap.put("numberField_m9cdcte5", totalZsl.doubleValue());
                    updateMap.put("numberField_ma14qlv3", totalZsl.doubleValue());
                    updateMap.put("numberField_ma14qlv7", totalZsl.doubleValue());
                    updateMap.put("numberField_m9cdcte7", totalXsDj.doubleValue() + totalYfzj.doubleValue());
                    updateMap.put("numberField_m9cdcte9", totalJexj.doubleValue());
                    updateMap.put("numberField_majmvrdq", totalYfzj.doubleValue());
                    updateMap.put("numberField_m9iap5e5", totalXsDj.doubleValue() + totalYfzj.doubleValue());
                    updateMap.put("numberField_m9iap5e6", totalXsDj.doubleValue() + totalYfzj.doubleValue());
                    System.out.println("updateMap=======" + updateMap);
                    // 更新主表单
                    ydClient.operateData(YDParam.builder()
                            .formInstanceId(formInstanceId1)
                            .updateFormDataJson(JSONObject.toJSONString(updateMap))
                            .useLatestVersion(true)
                            .build(), YDConf.FORM_OPERATION.update);

                    // 批量更新子表单状态为“已同步”
                    if (!details2.isEmpty()) {
                        ydClient.operateData(YDParam.builder()
                                .formInstanceIdList(details2)
                                .formUuid("FORM-5F8B2B6418E14EF0967CC60DA25A7558K5L7")
                                .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textField_md6z2k32", "已同步")))
                                .useLatestVersion(true)
                                .build(), YDConf.FORM_OPERATION.multi_update);
                    }
                }
            } catch (Exception e) {
                log.error("更新主表单或子表单状态时出错", e);
                return McR.errorParam("更新失败");
            }

            return McR.success();

        } else {
            return McR.success();
        }
    }

//    @Override
//    public McR lilinUpsert(List<String> rqqj, String bdbh) {
//        List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder()
//                .formUuid("FORM-B4B6126350C041EAAF4F04AECF6A3F0C95NV")
//                .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
//                        new YDSearch("createTime", rqqj, "DateField", YDSearch.Type.DATE_FIELD, YDSearch.Operator.BETWEEN),
//                        new YDSearch("textField_m9cdsn32", bdbh, "TextField", YDSearch.Type.TEXT_FIELD, YDSearch.Operator.EQ))))
//                .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();
//
//        List<Map> details = new ArrayList<>();
//        List<String> details2 = new ArrayList<>();
//        String ddbhMain = "";
//
//        if (list != null && !list.isEmpty()) {
//            for (Map map : list) {
//                try {
//                    Map detail = new HashMap<>();
//                    Map formData = (Map) map.get("formData");
//                    String zt = UtilMap.getString(formData, "textField_md6z2k32");
//
//                    // 如果状态是“已同步”，跳过
//                    if ("已同步".equals(zt)) {
//                        continue;
//                    }
//
//                    String formInstanceId = UtilMap.getString(map, "formInstanceId");
//                    details2.add(formInstanceId);
//
//                    ddbhMain = UtilMap.getString(formData, "textField_m9cdsn32");
//
//                    // 处理关联产品字段
//                    String dgcpRaw = String.valueOf(formData.get("associationFormField_m984zbpq_id"));
//                    String dgcpString = StringEscapeUtils.unescapeJava(dgcpRaw.substring(1, dgcpRaw.length() - 1));
//                    List<Map> dgcps = (List<Map>) JSONArray.parse(dgcpString);
//                    detail.put("associationFormField_m9cdctdo", dgcps);
//
//                    // 其他字段提取
//                    detail.put("textField_m9cdctdq", safeGetString(formData, "textField_m8yanvkl")); // 型号
//                    detail.put("numberField_m9cdctdx", safeGetString(formData, "numberField_m8yanvkx")); // 数量
//                    detail.put("numberField_m9cdctdw", safeGetString(formData, "numberField_m8yanvkw")); // 销售单价含税
//                    detail.put("selectField_manmqrif", safeGetString(formData, "selectField_m9me4py8")); // 销售税率
//                    detail.put("numberField_m9cdctdy", safeGetString(formData, "numberField_m9b2rwwo")); // 金额小计
//                    detail.put("numberField_majmvrdr", safeGetString(formData, "numberField_majn78hg")); // 运费补贴
//                    detail.put("numberField_m9cdctdv", safeGetString(formData, "numberField_m8yanvkv")); // 销售单价未税
//                    detail.put("numberField_m9cdctdz", safeGetString(formData, "numberField_m9b2rwwp")); // 金额未税小计
//                    detail.put("textField_m96r764p", safeGetString(formData, "textField_m96r764p")); // 产品小类
//                    detail.put("textField_m96r764o", safeGetString(formData, "textField_m96r764o")); // 产品大类
//                    detail.put("textField_m9cdctdp", safeGetString(formData, "numberField_majn78hg")); // 品牌
//                    detail.put("textField_m9cdctdr", safeGetString(formData, "textField_m8yanvks")); // 产品单位
//                    detail.put("textField_m9cdctds", safeGetString(formData, "textField_m8yanvkt")); // 颜色
//                    detail.put("selectField_m9cdcte0", safeGetString(formData, "selectField_m8yanvkn")); // 是否带采
//                    detail.put("selectField_m9cdcte1", safeGetString(formData, "selectField_m8yanvko")); // 是否定制
//                    detail.put("selectField_m9cdcte2", safeGetString(formData, "selectField_m8yanvkp")); // 是否明细
//                    detail.put("textField_m9gewnyi", safeGetString(formData, "textField_m9gfqsow")); // 产品名称
//                    detail.put("textField_m9gewnyj", safeGetString(formData, "textField_m9gfqsox")); // SKU码
//                    detail.put("selectField_m9qrzktd", safeGetString(formData, "selectField_m9cdsn2v")); // 订单状态
//                    detail.put("textField_m9qnz34u", safeGetString(formData, "textField_m9gfqsox")); // 订单明细编号
//                    detail.put("numberField_m9gfg5mi", safeGetString(formData, "numberField_m9gfg5mi")); // 成本单价含税
//                    detail.put("selectField_m9me4py8", safeGetString(formData, "selectField_m9me4py8")); // 成本税率
//                    detail.put("numberField_m9gfg5mj", safeGetString(formData, "numberField_m9gfg5mj")); // 成本单价无税
//                    detail.put("textField_maoynrp4", safeGetString(formData, "textField_maoynrp4")); // 供应商名称
//                    detail.put("textField_maoynrp5", safeGetString(formData, "textField_maoynrp5")); // 供应商编号
//
//                    details.add(detail);
//
//                } catch (Exception e) {
//                    log.error("处理子表单数据时发生异常", e);
//                }
//            }
//
//            try {
//                List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder()
//                        .formUuid("FORM-BDA88E0932564B64B1619AC05290A012L16E")
//                        .searchFieldJson(JSONObject.toJSONString(UtilMap.map("textField_m9cdcten", ddbhMain)))
//                        .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();
//
//                if (data == null || data.isEmpty()) {
//                    return McR.errorParam("主表单不存在");
//                }
//
//                Map mainMap = data.get(0);
//                log.info("主表单数据: {}", mainMap);
//
//                String formInstanceId1 = UtilMap.getString(mainMap, "formInstanceId");
//                Map mainFormData = (Map) mainMap.get("formData");
//                List<Map> tableField = (List<Map>) mainFormData.get("tableField_m9cdcte4");
//
//                if (tableField == null) {
//                    tableField = new ArrayList<>();
//                }
//
//                tableField.addAll(details);
//
//                // 按 SKU 码分组，合并数据
//                Map<String, Map<String, Object>> groupedMap = new LinkedHashMap<>();
//
//                for (Map item : details) {
//                    String sku = (String) item.get("textField_m9gewnyj");
//
//                    if (groupedMap.containsKey(sku)) {
//                        // 如果已存在该 SKU，进行字段累加
//                        Map<String, Object> existing = groupedMap.get(sku);
//
//                        // 累加数值字段
//                        existing.put("numberField_m9cdctdx", getDoubleSafe(existing, "numberField_m9cdctdx") + getDoubleSafe(item, "numberField_m8yanvkx")); // 数量
//                        existing.put("numberField_m9cdctdw", getDoubleSafe(existing, "numberField_m9cdctdw") + getDoubleSafe(item, "numberField_m8yanvkw")); // 销售单价含税
//                        existing.put("numberField_m9cdctdy", getDoubleSafe(existing, "numberField_m9cdctdy") + getDoubleSafe(item, "numberField_m9b2rwwo")); // 金额小计
//                        existing.put("numberField_majmvrdr", getDoubleSafe(existing, "numberField_majmvrdr") + getDoubleSafe(item, "numberField_majn78hg")); // 运费补贴
//                        existing.put("numberField_m9cdctdv", getDoubleSafe(existing, "numberField_m9cdctdv") + getDoubleSafe(item, "numberField_m8yanvkv")); // 销售单价未税
//                        existing.put("numberField_m9cdctdz", getDoubleSafe(existing, "numberField_m9cdctdz") + getDoubleSafe(item, "numberField_m9b2rwwp")); // 金额未税小计
//                        existing.put("numberField_m9gfg5mi", getDoubleSafe(existing, "numberField_m9gfg5mi") + getDoubleSafe(item, "numberField_m9gfg5mi")); // 成本单价含税
//                        existing.put("numberField_m9gfg5mj", getDoubleSafe(existing, "numberField_m9gfg5mj") + getDoubleSafe(item, "numberField_m9gfg5mj")); // 成本单价无税
//
//                    } else {
//                        // 第一次出现该 SKU，复制原始数据
//                        Map<String, Object> newItem = new HashMap<>();
//                        newItem.put("textField_m9gewnyj", sku);
//                        newItem.put("textField_m9gewnyi", item.get("textField_m9gewnyi")); // 产品名称
//                        newItem.put("textField_m96r764p", item.get("textField_m96r764p")); // 产品小类
//                        newItem.put("textField_m96r764o", item.get("textField_m96r764o")); // 产品大类
//                        newItem.put("textField_m9cdctdp", item.get("textField_m9cdctdp")); // 品牌
//                        newItem.put("textField_m9cdctdr", item.get("textField_m9cdctdr")); // 产品单位
//                        newItem.put("textField_m9cdctds", item.get("textField_m9cdctds")); // 颜色
//                        newItem.put("selectField_m9cdcte0", item.get("selectField_m9cdcte0")); // 是否带采
//                        newItem.put("selectField_m9cdcte1", item.get("selectField_m9cdcte1")); // 是否定制
//                        newItem.put("selectField_m9cdcte2", item.get("selectField_m9cdcte2")); // 是否明细
//                        newItem.put("selectField_m9qrzktd", item.get("selectField_m9qrzktd")); // 订单状态
//                        newItem.put("textField_m9qnz34u", item.get("textField_m9qnz34u")); // 订单明细编号
//                        newItem.put("selectField_m9me4py8", item.get("selectField_m9me4py8")); // 成本税率
//                        newItem.put("textField_maoynrp4", item.get("textField_maoynrp4")); // 供应商名称
//                        newItem.put("textField_maoynrp5", item.get("textField_maoynrp5")); // 供应商编号
//
//                        // 处理关联产品字段（假设每个 SKU 的关联产品一致，否则可取第一个）
//                        newItem.put("associationFormField_m9cdctdo", item.get("associationFormField_m9cdctdo"));
//
//                        // 初始化数值字段
//                        newItem.put("numberField_m9cdctdx", getDoubleSafe(item, "numberField_m8yanvkx")); // 数量
//                        newItem.put("numberField_m9cdctdw", getDoubleSafe(item, "numberField_m8yanvkw")); // 销售单价含税
//                        newItem.put("numberField_m9cdctdy", getDoubleSafe(item, "numberField_m9b2rwwo")); // 金额小计
//                        newItem.put("numberField_majmvrdr", getDoubleSafe(item, "numberField_majn78hg")); // 运费补贴
//                        newItem.put("numberField_m9cdctdv", getDoubleSafe(item, "numberField_m8yanvkv")); // 销售单价未税
//                        newItem.put("numberField_m9cdctdz", getDoubleSafe(item, "numberField_m9b2rwwp")); // 金额未税小计
//                        newItem.put("numberField_m9gfg5mi", getDoubleSafe(item, "numberField_m9gfg5mi")); // 成本单价含税
//                        newItem.put("numberField_m9gfg5mj", getDoubleSafe(item, "numberField_m9gfg5mj")); // 成本单价无税
//
//                        groupedMap.put(sku, newItem);
//                    }
//                }
//
//// 将合并后的数据转换为 list
//                List<Map> mergedDetails = new ArrayList<>(groupedMap.values());
//
//// 替换原来 distinctList 的定义
//                List<Map> distinctList = mergedDetails;
//
//// 汇总金额
//                AtomicReference<Double> totalZsl = new AtomicReference<>(0.0);
//                AtomicReference<Double> totalHszj = new AtomicReference<>(0.0);
//                AtomicReference<Double> totalWszj = new AtomicReference<>(0.0);
//                AtomicReference<Double> totalYfzj = new AtomicReference<>(0.0);
//
//                distinctList.forEach(item -> {
//                    totalZsl.updateAndGet(v -> v + getDoubleSafe(item, "numberField_m9cdctdx"));
//                    totalHszj.updateAndGet(v -> v + getDoubleSafe(item, "numberField_m9cdctdy"));
//                    totalWszj.updateAndGet(v -> v + getDoubleSafe(item, "numberField_m9cdctdz"));
//                    totalYfzj.updateAndGet(v -> v + getDoubleSafe(item, "numberField_majmvrdr"));
//                });
//
//                Map<String, Object> updateMap = new HashMap<>();
//                updateMap.put("tableField_m9cdcte4", distinctList);
//                updateMap.put("numberField_m9cdcte5", String.valueOf(totalZsl.get()));
//                updateMap.put("numberField_m9cdcte7", String.valueOf(totalHszj.get()));
//                updateMap.put("numberField_m9cdcte9", String.valueOf(totalWszj.get()));
//                updateMap.put("numberField_majmvrdq", String.valueOf(totalYfzj.get()));
//                updateMap.put("numberField_m9iap5e5", String.valueOf(totalHszj.get()));
//                updateMap.put("numberField_m9iap5e6", String.valueOf(totalHszj.get()));
//                updateMap.put("numberField_ma14qlv3", String.valueOf(totalZsl.get()));
//                updateMap.put("numberField_ma14qlv7", String.valueOf(totalZsl.get()));
//
//                // 更新主表单
//                ydClient.operateData(YDParam.builder()
//                        .formInstanceId(formInstanceId1)
//                        .updateFormDataJson(JSONObject.toJSONString(updateMap))
//                        .useLatestVersion(true)
//                        .build(), YDConf.FORM_OPERATION.update);
//
//                // 批量更新子表单状态为“已同步”
//                if (!details2.isEmpty()) {
//                    ydClient.operateData(YDParam.builder()
//                            .formInstanceIdList(details2)
//                            .formUuid("FORM-B4B6126350C041EAAF4F04AECF6A3F0C95NV")
//                            .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textField_md6z2k32", "已同步")))
//                            .useLatestVersion(true)
//                            .build(), YDConf.FORM_OPERATION.multi_update);
//                }
//
//            } catch (Exception e) {
//                log.error("更新主表单或子表单状态时出错", e);
//                return McR.errorParam( e.getMessage());
//            }
//
//            return McR.success("操作成功");
//
//        } else {
//            return McR.success("没有符合条件的数据");
//        }
//    }

    @Override
    public McR hqbank(Map<String, Object> map) {
        try {
            String name = String.valueOf(map.get("name"));
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://x.xencio.com/c4c3/api/bank/searchBranch?securityCode=" + token().getData() + "&queryType=full&name=" + name)
                    .get()
                    .addHeader("x-xencio-client-id", "7dc3a31209b94a91ba40a44358fe70eb")
                    .addHeader("content-type", "application/x-www-form-urlencoded")
                    .build();
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                return McR.errorParam(response.message());
            }
            String responseBody = response.body().string();
            // 使用 FastJSON 解析 JSON
            JSONObject json = com.alibaba.fastjson.JSON.parseObject(responseBody);

            // 获取 data.data 数组
            JSONArray dataList = json.getJSONObject("data")
                    .getJSONArray("data");
            String code = null;
            for (int i = 0; i < dataList.size(); i++) {
                JSONObject item = dataList.getJSONObject(i);
//                HashMap<Object, Object> map = new HashMap<>();
//                String areaName = item.getString("areaName");
//                 names = item.getString("name");
//                String province = item.getString("province");
                code = item.getString("code");
//                String updateTime = item.getString("updateTime");


            }
            return McR.success(code);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        HashMap mapAll = new HashMap<>();
        List<Map<String, Object>> tableField = new ArrayList<>();

        Map<String, Object> data1 = new HashMap<>();
        data1.put("trxNumber", "FK20250723621");
        data1.put("fromAccountCode", "755915704010801");
        data1.put("fromAccountName", "企业网银新20161341");
        data1.put("toAccountCode", "6214831150131511");
        data1.put("toAccountType", "0");
        data1.put("toAccountName", "吴极客");
        data1.put("payAmount", "1.00");
        data1.put("uses", "这是一个测试用途");
//        data1.put("instructionId", "F00000123");

        Map<String, Object> data2 = new HashMap<>();
        data2.put("trxNumber", "FK20250723631");
        data2.put("fromAccountCode", "755915704010801");
        data2.put("fromAccountName", "企业网银新20161341");
        data2.put("toAccountCode", "6214831150131511");
        data2.put("toAccountType", "0");
        data2.put("toAccountName", "吴极客");
        data2.put("payAmount", "1.00");
        data2.put("uses", "这是一个测试用途");
//        data2.put("instructionId", "F00000123");

// 添加到集合
        tableField.add(data1);
        tableField.add(data2);
        System.out.println(JSONObject.toJSONString(tableField));

        mapAll.put("totalRecord", "2");
        mapAll.put("list", JSONObject.toJSONString(tableField));
        mapAll.put("totalTrxNumber", "FK20250725713");
        mapAll.put("source", "OA");
//        mapAll.put("securityCode", securityCode);
        String url = "https://openapi.xencio.com/sandbox/cfa/api/payment/batchTransfer";
        String securityCode = String.valueOf(token().getData());
        Map headers = new HashMap<>();
        headers.put("x-xencio-client-id", "4d9414e89bc24b0d89b678d9f20bc56a");
        headers.put("content-Type", "application/x-www-form-urlencoded");
        Map params = new HashMap<>();
//        mapAll.put("securityCode", securityCode);
        mapAll.put("securityCode", securityCode);
        String response = UtilHttp.doPost(url, headers, null, null, mapAll);//批量制单
        log.info("response:{}", response);
    }

    @Override
    public McR lilinZD(Map<String, Object> map) throws IOException {
        String formInstanceId = String.valueOf(map.get("formInstanceId"));
        log.info("formInstanceId:{}", formInstanceId);

        // 获取表单数据
        Map data = (Map) ydClient.queryData(YDParam.builder()
                .formInstId(formInstanceId)
                .appType(ydConf.getAppType())
                .systemToken(ydConf.getSystemToken())
                .userId(ddConf.getOperator())
                .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

        log.info("form data: {}", data);

        // 主表字段（复用）
        String fklx = String.valueOf(data.get("selectField_m9ze3rjf")); // 付款类型
        String note = String.valueOf(data.get("textField_m9ze3rk3")); // 付款用途
        String toAccountCode = String.valueOf(data.get("textField_m9ze3rjs")); // 收款账号
        String toAccountName = String.valueOf(data.get("textField_m9ze3rjl")); // 收款户名
        String toBank = String.valueOf(data.get("textField_m9ze3rjr")); // 收款银行支行
        String fromAccountCode = String.valueOf(data.get("textField_mdfnuyl9")); // 付款账号
        String fromAccountName = String.valueOf(data.get("textField_m9ze3rj8")); // 付款户名

        // 公共参数
        String securityCode = String.valueOf(token().getData());
        Map<String, String> headers = new HashMap<>();
        headers.put("x-xencio-client-id", "7dc3a31209b94a91ba40a44358fe70eb");
        headers.put("content-type", "application/x-www-form-urlencoded");
        String url = "https://x.xencio.com/c4c3/api/payment/transfer";

        // 判断是否为物流付款单
        if ("物流付款单".equals(fklx)) {
            // 是物流付款单：遍历子表，每行发起一次支付
            List<Map<String, Object>> tableField = (List<Map<String, Object>>) data.get("tableField_m986c5os");
            if (tableField == null || tableField.isEmpty()) {
                log.warn("物流付款单但子表为空，formInstanceId: {}", formInstanceId);
                return McR.errorParam("子表数据为空");
            }

            for (Map<String, Object> row : tableField) {
                String instructionId = String.valueOf(row.get("textField_mdnuob9a")); // 子表电子凭证号
                String xmmc = String.valueOf(row.get("textField_m9lfmh1o")); // 项目名称
                String xmbh = String.valueOf(row.get("textField_m9lfmh1p")); // 项目编号
                String payAmount = String.valueOf(row.get("numberField_m8yf6gn1")); // 子表金额

                // 构建 projectInfo
                String projectInfo = xmbh + "-" + xmmc;
                Map<String, Object> metaJson1 = new HashMap<>();
                metaJson1.put("projectInfo", projectInfo);
                String metaJson = JSON.toJSONString(metaJson1);

                // 组装请求参数
                Map<String, Object> mapAll = new HashMap<>();
                mapAll.put("instructionId", instructionId);
                mapAll.put("fromAccountCode", fromAccountCode);
                mapAll.put("fromAccountName", fromAccountName);
                mapAll.put("toAccountCode", toAccountCode);
                mapAll.put("toAccountName", toAccountName);
                mapAll.put("payAmount", payAmount);
                mapAll.put("source", "OA");
                mapAll.put("note", note);
                mapAll.put("toBank", toBank);
                mapAll.put("securityCode", securityCode);
                mapAll.put("metaJson", metaJson);

                // 发起支付
                String response = UtilHttp.doPost(url, headers, null, null, mapAll);
                log.info("物流付款单 - 支付成功 [凭证号: {}, 金额: {}], 响应: {}", instructionId, payAmount, response);
            }

        } else {
            // 不是物流付款单：使用主表字段，发起一次支付
            String instructionId = String.valueOf(data.get("serialNumberField_m9ze3rjd")); // 主表电子凭证号
            String payAmount = String.valueOf(data.get("numberField_m9ze3rk4")); // 主表金额
            String xmbh = String.valueOf(data.get("textField_madmzxce"));
            String xmmc = String.valueOf(data.get("textField_mayubz25"));
            String projectInfo = xmbh + "-" + xmmc;

            Map<String, Object> metaJson1 = new HashMap<>();
            metaJson1.put("projectInfo", projectInfo);
            String metaJson = JSON.toJSONString(metaJson1);

            Map<String, Object> mapAll = new HashMap<>();
            mapAll.put("instructionId", instructionId);
            mapAll.put("fromAccountCode", fromAccountCode);
            mapAll.put("fromAccountName", fromAccountName);
            mapAll.put("toAccountCode", toAccountCode);
            mapAll.put("toAccountName", toAccountName);
            mapAll.put("payAmount", payAmount);
            mapAll.put("source", "OA");
            mapAll.put("note", note);
            mapAll.put("toBank", toBank);
            mapAll.put("securityCode", securityCode);
            mapAll.put("metaJson", metaJson);

            String response = UtilHttp.doPost(url, headers, null, null, mapAll);
            log.info("非物流付款单 - 支付成功 [凭证号: {}, 金额: {}], 响应: {}", instructionId, payAmount, response);
        }

        return McR.success("处理完成");
    }
//TODO


    @Override
    public McR lilinJG(Map<String, Object> map) throws IOException {
        // 1. 查询所有需要处理的单据（状态为“已审批待付款”或“已审批部分付款”）
        List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-754586D31BF6414586E5C20DB3774A7320CV")
                .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
                        new YDSearch("selectField_ma0w9yrb",
                                Arrays.asList("已审批待付款", "已审批部分付款"),
                                "SelectField",
                                YDSearch.Type.RADIO_FIELD,
                                YDSearch.Operator.CONTAINS)
                )))
                .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();

        if (list == null || list.isEmpty()) {
            log.info("没有找到需要处理的单据");
            return McR.success("无待处理数据");
        }

        log.info("共查询到 {} 条待处理单据", list.size());

        // 公共参数提前准备（安全码、URL、请求头等）
        String url = "https://x.xencio.com/c4c3/api/payment/transferStatus";
        String securityCode = String.valueOf(token().getData());

        Map<String, String> headers = new HashMap<>();
        headers.put("x-xencio-client-id", "7dc3a31209b94a91ba40a44358fe70eb");
        headers.put("content-type", "application/x-www-form-urlencoded");

        Map<String, Object> params = new HashMap<>();
        params.put("securityCode", securityCode);

        int successCount = 0;
        int failCount = 0;
        List<String> failedIds = new ArrayList<>();

        // 2. 遍历 list，逐个执行原 lilinJG 中的处理逻辑
        for (Map item : list) {
            String formInstanceId = String.valueOf(item.get("formInstanceId"));
            if (formInstanceId == null || "null".equals(formInstanceId.trim()) || formInstanceId.trim().isEmpty()) {
                log.warn("跳过无效的 formInstanceId: {}", item.get("formInstanceId"));
                continue;
            }

            try {
                log.info("开始处理单据: formInstanceId={}", formInstanceId);

                // ===== 开始执行原 lilinJG 方法中的核心逻辑 =====

                // 获取表单数据
                Map data = (Map) ydClient.queryData(YDParam.builder()
                        .formInstId(formInstanceId)
                        .appType(ydConf.getAppType())
                        .systemToken(ydConf.getSystemToken())
                        .userId(ddConf.getOperator())
                        .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

                if (data == null) {
                    log.warn("获取表单数据为空，formInstanceId: {}", formInstanceId);
                    failCount++;
                    failedIds.add(formInstanceId);
                    continue;
                }

                log.info("form data: {}", data);

                String fklx = String.valueOf(data.get("selectField_m9ze3rjf")); // 付款类型
                String mainInstructionId = String.valueOf(data.get("serialNumberField_m9ze3rjd")); // 主表流水号

                // ===== 区分处理：是否为物流付款单 =====
                if ("物流付款单".equals(fklx)) {
                    // 物流付款单：处理子表
                    List<Map<String, Object>> tableField = (List<Map<String, Object>>) data.get("tableField_m986c5os");
                    if (tableField == null || tableField.isEmpty()) {
                        log.warn("物流付款单但子表为空，formInstanceId: {}", formInstanceId);
                        failCount++;
                        failedIds.add(formInstanceId);
                        continue;
                    }

                    List<Map<String, Object>> updatedRows = new ArrayList<>();
                    int completedCount = 0;

                    for (int i = 0; i < tableField.size(); i++) {
                        Map<String, Object> row = new HashMap<>(tableField.get(i)); // 复制原数据用于更新
                        String subInstructionId = String.valueOf(row.get("textField_mdnuob9a")); // 子表流水号

                        // 查询子表支付状态
                        params.put("instructionId", subInstructionId);
                        String response = UtilHttp.doGet(url, headers, params);
                        log.info("查询子表支付状态 [instructionId: {}], 响应: {}", subInstructionId, response);

                        JSONObject jsonResponse = JSON.parseObject(response);
                        JSONObject dataObj = jsonResponse.getJSONObject("data");
                        String message = dataObj != null ? dataObj.getString("message") : "";

                        String status = "未完成";
                        if (message != null && message.contains("成功")) {
                            status = "已完成";
                            completedCount++;
                        }

                        // 更新子表字段：selectField_mdmwfibp
                        row.put("selectField_mdmwfibp", status);
                        updatedRows.add(row);
                    }

                    // 构建要更新的子表数据
                    Map<String, Object> updateData = new HashMap<>();
                    updateData.put("tableField_m986c5os", updatedRows);

                    // 更新主表状态
                    String mainStatus;
                    if (completedCount == 0) {
                        mainStatus = "已审批待付款";
                    } else if (completedCount == updatedRows.size()) {
                        mainStatus = "已审批已付款";
                    } else {
                        mainStatus = "已审批部分付款";
                    }

                    updateData.put("selectField_ma0w9yrb", mainStatus);

                    // 提交更新
                    ydClient.operateData(YDParam.builder()
                            .formInstanceId(formInstanceId)
                            .updateFormDataJson(JSON.toJSONString(updateData))
                            .build(), YDConf.FORM_OPERATION.update);

                    log.info("【物流】状态更新完成，主表状态: {}", mainStatus);
                    successCount++;

                } else {
                    // 非物流付款单：主表单处理
                    if (mainInstructionId == null || mainInstructionId.isEmpty()) {
                        log.warn("主表流水号为空，formInstanceId: {}", formInstanceId);
                        failCount++;
                        failedIds.add(formInstanceId);
                        continue;
                    }

                    params.put("instructionId", mainInstructionId);
                    String response = UtilHttp.doGet(url, headers, params);
                    log.info("查询主表支付状态 [instructionId: {}], 响应: {}", mainInstructionId, response);

                    JSONObject jsonResponse = JSON.parseObject(response);
                    JSONObject dataObj = jsonResponse.getJSONObject("data");
                    String message = dataObj != null ? dataObj.getString("message") : "";

                    if (message != null && message.contains("成功")) {
                        // 更新主表状态为“已审批已付款”
                        ydClient.operateData(YDParam.builder()
                                .formInstanceId(formInstanceId)
                                .updateFormDataJson(JSON.toJSONString(
                                        UtilMap.map("selectField_ma0w9yrb", "已审批已付款")))
                                .build(), YDConf.FORM_OPERATION.update);
                        log.info("【非物流】支付成功，已更新状态");
                        successCount++;
                    } else {
                        log.info("【非物流】支付未成功，状态未更新");
                        failCount++;
                        failedIds.add(formInstanceId);
                    }
                }

                // ===== 单条处理结束 =====

            } catch (Exception e) {
                log.error("处理单据失败: formInstanceId={}", formInstanceId, e);
                failCount++;
                failedIds.add(formInstanceId);
            }
        }

        // 3. 返回批量处理结果
        return McR.success("批量状态同步完成");

    }


    @Override
    public McR lilinLS(Map<String, Object> map) {
        List<String> khdaList = Arrays.asList(
                "1001260509024819590",
                "1001260509424880696",
                "1001260509324868573",
                "1001260509324808614"
        );

        // 获取 token 一次
        String securityCode;
        try {
            securityCode = String.valueOf(token().getData());
            if (securityCode == null || securityCode.isEmpty()) {
                log.error("获取 securityCode 失败");
                return McR.errorParam("安全码获取失败");
            }
        } catch (Exception e) {
            log.error("调用 token() 异常", e);
            return McR.errorParam("认证信息获取失败");
        }

        OkHttpClient client = new OkHttpClient();

        for (String accountNo : khdaList) {
            Response response = null;
            try {
                if (accountNo == null || accountNo.trim().isEmpty()) {
                    log.warn("跳过空 accountNo");
                    continue;
                }
                accountNo = accountNo.trim();

                log.info("开始对账单同步，accountNo: {}", accountNo);

                // 准备时间范围
                String dayFromId = LocalDate.now().minusDays(7).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
                String dayToId = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));

                // 构建 POST 请求体
                MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
                String requestBodyStr = "securityCode=" + securityCode +
                        "&accountNo=" + accountNo +
                        "&dayFromId=" + dayFromId +
                        "&dayToId=" + dayToId +
                        "&instructionIdFlag=1" +
                        "&pageNow=1" +
                        "&trxFlag=R" +
                        "&pageSize=20";

                RequestBody body = RequestBody.create(mediaType, requestBodyStr);

                Request request = new Request.Builder()
                        .url("https://x.xencio.com/c4c3/api/bs/list")
                        .post(body)
                        .addHeader("x-xencio-client-id", "7dc3a31209b94a91ba40a44358fe70eb")
                        .addHeader("content-type", "application/x-www-form-urlencoded")
                        .build();

                response = client.newCall(request).execute();

                if (!response.isSuccessful()) {
                    log.error("银行接口调用失败，HTTP状态码: {}", response.code());
                    continue; // 跳过当前账号
                }

                String jsonStr = response.body().string();
                log.debug("银行接口响应原始数据: {}", jsonStr);

                JSONObject jsonObject = JSON.parseObject(jsonStr);
                JSONObject data = jsonObject.getJSONObject("data");
                if (data == null) {
                    log.error("响应中缺少 'data' 字段, accountNo: {}", accountNo);
                    continue;
                }

                JSONArray statementList = data.getJSONArray("searchBankStatementList");
                if (statementList == null || statementList.isEmpty()) {
                    log.info("银行接口返回空列表，accountNo: {}", accountNo);
                    continue;
                }

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

                for (int i = 0; i < statementList.size(); i++) {
                    JSONObject item = statementList.getJSONObject(i);
                    if (item == null) continue;

                    Map<String, String> extractedItem = new HashMap<>();
                    String bankStatementId = getStringValue(item, "bankStatementId");
                    String bankComments = getStringValue(item, "bankComments");
                    String crAmount = getStringValue(item, "crAmount");
                    String trxDate = getStringValue(item, "trxDate");
                    String accountName = getStringValue(item, "accountName"); // 本方账号名称
                    String userRemarks = getStringValue(item, "userRemarks");
                    String userMemo = getStringValue(item, "userMemo");
                    String transFlag = getStringValue(item, "transFlag");
                    String catalogName = getStringValue(item, "catalogName");
                    String customerName = getStringValue(item, "customerName"); // 对手方账户名称

                    Long timestamp = null;
                    if (trxDate != null && !trxDate.isEmpty()) {
                        try {
                            timestamp = LocalDateTime.parse(trxDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
                                    .atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                        } catch (Exception e) {
                            log.warn("日期解析失败: {}", trxDate);
                        }
                    }

                    extractedItem.put("bankStatementId", bankStatementId);
                    extractedItem.put("bankComments", bankComments);
                    extractedItem.put("crAmount", crAmount);
                    extractedItem.put("accountName", accountName);
                    extractedItem.put("trxDate", trxDate);
                    extractedItem.put("userRemarks", userRemarks);
                    extractedItem.put("customerName", customerName);
                    extractedList.add(extractedItem);

                    // 检查是否已存在该对账单记录
                    boolean exists = false;
                    try {
                        List<Map> list = (List<Map>) ydClient.queryData(
                                YDParam.builder()
                                        .formUuid("FORM-AF6E9FBF8F7642D59A333BC9EF3DF3FB836A")
                                        .appType("APP_RPH7R3LF3SMXLRDY1ZJW")
                                        .systemToken("7M866K91D4LVACB4EADAZ5UJG7IN3OGA33WAMNT")
                                        .searchCondition(JSON.toJSONString(UtilMap.map("textField_mdh05xw6", bankStatementId)))
                                        .build(),
                                YDConf.FORM_QUERY.retrieve_list_all
                        ).getData();

                        exists = list != null && !list.isEmpty();
                    } catch (Exception e) {
                        log.error("查询宜搭系统失败，bankStatementId: {}", bankStatementId, e);
                    }

                    if (exists) {
                        log.debug("记录已存在，跳过: bankStatementId={}", bankStatementId);
                        continue;
                    }

                    // 查询客户主数据
                    String khformInstanceId = null;
                    String khaccountName = null;
                    try {
                        List<Map> khlist = (List<Map>) ydClient.queryData(
                                YDParam.builder()
                                        .formUuid("FORM-E18CC7F3495B45BB852CB386F325BF03IAS3")
                                        .appType("APP_RPH7R3LF3SMXLRDY1ZJW")
                                        .systemToken("7M866K91D4LVACB4EADAZ5UJG7IN3OGA33WAMNT")
                                        .searchCondition(JSON.toJSONString(UtilMap.map("textField_m8sjaafp", customerName)))
                                        .build(),
                                YDConf.FORM_QUERY.retrieve_list_all
                        ).getData();

                        if (khlist != null && !khlist.isEmpty()) {
                            for (Map<String, Object> khitem : khlist) {
                                Map<String, Object> khformData = (Map<String, Object>) khitem.get("formData");
                                if (khformData != null && customerName.equals(khformData.get("textField_m8sjaafp"))) {
                                    khformInstanceId = (String) khitem.get("formInstanceId");
                                    khaccountName = (String) khformData.get("textField_m8sjaafp");
                                    break;
                                }
                            }
                        }

                        if (khformInstanceId == null || khaccountName == null) {
                            log.warn("未找到匹配的客户信息: customerName={}", customerName);
                            // 可选择跳过或继续（不关联客户）
                        }

                        // 构造 formData
                        Map<String, Object> formData = new HashMap<>();
                        formData.put("dateField_m8yeduje", timestamp != null ? timestamp : System.currentTimeMillis());
                        formData.put("dateField_mbg6gyre", timestamp != null ? timestamp : System.currentTimeMillis());
                        formData.put("numberField_m9ibr418", crAmount);
                        formData.put("employeeField_m8yeduj3", Arrays.asList("275412081437800471"));
                        formData.put("textField_mdh05xw6", bankStatementId);
                        formData.put("textField_m9ibr419", userRemarks);
                        formData.put("textField_mdh51nxq", userMemo);
                        formData.put("textField_mdh51nxr", transFlag);
                        formData.put("textField_mdh51nxs", catalogName);
                        formData.put("selectField_m982msou", accountName);

                        if (khaccountName != null && khformInstanceId != null) {
                            formData.put("associationFormField_m8yeduj1",
                                    Arrays.asList(getkhAss(khaccountName, khformInstanceId)));
                        }

                        // 提交创建流程
                        ydClient.operateData(
                                YDParam.builder()
                                        .formUuid("FORM-AF6E9FBF8F7642D59A333BC9EF3DF3FB836A")
                                        .processCode("TPROC--FPB66VB19FKVVL56E43NW6XQCP7T3URA33WAML3")
                                        .formDataJson(JSON.toJSONString(formData))
                                        .userId("275412081437800471")
                                        .build(),
                                YDConf.FORM_OPERATION.start
                        );

                        log.info("成功创建对账记录: bankStatementId={}", bankStatementId);

                    } catch (Exception e) {
                        log.error("创建对账记录失败: bankStatementId={}", bankStatementId, e);
                    }
                }

                log.info("对账单同步完成，accountNo={}，共处理 {} 条", accountNo, extractedList.size());

            } catch (IOException e) {
                log.error("网络IO异常: accountNo={}", accountNo, e);
            } catch (Exception e) {
                log.error("处理账号异常: accountNo={}", accountNo, e);
            } finally {
                // 确保 response 被安全关闭
                if (response != null) {
                    try {
                        response.close();
                    } catch (Exception e) {
                        log.warn("关闭 HTTP 响应失败", e);
                    }
                }
            }
        }

        return McR.success("所有账号同步完成");
    }

    // 工具方法：安全获取字符串值
    private String getStringValue(JSONObject json, String key) {
        Object value = json.get(key);
        return value == null ? "" : value.toString();
    }

    @Override
    public McR lilinLSCF(Map<String, Object> map) throws IOException {
        String formInstanceId = String.valueOf(map.get("formInstanceId"));
        if (formInstanceId == null || formInstanceId.trim().isEmpty()) {
            return McR.errorParam("formInstanceId 不能为空");
        }
        log.info("开始处理对账单拆分，formInstanceId: {}", formInstanceId);

        String bankStatementIdStr = String.valueOf(map.get("bankStatementId"));
        if (bankStatementIdStr == null || bankStatementIdStr.trim().isEmpty()) {
            return McR.errorParam("bankStatementId 不能为空");
        }

        // 查询表单数据
        Map data;
        try {
            data = (Map) ydClient.queryData(YDParam.builder()
                    .formInstId(formInstanceId)
                    .appType(ydConf.getAppType())
                    .systemToken(ydConf.getSystemToken())
                    .userId(ddConf.getOperator())
                    .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
        } catch (Exception e) {
            log.error("查询宜搭表单失败，formInstanceId: {}", formInstanceId, e);
            return McR.errorParam("查询表单数据失败");
        }

        if (data == null) {
            log.warn("表单数据为空，formInstanceId: {}", formInstanceId);
            return McR.errorParam("表单数据为空");
        }

        // 提取字段（安全获取）
        String userMemo = getStringValue(data, "textField_mdh51nxq");
        String transFlag = getStringValue(data, "textField_mdh51nxr");
        String catalogName = getStringValue(data, "textField_mdh51nxs");

        // 处理子表数据
        List<Map<String, Object>> tableField = (List<Map<String, Object>>) data.get("tableField_m9m8zhyy");
        List<Map<String, Object>> subBankStatementList = new ArrayList<>();

        if (tableField != null && !tableField.isEmpty()) {
            for (int i = 0; i < tableField.size(); i++) {
                Map<String, Object> row = tableField.get(i);
                if (row == null) continue;

                Map<String, Object> newItem = new HashMap<>();
                newItem.put("itemIndex", i + 1);
                newItem.put("amount", getAmountFromMap(row, "numberField_m9u2a5tf").doubleValue());
                newItem.put("projectInfo", getStringValue(row, "textField_m9i9k5cl") + "-" + getStringValue(row, "textField_m8yedujm"));
                newItem.put("userMemo", userMemo);
                newItem.put("trxFlag", transFlag);
                newItem.put("catalogName", catalogName);

                subBankStatementList.add(newItem);
            }
        }

        // 构建主请求体
        Map<String, Object> requestBody = new HashMap<>();
        try {
            requestBody.put("bankStatementId", Integer.parseInt(bankStatementIdStr));
        } catch (NumberFormatException e) {
            log.error("bankStatementId 格式错误: {}", bankStatementIdStr);
            return McR.errorParam("bankStatementId 必须为整数");
        }
        requestBody.put("subBankStatementList", subBankStatementList);

        // 请求参数
        String url = "https://x.xencio.com/c4c3/api/sub/bs/splitBankStatement";
        String securityCode = String.valueOf(token().getData());
        if (securityCode == null || securityCode.isEmpty()) {
            log.error("获取 securityCode 失败");
            return McR.errorParam("认证失败");
        }

        Map<String, String> headers = new HashMap<>();
        headers.put("x-xencio-client-id", "7dc3a31209b94a91ba40a44358fe70eb");

//        Map params = new HashMap<>();
        headers.put("securityCode", securityCode);

        // 第一次调用：拆分对账单
        String response;
        try {
            response = UtilHttp.doPost(url, headers, new HashMap<>(), requestBody);
        } catch (Exception e) {
            log.error("调用 splitBankStatement 接口失败", e);
            return McR.errorParam("接口调用失败: splitBankStatement");
        }

        JSONObject jsonResponse;
        try {
            jsonResponse = JSON.parseObject(response);
        } catch (Exception e) {
            log.error("响应 JSON 解析失败: {}", response);
            return McR.errorParam("响应格式错误");
        }

        // 检查响应是否成功（假设返回 { "code": 0, "data": { ... } }）
        Integer code = jsonResponse.getInteger("code");
        if (code == null || code != 200) {
            String msg = jsonResponse.getString("msg");
            log.warn("splitBankStatement 接口调用失败，code: {}, msg: {}", code, msg);
            return McR.errorParam("接口返回错误: " + msg);
        }

        JSONObject dataObj = jsonResponse.getJSONObject("data");
        if (dataObj == null) {
            log.warn("响应中缺少 data 字段: {}", response);
            return McR.errorParam("响应数据缺失");
        }

        JSONObject subBankStatement = dataObj.getJSONObject("subBankStatement");
        if (subBankStatement == null) {
            log.warn("响应中缺少 subBankStatement 字段");
            return McR.errorParam("拆分结果缺失");
        }

        JSONArray subBankStatementList1 = subBankStatement.getJSONArray("subBankStatementList");
        if (subBankStatementList1 == null || subBankStatementList1.isEmpty()) {
            log.info("拆分结果为空");
            return McR.success("拆分成功，但无明细");
        }

        // 封装返回结果
        List<Map<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < subBankStatementList1.size(); i++) {
            JSONObject item = subBankStatementList1.getJSONObject(i);
            Map<String, Object> mapItem = new HashMap<>();
            mapItem.put("id", item.getInteger("id"));
            mapItem.put("projectInfo", item.getString("projectInfo"));
            result.add(mapItem);
        }

        int successCount = 0, failCount = 0;
        String updateUrl = "https://x.xencio.com/c4c3/api/sub/bs/updateProjectInfo";

        for (Map<String, Object> item : result) {
            try {
                String updateResponse = UtilHttp.doPost(updateUrl, headers, new HashMap<>(), item);
                // 可选：判断 updateResponse 是否成功
                successCount++;
            } catch (Exception e) {
                log.error("更新 projectInfo 失败，id: {}", item.get("id"), e);
                failCount++;
            }
        }

        log.info("拆分完成，共 {} 条，更新成功 {}，失败 {}", result.size(), successCount, failCount);

        // 返回结果（可附加统计）
        return McR.success(result);
    }

    @Override
    public McR taskPLJG() {
        List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-754586D31BF6414586E5C20DB3774A7320CV")
                .searchFieldJson(JSONObject.toJSONString(Arrays.asList(
                        new YDSearch("selectField_ma0w9yrb",
                                "已审批已付款",
                                "SelectField",
                                YDSearch.Type.RADIO_FIELD,
                                YDSearch.Operator.EQ)
                )))
                .build(), YDConf.FORM_QUERY.retrieve_list_all).getData();

        if (list == null || list.isEmpty()) {
            log.info("没有找到需要处理的单据");
            return McR.success("无待处理数据");
        }

        for (Map item : list) {
            try {
                String formInstanceId = getStringValue(item, "formInstanceId");
                if (formInstanceId == null || formInstanceId.isEmpty()) {
                    log.warn("跳过：formInstanceId 为空");
                    continue;
                }

                // 获取源表单详情
                Map data = (Map) ydClient.queryData(YDParam.builder()
                        .formInstId(formInstanceId)
                        .appType(ydConf.getAppType())
                        .systemToken(ydConf.getSystemToken())
                        .userId(ddConf.getOperator())
                        .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

                if (data == null) {
                    log.warn("formInstanceId={} 的 formData 为空，跳过", formInstanceId);
                    continue;
                }

                String fklx = getStringValue(data, "selectField_m9ze3rjf"); // 付款类型
                String mainInstructionId = getStringValue(data, "serialNumberField_m9ze3rjd"); // 主表流水号

                // 🔍 查询目标表单是否已存在该 mainInstructionId 的记录
                List<Map> list1 = (List<Map>) ydClient.queryData(
                        YDParam.builder()
                                .formUuid("FORM-6B8A683A23524D3596C8C6CA13327EF3552K")
                                .appType("APP_RPH7R3LF3SMXLRDY1ZJW")
                                .systemToken("7M866K91D4LVACB4EADAZ5UJG7IN3OGA33WAMNT")
                                .searchCondition(JSON.toJSONString(UtilMap.map("textField_mdsk7rlm", mainInstructionId)))
                                .build(),
                        YDConf.FORM_QUERY.retrieve_list_all
                ).getData();

                // ✅ 如果目标表单中已存在该流水号，则跳过，不再新增
                if (list1 != null && !list1.isEmpty()) {
                    log.info("已存在重复数据，跳过插入：mainInstructionId={}", mainInstructionId);
                    continue;
                }

                // 继续提取其他字段
                String note = getStringValue(data, "textField_m9ze3rk3"); // 付款用途
                String toAccountCode = getStringValue(data, "textField_m9ze3rjs"); // 收款账号
                String toAccountName = getStringValue(data, "textField_m9ze3rjl"); // 收款户名
                String toBank = getStringValue(data, "textField_m9ze3rjr"); // 收款银行支行
                String fromAccountCode = getStringValue(data, "textField_mdfnuyl9"); // 付款账号
                String fromAccountName = getStringValue(data, "textField_m9ze3rj8"); // 付款户名
                BigDecimal PayAmount;
                List<Map<String, Object>> updateList = new ArrayList<>();

                if ("物流付款单".equals(fklx)) {
                    List<Map<String, Object>> tableField = (List<Map<String, Object>>) data.get("tableField_m986c5os");
                    PayAmount = BigDecimal.ZERO; // 初始化累加器
                    if (tableField != null) {
                        for (Map<String, Object> row : tableField) {
                            String instructionId = getStringValue(row, "textField_mdnuob9a");
                            String xmbh = getStringValue(row, "textField_m9lfmh1p");
                            String payAmountStr = getStringValue(row, "numberField_m8yf6gn1");
                            BigDecimal payAmount1 = BigDecimal.ZERO;
                            if (payAmountStr != null && !payAmountStr.trim().isEmpty()) {
                                try {
                                    payAmount1 = new BigDecimal(payAmountStr.trim());
                                    PayAmount = PayAmount.add(payAmount1);
                                } catch (NumberFormatException e) {
                                    // 处理转换失败的情况（可选：记录日志或忽略）
                                    System.err.println("Invalid number format: " + payAmountStr);
                                }
                            }
                            Map<String, Object> tablemap = new HashMap<>();
                            tablemap.put("associationFormField_ma13k7v2", Arrays.asList(getAss(mainInstructionId, formInstanceId)));
                            tablemap.put("selectField_m9ze3rjf", fklx);
                            tablemap.put("numberField_m9ze3rk4", payAmountStr);
                            tablemap.put("textField_m9ze3rk3", note);
                            tablemap.put("textField_m9ze3rjl", toAccountName);
                            tablemap.put("textField_m9ze3rjr", toBank);
                            tablemap.put("textField_m9ze3rjs", toAccountCode);
                            tablemap.put("textField_ma13k7v3", instructionId);
                            tablemap.put("textField_ma13k7v4", mainInstructionId);
                            tablemap.put("textField_madmzxce", xmbh);
                            updateList.add(tablemap);
                        }
                    }
                } else {
                    String instructionId = mainInstructionId;
                    PayAmount = safeGetBigDecimal(data, "numberField_m9ze3rk4");
                    String xmbh = getStringValue(data, "textField_madmzxce");

                    Map<String, Object> tablemap = new HashMap<>();
                    tablemap.put("associationFormField_ma13k7v2", Arrays.asList(getAss(mainInstructionId, formInstanceId)));
                    tablemap.put("selectField_m9ze3rjf", fklx);
                    tablemap.put("numberField_m9ze3rk4", PayAmount);
                    tablemap.put("textField_m9ze3rk3", note);
                    tablemap.put("textField_m9ze3rjl", toAccountName);
                    tablemap.put("textField_m9ze3rjr", toBank);
                    tablemap.put("textField_m9ze3rjs", toAccountCode);
                    tablemap.put("textField_ma13k7v3", instructionId);
                    tablemap.put("textField_ma13k7v4", mainInstructionId);
                    tablemap.put("textField_madmzxce", xmbh);
                    updateList.add(tablemap);
                }

                // 构建目标数据
                Map<String, Object> formData = new HashMap<>();
                formData.put("tableField_ma13k7v1", updateList);
                formData.put("numberField_mac3ccn8", PayAmount);
                formData.put("textField_mdsk7rlm", mainInstructionId); // 主流水号用于去重
                formData.put("employeeField_m8yf6gkl", Arrays.asList("275412081437800471"));

                // 执行新增
                ydClient.operateData(YDParam.builder()
                        .formUuid("FORM-6B8A683A23524D3596C8C6CA13327EF3552K")
                        .formDataJson(JSON.toJSONString(formData))
                        .userId("275412081437800471")
                        .build(), YDConf.FORM_OPERATION.create);

                log.info("成功插入新数据：mainInstructionId={}, 付款类型={}", mainInstructionId, fklx);

            } catch (Exception e) {
                log.error("处理单据时发生异常", e);
            }
        }

        return McR.success();
    }

    private Object getAss(String title, String id) {
        return UtilMap.map("appType, formUuid, formType, instanceId, title, subTitle", "APP_RPH7R3LF3SMXLRDY1ZJW", "FORM-754586D31BF6414586E5C20DB3774A7320CV", "receipt", id, title, "");
    }

    private Object getkhAss(String title, String id) {
        return UtilMap.map("appType, formUuid, formType, instanceId, title, subTitle", "APP_RPH7R3LF3SMXLRDY1ZJW", "FORM-E18CC7F3495B45BB852CB386F325BF03IAS3", "receipt", id, title, "");
    }

    // 工具方法：安全获取字符串
    private String getStringValue(Map data, String key) {
        Object val = data.get(key);
        return val == null ? "" : val.toString().trim();
    }

    // 工具方法：安全获取金额（默认 0）
    private static BigDecimal getAmountFromMap(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            log.debug("金额字段为空，key: {}", key);
            return BigDecimal.ZERO;
        }

        if (value instanceof Number) {
            // 优先处理数值类型
            try {
                return BigDecimal.valueOf(((Number) value).doubleValue());
            } catch (Exception e) {
                log.warn("Number 转换为 BigDecimal 失败，key: {}, value: {}", key, value, e);
                return BigDecimal.ZERO;
            }
        }

        // 尝试字符串解析
        String strValue = value.toString().trim();
        if (strValue.isEmpty()) {
            return BigDecimal.ZERO;
        }

        try {
            return new BigDecimal(strValue);
        } catch (NumberFormatException e) {
            log.warn("金额字符串格式错误，key: {}, value: {}", key, value);
            return BigDecimal.ZERO;
        }
    }

    private static McR token() {
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String s = SecureUtil.md5("}bH5%t4_)6e3#" + today);
        return McR.success(s);
    }

    // 安全获取字符串值
    private String safeGetString(Map formData, String key) {
        Object val = formData.get(key);
        return val == null ? "" : String.valueOf(val);
    }

    // 安全获取 double 值
    private double getDoubleSafe(Map item, String key) {
        try {
            Object val = item.get(key);
            if (val instanceof Number) {
                return ((Number) val).doubleValue();
            } else if (val instanceof String) {
                return Double.parseDouble((String) val);
            }
            return 0.0;
        } catch (Exception e) {
            return 0.0;
        }
    }

    private BigDecimal safeGetBigDecimal(Map formData, String key) {
        Object value = formData.get(key);
        if (value == null) return BigDecimal.ZERO;
        try {
            return new BigDecimal(value.toString());
        } catch (NumberFormatException e) {
            return BigDecimal.ZERO;
        }
    }
//    private static double getDoubleSafe(Map<String, Object> map, String key) {
//        try {
//            Object value = map.get(key);
//            if (value instanceof Number) {
//                return ((Number) value).doubleValue();
//            }
//            return 0.0;
//        } catch (Exception e) {
//            return 0.0;
//        }
//    }

    /**
     * 格式化两位小数
     */
//    private static String formatDecimal(double value) {
//        return DECIMAL_FORMAT.format(value);
//    }
}
