Commit 80ee6af0 by 向延琳

行程信息保存

1 parent e2fbecda
package cn.com.glsx.stdinterface.modules.controller; package cn.com.glsx.stdinterface.modules.controller;
import cn.com.glsx.stdinterface.api.JourneyClient;
import cn.com.glsx.stdinterface.dto.DeviceJourneyDTO;
import cn.com.glsx.stdinterface.modules.service.JourneyService; import cn.com.glsx.stdinterface.modules.service.JourneyService;
import com.glsx.plat.core.web.R;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -14,14 +17,17 @@ import javax.annotation.Resource; ...@@ -14,14 +17,17 @@ import javax.annotation.Resource;
*/ */
@RestController @RestController
@RequestMapping("/journey") @RequestMapping("/journey")
public class JourneyController { public class JourneyController implements JourneyClient {
/** /**
* 服务对象 * 服务对象
*/ */
@Resource @Resource
private JourneyService journeyService; private JourneyService journeyService;
@Override
public R saveDeviceJourney(DeviceJourneyDTO deviceJourneyDTO) {
journeyService.saveDeviceJourney(deviceJourneyDTO);
return R.ok();
}
} }
...@@ -30,6 +30,6 @@ public class DeviceJourneyOverview extends BaseEntity { ...@@ -30,6 +30,6 @@ public class DeviceJourneyOverview extends BaseEntity {
/** /**
* 总里程km * 总里程km
*/ */
private String distance; private Integer distance;
} }
\ No newline at end of file \ No newline at end of file
...@@ -12,4 +12,11 @@ import org.apache.ibatis.annotations.Mapper; ...@@ -12,4 +12,11 @@ import org.apache.ibatis.annotations.Mapper;
*/ */
@Mapper @Mapper
public interface DeviceJourneyOverviewMapper extends CommonBaseMapper<DeviceJourneyOverview> { public interface DeviceJourneyOverviewMapper extends CommonBaseMapper<DeviceJourneyOverview> {
/**
*
* 如果不存在就新增。 如果存在则累加总行程 总次数
* @param deviceJourneyOverview 传入新的行程值
*/
void insertOrSumDistanceAndCount(DeviceJourneyOverview deviceJourneyOverview);
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.service; 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.DeviceJourneyMapper;
import cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyOverviewMapper; import cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyOverviewMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
/** /**
* 新e盾|里程服务实现类 * 新e盾|里程服务实现类
* *
* @author makejava * @author xiangyanlin
* @since 2022-04-07 16:51:41 * @since 2022-04-07 16:51:41
*/ */
@Slf4j
@Service @Service
public class JourneyService { public class JourneyService {
...@@ -21,4 +31,76 @@ public class JourneyService { ...@@ -21,4 +31,76 @@ public class JourneyService {
@Resource @Resource
private DeviceJourneyOverviewMapper deviceJourneyOverviewMapper; private DeviceJourneyOverviewMapper deviceJourneyOverviewMapper;
@Resource
private VehicleMapper vehicleMapper;
/**
* 行程数据新增
*/
@Transactional(rollbackFor = Exception.class)
public void saveDeviceJourney(DeviceJourneyDTO deviceJourneyDTO) {
if (Objects.isNull(deviceJourneyDTO)) {
return;
}
Vehicle vehicle = getVehicleBySn(deviceJourneyDTO.getSn());
if (Objects.isNull(vehicle)) {
log.warn("该设备没有绑定车辆" + deviceJourneyDTO.getSn());
return;
}
deviceJourneyDTO.setCreatedBy(vehicle.getCreatedBy());
deviceJourneyDTO.setUpdatedBy(vehicle.getUpdatedBy());
DeviceJourney deviceJourney = convertTo(deviceJourneyDTO);
deviceJourneyMapper.insert(deviceJourney);
//更新总行程次数 总里程
DeviceJourneyOverview overview = buildOverviewWitheJourney(deviceJourneyDTO);
deviceJourneyOverviewMapper.insertOrSumDistanceAndCount(overview);
}
public DeviceJourneyOverview buildOverviewWitheJourney(DeviceJourneyDTO deviceJourneyDTO) {
DeviceJourneyOverview deviceJourneyOverview = new DeviceJourneyOverview();
deviceJourneyOverview.setSn(deviceJourneyDTO.getSn());
deviceJourneyOverview.setVdId(deviceJourneyDTO.getVdId());
//每条数据加一
deviceJourneyOverview.setTotalJourneyCount(1);
deviceJourneyOverview.setDistance(deviceJourneyDTO.getMileage());
deviceJourneyOverview.setId(deviceJourneyDTO.getId());
deviceJourneyOverview.setCreatedBy(deviceJourneyDTO.getCreatedBy());
deviceJourneyOverview.setUpdatedBy(deviceJourneyDTO.getUpdatedBy());
deviceJourneyOverview.setCreatedDate(new Date());
deviceJourneyOverview.setUpdatedDate(new Date());
return deviceJourneyOverview;
}
/**
* 转换为数据库对象
*/
public DeviceJourney convertTo(DeviceJourneyDTO deviceJourneyDTO) {
DeviceJourney deviceJourney = new DeviceJourney();
deviceJourney.setSn(deviceJourneyDTO.getSn());
deviceJourney.setVdId(deviceJourneyDTO.getVdId());
deviceJourney.setBeginTime(deviceJourneyDTO.getBeginTime());
deviceJourney.setEndTime(deviceJourneyDTO.getEndTime());
deviceJourney.setBeginAddress(deviceJourneyDTO.getBeginAddress());
deviceJourney.setEndAddress(deviceJourneyDTO.getEndAddress());
deviceJourney.setMileage(deviceJourneyDTO.getMileage());
deviceJourney.setId(deviceJourneyDTO.getId());
deviceJourney.setCreatedBy(deviceJourneyDTO.getCreatedBy());
deviceJourney.setUpdatedBy(deviceJourneyDTO.getUpdatedBy());
deviceJourney.setCreatedDate(new Date());
deviceJourney.setUpdatedDate(new Date());
return deviceJourney;
}
/**
* 通过设备号获取绑定车辆的 createBy
*
* @param sn 设备号
* @return 用户Id
*/
private Vehicle getVehicleBySn(String sn) {
return vehicleMapper.getVehicleBySn(sn);
}
} }
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.alarm.modules.mapper.DeviceJourneyOverviewMapper"> <mapper namespace="cn.com.glsx.stdinterface.modules.mapper.DeviceJourneyOverviewMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.alarm.modules.entity.DeviceJourneyOverview"> <resultMap id="BaseResultMap" type="cn.com.glsx.stdinterface.modules.entity.DeviceJourneyOverview">
<!-- <!--
WARNING - @mbg.generated WARNING - @mbg.generated
--> -->
<id column="id" jdbcType="INTEGER" property="id" /> <id column="id" jdbcType="INTEGER" property="id"/>
<result column="sn" jdbcType="VARCHAR" property="sn" /> <result column="sn" jdbcType="VARCHAR" property="sn"/>
<result column="vd_id" jdbcType="INTEGER" property="vdId" /> <result column="vd_id" jdbcType="INTEGER" property="vdId"/>
<result column="total_journey_count" jdbcType="INTEGER" property="totalJourneyCount" /> <result column="total_journey_count" jdbcType="INTEGER" property="totalJourneyCount"/>
<result column="distance" jdbcType="VARCHAR" property="distance" /> <result column="distance" jdbcType="VARCHAR" property="distance"/>
<result column="del_flag" jdbcType="TINYINT" property="delFlag" /> <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
<result column="created_date" jdbcType="TIMESTAMP" property="createdDate" /> <result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
<result column="created_by" jdbcType="INTEGER" property="createdBy" /> <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
<result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate" /> <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
<result column="updated_by" jdbcType="INTEGER" property="updatedBy" /> <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
</resultMap> </resultMap>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
<!-- <!--
...@@ -23,4 +23,14 @@ ...@@ -23,4 +23,14 @@
id, sn, vd_id, total_journey_count, distance, del_flag, created_date, created_by, id, sn, vd_id, total_journey_count, distance, del_flag, created_date, created_by,
updated_date, updated_by updated_date, updated_by
</sql> </sql>
<insert id="insertOrSumDistanceAndCount" parameterType="cn.com.glsx.stdinterface.modules.entity.DeviceJourneyOverview">
insert into t_device_journey_overview (sn, vd_id, total_journey_count, distance, created_date, created_by, updated_date, updated_by)
values (#{sn}, #{vdId}, #{totalJourneyCount},#{distance}, #{createdDate},#{createdBy}, #{updatedDate}, #{updatedBy})
ON DUPLICATE key
update
total_journey_count =total_journey_count + #{totalJourneyCount},
distance = distance + #{distance}
</insert>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.mapper;
import static org.junit.jupiter.api.Assertions.*;
/**
* sql测试
* @author xiangyanlin
* @date 2022/4/8
*/
class DeviceJourneyOverviewMapperTest {
}
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!