package com.malk.diwei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.malk.diwei.entity.Client;
import com.malk.diwei.entity.ClientMoreLinkData;
import com.malk.diwei.mapper.ClientMapper;
import com.malk.diwei.mapper.ClientMoreLinkDataMapper;
import com.malk.diwei.service.ClientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WZY
 * @since 2025-03-11
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private ClientMoreLinkDataMapper clientMoreLinkDataMapper;

    @Autowired
    private YDClient ydClient;

    @Override
    public McR syncClient() {
        LambdaQueryWrapper<Client> clientLambdaQueryWrapper = new LambdaQueryWrapper<>();

        List<Client> clientList = clientMapper.selectList(clientLambdaQueryWrapper);

        for (Client client : clientList) {
            Map formData = new HashMap();

            formData.put("numberField_m858ik1r",client.getClientID());//客户id
            formData.put("textField_m7vidqxr",client.getClientCode());//客户编号
            formData.put("textField_m7vidqxq",client.getClientName());//客户名称
            formData.put("textField_m843zjgb",client.getClientShortName());//客户简称
            formData.put("textField_m843zjgd",client.getClientShortCode());//助记码
            //结款类型
            if (Objects.nonNull(client.getClientPayWay())){
                switch (client.getClientPayWay()){
                    case 1 : formData.put("selectField_m843zjgj","现金");break;
                    case 2 : formData.put("selectField_m843zjgj","非现金");break;
                    case 9 : formData.put("selectField_m843zjgj","现金");break;
                    default : break;
                }
            }

            formData.put("textField_m843zjgk",client.getClientBank());//开户银行
            formData.put("textField_m7vidqy7",client.getClientBankaccount());//银行账号
            formData.put("textField_m7vidqy8",client.getClientTaxno());//税号
            formData.put("textField_m843zjgq",client.getClientLegalPerson());//法人代表
            formData.put("numberField_m843zjgr",client.getClientCredit());//信用额度

            //客户类别
            if (Objects.nonNull(client.getClientCategory())){
                switch (client.getClientCategory()){
                    case 0 : formData.put("selectField_m843zjg3","不限");break;
                    case 1 : formData.put("selectField_m843zjg3","成品供应商");break;
                    case 2 : formData.put("selectField_m843zjg3","原料供应商");break;
                    case 3 : formData.put("selectField_m843zjg3","辅料供应商");break;
                    case 4 : formData.put("selectField_m843zjg3","加工厂");break;
                    case 5 : formData.put("selectField_m843zjg3","物流公司");break;
                    case 6 : formData.put("selectField_m843zjg3","其他供应商");break;
                    default : break;
                }
            }

            //客户类型
            if (Objects.nonNull(client.getClientClass())){
                switch (client.getClientClass()){
                    case 1 : formData.put("selectField_m843zjgv","批发客户");break;
                    case 2 : formData.put("selectField_m843zjgv","Vip客户");break;
                }
            }

            //客户级别
            if (Objects.nonNull(client.getClientLevel())){
                switch (client.getClientLevel()){
                    case 1 : formData.put("textField_m7vidqxx","一级");break;
                    case 2 : formData.put("textField_m7vidqxx","二级");break;
                    case 3 : formData.put("textField_m7vidqxx","三级");break;
                    case 4 : formData.put("textField_m7vidqxx","四级");break;
                    case 5 : formData.put("textField_m7vidqxx","五级");break;
                    default : break;
                }
            }

            //客户属性
            if (Objects.nonNull(client.getClientProperty())){
                switch (client.getClientProperty()){
                    case 1 : formData.put("selectField_m843zjg1","客户");break;
                    case 2 : formData.put("selectField_m843zjg1","供应商");break;
                    default : break;
                }
            }

            //正常/停用
            if (Objects.nonNull(client.getClientStatus())){
                switch (client.getClientStatus()){
                    case 1 : formData.put("selectField_m843zjg2","正常");break;
                    case 2 : formData.put("selectField_m843zjg2","停用");break;
                    default : break;
                }
            }

            formData.put("textareaField_m843zjga",client.getClientDescription());//备注

            //查询联系人
            LambdaQueryWrapper<ClientMoreLinkData> clientMoreLinkDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
            clientMoreLinkDataLambdaQueryWrapper.eq(ClientMoreLinkData::getClientMoreLinkDataClientID,client.getClientID())
                    .isNotNull(ClientMoreLinkData::getClientMoreLinkDataLinkMan)
                    .ne(ClientMoreLinkData::getClientMoreLinkDataLinkMan,"");
            List<ClientMoreLinkData> clientMoreLinkDataList = clientMoreLinkDataMapper.selectList(clientMoreLinkDataLambdaQueryWrapper);

            List<Map> detailList = new ArrayList<>();

            if (!clientMoreLinkDataList.isEmpty()){
                for (ClientMoreLinkData clientMoreLinkData : clientMoreLinkDataList) {
                    Map detail = new HashMap();
                    detail.put("textField_m843zjfm",clientMoreLinkData.getClientMoreLinkDataLinkMan());//联系人
                    detail.put("textField_m843zjfn",clientMoreLinkData.getClientMoreLinkDataMob());//手机

                    detailList.add(detail);
                }
                formData.put("tableField_m843zjfl",detailList);//联系人明细
            }


            //精准匹配
            List<Map> searchCondition = new ArrayList<>();
            searchCondition.add(UtilMap.map("key, value, type, operator, componentName","textField_m7vidqxr",client.getClientCode(),"TEXT","eq","TextField"));
            ydClient.operateData(YDParam.builder()
                    .formUuid("FORM-55BFAC57E0774F4FBC77295ACA75E09AZTL8")
                    .searchFieldJson(JSONObject.toJSONString(searchCondition))
                    .formDataJson(JSONObject.toJSONString(formData))
                    .build(), YDConf.FORM_OPERATION.upsert);

        }
        return McR.success();
    }
}
