package com.malk.huagao.service.impl; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.malk.huagao.entity.KdYdCustomer; import com.malk.huagao.entity.KdYdCustomerLiaison; import com.malk.huagao.entity.KdYdDeliveryDetail; import com.malk.huagao.mapper.KdYdCustomerLiaisonMapper; import com.malk.huagao.mapper.KdYdCustomerMapper; import com.malk.huagao.service.IKdYdCustomerService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.malk.server.aliwork.YDConf; import com.malk.server.aliwork.YDParam; import com.malk.server.dingtalk.DDR_New; import com.malk.service.aliwork.YDClient; import com.malk.utils.UtilMap; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** *

* 服务实现类 *

* * @author LQY * @since 2025-10-09 */ @Service @Slf4j public class KdYdCustomerServiceImpl extends ServiceImpl implements IKdYdCustomerService { @Autowired private YDClient ydClient; @Autowired private KdYdCustomerMapper kdYdCustomerMapper; @Autowired private KdYdCustomerLiaisonMapper kdYdCustomerLiaisonMapper; @Value(value = "${kd.host}") private String kdHost; @Override public Map insertCustomer(Map map) { Map result = new HashMap<>(); try { MDC.put("MDC_KEY_PID", "1003"); String formInstId = UtilMap.getString(map, "formInstId"); String action = UtilMap.getString(map, "type"); // 处理特殊操作类型 if ("del".equals(action)) { String bm = UtilMap.getString(map, "bm"); Map headers = new HashMap<>(); headers.put("Content-Type", "application/x-www-form-urlencoded"); Map formParams = new HashMap<>(); Map djbh = new HashMap<>(); djbh.put("djbh", bm); result.put("data", djbh); String jsonStr = JSON.toJSONString(result); formParams.put("jsons", jsonStr); System.out.println("====" + formParams); HttpResponse response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/")//todo 待完善接口 .headerMap(headers, true) .form(formParams) .timeout(30000) // 30秒超时 .execute(); String responseBody = response.body(); // 返回删除操作的格式 result.put("table", "KD_YD_CUSTOMER"); result.put("action", "del"); result.put("data", new HashMap() {{ put("bm", bm); // 删除的主键或标识 put("msg", "删除成功"); }}); return result; } // === 获取表单数据 === DDR_New ddrNew = ydClient.queryData( YDParam.builder().formInstId(formInstId).build(), YDConf.FORM_QUERY.retrieve_id ); Map formData = ddrNew.getFormData(); // === 提取主表字段 === String khmc = UtilMap.getString(formData, "textField_lqanqe6j"); String khbm = UtilMap.getString(formData, "textField_meqhqqvg"); String xssx = UtilMap.getString(formData, "selectField_megi74y7"); String khxypj = UtilMap.getString(formData, "selectField_mewirdx4"); String khsx = UtilMap.getString(formData, "selectField_megi74y8"); String jxr = cleanEmployeeField(UtilMap.getString(formData, "employeeField_megi74yl")); String khlb = UtilMap.getString(formData, "selectField_lqanqe6l"); String khfz = UtilMap.getString(formData, "selectField_lqanqe6q"); String fptt = UtilMap.getString(formData, "textField_llujklkr"); String nsdjh = UtilMap.getString(formData, "textField_meqhqqvm"); String khyh = UtilMap.getString(formData, "textField_meqhqqvn"); String yhzh = UtilMap.getString(formData, "textField_meqhqqvs"); String jxslx = UtilMap.getString(formData, "selectField_mis6kl56"); String kplx = UtilMap.getString(formData, "selectField_lwopqbye"); String kplxdh = UtilMap.getString(formData, "textField_llujklkp"); String kptxdz = UtilMap.getString(formData, "textField_llujklkv"); String tyshxydm = UtilMap.getString(formData, "textField_mfxrrysq"); String spdzyx = UtilMap.getString(formData, "textField_mg0fq6l4"); String khtjr = UtilMap.getString(formData, "textField_mfdrrg87"); String sktj = UtilMap.getString(formData, "selectField_meqhqqvh"); String xsy = cleanEmployeeField(UtilMap.getString(formData, "employeeField_lqanqe6n")); String jsbb = UtilMap.getString(formData, "selectField_mfxrryt0"); String khyxj = UtilMap.getString(formData, "numberField_mfxs7rd3"); String qyxygl = UtilMap.getString(formData, "radioField_mfxs7rd2"); String mrsl = UtilMap.getString(formData, "selectField_mfxrryt3"); String sfl = UtilMap.getString(formData, "selectField_mfxrryt2"); // 构建主表数据 Map customData = new HashMap<>(); customData.put("khmc", khmc); customData.put("khbm", khbm); customData.put("xssx", xssx); customData.put("khxypj", khxypj); customData.put("khsx", khsx); customData.put("jxr", jxr); customData.put("khlb", khlb); customData.put("khfz", khfz); customData.put("fptt", fptt); customData.put("nsdjh", nsdjh); customData.put("khyh", khyh); customData.put("yhzh", yhzh); customData.put("jxslx", jxslx); customData.put("kplx", kplx); customData.put("kplxdh", kplxdh); customData.put("kptxdz", kptxdz); customData.put("tyshxydm", tyshxydm); customData.put("spdzyx", spdzyx); customData.put("khtjr", khtjr); customData.put("sktj", sktj); customData.put("xsy", xsy); customData.put("jsbb", jsbb); customData.put("khyxj", khyxj); customData.put("qyxygl", qyxygl); customData.put("mrsl", mrsl); customData.put("sfl", sfl); // 处理子表数据 List> contactList = new ArrayList<>(); List tableField = (List) formData.get("tableField_mfkks8ww"); if (tableField != null && !tableField.isEmpty()) { // 用于收集回写用的子表行 List updatedTableRowsForYD = new ArrayList<>(); for (Map item : tableField) { Map contactData = new HashMap<>(); String xm = safeGetString(item, "textField_l3s6ubhq"); String yddh = safeGetString(item, "textField_lqbzc3gq"); String zw = safeGetString(item, "textField_l3s6ubht"); String gddh = safeGetString(item, "textField_mfxu5dnk"); String bm = safeGetString(item, "textField_mfkp33s5"); String yx = safeGetString(item, "textField_lqbzc3gr"); // 添加到联系人列表 contactData.put("xm", xm); contactData.put("yddh", yddh); contactData.put("zw", zw); contactData.put("gddh", gddh); contactData.put("bm", bm); contactData.put("yx", yx); contactList.add(contactData); } // 将联系人列表添加到主数据 customData.put("contactList", contactList); Map headers = new HashMap<>(); headers.put("Content-Type", "application/x-www-form-urlencoded"); result.put("data", customData); // 将result转换为JSON字符串 String jsonStr = JSON.toJSONString(result); // 构建form参数 Map formParams = new HashMap<>(); formParams.put("jsons", jsonStr); // 发送请求 HttpResponse response = null; if (action == "add") { response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/")//todo 修改接口 .headerMap(headers, true) .form(formParams) .timeout(30000) // 30秒超时 .execute(); } else { response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/") .headerMap(headers, true) .form(formParams) .timeout(30000) // 30秒超时 .execute(); } String responseBody = response.body(); try { // 一行代码提取 String jdbh = JSON.parseArray(responseBody) .getJSONObject(0) .getJSONObject("data") .getString("djbh"); String msg = JSON.parseArray(responseBody) .getJSONObject(0) .getString("msg"); ydClient.operateData( YDParam.builder() .formInstanceId(formInstId) .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textareaField_mkxqgrvq, textField_mjs6fuwo", msg, jdbh))) .useLatestVersion(true).build(), YDConf.FORM_OPERATION.update); // Map updatePayload = new HashMap<>(); // updatePayload.put("tableField_mfkks8ww", updatedTableRowsForYD); // ydClient.operateData( // YDParam.builder() // .formInstanceId(formInstId) // .updateFormDataJson(JSONObject.toJSONString(updatePayload)) // .useLatestVersion(true) // .build(), // YDConf.FORM_OPERATION.update // ); log.info("回写数据到宜搭成功,formInstId: {}", formInstId); } catch (Exception e) { log.warn("回写数据到宜搭失败,formInstId: {}, 错误: {}", formInstId, e.getMessage()); } } // 构建返回结果 result.put("table", "KD_YD_CUSTOMER"); result.put("action", action != null ? action : "add"); result.put("data", customData); log.info("客户信息同步成功: formInstId={}, khmc={}, khbm={}", formInstId, khmc, khbm); } catch (Exception e) { log.error("客户信息同步异常", e); result.put("table", "KD_YD_CUSTOMER"); result.put("action", "error"); result.put("data", new HashMap() {{ put("errorMsg", "操作失败:" + e.getMessage()); }}); } return result; } // 清理员工字段格式 private String cleanEmployeeField(String employeeField) { if (employeeField == null) return ""; return employeeField.replace("[\"", "").replace("\"]", "").trim(); } // === 工具方法(保持不变)=== private String safeGetString(Map formData, String key) { Object val = formData.get(key); return val == null ? "" : String.valueOf(val); } /** * 从编码如 CXR04454 生成 CXR04455 */ private String incrementCode(String code) { if (code == null || code.isEmpty()) { return "CXR00001"; } Pattern pattern = Pattern.compile("^(\\D*)(\\d+)$"); Matcher matcher = pattern.matcher(code.trim()); if (matcher.matches()) { String prefix = matcher.group(1); String numberPart = matcher.group(2); long number = Long.parseLong(numberPart); String newNumber = String.format("%0" + numberPart.length() + "d", number + 1); return prefix + newNumber; } else { return "CXR00001"; } } }