JourneyService.java 6.06 KB
package cn.com.glsx.stdinterface.modules.service;

import cn.com.glsx.stdinterface.dto.DeviceJourneyDTO;
import cn.com.glsx.stdinterface.modules.entity.DeviceJourney;
import cn.com.glsx.stdinterface.modules.entity.DeviceJourneyOverview;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyMapper;
import cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyOverviewMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 新e盾|里程服务实现类
 *
 * @author xiangyanlin
 * @since 2022-04-07 16:51:41
 */
@Slf4j
@Service
public class JourneyService {

    @Resource
    private DeviceJourneyMapper deviceJourneyMapper;

    @Resource
    private DeviceJourneyOverviewMapper deviceJourneyOverviewMapper;

    @Resource
    private VehicleMapper vehicleMapper;

    @Resource
    private VehicleService vehicleService;

    /**
     * 行程数据新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveDeviceJourney(DeviceJourneyDTO deviceJourneyDTO) {
        long t1 = System.currentTimeMillis();
        if (Objects.isNull(deviceJourneyDTO)) {
            return;
        }
        DeviceJourney journey = deviceJourneyMapper.selectBySnAndBeginTime(deviceJourneyDTO.getSn(), deviceJourneyDTO.getBeginTime());
        long t2 = System.currentTimeMillis();
        log.debug("重复校验时间{}", t2 - t1);
        if(Objects.nonNull(journey)) {
            log.warn("该设备同样的开始时间行程数据已经存在" +
                    "sn: " + deviceJourneyDTO.getSn() + "beginTime:" + DateUtils.formatNormal(deviceJourneyDTO.getBeginTime()));
            return;
        }

        //从车辆信息中获取创建人信息
        Vehicle vehicle = getVehicleBySn(deviceJourneyDTO.getSn());
        Long createdBy = Optional.ofNullable(vehicle).map(Vehicle::getCreatedBy).orElse(null);
        Long updatedBy =  Optional.ofNullable(vehicle).map(Vehicle::getUpdatedBy).orElse(null);
        long t3 = System.currentTimeMillis();
        log.debug("获取创建人时间{}", t3 - t2);

        //从关联表从获取关联id
        Long vdId = vehicleService.getVdId(deviceJourneyDTO.getSn());
        long t4 = System.currentTimeMillis();
        log.debug("获取创建人时间{}", t4 - t3);

        //行程信息入库
        DeviceJourney deviceJourney = convertTo(deviceJourneyDTO, vdId, createdBy, updatedBy);
        deviceJourneyMapper.insert(deviceJourney);

        //如果行程数据的设备未关联车辆 不计入从次数和总信息
        log.info("vdId{}", vdId);
        if(Objects.isNull(vdId)) {
            log.warn("该设备没有绑定车辆" + deviceJourneyDTO.getSn());
            log.debug("同步里程总耗时{}", t4 - t1);
            return;
        }

        //更新总行程次数 总里程
        DeviceJourneyOverview overviewDO = buildOverviewWitheJourney(deviceJourneyDTO, vdId, createdBy, updatedBy);
        DeviceJourneyOverview overview = deviceJourneyOverviewMapper.selectBySnAndVdId(deviceJourneyDTO.getSn(), vdId);
        long t5 = System.currentTimeMillis();
        log.debug("行程入库时间{}", t5 - t4);
        if (Objects.isNull(overview)) {
            //本设备的第一条里程数据
            deviceJourneyOverviewMapper.insert(overviewDO);
            log.debug("同步里程总耗时{}", t5 - t1);
            return;
        }
        overview.setDistance(overview.getDistance() + overviewDO.getDistance());
        overview.setTotalJourneyCount(overview.getTotalJourneyCount() + 1);
        overview.setUpdatedDate(new Date());
        deviceJourneyOverviewMapper.updateByPrimaryKeySelective(overview);
        long t6 = System.currentTimeMillis();
        log.debug("同步里程和总里程总耗时{}", t6 - t1);
    }



    public DeviceJourneyOverview buildOverviewWitheJourney(DeviceJourneyDTO deviceJourneyDTO, Long vnId, Long createdBy, Long updatedBy) {
        DeviceJourneyOverview deviceJourneyOverview = new DeviceJourneyOverview();
        deviceJourneyOverview.setSn(deviceJourneyDTO.getSn());
        deviceJourneyOverview.setVdId(vnId);
        //每条数据加一
        deviceJourneyOverview.setTotalJourneyCount(1);
        deviceJourneyOverview.setDistance(deviceJourneyDTO.getMileage());
        deviceJourneyOverview.setCreatedBy(deviceJourneyDTO.getCreatedBy());
        deviceJourneyOverview.setUpdatedBy(deviceJourneyDTO.getUpdatedBy());
        deviceJourneyOverview.setCreatedDate(new Date());
        deviceJourneyOverview.setUpdatedDate(new Date());
        deviceJourneyDTO.setCreatedBy(createdBy);
        deviceJourneyDTO.setUpdatedBy(updatedBy);
        return deviceJourneyOverview;

    }

    /**
     * 转换为数据库对象
     */
    public DeviceJourney convertTo(DeviceJourneyDTO deviceJourneyDTO, Long vnId, Long createdBy, Long updatedBy) {
        DeviceJourney deviceJourney = new DeviceJourney();
        deviceJourney.setSn(deviceJourneyDTO.getSn());
        //获取关联id
        deviceJourney.setVdId(vnId);
        deviceJourney.setBeginTime(deviceJourneyDTO.getBeginTime());
        deviceJourney.setEndTime(deviceJourneyDTO.getEndTime());
        deviceJourney.setBeginAddress(deviceJourneyDTO.getBeginAddress());
        deviceJourney.setEndAddress(deviceJourneyDTO.getEndAddress());
        deviceJourney.setMileage(deviceJourneyDTO.getMileage());
        deviceJourney.setCreatedBy(createdBy);
        deviceJourney.setUpdatedBy(updatedBy);
        deviceJourney.setCreatedDate(new Date());
        deviceJourney.setUpdatedDate(new Date());
        return deviceJourney;
    }

    /**
     * 通过设备号获取绑定车辆的 createBy
     *
     * @param sn 设备号
     * @return 用户Id
     */
    private Vehicle getVehicleBySn(String sn) {
        return vehicleMapper.getVehicleBySn(sn);
    }

}