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