KdYdCustomerServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.malk.huagao.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.malk.huagao.entity.KdYdCustomer;
  6. import com.malk.huagao.entity.KdYdCustomerLiaison;
  7. import com.malk.huagao.entity.KdYdDeliveryDetail;
  8. import com.malk.huagao.mapper.KdYdCustomerLiaisonMapper;
  9. import com.malk.huagao.mapper.KdYdCustomerMapper;
  10. import com.malk.huagao.service.IKdYdCustomerService;
  11. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  12. import com.malk.server.aliwork.YDConf;
  13. import com.malk.server.aliwork.YDParam;
  14. import com.malk.server.dingtalk.DDR_New;
  15. import com.malk.service.aliwork.YDClient;
  16. import com.malk.utils.UtilMap;
  17. import org.slf4j.MDC;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.time.LocalDateTime;
  21. import java.util.*;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. /**
  25. * <p>
  26. * 服务实现类
  27. * </p>
  28. *
  29. * @author LQY
  30. * @since 2025-10-09
  31. */
  32. @Service
  33. public class KdYdCustomerServiceImpl extends ServiceImpl<KdYdCustomerMapper, KdYdCustomer> implements IKdYdCustomerService {
  34. @Autowired
  35. private YDClient ydClient;
  36. @Autowired
  37. private KdYdCustomerMapper kdYdCustomerMapper;
  38. @Autowired
  39. private KdYdCustomerLiaisonMapper kdYdCustomerLiaisonMapper;
  40. @Override
  41. public void insertCustomer(Map map) {
  42. MDC.put("MDC_KEY_PID", "1003");
  43. String formInstId = UtilMap.getString(map, "formInstId");
  44. String type = UtilMap.getString(map, "type");
  45. // === 操作类型 3:仅软删除客户(根据 khbm 标记 operation_type=3)===
  46. if ("3".equals(type)) {
  47. String khbm = UtilMap.getString(map, "bm"); // 注意:这里 map 中的 "bm" 实际是 khbm
  48. KdYdCustomer updateObj = new KdYdCustomer();
  49. updateObj.setOperationType("3");
  50. updateObj.setSyncStatus("0");
  51. kdYdCustomerMapper.update(updateObj,
  52. new LambdaQueryWrapper<KdYdCustomer>().eq(KdYdCustomer::getKhbm, khbm));
  53. return;
  54. }
  55. // === 获取表单数据 ===
  56. DDR_New ddrNew = ydClient.queryData(
  57. YDParam.builder().formInstId(formInstId).build(),
  58. YDConf.FORM_QUERY.retrieve_id
  59. );
  60. Map formData = ddrNew.getFormData();
  61. // === 提取主表字段(完全保留原有写法)===
  62. String khmc = UtilMap.getString(formData, "textField_lqanqe6j");
  63. String khbm = UtilMap.getString(formData, "textField_meqhqqvg");
  64. String xssx = UtilMap.getString(formData, "selectField_megi74y7");
  65. String khxypj = UtilMap.getString(formData, "selectField_mewirdx4");
  66. String khsx = UtilMap.getString(formData, "selectField_megi74y8");
  67. String jxr = UtilMap.getString(formData, "employeeField_megi74yl").replace("[\"", "").replace("\"]", "").trim();
  68. String khlb = UtilMap.getString(formData, "selectField_lqanqe6l");
  69. String khfz = UtilMap.getString(formData, "selectField_lqanqe6q");
  70. String fptt = UtilMap.getString(formData, "textField_llujklkr");
  71. String nsdjh = UtilMap.getString(formData, "textField_meqhqqvm");
  72. String khyh = UtilMap.getString(formData, "textField_meqhqqvn");
  73. String yhzh = UtilMap.getString(formData, "textField_meqhqqvs");
  74. String jxslx = UtilMap.getString(formData, "selectField_mis6kl56");
  75. String kplx = UtilMap.getString(formData, "selectField_lwopqbye");
  76. String kplxdh = UtilMap.getString(formData, "textField_llujklkp");
  77. String kptxdz = UtilMap.getString(formData, "textField_llujklkv");
  78. String tyshxydm = UtilMap.getString(formData, "textField_mfxrrysq");
  79. String spdzyx = UtilMap.getString(formData, "textField_mg0fq6l4");
  80. String khtjr = UtilMap.getString(formData, "textField_mfdrrg87");
  81. String sktj = UtilMap.getString(formData, "selectField_meqhqqvh");
  82. String xsy = UtilMap.getString(formData, "employeeField_lqanqe6n").replace("[\"", "").replace("\"]", "").trim();
  83. String jsbb = UtilMap.getString(formData, "selectField_mfxrryt0");
  84. String khyxj = UtilMap.getString(formData, "numberField_mfxs7rd3");
  85. String qyxygl = UtilMap.getString(formData, "radioField_mfxs7rd2");
  86. String mrsl = UtilMap.getString(formData, "selectField_mfxrryt3");
  87. String sfl = UtilMap.getString(formData, "selectField_mfxrryt2");
  88. // === 构建客户对象 ===
  89. KdYdCustomer customer = new KdYdCustomer();
  90. customer.setKhmc(khmc);
  91. customer.setXssx(xssx);
  92. customer.setSpdzyx(spdzyx);
  93. customer.setKhsx(khsx);
  94. customer.setKhlb(khlb);
  95. customer.setKhfz(khfz);
  96. customer.setFptt(fptt);
  97. customer.setNsdjh(nsdjh);
  98. customer.setKhyh(khyh);
  99. customer.setYhzh(yhzh);
  100. customer.setKplx(kplx);
  101. customer.setKhlx(jxslx);
  102. customer.setKplxdh(kplxdh);
  103. customer.setKptxdz(kptxdz);
  104. customer.setTyshxydm(tyshxydm);
  105. customer.setKhtjr(khtjr);
  106. customer.setSktj(sktj);
  107. customer.setXsy(xsy);
  108. customer.setJsbb(jsbb);
  109. customer.setKhyxj(khyxj);
  110. customer.setQyxygl(qyxygl);
  111. customer.setMrsl(mrsl);
  112. customer.setSfl(sfl);
  113. customer.setKhbm(khbm);
  114. customer.setJxr(jxr);
  115. customer.setKhxypj(khxypj);
  116. customer.setFormInstId(formInstId);
  117. customer.setSyncStatus("0");
  118. customer.setOperationType(type);
  119. // === 判断是新增还是更新 ===
  120. LambdaQueryWrapper<KdYdCustomer> wrapper = new LambdaQueryWrapper<>();
  121. wrapper.eq(KdYdCustomer::getKhbm, khbm);
  122. KdYdCustomer existing = kdYdCustomerMapper.selectOne(wrapper);
  123. Long customerId;
  124. if (existing != null) {
  125. customer.setId(existing.getId());
  126. kdYdCustomerMapper.updateById(customer);
  127. customerId = existing.getId();
  128. } else {
  129. if ("2".equals(type)) {
  130. throw new RuntimeException("客户不存在,无法更新: " + khbm);
  131. }
  132. this.save(customer);
  133. customerId = customer.getId();
  134. }
  135. // === 处理联系人子表 ===
  136. List<Map> tableField = (List<Map>) formData.get("tableField_mfkks8ww");
  137. if (tableField == null || tableField.isEmpty()) {
  138. return;
  139. }
  140. // ✅【修改点1】全局查找最大 bm(不按 customer_id 筛选)
  141. String currentMaxBm = null;
  142. QueryWrapper<KdYdCustomerLiaison> maxBmWrapper = new QueryWrapper<>();
  143. maxBmWrapper.select("MAX(bm)");
  144. List<Object> maxResults = kdYdCustomerLiaisonMapper.selectObjs(maxBmWrapper);
  145. if (!maxResults.isEmpty()) {
  146. Object obj = maxResults.get(0);
  147. if (obj != null) {
  148. if (obj instanceof Object[]) {
  149. Object[] arr = (Object[]) obj;
  150. if (arr.length > 0 && arr[0] != null) {
  151. currentMaxBm = arr[0].toString();
  152. }
  153. } else {
  154. currentMaxBm = obj.toString();
  155. }
  156. }
  157. }
  158. // ✅【新增】用于收集回写用的子表行
  159. List<Map> updatedTableRowsForYD = new ArrayList<>();
  160. for (Map item : tableField) {
  161. String xm = safeGetString(item, "textField_l3s6ubhq");
  162. String dh = safeGetString(item, "textField_lqbzc3gq");
  163. String zw = safeGetString(item, "textField_l3s6ubht");
  164. String gddh = safeGetString(item, "textField_mfxu5dnk");
  165. String bm = safeGetString(item, "textField_mfkp33s5");
  166. String finalBm;
  167. if (bm != null && !bm.trim().isEmpty()) {
  168. finalBm = bm.trim();
  169. } else {
  170. // 全局自增
  171. if (currentMaxBm == null) {
  172. finalBm = "CXR00001";
  173. } else {
  174. finalBm = incrementCode(currentMaxBm);
  175. }
  176. currentMaxBm = finalBm; // 下一条继续递增
  177. }
  178. // ✅【新增】构造回写行(复制原行并覆盖 bm)
  179. Map updatedRow = new HashMap(item);
  180. updatedRow.put("textField_mfkp33s5", finalBm);
  181. updatedTableRowsForYD.add(updatedRow);
  182. KdYdCustomerLiaison liaison = new KdYdCustomerLiaison();
  183. liaison.setXm(xm);
  184. liaison.setZw(zw);
  185. liaison.setBm(finalBm);
  186. liaison.setGddh(gddh);
  187. liaison.setCustomerId(customerId);
  188. liaison.setSyncStatus("0");
  189. liaison.setOperationType(type);
  190. if ("2".equals(type)) {
  191. LambdaQueryWrapper<KdYdCustomerLiaison> updateWrapper = new LambdaQueryWrapper<>();
  192. updateWrapper.eq(KdYdCustomerLiaison::getBm, finalBm);
  193. KdYdCustomerLiaison exists = kdYdCustomerLiaisonMapper.selectOne(updateWrapper);
  194. if (exists != null) {
  195. liaison.setId(exists.getId());
  196. kdYdCustomerLiaisonMapper.updateById(liaison);
  197. } else {
  198. kdYdCustomerLiaisonMapper.insert(liaison);
  199. }
  200. } else {
  201. kdYdCustomerLiaisonMapper.insert(liaison);
  202. }
  203. }
  204. // ✅【修改点2】回写 bm 到宜搭表单(仅在此处新增)
  205. try {
  206. Map<String, Object> updatePayload = new HashMap<>();
  207. updatePayload.put("tableField_mfkks8ww", updatedTableRowsForYD);
  208. ydClient.operateData(
  209. YDParam.builder()
  210. .formInstId(formInstId)
  211. .updateFormDataJson(JSONObject.toJSONString(updatePayload))
  212. .build(),
  213. YDConf.FORM_OPERATION.update
  214. );
  215. } catch (Exception e) {
  216. // 使用兼容性日志写法(避免占位符问题)
  217. log.warn("回写 bm 到宜搭失败,formInstId: " + formInstId + ", 错误: " + e.getMessage());
  218. }
  219. }
  220. // === 工具方法(保持不变)===
  221. private String safeGetString(Map formData, String key) {
  222. Object val = formData.get(key);
  223. return val == null ? "" : String.valueOf(val);
  224. }
  225. /**
  226. * 从编码如 CXR04454 生成 CXR04455
  227. */
  228. private String incrementCode(String code) {
  229. if (code == null || code.isEmpty()) {
  230. return "CXR00001";
  231. }
  232. Pattern pattern = Pattern.compile("^(\\D*)(\\d+)$");
  233. Matcher matcher = pattern.matcher(code.trim());
  234. if (matcher.matches()) {
  235. String prefix = matcher.group(1);
  236. String numberPart = matcher.group(2);
  237. long number = Long.parseLong(numberPart);
  238. String newNumber = String.format("%0" + numberPart.length() + "d", number + 1);
  239. return prefix + newNumber;
  240. } else {
  241. return "CXR00001";
  242. }
  243. }
  244. }