KdYdCustomerServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package com.malk.huagao.service.impl;
  2. import cn.hutool.http.HttpRequest;
  3. import cn.hutool.http.HttpResponse;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.malk.huagao.entity.KdYdCustomer;
  9. import com.malk.huagao.entity.KdYdCustomerLiaison;
  10. import com.malk.huagao.entity.KdYdDeliveryDetail;
  11. import com.malk.huagao.mapper.KdYdCustomerLiaisonMapper;
  12. import com.malk.huagao.mapper.KdYdCustomerMapper;
  13. import com.malk.huagao.service.IKdYdCustomerService;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.malk.server.aliwork.YDConf;
  16. import com.malk.server.aliwork.YDParam;
  17. import com.malk.server.dingtalk.DDR_New;
  18. import com.malk.service.aliwork.YDClient;
  19. import com.malk.utils.UtilMap;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.slf4j.MDC;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Service;
  25. import java.time.LocalDateTime;
  26. import java.util.*;
  27. import java.util.regex.Matcher;
  28. import java.util.regex.Pattern;
  29. /**
  30. * <p>
  31. * 服务实现类
  32. * </p>
  33. *
  34. * @author LQY
  35. * @since 2025-10-09
  36. */
  37. @Service
  38. @Slf4j
  39. public class KdYdCustomerServiceImpl extends ServiceImpl<KdYdCustomerMapper, KdYdCustomer> implements IKdYdCustomerService {
  40. @Autowired
  41. private YDClient ydClient;
  42. @Autowired
  43. private KdYdCustomerMapper kdYdCustomerMapper;
  44. @Autowired
  45. private KdYdCustomerLiaisonMapper kdYdCustomerLiaisonMapper;
  46. @Value(value = "${kd.host}")
  47. private String kdHost;
  48. @Override
  49. public Map<String, Object> insertCustomer(Map map) {
  50. Map<String, Object> result = new HashMap<>();
  51. try {
  52. MDC.put("MDC_KEY_PID", "1003");
  53. String formInstId = UtilMap.getString(map, "formInstId");
  54. String action = UtilMap.getString(map, "type");
  55. // 处理特殊操作类型
  56. if ("del".equals(action)) {
  57. String bm = UtilMap.getString(map, "bm");
  58. Map<String, String> headers = new HashMap<>();
  59. headers.put("Content-Type", "application/x-www-form-urlencoded");
  60. Map<String, Object> formParams = new HashMap<>();
  61. Map<String, Object> djbh = new HashMap<>();
  62. djbh.put("djbh", bm);
  63. result.put("data", djbh);
  64. String jsonStr = JSON.toJSONString(result);
  65. formParams.put("jsons", jsonStr);
  66. System.out.println("====" + formParams);
  67. HttpResponse response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/")//todo 待完善接口
  68. .headerMap(headers, true)
  69. .form(formParams)
  70. .timeout(30000) // 30秒超时
  71. .execute();
  72. String responseBody = response.body();
  73. // 返回删除操作的格式
  74. result.put("table", "KD_YD_CUSTOMER");
  75. result.put("action", "del");
  76. result.put("data", new HashMap<String, Object>() {{
  77. put("bm", bm); // 删除的主键或标识
  78. put("msg", "删除成功");
  79. }});
  80. return result;
  81. }
  82. // === 获取表单数据 ===
  83. DDR_New ddrNew = ydClient.queryData(
  84. YDParam.builder().formInstId(formInstId).build(),
  85. YDConf.FORM_QUERY.retrieve_id
  86. );
  87. Map formData = ddrNew.getFormData();
  88. // === 提取主表字段 ===
  89. String khmc = UtilMap.getString(formData, "textField_lqanqe6j");
  90. String khbm = UtilMap.getString(formData, "textField_meqhqqvg");
  91. String xssx = UtilMap.getString(formData, "selectField_megi74y7");
  92. String khxypj = UtilMap.getString(formData, "selectField_mewirdx4");
  93. String khsx = UtilMap.getString(formData, "selectField_megi74y8");
  94. String jxr = cleanEmployeeField(UtilMap.getString(formData, "employeeField_megi74yl"));
  95. String khlb = UtilMap.getString(formData, "selectField_lqanqe6l");
  96. String khfz = UtilMap.getString(formData, "selectField_lqanqe6q");
  97. String fptt = UtilMap.getString(formData, "textField_llujklkr");
  98. String nsdjh = UtilMap.getString(formData, "textField_meqhqqvm");
  99. String khyh = UtilMap.getString(formData, "textField_meqhqqvn");
  100. String yhzh = UtilMap.getString(formData, "textField_meqhqqvs");
  101. String jxslx = UtilMap.getString(formData, "selectField_mis6kl56");
  102. String kplx = UtilMap.getString(formData, "selectField_lwopqbye");
  103. String kplxdh = UtilMap.getString(formData, "textField_llujklkp");
  104. String kptxdz = UtilMap.getString(formData, "textField_llujklkv");
  105. String tyshxydm = UtilMap.getString(formData, "textField_mfxrrysq");
  106. String spdzyx = UtilMap.getString(formData, "textField_mg0fq6l4");
  107. String khtjr = UtilMap.getString(formData, "textField_mfdrrg87");
  108. String sktj = UtilMap.getString(formData, "selectField_meqhqqvh");
  109. String xsy = cleanEmployeeField(UtilMap.getString(formData, "employeeField_lqanqe6n"));
  110. String jsbb = UtilMap.getString(formData, "selectField_mfxrryt0");
  111. String khyxj = UtilMap.getString(formData, "numberField_mfxs7rd3");
  112. String qyxygl = UtilMap.getString(formData, "radioField_mfxs7rd2");
  113. String mrsl = UtilMap.getString(formData, "selectField_mfxrryt3");
  114. String sfl = UtilMap.getString(formData, "selectField_mfxrryt2");
  115. // 构建主表数据
  116. Map<String, Object> customData = new HashMap<>();
  117. customData.put("khmc", khmc);
  118. customData.put("khbm", khbm);
  119. customData.put("xssx", xssx);
  120. customData.put("khxypj", khxypj);
  121. customData.put("khsx", khsx);
  122. customData.put("jxr", jxr);
  123. customData.put("khlb", khlb);
  124. customData.put("khfz", khfz);
  125. customData.put("fptt", fptt);
  126. customData.put("nsdjh", nsdjh);
  127. customData.put("khyh", khyh);
  128. customData.put("yhzh", yhzh);
  129. customData.put("jxslx", jxslx);
  130. customData.put("kplx", kplx);
  131. customData.put("kplxdh", kplxdh);
  132. customData.put("kptxdz", kptxdz);
  133. customData.put("tyshxydm", tyshxydm);
  134. customData.put("spdzyx", spdzyx);
  135. customData.put("khtjr", khtjr);
  136. customData.put("sktj", sktj);
  137. customData.put("xsy", xsy);
  138. customData.put("jsbb", jsbb);
  139. customData.put("khyxj", khyxj);
  140. customData.put("qyxygl", qyxygl);
  141. customData.put("mrsl", mrsl);
  142. customData.put("sfl", sfl);
  143. // 处理子表数据
  144. List<Map<String, Object>> contactList = new ArrayList<>();
  145. List<Map> tableField = (List<Map>) formData.get("tableField_mfkks8ww");
  146. if (tableField != null && !tableField.isEmpty()) {
  147. // 用于收集回写用的子表行
  148. List<Map> updatedTableRowsForYD = new ArrayList<>();
  149. for (Map item : tableField) {
  150. Map<String, Object> contactData = new HashMap<>();
  151. String xm = safeGetString(item, "textField_l3s6ubhq");
  152. String yddh = safeGetString(item, "textField_lqbzc3gq");
  153. String zw = safeGetString(item, "textField_l3s6ubht");
  154. String gddh = safeGetString(item, "textField_mfxu5dnk");
  155. String bm = safeGetString(item, "textField_mfkp33s5");
  156. String yx = safeGetString(item, "textField_lqbzc3gr");
  157. // 添加到联系人列表
  158. contactData.put("xm", xm);
  159. contactData.put("yddh", yddh);
  160. contactData.put("zw", zw);
  161. contactData.put("gddh", gddh);
  162. contactData.put("bm", bm);
  163. contactData.put("yx", yx);
  164. contactList.add(contactData);
  165. }
  166. // 将联系人列表添加到主数据
  167. customData.put("contactList", contactList);
  168. Map<String, String> headers = new HashMap<>();
  169. headers.put("Content-Type", "application/x-www-form-urlencoded");
  170. result.put("data", customData);
  171. // 将result转换为JSON字符串
  172. String jsonStr = JSON.toJSONString(result);
  173. // 构建form参数
  174. Map<String, Object> formParams = new HashMap<>();
  175. formParams.put("jsons", jsonStr);
  176. // 发送请求
  177. HttpResponse response = null;
  178. if (action == "add") {
  179. response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/")//todo 修改接口
  180. .headerMap(headers, true)
  181. .form(formParams)
  182. .timeout(30000) // 30秒超时
  183. .execute();
  184. } else {
  185. response = HttpRequest.post(kdHost+"/xk/dyzn/WsBasedyzn.asmx/")
  186. .headerMap(headers, true)
  187. .form(formParams)
  188. .timeout(30000) // 30秒超时
  189. .execute();
  190. }
  191. String responseBody = response.body();
  192. try {
  193. // 一行代码提取
  194. String jdbh = JSON.parseArray(responseBody)
  195. .getJSONObject(0)
  196. .getJSONObject("data")
  197. .getString("djbh");
  198. String msg = JSON.parseArray(responseBody)
  199. .getJSONObject(0)
  200. .getString("msg");
  201. ydClient.operateData(
  202. YDParam.builder()
  203. .formInstanceId(formInstId)
  204. .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textareaField_mkxqgrvq, textField_mjs6fuwo", msg, jdbh)))
  205. .useLatestVersion(true).build(),
  206. YDConf.FORM_OPERATION.update);
  207. // Map<String, Object> updatePayload = new HashMap<>();
  208. // updatePayload.put("tableField_mfkks8ww", updatedTableRowsForYD);
  209. // ydClient.operateData(
  210. // YDParam.builder()
  211. // .formInstanceId(formInstId)
  212. // .updateFormDataJson(JSONObject.toJSONString(updatePayload))
  213. // .useLatestVersion(true)
  214. // .build(),
  215. // YDConf.FORM_OPERATION.update
  216. // );
  217. log.info("回写数据到宜搭成功,formInstId: {}", formInstId);
  218. } catch (Exception e) {
  219. log.warn("回写数据到宜搭失败,formInstId: {}, 错误: {}", formInstId, e.getMessage());
  220. }
  221. }
  222. // 构建返回结果
  223. result.put("table", "KD_YD_CUSTOMER");
  224. result.put("action", action != null ? action : "add");
  225. result.put("data", customData);
  226. log.info("客户信息同步成功: formInstId={}, khmc={}, khbm={}", formInstId, khmc, khbm);
  227. } catch (Exception e) {
  228. log.error("客户信息同步异常", e);
  229. result.put("table", "KD_YD_CUSTOMER");
  230. result.put("action", "error");
  231. result.put("data", new HashMap<String, Object>() {{
  232. put("errorMsg", "操作失败:" + e.getMessage());
  233. }});
  234. }
  235. return result;
  236. }
  237. // 清理员工字段格式
  238. private String cleanEmployeeField(String employeeField) {
  239. if (employeeField == null) return "";
  240. return employeeField.replace("[\"", "").replace("\"]", "").trim();
  241. }
  242. // === 工具方法(保持不变)===
  243. private String safeGetString(Map formData, String key) {
  244. Object val = formData.get(key);
  245. return val == null ? "" : String.valueOf(val);
  246. }
  247. /**
  248. * 从编码如 CXR04454 生成 CXR04455
  249. */
  250. private String incrementCode(String code) {
  251. if (code == null || code.isEmpty()) {
  252. return "CXR00001";
  253. }
  254. Pattern pattern = Pattern.compile("^(\\D*)(\\d+)$");
  255. Matcher matcher = pattern.matcher(code.trim());
  256. if (matcher.matches()) {
  257. String prefix = matcher.group(1);
  258. String numberPart = matcher.group(2);
  259. long number = Long.parseLong(numberPart);
  260. String newNumber = String.format("%0" + numberPart.length() + "d", number + 1);
  261. return prefix + newNumber;
  262. } else {
  263. return "CXR00001";
  264. }
  265. }
  266. }