业务优化
parent
b996153c77
commit
2b3305bea6
|
@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
|
|||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @Description : 车辆基类
|
||||
* @Description : 车辆信息管理对象
|
||||
* @Author : YHY
|
||||
* @Date: 2023-11-19 20:56
|
||||
*/
|
||||
|
@ -43,7 +43,7 @@ public class Car extends BaseEntity {
|
|||
|
||||
@ApiModelProperty("车辆状态")
|
||||
@TableField("car_status")
|
||||
private String car_status;
|
||||
private String carStatus;
|
||||
|
||||
@ApiModelProperty("车辆类型")
|
||||
@TableField("type_id")
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.shiyi.internet.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Yanghaoyu
|
||||
* @Date 2023/11/27 09:41
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class VehicleStatusEntity {
|
||||
/**
|
||||
* vin
|
||||
*/
|
||||
private String carVin;
|
||||
/**
|
||||
* 车辆状态
|
||||
*/
|
||||
private Integer status;
|
||||
}
|
|
@ -50,6 +50,19 @@
|
|||
<version>3.15</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- RabbitMq消息队列依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- kafka-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package com.shiyi.internet.config;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.TypeReference;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.shiyi.internet.constants.VehicleConstant;
|
||||
import com.shiyi.internet.domain.DrivingRecord;
|
||||
import com.shiyi.internet.domain.VehicleStatusEntity;
|
||||
import com.shiyi.internet.mapper.DrivingRecordMapper;
|
||||
import com.shiyi.internet.mapper.VehicleMapper;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* @Description :
|
||||
* @Author : YangHaoYu
|
||||
* @Date: 2023-11-25 18:49
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class RabbitConfig {
|
||||
@Autowired
|
||||
private VehicleMapper vehicleMapper;
|
||||
|
||||
@Autowired
|
||||
private DrivingRecordMapper drivingRecordMapper;
|
||||
|
||||
@Bean
|
||||
public Queue initVehicleUpdateStatusQueue(){
|
||||
return new Queue(VehicleConstant.VEHICLE_STATUS_UPDATE_QUEUE,true);
|
||||
}
|
||||
|
||||
@RabbitListener(queues = {VehicleConstant.VEHICLE_STATUS_UPDATE_QUEUE})
|
||||
public void vehicleUpdateStatusHandler(String info, Message message, Channel channel){
|
||||
log.info("队列:{},接收到的消息:{}",VehicleConstant.VEHICLE_STATUS_UPDATE_QUEUE,true);
|
||||
VehicleStatusEntity statusEntity = JSON.parseObject(info, new TypeReference<VehicleStatusEntity>(){
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void updateVehicleStatus(String carVin, Integer status, long timestamp){
|
||||
vehicleMapper.updateVehicleStatus(carVin,status);
|
||||
//判断车辆状态
|
||||
if (status.equals(VehicleConstant.VEHICLE_START)){
|
||||
DrivingRecord drivingRecord =drivingRecordMapper.selectLastByVin(carVin);
|
||||
//车辆此前从未有过记录
|
||||
if (drivingRecord == null){
|
||||
DrivingRecord record = new DrivingRecord();
|
||||
record.setCarVin(carVin);
|
||||
record.setStartTime(new Date()); //开始时间
|
||||
record.setStartKey(String.valueOf(timestamp)); //开始标识
|
||||
log.info("车辆启动:{},时间:{}",carVin,timestamp);
|
||||
drivingRecordMapper.insertDriving(record);
|
||||
}
|
||||
//车辆此前记录不为空并且启动和结束时间也不为空
|
||||
if (drivingRecord != null && drivingRecord.getStartTime() != null && drivingRecord.getEndTime() != null){
|
||||
DrivingRecord record = new DrivingRecord();
|
||||
record.setCarVin(carVin);
|
||||
record.setStartTime(new Date(timestamp));
|
||||
record.setStartKey(String.valueOf(timestamp));
|
||||
log.info("车辆启动:{},time:{}",carVin,timestamp);
|
||||
drivingRecordMapper.insertDrivingRecord(record);
|
||||
}
|
||||
}else {
|
||||
//车辆停止
|
||||
Integer id = drivingRecordMapper.selectLastId(carVin);
|
||||
String endKey =String.valueOf(timestamp);
|
||||
log.info("车辆停止:{},时间:{}",carVin,timestamp);
|
||||
drivingRecordMapper.endVehicle(id, new Date(timestamp),endKey);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.shiyi.internet.config;
|
||||
|
||||
import com.shiyi.internet.constants.VehicleConstant;
|
||||
import com.shiyi.internet.domain.Car;
|
||||
import com.shiyi.internet.mapper.VehicleMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Description :
|
||||
* @Author : YangHaoYu
|
||||
* @Date: 2023-11-25 19:59
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RunnerAfter implements ApplicationRunner {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private VehicleMapper vehicleMapper;
|
||||
|
||||
|
||||
/**
|
||||
* @param args
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("启动:初始化车辆数据到Redis");
|
||||
List<Car> carList = vehicleMapper.selectVehicleInfoList(null);
|
||||
Map<String, Car> collect = carList.stream().collect(Collectors.toMap(Car::getCarVin, car -> car));
|
||||
redisTemplate.opsForHash().putAll(VehicleConstant.VEHICLE_INFO_MAP, collect);
|
||||
}
|
||||
}
|
|
@ -43,4 +43,8 @@ public interface DrivingRecordMapper extends BaseMapper<DrivingRecord> {
|
|||
|
||||
void endVehicle(@Param("id") Integer id, @Param("date") Date date, @Param("endKey") String endKey);
|
||||
|
||||
DrivingRecord selectLastByVin(@Param("carVin") String carVin);
|
||||
|
||||
void insertDrivingRecord(@Param("record") DrivingRecord record);
|
||||
|
||||
}
|
||||
|
|
|
@ -39,4 +39,6 @@ public interface VehicleMapper extends BaseMapper<Car> {
|
|||
Integer getOnlineCount();
|
||||
|
||||
Integer getUnOnlineCount();
|
||||
|
||||
List<Car> selectVehicleInfoList(Car car);
|
||||
}
|
||||
|
|
|
@ -14,17 +14,24 @@
|
|||
|
||||
<insert id="insertDriving">
|
||||
insert into car_record (
|
||||
record_id,
|
||||
car_vin,
|
||||
start_time,
|
||||
end_time,
|
||||
start_key,
|
||||
end_key) values(
|
||||
#{recordId},
|
||||
#{carVin},
|
||||
#{startTime},
|
||||
#{endTime},
|
||||
#{startKey},
|
||||
#{endKey})
|
||||
</insert>
|
||||
<insert id="insertDrivingRecord">
|
||||
insert into car_record (
|
||||
record_id,car_vin,start_time,end_time, start_key,end_key) values(
|
||||
#{recordId},#{carVin},#{startTime},#{endTime},#{startKey},#{endKey})
|
||||
</insert>
|
||||
<update id="updateDrivingRecord">
|
||||
update car_record set
|
||||
car_vin=#{carVin},
|
||||
|
@ -34,7 +41,7 @@
|
|||
end_key=#{endKey} where record_id=#{recordId}
|
||||
</update>
|
||||
<update id="endVehicle">
|
||||
update driving_record set
|
||||
update car_record set
|
||||
end_time=#{date},end_key=#{endKey}
|
||||
where record_id=#{id}
|
||||
</update>
|
||||
|
@ -50,6 +57,9 @@
|
|||
select * from car_record where record_id=#{recordId}
|
||||
</select>
|
||||
<select id="selectLastId" resultType="java.lang.Integer">
|
||||
select max(recordId) from driving_record where car_vin = #{carVin}
|
||||
select max(recordId) from car_record where car_vin = #{carVin}
|
||||
</select>
|
||||
<select id="selectLastByVin" resultType="com.shiyi.internet.domain.DrivingRecord">
|
||||
select * from car_record dr left join (select max(recordId) id from car_record where vin = #{carVin}) l on dr.recordId = l.recordId
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
|
@ -2,18 +2,22 @@
|
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.shiyi.internet.mapper.VehicleMapper">
|
||||
<insert id="addVehicl">
|
||||
insert into (
|
||||
insert into t_car (
|
||||
car_id,
|
||||
car_name,
|
||||
car_license,
|
||||
typ_eId,
|
||||
fen_ceId,
|
||||
dri_veId,
|
||||
bat_teryId,
|
||||
car_vin,
|
||||
car_status,
|
||||
type_id,
|
||||
fence_id,
|
||||
drive_id,
|
||||
battery_id,
|
||||
car_address) values(
|
||||
#{carId},
|
||||
#{carName},
|
||||
#{carLicense},
|
||||
#{carVin},
|
||||
#{carStatus},
|
||||
#{typeId},
|
||||
#{fenceId},
|
||||
#{driveId},
|
||||
|
@ -21,12 +25,14 @@
|
|||
#{carAddress})
|
||||
</insert>
|
||||
<insert id="insertRecord">
|
||||
insert into driving_record values (#{recordId},#{carVin},#{startTime},#{endTime},#{startKey},#{endKey})
|
||||
insert into car_record values (#{recordId},#{carVin},#{startTime},#{endTime},#{startKey},#{endKey})
|
||||
</insert>
|
||||
<update id="updateVehicl">
|
||||
update t_car set
|
||||
car_name=#{carName},
|
||||
car_license=#{carLicense},
|
||||
car_vin=#{carVin},
|
||||
car_status=#{carStatus},
|
||||
type_id=#{typeId},
|
||||
fence_id=#{fenceId},
|
||||
drive_id=#{driveId},
|
||||
|
@ -53,7 +59,7 @@
|
|||
select * from t_car where vin=#{vin}
|
||||
</select>
|
||||
<select id="selectLastByVin" resultType="com.shiyi.internet.domain.DrivingRecord">
|
||||
select * from driving_record dr left join (select max(recordId) id from friving_record where car_vin=#{carVin}) l on dr.record_id=l.record_id
|
||||
select * from car_record dr left join (select max(recordId) id from friving_record where car_vin=#{carVin}) l on dr.record_id=l.record_id
|
||||
</select>
|
||||
<select id="getOnlineVehicleIds" resultType="java.lang.String">
|
||||
select car_vin from t_car where car_status=1
|
||||
|
@ -64,4 +70,19 @@
|
|||
<select id="getUnOnlineCount" resultType="java.lang.Integer">
|
||||
select count(*) from t_car
|
||||
</select>
|
||||
<select id="selectVehicleInfoList" resultType="com.shiyi.internet.domain.Car">
|
||||
select * from t_car
|
||||
<where>
|
||||
<if test="carId != null and carId != ''"> and car_id = #{carId}</if>
|
||||
<if test="carName != null "> and car_name = #{carName}</if>
|
||||
<if test="carLicense != null and carLicense != ''"> and fence_id = #{carLicense}</if>
|
||||
<if test="carVin != null "> and car_vin = #{carVin}</if>
|
||||
<if test="carStatus != null and carStatus != ''"> and car_status = #{carStatus}</if>
|
||||
<if test="typeId != null and typeId != ''"> and type_id = #{typeId}</if>
|
||||
<if test="fenceId != null and fenceId != ''"> and fence_-d = #{fenceId}</if>
|
||||
<if test="driveId != null and driveId != ''"> and drive_id = #{driveId}</if>
|
||||
<if test="batteryId != null and batteryId != ''"> and battery_id = #{batteryId}</if>
|
||||
<if test="carAddress != null and carAddress != ''"> and car_address = #{carAddress}</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue