DataCustomerService.java 4.1 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.dto.DataTotalCustomerDTO;
import cn.com.glsx.entmicro.model.CustomerStaticRequest;
import cn.com.glsx.entmicro.model.CustomerStatisticModel;
import cn.com.glsx.entmicro.model.request.CustomerSearchRequest;
import cn.com.glsx.entmicro.model.response.CustomerListResponse;
import cn.com.glsx.entmicro.util.DateFormatUtil;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import com.alibaba.metrics.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangxw
 * @version 1.0.0
 * @ClassName DataCustomerService.java
 * @description: ${description}
 * @createTime 2021年11月21日 16:38:00
 */
@Service
public class DataCustomerService {

    @Autowired
    private CustomerStatisticsService customerStatisticsService;

    @Autowired
    private CustomerService customerService;

    /**
     * 统计客户数量
     *
     * @param merchantId
     */
    public DataTotalCustomerDTO statisticCustomerTotalCount(Integer merchantId) {
        DataTotalCustomerDTO resultDTO = new DataTotalCustomerDTO();
        CustomerSearchRequest customerSearchRequest = new CustomerSearchRequest();
        customerSearchRequest.setMerchantId(merchantId);
        customerSearchRequest.setDatalist(Arrays.asList(merchantId));
        customerSearchRequest.setAddWay(101);
        customerSearchRequest.setUserType(1);
        List<CustomerListResponse> customerListResponses = customerService.customerList(customerSearchRequest);
        if (null != customerListResponses){
            //魔方导入客户数
            resultDTO.setMagicCount(customerListResponses.size());
            List<CustomerListResponse> collect = customerListResponses.stream().filter(index -> null != index.getIsJoin()
                    && index.getIsJoin() == Constants.CUSTOMER_BASE_CONSTANTS.IS_ADD_CONTACT).collect(Collectors.toList());
            //魔方已加企微客户数
            resultDTO.setQyCount(collect.size());
            //魔方未加企微客户数
            resultDTO.setUnQyCount(customerListResponses.size() - collect.size());
        }else {
            resultDTO.setMagicCount(0);
            resultDTO.setUnQyCount(0);
            resultDTO.setQyCount(0);
        }
        return resultDTO;
    }

    /**
     * 统计客户数量 - 数据整理
     *
     * @param merchantIds
     * @param startDate
     * @param endDate
     * @return
     */
    public DataTotalCustomerDTO staticCustomerListByTime(List<Integer> merchantIds, Date startDate, Date endDate) {
        DataTotalCustomerDTO resultDTO = new DataTotalCustomerDTO(0, 0, 0, 0);
        if (CollectionUtils.isEmpty(merchantIds)) {
            throw new RpcServiceException(DefaultErrorEnum.PARAMETER_NULL);
        }

        CustomerStaticRequest staticRequest = new CustomerStaticRequest();
        staticRequest.setMerchantIds(merchantIds);
        staticRequest.setStartTime((startDate != null) ? DateFormatUtil.toString(startDate, DateFormatUtil.pattern10) : null);
        staticRequest.setEndTime((endDate != null) ? DateFormatUtil.toString(endDate, DateFormatUtil.pattern10) : null);
        List<CustomerStatisticModel> customerList = customerStatisticsService.findTimeCustomerStatistic(staticRequest);
        if (CollectionUtils.isEmpty(customerList)) {
            return resultDTO;
        }

        int qyCount = (int) customerList.stream().filter(cus -> StringUtils.isNotBlank(cus.getUserId()) && cus.getFlag() == 0).count();
        resultDTO.setTotalCount(customerList.size());
        resultDTO.setQyCount(qyCount);//TODO 从合并表拿
        resultDTO.setUnQyCount(customerList.size() - qyCount);
        return resultDTO;
    }

}