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