Commit e91e80359e8e2b29410c2a4eec49e9fc4ebe083d
1 parent
a78dc8be
保有客多次进站记录一次
Showing
1 changed file
with
41 additions
and
11 deletions
fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/ReachLogBizService.java
1 | 1 | package cn.fw.valhalla.service.bus; |
2 | 2 | |
3 | +import cn.fw.valhalla.common.utils.DateUtil; | |
3 | 4 | import cn.fw.valhalla.domain.db.customer.Customer; |
4 | 5 | import cn.fw.valhalla.domain.db.customer.CustomerReachLog; |
5 | 6 | import cn.fw.valhalla.rpc.erp.UserService; |
... | ... | @@ -7,10 +8,15 @@ import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO; |
7 | 8 | import cn.fw.valhalla.sdk.param.ReachLogReq; |
8 | 9 | import cn.fw.valhalla.service.data.CustomerReachLogService; |
9 | 10 | import cn.fw.valhalla.service.data.CustomerService; |
11 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |
10 | 12 | import lombok.RequiredArgsConstructor; |
11 | 13 | import org.springframework.beans.BeanUtils; |
12 | 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 | 20 | import java.util.Objects; |
15 | 21 | |
16 | 22 | /** |
... | ... | @@ -26,27 +32,51 @@ public class ReachLogBizService { |
26 | 32 | private final CustomerReachLogService customerReachLogService; |
27 | 33 | private final UserService userService; |
28 | 34 | |
29 | - | |
35 | + @Transactional(rollbackFor = Exception.class) | |
30 | 36 | public void saveReachLog(ReachLogReq reachLogReq) { |
31 | 37 | Long customerId = reachLogReq.getCustomerId(); |
32 | 38 | Customer customer = customerService.getById(customerId); |
33 | 39 | if (Objects.isNull(customer)) { |
34 | 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 | 56 | int count = Objects.nonNull(customer.getArrivalCount()) ? customer.getArrivalCount() : 0; |
47 | - customer.setArrivalTime(reachLogReq.getArrivalTime()); | |
57 | + customer.setArrivalTime(arrivalTime); | |
48 | 58 | customer.setArrivalCount(count + 1); |
49 | 59 | customer.setCurrentMileage(reachLogReq.getArrivalMileage()); |
50 | 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 | } | ... | ... |