DeviceRelationServiceImpl.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.kym.service.impl;
  2. import com.kym.common.exception.BusinessException;
  3. import com.kym.common.utils.CommUtil;
  4. import com.kym.entity.DeviceRelation;
  5. import com.kym.mapper.DeviceRelationMapper;
  6. import com.kym.service.DeviceRelationService;
  7. import com.kym.service.cache.KymCache;
  8. import com.kym.service.mybatisplus.MyBaseServiceImpl;
  9. import jakarta.annotation.PostConstruct;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import java.util.List;
  13. import java.util.Map;
  14. /**
  15. * <p>
  16. * 服务实现类
  17. * </p>
  18. *
  19. * @author skyline
  20. * @since 2024-11-11
  21. */
  22. @Service
  23. public class DeviceRelationServiceImpl extends MyBaseServiceImpl<DeviceRelationMapper, DeviceRelation> implements DeviceRelationService {
  24. @PostConstruct
  25. private void init() {
  26. list().forEach(item -> {
  27. // 初始化短编号和设备信息的关联
  28. KymCache.INSTANCE.putWashShortId2ProductKeyAndDeviceName(Map.of(item.getShortId(), item.getProductKey() + "," + item.getDeviceName()));
  29. // shortId和stationId的对应关系
  30. KymCache.INSTANCE.putShortId2StationId(Map.of(item.getShortId(), item.getStationId()));
  31. });
  32. }
  33. @Override
  34. @Transactional(rollbackFor = Exception.class)
  35. public void add(DeviceRelation deviceRelation) {
  36. CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceRelation.getShortId()), "短编号不能为空");
  37. CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceRelation.getProductKey()), "产品key不能为空");
  38. CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceRelation.getDeviceName()), "设备名称不能为空");
  39. CommUtil.asserts(CommUtil.isNotEmptyAndNull(deviceRelation.getStationId()), "站点id不能为空");
  40. Long count = lambdaQuery()
  41. .eq(DeviceRelation::getShortId, deviceRelation.getShortId())
  42. .count();
  43. CommUtil.asserts(count == 0, "该短编号已存在");
  44. count = lambdaQuery()
  45. .eq(DeviceRelation::getProductKey, deviceRelation.getProductKey())
  46. .eq(DeviceRelation::getDeviceName, deviceRelation.getDeviceName())
  47. .count();
  48. CommUtil.asserts(count == 0, "该设备已绑定短编号");
  49. save(deviceRelation);
  50. // 同步到缓存
  51. KymCache.INSTANCE.putWashShortId2ProductKeyAndDeviceName(Map.of(deviceRelation.getShortId(), deviceRelation.getProductKey() + "," + deviceRelation.getDeviceName()));
  52. KymCache.INSTANCE.putShortId2StationId(Map.of(deviceRelation.getShortId(), deviceRelation.getStationId()));
  53. }
  54. @Override
  55. @Transactional(rollbackFor = Exception.class)
  56. public void removeByDevice(String productKey, String deviceName) {
  57. var relation = lambdaQuery()
  58. .eq(DeviceRelation::getProductKey, productKey)
  59. .eq(DeviceRelation::getDeviceName, deviceName)
  60. .one();
  61. if (relation != null) {
  62. removeById(relation.getId());
  63. }
  64. }
  65. @Override
  66. @Transactional(rollbackFor = Exception.class)
  67. public void removeById(Long id) {
  68. var relation = getById(id);
  69. if (relation == null) {
  70. throw new BusinessException("设备关系不存在");
  71. }
  72. super.removeById(id);
  73. KymCache.INSTANCE.removeWashShortIdMapping(relation.getShortId());
  74. KymCache.INSTANCE.removeShortId2StationId(relation.getShortId());
  75. }
  76. @Override
  77. public List<DeviceRelation> listByStationId(String stationId) {
  78. return lambdaQuery()
  79. .eq(DeviceRelation::getStationId, stationId)
  80. .orderByAsc(DeviceRelation::getShortId)
  81. .list();
  82. }
  83. }