You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
12 KiB
Java

package com.kiisoo.ic.customer.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.customer.entity.*;
import com.kiisoo.ic.customer.mapper.OpCustomerDOMapper;
import com.kiisoo.ic.customer.mapper.OpVipDOMapper;
import com.kiisoo.ic.domain.service.PrivilageDomainService;
import com.kiisoo.ic.generalize.entity.PoiCustomerContactDataStat;
import com.kiisoo.ic.generalize.mapper.PoiCustomerContactDataStatMapper;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* @Description: service
* @Author: wangyinjia
* @Date: 2020/4/9
* @Company: kiisoo
* @Version: 1.0
*/
@Slf4j
@Service
public class CustomerViewService {
/**有效*/
static final Integer VALID = 1;
static final Integer TEN = 10;
/**
* mapper
*/
@Autowired
private PrivilageDomainService privilageDomainService;
/**
* mapper
*/
@Autowired
private PoiStoreDOMapper poiStoreDOMapper;
/**
* mapper
*/
@Autowired
private OpCustomerDOMapper opCustomerDOMapper;
/**
* mapper
*/
@Autowired
private PoiCustomerContactDataStatMapper poiCustomerContactDataStatMapper;
/**
* vip mapper
*/
@Autowired
private OpVipDOMapper opVipDOMapper;
/**
* main
* @param userId id
* @param selectStartTime
* @param selectEndTime
* @param startTime
* @param endTime
* @return VO
*/
public CustomerViewVO selectCustomerViewMain(Long userId, String selectStartTime, String selectEndTime, String startTime, String endTime){
CustomerViewVO customerViewVO = new CustomerViewVO();
//shopIds
List<Long> shopIds = getShopIds(userId);
if(CollectionUtils.isEmpty(shopIds)){
return customerViewVO;
}
//柱状图好友list
List<OpCustomer> customerList = opCustomerDOMapper.selectCustomerList(shopIds, null, null, startTime, endTime);
//好友总数
Long customerCount = opCustomerDOMapper.selectCustomerCount(shopIds, null, null, null);
//好友总数(去重)
Long validCustomerCount = opCustomerDOMapper.selectCustomerCount(shopIds, VALID, null, null);
//删除拉黑数
QueryWrapper<PoiCustomerContactDataStat> wrapper = new QueryWrapper<>();
wrapper.in("store_id", shopIds).le("stat_time", selectEndTime).ge("stat_time", selectStartTime);
Long validDeleteCustomerCount = poiCustomerContactDataStatMapper.selectList(wrapper).stream()
.filter(contactDO -> contactDO.getNegativeFeedbackCnt() != null)
.map(PoiCustomerContactDataStat::getNegativeFeedbackCnt).mapToLong(a -> a).sum();
//会员总数
Long vipCount = opVipDOMapper.selectVipCount(shopIds, null, null);
//设置柱状图list
List<OpCustomer> validCustomerList = customerList.stream().filter(customerDO -> VALID.equals(customerDO.getValidType())).collect(Collectors.toList());
customerViewVO.setCustomerList(customerList);
customerViewVO.setValidCustomerList(validCustomerList);
//设置新增好友好友总数拉黑数vip人数柱状图list等数据
setCustomerViewData(customerViewVO, customerCount, validCustomerCount, validDeleteCustomerCount, vipCount, customerList, validCustomerList, selectStartTime, selectEndTime);
//前十名排行list等
List<OpCustomer> newCustimerList = customerList.stream().filter(customerDO -> filterCustomerByRegisterTime(customerDO, selectStartTime, selectEndTime)).collect(Collectors.toList());
List<CustomerViewCompanyVO> orgNewCustomerList = getOrgNewCustomerList(newCustimerList);
List<CustomerViewShopVO> shopNewCustomerList = getShopNewCustomerList(newCustimerList);
List<CustomerViewZeroExtendVO> zeroExtendList = getZeroExtendList(newCustimerList, shopIds);
customerViewVO.setOrgNewCustomerList(orgNewCustomerList);
customerViewVO.setShopNewCustomerList(shopNewCustomerList);
customerViewVO.setZeroExtendList(zeroExtendList);
return customerViewVO;
}
/**
* shopIds
* @param userId id
* @return shopIds
*/
List<Long> getShopIds(Long userId){
List<Long> shopIds = privilageDomainService.listUserDatePermission(userId);
return shopIds;
}
/**
* viplist
* @param customerViewVO VO
* @param customerCount
* @param validCustomerCount ()
* @param validDeleteCustomerCount
* @param vipCount
* @param customerList list
* @param validCustomerList list
* @param selectStartTime
* @param selectEndTime
*/
public void setCustomerViewData(CustomerViewVO customerViewVO, Long customerCount, Long validCustomerCount, Long validDeleteCustomerCount, Long vipCount,
List<OpCustomer> customerList, List<OpCustomer> validCustomerList, String selectStartTime, String selectEndTime){
//新增好友
Long newCustomerCount = customerList.stream().filter(customerDO -> filterCustomerByRegisterTime(customerDO, selectStartTime, selectEndTime)).count();
customerViewVO.setNewCustomer(newCustomerCount);
//新增好友(去重)
Long newValidCustomerCount = validCustomerList.stream().filter(customerDO -> filterCustomerByRegisterTime(customerDO, selectStartTime, selectEndTime)).count();
customerViewVO.setNewValidCustomer(newValidCustomerCount);
//好友总数
customerViewVO.setAllCustomer(customerCount);
//好友总数(去重)
customerViewVO.setAllValidCustomer(validCustomerCount);
//删除拉黑数(累计去重)
customerViewVO.setValidDeleteCustomer(validDeleteCustomerCount);
//会员总数
customerViewVO.setAllVipCount(vipCount);
}
/**
*
* @param customerDO DO
* @param selectStartTime
* @param selectEndTime
* @return true/false
*/
boolean filterCustomerByRegisterTime(OpCustomer customerDO, String selectStartTime, String selectEndTime){
Date registerDate = customerDO.getRegisterTime();
if(registerDate == null){
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
long startTime = sdf.parse(selectStartTime).getTime();
long endTime = sdf.parse(selectEndTime).getTime();
long registerTime = registerDate.getTime();
return startTime <= registerTime && endTime >= registerTime;
}catch (Exception e){
log.error("格式化时间出错", e);
return false;
}
}
/**
* list
* @param newCustimerList list
* @return list
*/
List<CustomerViewCompanyVO> getOrgNewCustomerList(List<OpCustomer> newCustimerList){
List<CustomerViewCompanyVO> orgNewCustomerList = new ArrayList<>();
//店铺id-新增好友数map
Map<Long, Long> shopIdCountMap = newCustimerList.stream().collect(Collectors.groupingBy(OpCustomer::getShopId, Collectors.counting()));
//店铺id→零售公司名称
List<Long> shopIds = newCustimerList.stream().map(OpCustomer::getShopId).collect(Collectors.toList());
if(CollectionUtils.isEmpty(shopIds)){
return orgNewCustomerList;
}
//公司id-公司店铺list map
Map<Long, List<CustomerViewShopVO>> orgShopListMap = opCustomerDOMapper.selectOrgByShopIds(shopIds).stream().distinct().collect(Collectors.groupingBy(CustomerViewShopVO::getOrgId));
//每个公司新增数
List<CustomerViewCompanyVO> tmpList = new ArrayList<>();
orgShopListMap.forEach((orgId,customerViewShopList) -> {
CustomerViewCompanyVO orgViewDO = new CustomerViewCompanyVO();
orgViewDO.setOrgId(orgId);
orgViewDO.setOrgName(customerViewShopList.get(0).getOrgName());
AtomicInteger newCustomerCount = new AtomicInteger(0);
customerViewShopList.forEach(customerViewShopVO -> {
Long shopId = customerViewShopVO.getShopId();
int tmpCount = shopIdCountMap.get(shopId) == null ? 0 : Math.toIntExact(shopIdCountMap.get(shopId));
newCustomerCount.updateAndGet(v -> v + tmpCount);
});
orgViewDO.setNewCustomerCount(newCustomerCount.get());
tmpList.add(orgViewDO);
});
//排序
orgNewCustomerList = tmpList.stream().sorted(Comparator.comparing(CustomerViewCompanyVO::getNewCustomerCount).reversed()).limit(TEN).collect(Collectors.toList());
return orgNewCustomerList;
}
/**
* list
* @param newCustimerList list
* @return list
*/
List<CustomerViewShopVO> getShopNewCustomerList(List<OpCustomer> newCustimerList){
//店铺分组
Map<Long, List<OpCustomer>> shopIdMap = newCustimerList.stream().collect(Collectors.groupingBy(OpCustomer::getShopId));
List<CustomerViewShopVO> shopViewList = new ArrayList<>();
shopIdMap.forEach((k,v) -> {
CustomerViewShopVO shopVO = new CustomerViewShopVO();
shopVO.setShopId(k);
shopVO.setNewCustomerCount(v.size());
PoiStore shopDO = poiStoreDOMapper.selectById(k);
if(shopDO != null){
shopVO.setShopName(shopDO.getName());
}
shopViewList.add(shopVO);
});
//排序
List<CustomerViewShopVO> shopNewCustomerList = shopViewList.stream().sorted(Comparator.comparing(CustomerViewShopVO::getNewCustomerCount).reversed()).limit(TEN).collect(Collectors.toList());
return shopNewCustomerList;
}
/**
* 广list
* @param newCustimerList list
* @param shopIds ids
* @return 广list
*/
List<CustomerViewZeroExtendVO> getZeroExtendList(List<OpCustomer> newCustimerList, List<Long> shopIds){
List<CustomerViewZeroExtendVO> zeroExtendList = new ArrayList<>();
//有推广的店铺ids
List<Long> notZeroShopIds = newCustimerList.stream().map(OpCustomer::getShopId).distinct().collect(Collectors.toList());
//公司id-公司店铺list map
Map<Long, List<CustomerViewShopVO>> orgShopListMap = opCustomerDOMapper.selectOrgByShopIds(shopIds).stream().distinct().collect(Collectors.groupingBy(CustomerViewShopVO::getOrgId));
List<CustomerViewZeroExtendVO> tmpZeroExtendList = new ArrayList<>();
orgShopListMap.forEach((orgId, orgShopRelationList) -> {
List<Long> tmpShopIds = orgShopRelationList.stream().map(CustomerViewShopVO::getShopId).distinct().collect(Collectors.toList());
int tmpShopCount = tmpShopIds.size();
CustomerViewZeroExtendVO zeroExtendVO = new CustomerViewZeroExtendVO();
zeroExtendVO.setOrgId(orgId);
zeroExtendVO.setOrgName(orgShopRelationList.get(0).getOrgName());
zeroExtendVO.setAllShopCount(tmpShopCount);
//推广的店铺
tmpShopIds.retainAll(notZeroShopIds);
zeroExtendVO.setZeroShopCount(tmpShopCount - tmpShopIds.size());
tmpZeroExtendList.add(zeroExtendVO);
});
zeroExtendList = tmpZeroExtendList.stream()
.filter(zeroExtendDO -> zeroExtendDO.getZeroShopCount() > 0)
.sorted(Comparator.comparing(CustomerViewZeroExtendVO::getZeroShopCount).reversed())
.limit(TEN).collect(Collectors.toList());
return zeroExtendList;
}
}