CustomerDetailMagicAsyncComponent.java 10.5 KB
package cn.com.glsx.entmicro.manager;

import cn.com.glsx.entmicro.converter.MagicImportCustomerConvert;
import cn.com.glsx.entmicro.entity.Customer;
import cn.com.glsx.entmicro.entity.CustomerDetail;
import cn.com.glsx.entmicro.mapper.CustomerDetailMapper;
import cn.com.glsx.entmicro.mapper.CustomerMapper;
import cn.com.glsx.entmicro.model.CustomerMagicRopRequest;
import cn.com.glsx.entmicro.model.request.CustomerMagicRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicInfoRequest;
import cn.com.glsx.entmicro.model.request.MagicDynamicRequest;
import cn.com.glsx.entmicro.service.SyncCustomerService;
import cn.com.glsx.entmicro.util.BeanConverUtil;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wangxw
 * @version 1.0.0
 * @ClassName CustomerDetailMagicAsyncComponent.java
 * @description: ${description}
 * @createTime 2021年12月11日 18:15:00
 */
@Slf4j
@Component
public class CustomerDetailMagicAsyncComponent {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerDetailMapper customerDetailMapper;

    @Autowired
    private SyncCustomerService syncCustomerService;

    @Value("${addway.magic}")
    private Integer addwayMagic;

    @Async("taskExecutor")
    public void asyncUpdateCustomerDetailForMagic(List<CustomerMagicRequest> addList, List<CustomerMagicRequest> updateList, Map<String, Customer> existPhonesMap, MagicDynamicRequest magicDynamicRequest) {
        List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
        Map<String, MagicDynamicInfoRequest> customer_phone = customers.stream().collect(Collectors.toMap(MagicDynamicInfoRequest::getCustomerPhone, Function.identity()));
        // 插入数据
        saveMagicCustomer(addList, magicDynamicRequest.getMerchantId(), customer_phone);

        // 更新数据
        long start = System.currentTimeMillis();
        updateMagicCustomer(existPhonesMap, updateList, magicDynamicRequest.getMerchantId(), customer_phone);
        long end = System.currentTimeMillis();
        long time = end - start;
        log.info("批量更新开始时间:" + start);
        log.info("结束时间:" + end);
        log.info("用时:" + time);
        log.info("时间:" + (time / 1000));

        // 同步数据
        syncCustomerService.syncBatchCustomer(magicDynamicRequest);
    }

    /**
     *
     * @param addList
     * @param updateList
     * @param existPhonesMap
     * @param magicDynamicRequest
     */
    public void updateCustomerDetailForMagic(List<CustomerMagicRequest> addList, List<CustomerMagicRequest> updateList, Map<String, Customer> existPhonesMap, MagicDynamicRequest magicDynamicRequest) {
        List<MagicDynamicInfoRequest> customers = magicDynamicRequest.getCustomers();
        Map<String, MagicDynamicInfoRequest> customer_phone = customers.stream().collect(Collectors.toMap(MagicDynamicInfoRequest::getCustomerPhone, Function.identity(), (t1, t2) -> t1));
        // 插入数据
        saveMagicCustomer(addList, magicDynamicRequest.getMerchantId(), customer_phone);

        // 更新数据
        long start = System.currentTimeMillis();
        updateMagicCustomer(existPhonesMap, updateList, magicDynamicRequest.getMerchantId(), customer_phone);
        long end = System.currentTimeMillis();
        long time = end - start;
        log.info("批量更新开始时间:" + start);
        log.info("结束时间:" + end);
        log.info("用时:" + time);
        log.info("时间:" + (time / 1000));

        // 同步数据
        syncCustomerService.syncBatchCustomer(magicDynamicRequest);
    }

    /**
     * 保存魔方客户数据到基础表
     * @param addList 添加数据
     * @param merchantId 门店号
     * @param customer_phone
     */
    private void saveMagicCustomer(List<CustomerMagicRequest> addList, Integer merchantId, Map<String, MagicDynamicInfoRequest> customer_phone) {
        // 整理插入数据
        List<Customer> saveCustomerList = addList.stream().filter(req -> !StringUtils.isEmpty(req.getCustomerPhone())).map(req -> {
            Customer customer = MagicImportCustomerConvert.buildCustomerByMagic(merchantId, req, true, addwayMagic, req.getAddWay());
            return customer;
        }).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(saveCustomerList)) {
            return;
        }

        // 批量保存客户数据
        log.info("保存魔方客户数据到基础表: {}", JSONArray.toJSONString(saveCustomerList));
        customerMapper.insertList(saveCustomerList);

