WashDeviceServiceImpl.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.kym.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.github.pagehelper.PageHelper;
  4. import com.kym.common.utils.CommUtil;
  5. import com.kym.entity.WashDevice;
  6. import com.kym.entity.WashOrder;
  7. import com.kym.entity.common.PageBean;
  8. import com.kym.entity.queryParams.DeviceQueryParams;
  9. import com.kym.entity.vo.WashDeviceVo;
  10. import com.kym.mapper.WashDeviceMapper;
  11. import com.kym.service.WashDeviceService;
  12. import com.kym.service.WashOrderService;
  13. import com.kym.service.cache.KymCache;
  14. import com.kym.service.mybatisplus.MyBaseServiceImpl;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.stereotype.Service;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. /**
  20. * <p>
  21. * 洗车设备表 服务实现类
  22. * </p>
  23. *
  24. * @author skyline
  25. * @since 2024-10-09
  26. */
  27. @Service
  28. public class WashDeviceServiceImpl extends MyBaseServiceImpl<WashDeviceMapper, WashDevice> implements WashDeviceService {
  29. private WashOrderService washOrderService;
  30. public WashDeviceServiceImpl(WashOrderService washOrderService) {
  31. this.washOrderService = washOrderService;
  32. }
  33. //region 小程序
  34. /**
  35. * 获取设备列表
  36. *
  37. * @param params
  38. * @return
  39. */
  40. @Override
  41. public List<WashDeviceVo> listWashDevice(DeviceQueryParams params) {
  42. var list = lambdaQuery()
  43. .eq(CommUtil.isNotEmptyAndNull(params.getId()), WashDevice::getId, params.getId())
  44. .eq(CommUtil.isNotEmptyAndNull(params.getStationId()), WashDevice::getStationId, params.getStationId())
  45. .orderByAsc(WashDevice::getDeviceName)
  46. .list();
  47. var voList = new ArrayList<WashDeviceVo>();
  48. //TODO 根据订单查询占用中的设备
  49. // var currentUserId = StpUtil.getLoginIdAsLong();
  50. for (WashDevice washDevice : list) {
  51. var vo = new WashDeviceVo();
  52. // .setCurrentUserId(currentUserId);
  53. BeanUtils.copyProperties(washDevice, vo);
  54. vo.setShortId(KymCache.INSTANCE.getShortIdByProductKeyAndDeviceName(washDevice.getProductKey(), washDevice.getDeviceName()));
  55. vo.setSeqName(KymCache.INSTANCE.getSeqNameByShortId(vo.getShortId()));
  56. voList.add(vo);
  57. }
  58. return voList;
  59. }
  60. /**
  61. * 根据设备短id获取设备信息
  62. *
  63. * @param shortId
  64. * @return
  65. */
  66. @Override
  67. public WashDeviceVo getDevice(String shortId) {
  68. var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
  69. // 校验操作人权限
  70. var order = washOrderService.lambdaQuery()
  71. .eq(WashOrder::getProductKey, productKeyAndDeviceName[0])
  72. .eq(WashOrder::getDeviceName, productKeyAndDeviceName[1])
  73. .eq(WashOrder::getOrderStatus, WashOrder.ORDER_STATUS_开机)
  74. .eq(WashOrder::getPayStatus, WashOrder.PAY_STATUS_未支付)
  75. .one();
  76. CommUtil.asserts(order != null && order.getUserId() != StpUtil.getLoginIdAsLong(), "设备正在使用中!");
  77. var washDevice = lambdaQuery()
  78. .eq(WashDevice::getProductKey, productKeyAndDeviceName[0])
  79. .eq(WashDevice::getDeviceName, productKeyAndDeviceName[1])
  80. .one();
  81. var vo = new WashDeviceVo().setCurrentUserId(StpUtil.getLoginIdAsLong());
  82. BeanUtils.copyProperties(washDevice, vo.setShortId(shortId));
  83. return vo;
  84. }
  85. @Override
  86. public String startDevice(String shortId) {
  87. var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
  88. return washOrderService.createOrder(new DeviceQueryParams(shortId, productKeyAndDeviceName[0], productKeyAndDeviceName[1]));
  89. }
  90. @Override
  91. public void stopDevice(String shortId) {
  92. var productKeyAndDeviceName = KymCache.INSTANCE.getProductKeyAndDeviceNameByWashShortId(shortId);
  93. washOrderService.closeOrder(new DeviceQueryParams(shortId, productKeyAndDeviceName[0], productKeyAndDeviceName[1]));
  94. }
  95. //endregion
  96. //region 管理后台
  97. @Override
  98. public Object detail(long id) {
  99. return getById(id);
  100. }
  101. @Override
  102. public void remove(long id) {
  103. }
  104. @Override
  105. public void modify(WashDevice device) {
  106. CommUtil.assertsNonNulls(List.of(device.getDeviceName(), device.getStationId(), device.getProductKey(), device.getId()), "参数异常");
  107. List<WashDevice> list = lambdaQuery()
  108. .eq(WashDevice::getStationId, device.getStationId())
  109. .eq(WashDevice::getDeviceName, device.getDeviceName())
  110. .eq(WashDevice::getProductKey, device.getProductKey())
  111. .list();
  112. CommUtil.asserts(list.size() <= 1, "设备信息异常");
  113. if (list.size() == 1) {
  114. CommUtil.asserts(list.get(0).getDeviceName().equalsIgnoreCase(device.getDeviceName()), "站点已存在");
  115. }
  116. updateById(device);
  117. }
  118. @Override
  119. public void add(WashDevice device) {
  120. CommUtil.assertsNonNulls(List.of(device.getDeviceName(), device.getStationId(), device.getProductKey()), "参数异常");
  121. Long count = lambdaQuery()
  122. .eq(WashDevice::getStationId, device.getStationId())
  123. .eq(WashDevice::getProductKey, device.getProductKey()).count();
  124. CommUtil.asserts(count == 0, "设备已存在");
  125. save(device);
  126. }
  127. @Override
  128. public PageBean<WashDeviceVo> list(DeviceQueryParams query) {
  129. PageHelper.startPage(query.getPageNum(), query.getPageSize());
  130. List<WashDevice> list = lambdaQuery()
  131. .eq(CommUtil.isNotEmptyAndNull(query.getStationId()), WashDevice::getStationId, query.getStationId())
  132. .list();
  133. var voList = new ArrayList<WashDeviceVo>();
  134. list.forEach(item -> {
  135. var vo = new WashDeviceVo();
  136. BeanUtils.copyProperties(item, vo);
  137. vo.setShortId(KymCache.INSTANCE.getShortIdByProductKeyAndDeviceName(item.getProductKey(), item.getDeviceName()));
  138. vo.setStationName(KymCache.INSTANCE.getStationNameById(item.getStationId()));
  139. voList.add(vo);
  140. });
  141. return new PageBean<>(voList);
  142. }
  143. //endregion
  144. }