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.

196 lines
6.8 KiB
Java

package com.kiisoo.ic.customer.service;
import com.kiisoo.ic.customer.entity.CustomerViewVO;
import com.kiisoo.ic.customer.entity.OpCustomer;
import com.kiisoo.ic.customer.entity.OpVip;
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.store.entity.PoiStore;
import com.kiisoo.ic.store.service.PoiStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
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 Long ZERO_L= 0L;
/**
* mapper
*/
@Autowired
private PrivilageDomainService privilageDomainService;
/**
* mapper
*/
@Autowired
private PoiStoreService poiStoreService;
/**
* mapper
*/
@Autowired
private OpCustomerDOMapper opCustomerDOMapper;
/**
* vip mapper
*/
@Autowired
private OpVipDOMapper opVipDOMapper;
/**
* main
* @param userId id
* @param organizationId id
* @param shopId id
* @param sevenDayStartTime
* @param sevenDayEndTime
* @return VO
*/
public CustomerViewVO selectCustomerViewMain(Long userId, Long organizationId, Long shopId, String sevenDayStartTime, String sevenDayEndTime){
CustomerViewVO customerViewVO = new CustomerViewVO();
//shopIds
// List<Long> shopIds = getShopIds(userId, regionId, shopId);
List<Long> shopIds = new ArrayList<>();
//客户list
List<OpCustomer> customerList = opCustomerDOMapper.selectCustomerList(shopIds, null, null, null, null);
//会员list
List<OpVip> vipList = opVipDOMapper.selectVipList(shopIds, null, null);
//5大块客户数
//设置客户关联vip人数
setCustomerAndVipSize(customerList, vipList, customerViewVO);
//近七天新增list
List<OpCustomer> sevenDayCustomerList = customerList.stream().filter(customerDO -> filterSevenDayCustomer(customerDO, sevenDayStartTime, sevenDayEndTime)).collect(Collectors.toList());
List<OpVip> sevenDayVipList = vipList.stream().filter(vipDO -> filterSevenDayVip(vipDO, sevenDayStartTime, sevenDayEndTime)).collect(Collectors.toList());
customerViewVO.setSevenDayCustomerList(sevenDayCustomerList);
customerViewVO.setSevenDayVipList(sevenDayVipList);
return customerViewVO;
}
/**
* shopIds
* @param userId id
* @param regionId id
* @param shopId id
* @return shopIds
*/
List<Long> getShopIds(Long userId, Long regionId, Long shopId){
List<Long> shopIds = new ArrayList<>();
//单店
if(shopId != null){
shopIds.add(shopId);
return shopIds;
}
//单区域
if(regionId != null){
List<PoiStore> shopList = poiStoreService.getRegionShop(regionId);
shopIds = shopList.stream().map(PoiStore::getId).collect(Collectors.toList());
return shopIds;
}
//所有shopIds
shopIds = privilageDomainService.listUserDatePermission(userId);
return shopIds;
}
/**
* vip
* @param customerList List
* @param vipList vipList
* @param customerViewVO VO
*/
public void setCustomerAndVipSize(List<OpCustomer> customerList, List<OpVip> vipList, CustomerViewVO customerViewVO){
//关联人数(手机号确定)
List<String> customerPhoneList = customerList.stream().map(OpCustomer::getPhone).distinct().collect(Collectors.toList());
List<String> vipPhoneList = vipList.stream().map(OpVip::getPhone).distinct().collect(Collectors.toList());
AtomicLong commonInt = new AtomicLong(0);
customerPhoneList.forEach(customerPhone -> {
if(vipPhoneList.contains(customerPhone)){
commonInt.incrementAndGet();
}
});
Long common = commonInt.get();
customerViewVO.setCommon(common);
//未关联的客户数
Long customer = customerList.stream().filter(customerDO -> ZERO_L.equals(customerDO.getMemberId())).map(OpCustomer::getPhone).distinct().count();
customerViewVO.setCustomer(customer);
//未关联会员数
Long vip = vipList.stream().map(OpVip::getPhone).distinct().count();
customerViewVO.setVip(vip - common);
}
/**
* list
* @param customerDO DO
* @param sevenDayStartTime
* @param sevenDayEndTime
* @return list
*/
Boolean filterSevenDayCustomer(OpCustomer customerDO, String sevenDayStartTime, String sevenDayEndTime){
try {
if(customerDO.getRegisterTime() != null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long start = sdf.parse(sevenDayStartTime).getTime();
Long end = sdf.parse(sevenDayEndTime).getTime();
if(customerDO.getRegisterTime().getTime() >= start && customerDO.getRegisterTime().getTime() <= end){
return true;
}else{
return false;
}
}else{
return false;
}
}catch (Exception e){
log.error("过滤近7天客户出错", e);
return false;
}
}
/**
* list
* @param vipDO DO
* @param sevenDayStartTime
* @param sevenDayEndTime
* @return list
*/
Boolean filterSevenDayVip(OpVip vipDO, String sevenDayStartTime, String sevenDayEndTime){
try {
if(vipDO.getRegisterTime() != null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long start = sdf.parse(sevenDayStartTime).getTime();
Long end = sdf.parse(sevenDayEndTime).getTime();
if(vipDO.getRegisterTime().getTime() >= start && vipDO.getRegisterTime().getTime() <= end){
return true;
}else{
return false;
}
}else{
return false;
}
}catch (Exception e){
log.error("过滤近7天会员出错", e);
return false;
}
}
}