Commit e8f348de by 王欣伟

修改设备号修改逻辑

1 parent 7afee269
...@@ -54,4 +54,13 @@ public final class ConstantsDevice { ...@@ -54,4 +54,13 @@ public final class ConstantsDevice {
*/ */
public static final int NOT_DEL_FLAG = 0; public static final int NOT_DEL_FLAG = 0;
/**
* 设备未激活(0=未激活 1=激活)
*/
public static final int DEVICE_UN_ACTIVED = 0;
/**
* 设备已激活(0=未激活 1=激活)
*/
public static final int DEVICE_ACTIVED = 1;
} }
...@@ -97,4 +97,5 @@ public interface VehicleDeviceMapper extends CommonBaseMapper<VehicleDevice> { ...@@ -97,4 +97,5 @@ public interface VehicleDeviceMapper extends CommonBaseMapper<VehicleDevice> {
@DataPermShare(linkTable = "t_vehicle", showSql = true) @DataPermShare(linkTable = "t_vehicle", showSql = true)
List<StatActivatedVehicleDevice> getOfflineDeviceWithAuth(@Param("offlineTime") long offlineTime, @Param("offlineSnStr") String offlineSnStr); List<StatActivatedVehicleDevice> getOfflineDeviceWithAuth(@Param("offlineTime") long offlineTime, @Param("offlineSnStr") String offlineSnStr);
VehicleDevice getVehicleDeviceByVehicleAndSn(@Param("vehicleId") Long vehicleId, @Param("sn") String sn, @Param("bindStatus") Integer bindStatus);
} }
...@@ -59,7 +59,9 @@ public class VehicleDeviceService { ...@@ -59,7 +59,9 @@ public class VehicleDeviceService {
* @param vehicle * @param vehicle
*/ */
public void saveSnToVehicleId(String sn, Vehicle vehicle) { public void saveSnToVehicleId(String sn, Vehicle vehicle) {
linkSnToVehicleId(sn, vehicle, null, false); DeviceDTO deviceDTO = new DeviceDTO();
deviceDTO.setActivateTime((vehicle!=null && vehicle.getCreatedDate()!=null)?vehicle.getCreatedDate():new Date());
linkSnToVehicleId(sn, vehicle, deviceDTO, false);
} }
/** /**
...@@ -165,4 +167,15 @@ public class VehicleDeviceService { ...@@ -165,4 +167,15 @@ public class VehicleDeviceService {
//匹配设备 //匹配设备
return vehicleDeviceMapper.select(param); return vehicleDeviceMapper.select(param);
} }
/**
* @description: 通过车辆和设备号获取已经解绑的记录
* @author: wangxw1
* @date: 2022/4/14 14:38
* @param: [vehicleId, sn]
* @return: cn.com.glsx.admin.modules.entity.VehicleDevice
**/
public VehicleDevice getUnbindVehicleDevice(Long vehicleId, String sn) {
return vehicleDeviceMapper.getVehicleDeviceByVehicleAndSn(vehicleId, sn, ConstantsDevice.UN_BIND_VEHICLE_STATUS);
}
} }
...@@ -1319,18 +1319,20 @@ public class VehicleService extends AbstractCommonService { ...@@ -1319,18 +1319,20 @@ public class VehicleService extends AbstractCommonService {
// 修改设备时 只允许添加 不允许修改 如果设备已经绑定 则不做绑定操作 如果没有绑定 则做绑定操作 // 修改设备时 只允许添加 不允许修改 如果设备已经绑定 则不做绑定操作 如果没有绑定 则做绑定操作
VehicleDevice vehicleDevice = vehicleDeviceMap.get(deviceInfoDto.getSn()); VehicleDevice vehicleDevice = vehicleDeviceMap.get(deviceInfoDto.getSn());
if (vehicleDevice == null) { if (vehicleDevice == null) {
VehicleDevice unbindVehicleDevice = vehicleDeviceService.getUnbindVehicleDevice(vehicle.getId(), deviceInfoDto.getSn());
// 如果存在已解绑的数据,则直接修改绑定状态
if (unbindVehicleDevice!=null) {
log.info("车辆修改绑定状态:{}", JSONObject.toJSONString(unbindVehicleDevice));
unbindVehicleDevice.setBindStatus(ConstantsDevice.BIND_VEHICLE_STATUS);
vehicleDeviceService.updateByPrimaryKey(unbindVehicleDevice);
} else {
log.info("车辆" + vehicle.getId() + "新绑定的设备号===" + deviceInfoDto.getSn()); log.info("车辆" + vehicle.getId() + "新绑定的设备号===" + deviceInfoDto.getSn());
vehicleDeviceService.saveSnToVehicleId(deviceInfoDto.getSn(), vehicle); vehicleDeviceService.saveSnToVehicleId(deviceInfoDto.getSn(), vehicle);
}
bindSnList.add(deviceInfoDto.getSn()); bindSnList.add(deviceInfoDto.getSn());
// 修改设备为激活状态 // 修改设备为激活状态
Device unActiveDeviceBySn = deviceService.getUnActiveDeviceBySn(deviceInfoDto.getSn()); updateDeviceActiveStatus(deviceInfoDto.getSn(), vehicle.getUpdatedBy(), ConstantsDevice.DEVICE_ACTIVED);
// 激活状态:0未激活,1激活
if (unActiveDeviceBySn != null) {
log.info("设置设备为激活===" + deviceInfoDto.getSn());
unActiveDeviceBySn.setActivateStatus(1);
unActiveDeviceBySn.setActivateTime(new Date());
deviceService.updateDevice(unActiveDeviceBySn);
}
} }
currentDeviceInfoDtoMap.put(deviceInfoDto.getSn(), deviceInfoDto); currentDeviceInfoDtoMap.put(deviceInfoDto.getSn(), deviceInfoDto);
} }
...@@ -1364,6 +1366,32 @@ public class VehicleService extends AbstractCommonService { ...@@ -1364,6 +1366,32 @@ public class VehicleService extends AbstractCommonService {
return saveLog; return saveLog;
} }
/**
* @description: 修改设备激活状态
* @author: wangxw1
* @date: 2022/4/14 14:30
* @param: [sn, updatedBy, deviceActived]
* @return: void
**/
private void updateDeviceActiveStatus(String sn, Long updatedBy, int deviceStatus) {
Device deviceInfo = deviceService.getDeviceBySn(sn);
log.info("设置设备激活状态:{}, updateBy:{}, lastStatus:{}", JSONObject.toJSONString(deviceInfo), updatedBy, deviceStatus);
if (deviceInfo==null) {
return;
}
deviceInfo.setActivateStatus(deviceStatus);
deviceInfo.setUpdatedBy(updatedBy);
deviceInfo.setUpdatedDate(new Date());
// 激活状态:0未激活,1激活
if (deviceStatus==ConstantsDevice.DEVICE_ACTIVED) {
deviceInfo.setActivateTime(new Date());
} else if (deviceStatus==ConstantsDevice.DEVICE_UN_ACTIVED) {
deviceInfo.setActivateTime(null);
}
deviceService.updateDevice(deviceInfo);
}
// 解绑设备 // 解绑设备
private List<String> unbindVehicleDevice(List<VehicleDevice> sourceVehicleDeviceList, Map<String, DeviceInfoDto> deviceInfoDtoMap, Long updatedBy) { private List<String> unbindVehicleDevice(List<VehicleDevice> sourceVehicleDeviceList, Map<String, DeviceInfoDto> deviceInfoDtoMap, Long updatedBy) {
if (CollectionUtil.isEmpty(sourceVehicleDeviceList)) { if (CollectionUtil.isEmpty(sourceVehicleDeviceList)) {
...@@ -1381,6 +1409,9 @@ public class VehicleService extends AbstractCommonService { ...@@ -1381,6 +1409,9 @@ public class VehicleService extends AbstractCommonService {
vehicleDevice.setUpdatedBy(updatedBy); vehicleDevice.setUpdatedBy(updatedBy);
vehicleDeviceService.updateByPrimaryKey(vehicleDevice); vehicleDeviceService.updateByPrimaryKey(vehicleDevice);
deleteSnList.add(vehicleDevice.getSn()); deleteSnList.add(vehicleDevice.getSn());
// 修改设备解绑状态
updateDeviceActiveStatus(deviceInfoDto.getSn(), updatedBy, ConstantsDevice.DEVICE_UN_ACTIVED);
} }
return deleteSnList; return deleteSnList;
} }
...@@ -1661,6 +1692,7 @@ public class VehicleService extends AbstractCommonService { ...@@ -1661,6 +1692,7 @@ public class VehicleService extends AbstractCommonService {
} }
private void pushDeviceEvent(List<String> snList, TenantDataOperatorEnum tenantDataOperatorEnum) { private void pushDeviceEvent(List<String> snList, TenantDataOperatorEnum tenantDataOperatorEnum) {
log.info("update vehicle device publisher event:{}, operator:{}", JSONObject.toJSONString(snList), tenantDataOperatorEnum.getMessage());
applicationEventPublisher.publishEvent(new TenantDeviceSync(ShieldContextHolder.getTenantId(), snList, tenantDataOperatorEnum)); applicationEventPublisher.publishEvent(new TenantDeviceSync(ShieldContextHolder.getTenantId(), snList, tenantDataOperatorEnum));
} }
......
...@@ -123,4 +123,16 @@ ...@@ -123,4 +123,16 @@
and tvd.sn not in (${offlineSnStr}) and tvd.sn not in (${offlineSnStr})
</if> </if>
</select> </select>
<select id="getVehicleDeviceByVehicleAndSn" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_vehicle_device tvd
where tvd.sn=#{sn}
and tvd.vehicle_id=#{vehicleId}
and tvd.del_flag = 0
and tvd.bind_status =#{bindStatus}
order by id desc limit 1
</select>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
package cn.com.glsx.admin.modules.service; package cn.com.glsx.admin.modules.service;
import cn.com.glsx.Application; import cn.com.glsx.Application;
import cn.com.glsx.admin.modules.entity.VehicleDevice;
import cn.com.glsx.auth.model.Department; import cn.com.glsx.auth.model.Department;
import cn.com.glsx.auth.model.Role; import cn.com.glsx.auth.model.Role;
import cn.com.glsx.auth.model.SyntheticUser; import cn.com.glsx.auth.model.SyntheticUser;
import cn.com.glsx.auth.utils.ShieldContextHolder; import cn.com.glsx.auth.utils.ShieldContextHolder;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -28,7 +30,7 @@ import java.util.List; ...@@ -28,7 +30,7 @@ import java.util.List;
public class VehicleDeviceServiceTest { public class VehicleDeviceServiceTest {
@Autowired @Autowired
private VehicleDeviceService VehicleDeviceService; private VehicleDeviceService vehicleDeviceService;
@Before @Before
public void runBeforeTestMethod() { public void runBeforeTestMethod() {
...@@ -49,6 +51,14 @@ public class VehicleDeviceServiceTest { ...@@ -49,6 +51,14 @@ public class VehicleDeviceServiceTest {
@Test @Test
public void testUnBindBySn() { public void testUnBindBySn() {
VehicleDeviceService.unbindBySn("8700922797"); vehicleDeviceService.unbindBySn("8700922797");
}
@Test
public void testGetVehicleDevice() {
Long vehicleId = 9422L;
String sn = "2192801101";
VehicleDevice vehicleDevice = vehicleDeviceService.getUnbindVehicleDevice(vehicleId, sn);
System.out.println("===" + JSONObject.toJSONString(vehicleDevice));
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!