ScrmBaseInfoService.java 5.55 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.entity.magic.MagicTemplateConfig;
import cn.com.glsx.entmicro.entity.magic.MagicTemplateConfigInfo;
import cn.com.glsx.entmicro.entity.magic.TableConfig;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.scrm.system.remote.SystemLoginLogRemote;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.metrics.StringUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhouhaibao
 * @version 1.0
 * @description: 数仓数据处理
 * @date 2022/1/18 21:07
 */
@Service
@Slf4j
public class ScrmBaseInfoService {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private MagicTemplateConfigInfoService magicTemplateConfigInfoService;

    public void addCustomer(List<String> baseInfo) {
        if (!CollectionUtils.isEmpty(baseInfo)) {
            List<JSONObject> collect = baseInfo.stream().map(index -> {
                JSONObject jsonObject = JSONObject.parseObject(index);
                return jsonObject;
            }).collect(Collectors.toList());
            Map<Object, List<JSONObject>> merchant_id = collect.stream().collect(Collectors.groupingBy(item -> item.get("merchant_id")));
            Set<Map.Entry<Object, List<JSONObject>>> entries = merchant_id.entrySet();
            entries.forEach(entry -> {
                Integer merchantId = Integer.parseInt(entry.getKey() + "");
                TableConfig byMerchantIdAndType = magicTemplateConfigInfoService.findByMerchantIdAndType(merchantId, 2);
                if (byMerchantIdAndType != null) {
                    List<MagicTemplateConfigInfo> magicConfigList = byMerchantIdAndType.getMagicTemplateConfigInfoList();
                    log.info("==" + JSONObject.toJSONString(magicConfigList));
                    Map<String, String> magicConfigMapper = magicConfigList.stream().filter(magic -> StringUtils.isNotBlank(magic.getFieldName())).collect(Collectors.toMap(MagicTemplateConfigInfo::getFieldName, MagicTemplateConfigInfo::getCloumnName, (item1, item2) -> item1));
                    MagicDynamicRequest syncJson = parseDataJson(entry.getValue(), magicConfigMapper, byMerchantIdAndType.getMagicTemplateConfig());
                    syncJson.setDataType(2);
                    customerService.customerAddMagic(syncJson);
                } else {
                    log.info("未找到该商户的数仓配置");
                }
            });
        }


    }

    /**
     * 数仓数据转化
     *
     * @param dataJson            入参
     * @param magicConfigMapper   转化mapper
     * @param magicTemplateConfig
     * @return
     */
    private MagicDynamicRequest parseDataJson(List<JSONObject> dataJson, Map<String, String> magicConfigMapper, MagicTemplateConfig magicTemplateConfig) {
        MagicDynamicRequest magicDynamicRequest = new MagicDynamicRequest();
        log.info("映射关系{}", magicConfigMapper);
        List<MagicDynamicInfoRequest> collect = dataJson.stream().map(index -> {
            MagicDynamicInfoRequest magicDynamicInfoRequest = new MagicDynamicInfoRequest();
            Map<String, Object> custAttr = new HashMap<>();
            Map<String, Object> baseAtt = new HashMap<>();
            for (Map.Entry<String, String> entryItem : magicConfigMapper.entrySet()) {
                String dataValue = index.getString(entryItem.getKey());
                if (StringUtils.isBlank(dataValue)) {
                    dataValue = null;
                }
                log.info("key值为{},value为{}", entryItem.getKey(), dataValue);
                if (entryItem.getValue().equals("customer_phone")) {
                    magicDynamicInfoRequest.setCustomerPhone(dataValue);
                    custAttr.put(entryItem.getValue(), dataValue);
                } else if (entryItem.getValue().equals("customer_name") || entryItem.getValue().equals("customer_sex") || entryItem.getValue().equals("customer_idcard_no")) {
                    if (entryItem.getValue().equals("customer_sex")) {
                        custAttr.put(entryItem.getValue(), converSex(dataValue));
                    } else {
                        custAttr.put(entryItem.getValue(), dataValue);
                    }
                } else {
                    baseAtt.put(entryItem.getValue(), dataValue);
                }
            }
            magicDynamicInfoRequest.setBaseAttr(baseAtt);
            magicDynamicInfoRequest.setCustAttr(custAttr);
            return magicDynamicInfoRequest;
        }).collect(Collectors.toList());
        List<MagicDynamicInfoRequest> newList = collect.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MagicDynamicInfoRequest::getCustomerPhone))), ArrayList::new));
        magicDynamicRequest.setMerchantId(magicTemplateConfig.getMerchantId());
        magicDynamicRequest.setTableName(magicTemplateConfig.getTableName());
        magicDynamicRequest.setCustomers(newList);
        return magicDynamicRequest;
    }

    private String converSex(String dataValue) {
        String sex = null;
        if (dataValue != null) {
            if (dataValue.equals("男")) {
                sex = "1";
            } else {
                sex = "2";
            }
        }
        return sex;
    }
}