ReachLogBizService.java 3.18 KB
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;
    }
}