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