package com.malk.huagao.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.malk.huagao.entity.KdYdCustomer;
import com.malk.huagao.entity.KdYdCustomerLiaison;
import com.malk.huagao.mapper.KdYdCustomerLiaisonMapper;
import com.malk.huagao.mapper.KdYdCustomerMapper;
import com.malk.huagao.service.IKdYdCustomerLiaisonService;
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.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LQY
 * @since 2025-10-13
 */
@Service
public class KdYdCustomerLiaisonServiceImpl extends ServiceImpl<KdYdCustomerLiaisonMapper, KdYdCustomerLiaison> implements IKdYdCustomerLiaisonService {
    @Autowired
    private YDClient ydClient;
    @Autowired
    private KdYdCustomerLiaisonMapper kdYdCustomerLiaisonMapper;
    @Autowired
    private KdYdCustomerMapper kdYdCustomerMapper;

    @Override
    public void insertCustomerLiaison(Map map) {
        MDC.put("MDC_KEY_PID", "1003");
        String formInstId = UtilMap.getString(map, "formInstId");
        String type = UtilMap.getString(map, "type");

        if ("3".equals(type)) {
            String bm = UtilMap.getString(map, "bm");
            KdYdCustomerLiaison kdYdCustomerLiaison = new KdYdCustomerLiaison();
            kdYdCustomerLiaison.setOperationType("3");
            kdYdCustomerLiaison.setSyncStatus("0");
            kdYdCustomerLiaisonMapper.update(kdYdCustomerLiaison,
                    new LambdaQueryWrapper<KdYdCustomerLiaison>().eq(KdYdCustomerLiaison::getBm, bm));
            return; // 或者根据你的业务逻辑返回相应的结果
        }
        DDR_New ddrNew = ydClient.queryData(YDParam.builder()
                .formInstId(formInstId)
                .build(), YDConf.FORM_QUERY.retrieve_id);
        Map formData = ddrNew.getFormData();

        String xm = UtilMap.getString(formData, "textField_l3s6ubhq");
        String bm1 = UtilMap.getString(formData, "serialNumberField_mhk5zwzs");
        String zw = UtilMap.getString(formData, "textField_l3s6ubht");
        String gddh = UtilMap.getString(formData, "textField_mfxu5dnk");
        String yddh = UtilMap.getString(formData, "textField_lqbzc3gq");
        String cz = UtilMap.getString(formData, "textField_mfxu5dnl");
        String yx = UtilMap.getString(formData, "textField_lqbzc3gr");
        String xb = UtilMap.getString(formData, "radioField_l3s6ubhx");
//        String customerId = UtilMap.getString(formData, "textField_mgoxa46w");
//        String uuid = UtilMap.getString(formData, "textField_mfkp33s5");
        String khbh = UtilMap.getString(formData, "textField_mfkks8wx");

        LambdaQueryWrapper<KdYdCustomer> YdCustomerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        YdCustomerLambdaQueryWrapper.eq(KdYdCustomer::getKhbm, khbh);
        KdYdCustomer kdYdCustomer1 = kdYdCustomerMapper.selectOne(YdCustomerLambdaQueryWrapper);
        Long id = kdYdCustomer1.getId();
        KdYdCustomerLiaison kdYdCustomerLiaison = new KdYdCustomerLiaison();
        kdYdCustomerLiaison.setXm(xm);
        kdYdCustomerLiaison.setZw(zw);
        kdYdCustomerLiaison.setBm(bm1);
        kdYdCustomerLiaison.setGddh(gddh);
        kdYdCustomerLiaison.setYddh(yddh);
        kdYdCustomerLiaison.setCz(cz);
        kdYdCustomerLiaison.setYx(yx);
        kdYdCustomerLiaison.setXb(xb);
//        kdYdCustomerLiaison.setFormInstId(formInstId);
        kdYdCustomerLiaison.setCustomerId(id);
        kdYdCustomerLiaison.setSyncStatus("0");
        kdYdCustomerLiaison.setOperationType("1");
        if ("2".equals(type)) {
            kdYdCustomerLiaison.setOperationType(type);
            kdYdCustomerLiaisonMapper.update(kdYdCustomerLiaison,
                    new LambdaQueryWrapper<KdYdCustomerLiaison>().eq(KdYdCustomerLiaison::getBm, bm1));
        } else {
            this.save(kdYdCustomerLiaison);
        }
//        ydClient.operateData(
//                YDParam.builder()
//                        .formInstanceId(formInstId)
//                        .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textField_mfkks8wx", khbm)))
//                        .useLatestVersion(true).build(), YDConf.FORM_OPERATION.update);
    }
}
