diff --git a/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/ReachLogBizService.java b/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/ReachLogBizService.java index 8387986..2e5b199 100644 --- a/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/ReachLogBizService.java +++ b/fw-valhalla-service/src/main/java/cn/fw/valhalla/service/bus/ReachLogBizService.java @@ -1,5 +1,6 @@ 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.erp.UserService; @@ -7,10 +8,15 @@ 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; /** @@ -26,27 +32,51 @@ public class ReachLogBizService { private final CustomerReachLogService customerReachLogService; private final UserService userService; - + @Transactional(rollbackFor = Exception.class) public void saveReachLog(ReachLogReq reachLogReq) { Long customerId = reachLogReq.getCustomerId(); Customer customer = customerService.getById(customerId); if (Objects.isNull(customer)) { return; } - CustomerReachLog reachLog = new CustomerReachLog(); - BeanUtils.copyProperties(reachLogReq, reachLog); - reachLog.setFrameNo(customer.getFrameNo()); - reachLog.setAdviserId(customer.getAdviserId()); - UserInfoDTO user = userService.user(customer.getAdviserId()); - if (Objects.nonNull(user)) { - reachLog.setAdviserId(user.getId()); - reachLog.setAdviserName(user.getUserName()); + 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 = userService.user(customer.getAdviserId()); + if (Objects.nonNull(user)) { + reachLog.setAdviserId(user.getId()); + reachLog.setAdviserName(user.getUserName()); + } + customerReachLogService.save(reachLog); } - customerReachLogService.save(reachLog); int count = Objects.nonNull(customer.getArrivalCount()) ? customer.getArrivalCount() : 0; - customer.setArrivalTime(reachLogReq.getArrivalTime()); + 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.lambdaQuery() + .eq(CustomerReachLog::getFrameNo, vin) + .ge(CustomerReachLog::getArrivalTime, monthStart) + .le(CustomerReachLog::getArrivalTime, monthEnd) + ) > 0; + } }