Commit e91e80359e8e2b29410c2a4eec49e9fc4ebe083d

Authored by 张志伟
1 parent a78dc8be

保有客多次进站记录一次

fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/ReachLogBizService.java
1 package cn.fw.valhalla.service.bus; 1 package cn.fw.valhalla.service.bus;
2 2
  3 +import cn.fw.valhalla.common.utils.DateUtil;
3 import cn.fw.valhalla.domain.db.customer.Customer; 4 import cn.fw.valhalla.domain.db.customer.Customer;
4 import cn.fw.valhalla.domain.db.customer.CustomerReachLog; 5 import cn.fw.valhalla.domain.db.customer.CustomerReachLog;
5 import cn.fw.valhalla.rpc.erp.UserService; 6 import cn.fw.valhalla.rpc.erp.UserService;
@@ -7,10 +8,15 @@ import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO; @@ -7,10 +8,15 @@ import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
7 import cn.fw.valhalla.sdk.param.ReachLogReq; 8 import cn.fw.valhalla.sdk.param.ReachLogReq;
8 import cn.fw.valhalla.service.data.CustomerReachLogService; 9 import cn.fw.valhalla.service.data.CustomerReachLogService;
9 import cn.fw.valhalla.service.data.CustomerService; 10 import cn.fw.valhalla.service.data.CustomerService;
  11 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
10 import lombok.RequiredArgsConstructor; 12 import lombok.RequiredArgsConstructor;
11 import org.springframework.beans.BeanUtils; 13 import org.springframework.beans.BeanUtils;
12 import org.springframework.stereotype.Service; 14 import org.springframework.stereotype.Service;
  15 +import org.springframework.transaction.annotation.Transactional;
13 16
  17 +import java.time.LocalDate;
  18 +import java.time.YearMonth;
  19 +import java.util.Date;
14 import java.util.Objects; 20 import java.util.Objects;
15 21
16 /** 22 /**
@@ -26,27 +32,51 @@ public class ReachLogBizService { @@ -26,27 +32,51 @@ public class ReachLogBizService {
26 private final CustomerReachLogService customerReachLogService; 32 private final CustomerReachLogService customerReachLogService;
27 private final UserService userService; 33 private final UserService userService;
28 34
29 - 35 + @Transactional(rollbackFor = Exception.class)
30 public void saveReachLog(ReachLogReq reachLogReq) { 36 public void saveReachLog(ReachLogReq reachLogReq) {
31 Long customerId = reachLogReq.getCustomerId(); 37 Long customerId = reachLogReq.getCustomerId();
32 Customer customer = customerService.getById(customerId); 38 Customer customer = customerService.getById(customerId);
33 if (Objects.isNull(customer)) { 39 if (Objects.isNull(customer)) {
34 return; 40 return;
35 } 41 }
36 - CustomerReachLog reachLog = new CustomerReachLog();  
37 - BeanUtils.copyProperties(reachLogReq, reachLog);  
38 - reachLog.setFrameNo(customer.getFrameNo());  
39 - reachLog.setAdviserId(customer.getAdviserId());  
40 - UserInfoDTO user = userService.user(customer.getAdviserId());  
41 - if (Objects.nonNull(user)) {  
42 - reachLog.setAdviserId(user.getId());  
43 - reachLog.setAdviserName(user.getUserName()); 42 + Date arrivalTime = reachLogReq.getArrivalTime();
  43 + boolean exist = currentMonthExist(customer.getFrameNo(), arrivalTime);
  44 + if (!exist) {
  45 + CustomerReachLog reachLog = new CustomerReachLog();
  46 + BeanUtils.copyProperties(reachLogReq, reachLog);
  47 + reachLog.setFrameNo(customer.getFrameNo());
  48 + reachLog.setAdviserId(customer.getAdviserId());
  49 + UserInfoDTO user = userService.user(customer.getAdviserId());
  50 + if (Objects.nonNull(user)) {
  51 + reachLog.setAdviserId(user.getId());
  52 + reachLog.setAdviserName(user.getUserName());
  53 + }
  54 + customerReachLogService.save(reachLog);
44 } 55 }
45 - customerReachLogService.save(reachLog);  
46 int count = Objects.nonNull(customer.getArrivalCount()) ? customer.getArrivalCount() : 0; 56 int count = Objects.nonNull(customer.getArrivalCount()) ? customer.getArrivalCount() : 0;
47 - customer.setArrivalTime(reachLogReq.getArrivalTime()); 57 + customer.setArrivalTime(arrivalTime);
48 customer.setArrivalCount(count + 1); 58 customer.setArrivalCount(count + 1);
49 customer.setCurrentMileage(reachLogReq.getArrivalMileage()); 59 customer.setCurrentMileage(reachLogReq.getArrivalMileage());
50 customerService.updateById(customer); 60 customerService.updateById(customer);
51 } 61 }
  62 +
  63 + /**
  64 + * 本月是否已经记录进站信息
  65 + *
  66 + * @param vin
  67 + * @param arrivalTime
  68 + * @return
  69 + */
  70 + public boolean currentMonthExist(String vin, Date arrivalTime) {
  71 + LocalDate localDate = DateUtil.date2LocalDate(arrivalTime);
  72 + YearMonth month = YearMonth.from(localDate);
  73 + Date monthStart = DateUtil.localDateTime2Date(month.atDay(1).atStartOfDay());
  74 + Date monthEnd = DateUtil.localDateTime2Date(month.atEndOfMonth().atTime(23, 59, 59));
  75 +
  76 + return customerReachLogService.count(Wrappers.<CustomerReachLog>lambdaQuery()
  77 + .eq(CustomerReachLog::getFrameNo, vin)
  78 + .ge(CustomerReachLog::getArrivalTime, monthStart)
  79 + .le(CustomerReachLog::getArrivalTime, monthEnd)
  80 + ) > 0;
  81 + }
52 } 82 }