BatteryService.java 2.36 KB
package cn.com.glsx.stdinterface.modules.service;

import cn.com.glsx.stdinterface.dto.DevicePowerDTO;
import cn.com.glsx.stdinterface.modules.entity.DevicePower;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.mapper.DevicePowerMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 电量
 * @author xiangyanlin
 * @date 2022/4/12
 */
@Slf4j
@Service
public class BatteryService {

    @Resource
    private DevicePowerMapper devicePowerMapper;

    @Resource
    private VehicleMapper vehicleMapper;

    @Resource
    private VehicleService vehicleService;

    /**
     * 保存电量信息
     */
    public void saveBatteryInfo(DevicePowerDTO devicePowerDTO) {
        //从车辆信息中获取创建人信息
        Vehicle vehicle = vehicleMapper.getVehicleBySn(devicePowerDTO.getSn());
        if (Objects.isNull(vehicle)) {
            log.warn("该设备没有绑定车辆" + devicePowerDTO.getSn());
            return;
        }
        //从关联表从获取关联id
        Long vnId = vehicleService.getVdId(devicePowerDTO.getSn());
        DevicePower devicePower = convert(devicePowerDTO, vehicle.getCreatedBy(), vehicle.getUpdatedBy(), vnId);


        DevicePower devicePowerOld = devicePowerMapper.selectBySn(devicePowerDTO.getSn());
        if (Objects.isNull(devicePowerOld)) {
            devicePowerMapper.insert(devicePower);
            return;
        }
        devicePowerMapper.updateByPrimaryKeySelective(devicePower);
    }

    public DevicePower convert(DevicePowerDTO devicePowerDTO, Long createBy, Long updateBy, Long vnId) {
        DevicePower devicePower = new DevicePower();
        devicePower.setSn(devicePowerDTO.getSn());
        devicePower.setVdId(vnId);
        devicePower.setAlarmTime(devicePowerDTO.getAlarmTime());
        devicePower.setSoc(devicePowerDTO.getSoc());
        devicePower.setId(devicePowerDTO.getId());
        devicePower.setCreatedBy(createBy);
        devicePower.setUpdatedBy(updateBy);
        devicePower.setCreatedDate(devicePowerDTO.getCreatedDate());
        devicePower.setUpdatedDate(devicePowerDTO.getUpdatedDate());
        return devicePower;

    }
}