package com.malk.huagao.service.impl; 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 org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; 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 public class KdYdCustomerServiceImpl extends ServiceImpl implements IKdYdCustomerService { @Autowired private YDClient ydClient; @Autowired private KdYdCustomerMapper kdYdCustomerMapper; @Autowired private KdYdCustomerLiaisonMapper kdYdCustomerLiaisonMapper; @Override public void insertCustomer(Map map) { MDC.put("MDC_KEY_PID", "1003"); String formInstId = UtilMap.getString(map, "formInstId"); String type = UtilMap.getString(map, "type"); // === 操作类型 3:仅软删除客户(根据 khbm 标记 operation_type=3)=== if ("3".equals(type)) { String khbm = UtilMap.getString(map, "bm"); // 注意:这里 map 中的 "bm" 实际是 khbm KdYdCustomer updateObj = new KdYdCustomer(); updateObj.setOperationType("3"); updateObj.setSyncStatus("0"); kdYdCustomerMapper.update(updateObj, new LambdaQueryWrapper().eq(KdYdCustomer::getKhbm, khbm)); return; } // === 获取表单数据 === 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 = UtilMap.getString(formData, "employeeField_megi74yl").replace("[\"", "").replace("\"]", "").trim(); 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 = UtilMap.getString(formData, "employeeField_lqanqe6n").replace("[\"", "").replace("\"]", "").trim(); 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"); // === 构建客户对象 === KdYdCustomer customer = new KdYdCustomer(); customer.setKhmc(khmc); customer.setXssx(xssx); customer.setSpdzyx(spdzyx); customer.setKhsx(khsx); customer.setKhlb(khlb); customer.setKhfz(khfz); customer.setFptt(fptt); customer.setNsdjh(nsdjh); customer.setKhyh(khyh); customer.setYhzh(yhzh); customer.setKplx(kplx); customer.setKhlx(jxslx); customer.setKplxdh(kplxdh); customer.setKptxdz(kptxdz); customer.setTyshxydm(tyshxydm); customer.setKhtjr(khtjr); customer.setSktj(sktj); customer.setXsy(xsy); customer.setJsbb(jsbb); customer.setKhyxj(khyxj); customer.setQyxygl(qyxygl); customer.setMrsl(mrsl); customer.setSfl(sfl); customer.setKhbm(khbm); customer.setJxr(jxr); customer.setKhxypj(khxypj); customer.setFormInstId(formInstId); customer.setSyncStatus("0"); customer.setOperationType(type); // === 判断是新增还是更新 === LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(KdYdCustomer::getKhbm, khbm); KdYdCustomer existing = kdYdCustomerMapper.selectOne(wrapper); Long customerId; if (existing != null) { customer.setId(existing.getId()); kdYdCustomerMapper.updateById(customer); customerId = existing.getId(); } else { if ("2".equals(type)) { throw new RuntimeException("客户不存在,无法更新: " + khbm); } this.save(customer); customerId = customer.getId(); } // === 处理联系人子表 === List tableField = (List) formData.get("tableField_mfkks8ww"); if (tableField == null || tableField.isEmpty()) { return; } // ✅【修改点1】全局查找最大 bm(不按 customer_id 筛选) String currentMaxBm = null; QueryWrapper maxBmWrapper = new QueryWrapper<>(); maxBmWrapper.select("MAX(bm)"); List maxResults = kdYdCustomerLiaisonMapper.selectObjs(maxBmWrapper); if (!maxResults.isEmpty()) { Object obj = maxResults.get(0); if (obj != null) { if (obj instanceof Object[]) { Object[] arr = (Object[]) obj; if (arr.length > 0 && arr[0] != null) { currentMaxBm = arr[0].toString(); } } else { currentMaxBm = obj.toString(); } } } // ✅【新增】用于收集回写用的子表行 List updatedTableRowsForYD = new ArrayList<>(); for (Map item : tableField) { String xm = safeGetString(item, "textField_l3s6ubhq"); String dh = safeGetString(item, "textField_lqbzc3gq"); String zw = safeGetString(item, "textField_l3s6ubht"); String gddh = safeGetString(item, "textField_mfxu5dnk"); String bm = safeGetString(item, "textField_mfkp33s5"); String finalBm; if (bm != null && !bm.trim().isEmpty()) { finalBm = bm.trim(); } else { // 全局自增 if (currentMaxBm == null) { finalBm = "CXR00001"; } else { finalBm = incrementCode(currentMaxBm); } currentMaxBm = finalBm; // 下一条继续递增 } // ✅【新增】构造回写行(复制原行并覆盖 bm) Map updatedRow = new HashMap(item); updatedRow.put("textField_mfkp33s5", finalBm); updatedTableRowsForYD.add(updatedRow); KdYdCustomerLiaison liaison = new KdYdCustomerLiaison(); liaison.setXm(xm); liaison.setZw(zw); liaison.setBm(finalBm); liaison.setGddh(gddh); liaison.setCustomerId(customerId); liaison.setSyncStatus("0"); liaison.setOperationType(type); if ("2".equals(type)) { LambdaQueryWrapper updateWrapper = new LambdaQueryWrapper<>(); updateWrapper.eq(KdYdCustomerLiaison::getBm, finalBm); KdYdCustomerLiaison exists = kdYdCustomerLiaisonMapper.selectOne(updateWrapper); if (exists != null) { liaison.setId(exists.getId()); kdYdCustomerLiaisonMapper.updateById(liaison); } else { kdYdCustomerLiaisonMapper.insert(liaison); } } else { kdYdCustomerLiaisonMapper.insert(liaison); } } // ✅【修改点2】回写 bm 到宜搭表单(仅在此处新增) try { Map updatePayload = new HashMap<>(); updatePayload.put("tableField_mfkks8ww", updatedTableRowsForYD); ydClient.operateData( YDParam.builder() .formInstId(formInstId) .updateFormDataJson(JSONObject.toJSONString(updatePayload)) .build(), YDConf.FORM_OPERATION.update ); } catch (Exception e) { // 使用兼容性日志写法(避免占位符问题) log.warn("回写 bm 到宜搭失败,formInstId: " + formInstId + ", 错误: " + e.getMessage()); } } // === 工具方法(保持不变)=== 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"; } } }