        //构建客户车辆详情数据
        Map<String, CustomerMagicRequest> custMap = addList.stream().collect(Collectors.toMap(CustomerMagicRequest::getCustomerPhone, Function.identity()));
        List<CustomerDetail> customerDetailAddList = saveCustomerList.stream().map(req -> {
            CustomerMagicRequest request = custMap.get(req.getCustomerPhone());
            MagicDynamicInfoRequest magicDynamicInfoRequest = customer_phone.get(req.getCustomerPhone());
            CustomerDetail detail = MagicImportCustomerConvert.buildCustomerDetailByMagic(merchantId, request, true);
            detail.setCustomerId(req.getId());
            Map<String, Object> custAttr = magicDynamicInfoRequest.getCustAttr();
            custAttr.put("customer_id", req.getId());
            magicDynamicInfoRequest.setCustomerId(req.getId());
            return detail;
        }).collect(Collectors.toList());

        List<CustomerDetail> custDetailAddList = filterEmpty(customerDetailAddList);
        if (!CollectionUtils.isEmpty(custDetailAddList)) {
            // 批量保存客户详情
            customerDetailMapper.insertList(custDetailAddList);
        }


    }

    /**
     * 更新魔方客户数据
     * @param existPhonesMap
     * @param updateList
     * @param merchantId
     * @param customer_phone
     */
    public void updateMagicCustomer(Map<String, Customer> existPhonesMap, List<CustomerMagicRequest> updateList, Integer merchantId, Map<String, MagicDynamicInfoRequest> customer_phone) {

        // 1、客户更新数据
        List<Customer> customerUpdateList = updateList.stream().filter(req -> existPhonesMap.get(req.getCustomerPhone()) != null).map(req -> {
            Customer customer = existPhonesMap.get(req.getCustomerPhone());
            Customer customerUpdate = MagicImportCustomerConvert.buildCustomerByMagic(merchantId, req, false, addwayMagic, req.getAddWay());
            customerUpdate.setId(customer.getId());
            return customerUpdate;
        }).collect(Collectors.toList());

        // 2、客户详情更新数据
        List<CustomerDetail> customerDetailUpdateList = updateList.stream().filter(req -> existPhonesMap.get(req.getCustomerPhone()) != null).map(req -> {
            Customer customer = existPhonesMap.get(req.getCustomerPhone());
            MagicDynamicInfoRequest magicDynamicInfoRequest = customer_phone.get(req.getCustomerPhone());
            CustomerDetail detail = MagicImportCustomerConvert.buildCustomerDetailByMagic(merchantId, req, false);
            detail.setCustomerId(customer.getId());
            Map<String, Object> custAttr = magicDynamicInfoRequest.getCustAttr();
            custAttr.put("customer_id", customer.getId());
            magicDynamicInfoRequest.setCustomerId(customer.getId());
            return detail;
        }).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(customerUpdateList)) {
            log.info("更新魔方客户数据到基础表: {}", JSONArray.toJSONString(customerUpdateList));
            //以下来源才可以修改企微名称
            List<Integer> addWayList = Arrays.asList(0,1,2,3,4,5,6,7,8,9,201,202,104);
            customerUpdateList.stream().filter(s -> !addWayList.contains(s.getAddWay())).forEach(s -> s.setCustomerName(null));
            customerMapper.batchUpdate(customerUpdateList);
        }

        if (!CollectionUtils.isEmpty(customerDetailUpdateList)) {

            customerDetailUpdateList = filterEmpty(customerDetailUpdateList);

            List<CustomerDetail> addlist = new ArrayList<>();
            List<CustomerDetail> updatelist = new ArrayList<>();


            List<Integer> collect = customerDetailUpdateList.stream().map(CustomerDetail::getCustomerId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(collect)) {
                List<CustomerDetail> customerDetails = customerDetailMapper.selectByCustomerIds(collect);
                if (CollectionUtils.isEmpty(customerDetails)) {
                    customerDetails = new ArrayList<>();
                }
                List<CustomerDetail> finalCustomerDetails = customerDetails;
                updatelist = customerDetailUpdateList.stream().filter(index -> finalCustomerDetails.contains(index)).collect(Collectors.toList());
                addlist = customerDetailUpdateList.stream().filter(index -> !finalCustomerDetails.contains(index)).collect(Collectors.toList());
            }
            if (!CollectionUtils.isEmpty(updatelist)) {
                customerDetailMapper.batchUpdate(updatelist);
            }
            if (!CollectionUtils.isEmpty(addlist)) {
                customerDetailMapper.insertList(addlist);
            }
        }
    }

    private List<CustomerDetail> filterEmpty(List<CustomerDetail> customerDetailUpdateList) {
        List<CustomerDetail> newCustomerDetail = new ArrayList<>();
        for (CustomerDetail index : customerDetailUpdateList) {
            if (StringUtils.isEmpty(index.getAudi()) && StringUtils.isEmpty(index.getCarBrand())
                    && StringUtils.isEmpty(index.getCarBrandIds()) && StringUtils.isEmpty(index.getCarNumber())
                    && StringUtils.isEmpty(index.getCarVin()) && StringUtils.isEmpty(index.getCarType())
                    && index.getMaintenanceDate() == null && index.getComInsuranceEndtime() == null
                    && index.getComInsuranceStarttime() == null && index.getBuyDate() == null && index.getArrivalDate() == null) {
                continue;
            }
            newCustomerDetail.add(index);
        }
        return newCustomerDetail;

    }
}