Commit e8cad08f by 杨林龙

开发假性离线功能

1 parent 6a2708d3
package cn.com.glsx.stdinterface.modules.controller;
import cn.com.glsx.stdinterface.api.FakeOfficeClient;
import cn.com.glsx.stdinterface.dto.FakeOfflineFenceSnDto;
import cn.com.glsx.stdinterface.modules.service.FakeOfficeService;
import com.glsx.plat.common.annotation.SysLog;
import com.glsx.plat.common.enums.OperateType;
import com.glsx.plat.core.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 试驾统计 Controller
*/
@Slf4j
@RestController
@RequestMapping(value = "/fakeoffice")
@Api(value = "假性离线数据", tags = {"假性离线数据"})
public class FakeOfficeController implements FakeOfficeClient {
private final static String MODULE = "假性离线数据";
@Autowired
private FakeOfficeService fakeOfficeService;
@Override
@PostMapping(value = "/storeFakeList")
@SysLog(module = MODULE, value = "保存假性离线数据", action = OperateType.ADD, saveLog = false)
@ApiOperation(value = "保存假性离线数据")
public R storeFakeData(@RequestBody FakeOfflineFenceSnDto fakeOfflineFenceSnDto) {
fakeOfficeService.storeFakeData(fakeOfflineFenceSnDto);
return R.ok();
}
}
package cn.com.glsx.stdinterface.modules.converter;
import cn.com.glsx.stdinterface.dto.FakeOfflineFenceSnDto;
import cn.com.glsx.stdinterface.modules.entity.FakeOfflineFenceSn;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface FakeOfficeConverter {
FakeOfficeConverter INSTANCE = Mappers.getMapper(FakeOfficeConverter.class);
FakeOfflineFenceSn syncDto2Entity(FakeOfflineFenceSnDto item);
}
package cn.com.glsx.stdinterface.modules.entity; import com.glsx.plat.mybatis.base.BaseEntity;import lombok.Data;import lombok.experimental.Accessors; import javax.persistence.Table;import java.sql.Date; /** * 假性离线围栏数据 * * @author yangll */@Data@Accessors(chain = true)@Table(name = "t_fake_offline_fence_sn")public class FakeOfflineFenceSn extends BaseEntity { /** * 设备号 */ private String sn; /** * 是否在家庭围栏或者工作围栏内, 1表是在围栏内, 2表示不在围栏内 */ private Integer status; /** * 围栏类型: 1.家庭所在地围栏 2.工作所在地围栏 */ private Integer merchantType; /** * 进围栏时间 */ private Date entryTime; /** * 出围栏时间 */ private Date outTime; /** * 家庭所在地围栏|工作所在地围栏 */ private String addr;}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.entity; import com.glsx.plat.mybatis.base.BaseEntity;import lombok.Data;import lombok.experimental.Accessors; import javax.persistence.Table; /** * 假性离线车架号数据 * * @author yangll */@Data@Accessors(chain = true)@Table(name = "t_fake_offline_vin")public class FakeOfflineVin extends BaseEntity { /** * 车架号 */ private String vin; /** * 是否是假性离线车架号, 1表是假性离线车架号, 2表示不是假性离线车架号 */ private Integer status; /** * 预测家庭地址 */ private String homeAddr; /** * 预测工作地址 */ private String workAddr;}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.mapper;
import cn.com.glsx.stdinterface.modules.entity.FakeOfflineFenceSn;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* mapper
*
* @author yangll
*/
@Mapper
public interface FakeOfflineFenceSnMapper extends CommonBaseMapper<FakeOfflineFenceSn> {
}
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.mapper;
import cn.com.glsx.stdinterface.modules.entity.FakeOfflineVin;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* mapper
*
* @author yangll
*/
@Mapper
public interface FakeOfflineVinMapper extends CommonBaseMapper<FakeOfflineVin> {
}
\ No newline at end of file \ No newline at end of file
...@@ -24,5 +24,4 @@ public interface VehicleDeviceMapper extends CommonBaseMapper<VehicleDevice> { ...@@ -24,5 +24,4 @@ public interface VehicleDeviceMapper extends CommonBaseMapper<VehicleDevice> {
List<VehicleDevice> selectBindingDevices(@Param("sns") Collection<String> sns); List<VehicleDevice> selectBindingDevices(@Param("sns") Collection<String> sns);
List<VehicleDevice> selectBindingDevicesByVehicleId(@Param("vehicleId") Long vehicleId); List<VehicleDevice> selectBindingDevicesByVehicleId(@Param("vehicleId") Long vehicleId);
} }
...@@ -24,4 +24,5 @@ public interface VehicleMapper extends CommonBaseMapper<Vehicle> { ...@@ -24,4 +24,5 @@ public interface VehicleMapper extends CommonBaseMapper<Vehicle> {
int updateCreateByByRefVehicleIds(@Param("userId") Long userId, @Param("refVehicleIds") Collection<Long> refVehicleIds); int updateCreateByByRefVehicleIds(@Param("userId") Long userId, @Param("refVehicleIds") Collection<Long> refVehicleIds);
Vehicle getVehicleBySn(@Param("sn") String sn);
} }
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.remote;
import cn.com.glsx.devicemonitor.api.dto.response.DeviceSearchDTO;
import cn.com.glsx.devicemonitor.api.dto.response.DeviceStatusModel;
import cn.com.glsx.devicemonitor.api.feign.DeviceFeignService;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author Yanglinlong
* @date 2022/3/11 18:32
*/
@Slf4j
@Service
public class RemoteFeignDeviceService {
@Autowired
private DeviceFeignService deviceFeignService;
public Map<String, DeviceStatusModel> listDeviceStatusBySns(Collection<String> sns) {
log.info("调用监控服务获取设备在线和离线的状态===" + JSONUtil.toJsonStr(sns));
DeviceSearchDTO deviceSearchDTO = new DeviceSearchDTO();
deviceSearchDTO.setSns(sns);
R<List<DeviceStatusModel>> listDeviceStatusBySnsR = deviceFeignService.listDeviceStatusBySns(deviceSearchDTO);
List<DeviceStatusModel> listDeviceStatusBySnsDataList = listDeviceStatusBySnsR.getDataList(new TypeToken<List<DeviceStatusModel>>() {
}.getType());
Map<String, DeviceStatusModel> deviceStatusBsDataMap = Maps.newHashMap();
if (CollectionUtils.isNotEmpty(listDeviceStatusBySnsDataList)) {
deviceStatusBsDataMap = listDeviceStatusBySnsDataList.stream().collect(Collectors.toMap(DeviceStatusModel::getSn, Function.identity(), (k1, k2) -> k2));
}
return deviceStatusBsDataMap;
}
}
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.devicemonitor.api.dto.response.DeviceStatusModel;
import cn.com.glsx.stdinterface.common.constants.Constants;
import cn.com.glsx.stdinterface.dto.FakeOfflineFenceSnDto;
import cn.com.glsx.stdinterface.enums.FakeOfficeStatusEnum;
import cn.com.glsx.stdinterface.enums.FakeOfficeTypeEnum;
import cn.com.glsx.stdinterface.modules.converter.FakeOfficeConverter;
import cn.com.glsx.stdinterface.modules.entity.FakeOfflineFenceSn;
import cn.com.glsx.stdinterface.modules.entity.FakeOfflineVin;
import cn.com.glsx.stdinterface.modules.entity.Vehicle;
import cn.com.glsx.stdinterface.modules.entity.VehicleDevice;
import cn.com.glsx.stdinterface.modules.mapper.FakeOfflineFenceSnMapper;
import cn.com.glsx.stdinterface.modules.mapper.FakeOfflineVinMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleDeviceMapper;
import cn.com.glsx.stdinterface.modules.mapper.VehicleMapper;
import cn.com.glsx.stdinterface.modules.remote.RemoteFeignDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 假性离线 Service
*
* @author yangll
*/
@Slf4j
@Service
public class FakeOfficeService {
@Autowired
private FakeOfflineFenceSnMapper fakeOfflineFenceSnMapper;
@Autowired
private FakeOfflineVinMapper fakeOfflineVinMapper;
@Autowired
private VehicleMapper vehicleMapper;
@Autowired
private VehicleDeviceMapper vehicleDeviceMapper;
@Autowired
private RemoteFeignDeviceService remoteFeignDeviceService;
/**
* 保存假性离线数据
*
* @param fakeOfflineFenceSnDto dto
*/
public void storeFakeData(FakeOfflineFenceSnDto fakeOfflineFenceSnDto) {
// 消费设备假性离线数据入库
FakeOfflineFenceSn fakeOfflineFenceSn = FakeOfficeConverter.INSTANCE.syncDto2Entity(fakeOfflineFenceSnDto);
// 设置createBy 字段
Vehicle vehicle = getUserIdBySn(fakeOfflineFenceSn.getSn());
if (vehicle == null) {
log.warn("该设备没有绑定车辆" + fakeOfflineFenceSn.getSn());
return;
}
fakeOfflineFenceSn.setCreatedBy(vehicle.getCreatedBy());
// 设置入库时间
fakeOfflineFenceSn.setCreatedDate(new Date());
fakeOfflineFenceSnMapper.insert(fakeOfflineFenceSn);
// 同步计算假性离线车辆并保存到数据库
// 计算该车辆板顶的其他设备的在线状态 如果设备都离线 并且有一个设备在家庭围栏或者工作围栏内就是假性离线
List<VehicleDevice> vehicleDevices = vehicleDeviceMapper.selectBindingDevicesByVehicleId(vehicle.getId());
List<String> sns = vehicleDevices.stream().map(VehicleDevice::getSn).collect(Collectors.toList());
Map<String, DeviceStatusModel> deviceStatusMap = remoteFeignDeviceService.listDeviceStatusBySns(sns);
// 初始化条件
boolean flag = false;
for (String sn : sns) {
DeviceStatusModel deviceStatusModel = deviceStatusMap.get(sn);
if (deviceStatusModel != null) {
Integer status = deviceStatusModel.getStatus();
if (status != null && Constants.DeviceOnline.OFFLINE != status) {
// 有设备离线 有一个设备在线就跳出循环
flag = true;
break;
}
}
}
// 有设备在线就说明不是假性离线 没有设备在线 并且在家庭围栏或者工作围栏内你就是假性离线
if (!flag && FakeOfficeStatusEnum.FAKE_OFFICE.getStatus().equals(fakeOfflineFenceSn.getStatus())) {
FakeOfflineVin fakeOfflineVin = new FakeOfflineVin();
fakeOfflineVin.setVin(vehicle.getVin());
fakeOfflineVin.setStatus(fakeOfflineFenceSn.getStatus());
if (FakeOfficeTypeEnum.HOME_FENCE.getType().equals(fakeOfflineFenceSn.getMerchantType())) {
fakeOfflineVin.setHomeAddr(fakeOfflineFenceSn.getAddr());
} else if (FakeOfficeTypeEnum.WORK_FENCE.getType().equals(fakeOfflineFenceSn.getMerchantType())) {
fakeOfflineVin.setWorkAddr(fakeOfflineFenceSn.getAddr());
}
fakeOfflineVin.setCreatedBy(vehicle.getCreatedBy());
fakeOfflineVin.setCreatedDate(new Date());
fakeOfflineVinMapper.insert(fakeOfflineVin);
}
}
/**
* 通过设备号获取绑定车辆的 createBy
*
* @param sn 设备号
* @return 用户Id
*/
private Vehicle getUserIdBySn(String sn) {
return vehicleMapper.getVehicleBySn(sn);
}
}
<?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">
<mapper namespace="cn.com.glsx.stdinterface.modules.mapper.FakeOfflineFenceSnMapper">
<resultMap id="BaseMap" type="cn.com.glsx.stdinterface.modules.entity.FakeOfflineFenceSn">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="sn" property="sn" jdbcType="VARCHAR"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="merchant_type" property="merchantType" jdbcType="TINYINT"/>
<result column="entry_time" property="entryTime" jdbcType="TIMESTAMP"/>
<result column="out_time" property="outTime" jdbcType="TIMESTAMP"/>
<result column="del_flag" property="delFlag" jdbcType="TINYINT"/>
<result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
<result column="created_by" property="createdBy" jdbcType="INTEGER"/>
<result column="updated_date" property="updatedDate" jdbcType="TIMESTAMP"/>
<result column="updated_by" property="updatedBy" jdbcType="INTEGER"/>
</resultMap>
<sql id="BaseColumns">
a.id, a.sn, a.status, a.merchant_type, a.entry_time, a.out_time, a.del_flag, a.created_date,
a.created_by, a.updated_date, a.updated_by
</sql>
</mapper>
\ No newline at end of file \ No newline at end of file
<?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">
<mapper namespace="cn.com.glsx.stdinterface.modules.mapper.FakeOfflineVinMapper">
<resultMap id="BaseMap" type="cn.com.glsx.stdinterface.modules.entity.FakeOfflineVin">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="vin" property="vin" jdbcType="VARCHAR"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="del_flag" property="delFlag" jdbcType="TINYINT"/>
<result column="created_date" property="createdDate" jdbcType="TIMESTAMP"/>
<result column="created_by" property="createdBy" jdbcType="INTEGER"/>
<result column="updated_date" property="updatedDate" jdbcType="TIMESTAMP"/>
<result column="updated_by" property="updatedBy" jdbcType="INTEGER"/>
</resultMap>
<sql id="BaseColumns">
a.id, a.vin, a.status, a.del_flag, a.created_date, a.created_by, a.updated_date,
a.updated_by
</sql>
</mapper>
\ No newline at end of file \ No newline at end of file
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
<!-- <!--
WARNING - @mbg.generated WARNING - @mbg.generated
--> -->
id, vehicle_id, sn, bind_status, trial_type, his_type, wire_type, del_flag, created_date, created_by, updated_date, id, vehicle_id, sn, bind_status, trial_type, his_type, wire_type, del_flag, created_date, created_by,
updated_date,
updated_by updated_by
</sql> </sql>
......
...@@ -117,4 +117,16 @@ ...@@ -117,4 +117,16 @@
#{item} #{item}
</foreach> </foreach>
</update> </update>
<select id="getVehicleBySn" resultMap="BaseResultMap">
select
tv.id,
tv.vin,
tv.created_by
from t_vehicle tv
left join t_vehicle_device tvd on tv.id = tvd.vehicle_id
where tvd.sn = #{sn}
and tvd.del_flag = 0 and tv.del_flag = 0
and tvd.bind_status = 1;
</select>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.stdinterface.modules.service;
import cn.com.glsx.Application;
import cn.com.glsx.stdinterface.dto.FakeOfflineFenceSnDto;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class FakeOfficeServiceTest {
@Autowired
private FakeOfficeService fakeOfficeService;
@Test
public void storeFakeData() {
System.out.println("test storeFakeData start ");
FakeOfflineFenceSnDto fakeOfflineFenceSnDto = new FakeOfflineFenceSnDto();
fakeOfflineFenceSnDto.setSn("4711014037");
fakeOfflineFenceSnDto.setStatus(1);
fakeOfflineFenceSnDto.setMerchantType(1);
fakeOfflineFenceSnDto.setEntryTime(new Date());
fakeOfflineFenceSnDto.setOutTime(new Date());
fakeOfflineFenceSnDto.setAddr("深圳市宝安区德兴公寓");
fakeOfficeService.storeFakeData(fakeOfflineFenceSnDto);
System.out.println("test storeFakeData end ");
}
}
\ 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!