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.

224 lines
9.5 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.kiisoo.ic.customer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kiisoo.ic.constants.Constants;
import com.kiisoo.ic.customer.bean.CustomerDTO;
import com.kiisoo.ic.customer.bean.CustomerModifyDTO;
import com.kiisoo.ic.customer.entity.OpCustomer;
import com.kiisoo.ic.customer.bean.OpCustomerDTO;
import com.kiisoo.ic.customer.entity.OpSellerCustomerRelation;
import com.kiisoo.ic.customer.entity.OpVip;
import com.kiisoo.ic.customer.mapper.OpCustomerDOMapper;
import com.kiisoo.ic.customer.mapper.OpSellerCustomerRelationDOMapper;
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 com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* @ClassName: CustomerService
* @Description: 客户业务类
* @Auther: Caps
* @Date: 2020/4/2 0002 17:06
* @Version: v1
*/
@Service
public class CustomerService {
private final OpCustomerDOMapper opCustomerDOMapper;
private final OpSellerCustomerRelationDOMapper opSellerCustomerRelationDOMapper;
private final OpVipDOMapper opVipDOMapper;
private final PoiStoreDOMapper poiStoreDOMapper;
private final PoiStoreStaffDOMapper poiStoreStaffDOMapper;
@Autowired
private PoiStoreService poiStoreService;
@Autowired
private PrivilageDomainService privilageDomainService;
@Autowired
public CustomerService(OpCustomerDOMapper opCustomerDOMapper, OpSellerCustomerRelationDOMapper opSellerCustomerRelationDOMapper, OpVipDOMapper opVipDOMapper, PoiStoreDOMapper poiStoreDOMapper, PoiStoreStaffDOMapper poiStoreStaffDOMapper) {
this.opCustomerDOMapper = opCustomerDOMapper;
this.opSellerCustomerRelationDOMapper = opSellerCustomerRelationDOMapper;
this.opVipDOMapper = opVipDOMapper;
this.poiStoreDOMapper = poiStoreDOMapper;
this.poiStoreStaffDOMapper = poiStoreStaffDOMapper;
}
/**
* 绑定客户的关系
* @param customerDTO 传输实体
* @Description: 根据客户联系方式查询客户id 1.判断客户是否存在,不存在就添加,存在就进行下一步 2.判断客户id和导购是否绑定了关系如果绑定了就直接下一步如果没有就绑定 3.判断客户id和vip是否绑定了关系如果绑定了就直接下一步如果没有就绑定
*/
@Transactional(rollbackFor = Exception.class)
public Long customerRelation(CustomerDTO customerDTO){
//客户信息
QueryWrapper<OpCustomer> customerWrapper = new QueryWrapper<>();
customerWrapper.eq("wechat_uni_id",customerDTO.getUniId()).last("limit 1");
OpCustomer opCustomer = opCustomerDOMapper.selectOne(customerWrapper);
//店铺信息
QueryWrapper<PoiStore> poiWrapper = new QueryWrapper<>();
poiWrapper.eq("code",customerDTO.getShopCode()).last("limit 1");
PoiStore poiStore = poiStoreDOMapper.selectOne(poiWrapper);
//导购信息
QueryWrapper<PoiStoreStaff> wrapper = new QueryWrapper<>();
wrapper.eq("staff_code",customerDTO.getStaffCode()).eq("store_code",customerDTO.getShopCode()).last("limit 1");
PoiStoreStaff poiStoreStaff = poiStoreStaffDOMapper.selectOne(wrapper);
if(poiStoreStaff == null || poiStore == null){
return null;
}
//做插入使用
OpCustomer insertCustomer = new OpCustomer();
insertCustomer.setWechatUniId(customerDTO.getUniId());
insertCustomer.setName(customerDTO.getName());
if(null != opCustomer){
//存在--1.处理客户导购关系。
sellerCustomerRelation(opCustomer,customerDTO.getStaffCode(),poiStore.getId(),poiStoreStaff.getUserId());
return opCustomer.getId();
}else {
insertCustomer.setCreateBy(Constants.SYS_OPERATION);
//不存在
opCustomerDOMapper.insert(insertCustomer);
//添加关系
sellerCustomerRelation(insertCustomer,customerDTO.getStaffCode(),poiStore.getId(),poiStoreStaff.getUserId());
return insertCustomer.getId();
}
}
/**
* 录入客户信息/绑定客户vip关系如果是VIP的话
* @param customerModifyDTO 更新实体
*/
@Transactional(rollbackFor = Exception.class)
public void customerVipRelation(CustomerModifyDTO customerModifyDTO){
OpCustomer opCustomer = new OpCustomer(customerModifyDTO.getName(),customerModifyDTO.getPhone());
opCustomer.setUpdateBy(Constants.SYS_OPERATION);
QueryWrapper<OpVip> wrapper = new QueryWrapper<>();
wrapper.eq("phone",customerModifyDTO.getPhone()).last("limit 1");
OpVip opVip = opVipDOMapper.selectOne(wrapper);
if(null != opVip){
//有vip信息就绑定信息
opCustomer.setMemberId(opVip.getId());
}
QueryWrapper<OpCustomer> wrapper1 = new QueryWrapper<>();
wrapper1.eq("wechat_uni_id",customerModifyDTO.getUniId());
opCustomerDOMapper.update(opCustomer,wrapper1);
}
/**
* 处理客户和导购的关系
* @param opCustomer 客户的数据信息
* @param staffCode 导购Code
*/
private void sellerCustomerRelation(OpCustomer opCustomer,String staffCode,long shopId,long sellerId){
QueryWrapper<OpSellerCustomerRelation> wrapper1 = new QueryWrapper<>();
wrapper1.eq("customer_id",opCustomer.getId()).eq("staff_code",staffCode).eq("store_id",shopId).last("limit 1");
OpSellerCustomerRelation opSellerCustomerRelation = opSellerCustomerRelationDOMapper.selectOne(wrapper1);
if(null == opSellerCustomerRelation){
//不存在就添加
OpSellerCustomerRelation insertRelation = new OpSellerCustomerRelation();
insertRelation.setCreateTime(new Date());
insertRelation.setUpdateTime(new Date());
insertRelation.setCustomerId(opCustomer.getId());
insertRelation.setStoreId(shopId);
insertRelation.setUserId(sellerId);
insertRelation.setCreateBy(Constants.SYS_OPERATION);
insertRelation.setUpdateBy(Constants.SYS_OPERATION);
opSellerCustomerRelationDOMapper.insert(insertRelation);
}
}
/**
* 根据店铺id找到顾客信息
* @param shopIds
*/
public List<OpCustomerDTO> getCustomerInfoByShopIds(Set<Long> shopIds, Date startDate, Date endDate, int pageNum, int pageSize){
PageHelper.startPage(pageNum,pageSize);
List<OpCustomerDTO> customers = opCustomerDOMapper.selectCustomerInfoByShopIds(shopIds, startDate, endDate);
AtomicInteger number = new AtomicInteger(1 + ((pageNum-1) * pageSize));
//求分页总数
Page<OpCustomerDTO> pageList = (Page<OpCustomerDTO>)customers;
Long totalSize = pageList.getTotal();
for (OpCustomerDTO m : customers ) {
m.setTotalSize(totalSize);
m.setNumber(number.get());
number.incrementAndGet();
}
return customers;
}
/**
* 根据导购id找到顾客信息
* @param sellerId
*/
public List<OpCustomerDTO> getCustomerInfoByShopIds(long sellerId, Date startDate, Date endDate){
List<OpCustomerDTO> customers = opCustomerDOMapper.selectCustomerInfoBySellerId(sellerId, startDate, endDate);
return customers;
}
/**
* 根据条件查询扫码推广的客户信息
* @param userId 用户id
* @param shopId 店铺id
* @param regionId 区域id
* @param startDate 开始时间
* @param endDate 结束时间
*/
public List<OpCustomerDTO> getCustomerByUserAndDate(long userId,Long shopId, Long regionId , Long sellerId, Date startDate, Date endDate, int pageNum, int pageSize){
Set<Long> shopIds = new HashSet<>();
//根据店铺查询所有扫码客户信息
List<OpCustomerDTO> opCustomerDTOS;
//如果有店铺则查店铺
if(sellerId != null){
opCustomerDTOS = getCustomerInfoByShopIds(sellerId, startDate, endDate);
return opCustomerDTOS;
}else if(shopId != null){
shopIds.add(shopId);
}else if(regionId != null){
//如果有区域则查区域店铺
List<PoiStore> shops = poiStoreService.getRegionShop(regionId);
shopIds = shops.stream().map(p -> p.getId()).collect(Collectors.toSet());
List<Long> userAllShops = privilageDomainService.listUserDatePermission(userId);
shopIds.retainAll(userAllShops);
}else{
//查询用户权限店铺
shopIds = new HashSet<>(privilageDomainService.listUserDatePermission(userId));
}
//根据店铺查询所有扫码客户信息
opCustomerDTOS = getCustomerInfoByShopIds(shopIds, startDate, endDate,pageNum,pageSize);
return opCustomerDTOS;
}
/**
* 修改客户信息
* @param opCustomer
*/
public void editCustomerInfo(OpCustomer opCustomer){
opCustomerDOMapper.updateById(opCustomer);
}
}