AliPayAspet.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.xy.alipay;
  2. import cn.hutool.json.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.xy.collections.list.JArrayList;
  5. import com.xy.collections.map.JMap;
  6. import com.xy.dto.CommandMqtt;
  7. import com.xy.dto.DeviceChangeStatusDTO;
  8. import com.xy.dto.MercMiniDeviceDto;
  9. import com.xy.entity.DeviceInfo;
  10. import com.xy.service.AlipayDeviceService;
  11. import com.xy.service.DeviceInfoServiceImpl;
  12. import com.xy.utils.AuthorizeUtils;
  13. import com.xy.utils.R;
  14. import com.xy.utils.enums.DictsSonEnum;
  15. import lombok.AllArgsConstructor;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.aspectj.lang.ProceedingJoinPoint;
  18. import org.aspectj.lang.annotation.Around;
  19. import org.aspectj.lang.annotation.Aspect;
  20. import org.springframework.stereotype.Component;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. @Slf4j
  24. @Aspect
  25. @Component
  26. @AllArgsConstructor
  27. public class AliPayAspet {
  28. private DeviceInfoServiceImpl deviceInfoService;
  29. private AlipayDeviceService alipayDeviceService;
  30. /**
  31. * 支付宝设备
  32. *
  33. * @param joinPoint
  34. * @param aliPay
  35. * @return
  36. * @throws Throwable
  37. */
  38. @Around("@annotation(aliPay)")
  39. public Object timer(ProceedingJoinPoint joinPoint, AliPay aliPay) throws Throwable {
  40. String type = aliPay.value().getType();
  41. Object[] args = joinPoint.getArgs();
  42. Object result = R.ok();
  43. try {
  44. boolean fal = before(type, args);
  45. if (fal) {
  46. result = joinPoint.proceed(args);
  47. }
  48. } catch (Exception e) {
  49. throw e;
  50. }
  51. return result;
  52. }
  53. public boolean before(String type, Object[] args) {
  54. boolean fal = true;
  55. //重启设备
  56. if (type.equals(AliPay.Type.RESTART.getType())) {
  57. List<CommandMqtt> restart = restart(args);
  58. if (restart.size() > 0) {
  59. args[0] = restart;
  60. } else {
  61. fal = false;
  62. }
  63. }
  64. //修改运营状态
  65. if (type.equals(AliPay.Type.BUSY_STATE.getType())) {
  66. fal = busyState(args);
  67. }
  68. return fal;
  69. }
  70. /**
  71. * 重启设备
  72. *
  73. * @param args
  74. */
  75. private List<CommandMqtt> restart(Object[] args) {
  76. List<CommandMqtt> result = new ArrayList<>();
  77. List<CommandMqtt> commandMqtts = (List<CommandMqtt>) args[0];
  78. //查询支付宝设备信息
  79. List<DeviceInfo> deviceInfos = deviceInfoService.list(new LambdaQueryWrapper<DeviceInfo>()
  80. .in(DeviceInfo::getDeviceId, new JArrayList<>(commandMqtts).comparing(CommandMqtt::getDeviceId).getProperty(CommandMqtt::getDeviceId))
  81. .eq(DeviceInfo::getDeviceType, DictsSonEnum.DEVICE_TYPE_5.getKey())
  82. );
  83. if (deviceInfos.size() == 0) {
  84. return commandMqtts;
  85. }
  86. JMap<Long, DeviceInfo> deviceInfosJMaps = new JArrayList<>(deviceInfos).toMap(DeviceInfo::getDeviceId).cover();
  87. commandMqtts.forEach(commandMqtt -> {
  88. JSONObject templet = commandMqtt.getTemplet();
  89. JSONObject data = templet.getJSONObject("data");
  90. DeviceInfo deviceInfo = deviceInfosJMaps.get(commandMqtt.getDeviceId());
  91. if (deviceInfo == null) {
  92. result.add(commandMqtt);
  93. return;
  94. }
  95. //发送支付宝设备重启请求
  96. if (data.getStr("type").equals("app") && data.getStr("task").equals("restart")) {
  97. alipayDeviceService.deviceReboot(deviceInfo.getDeviceId().toString());
  98. }
  99. });
  100. return result;
  101. }
  102. /**
  103. * 修改运营状态
  104. *
  105. * @param args
  106. * @return
  107. */
  108. private boolean busyState(Object[] args) {
  109. MercMiniDeviceDto.BusySate busySate = (MercMiniDeviceDto.BusySate) args[0];
  110. //查询支付宝设备信息
  111. DeviceInfo deviceInfo = deviceInfoService.getOne(new LambdaQueryWrapper<DeviceInfo>()
  112. .eq(DeviceInfo::getDeviceId, busySate.getDeviceId())
  113. .eq(DeviceInfo::getDeviceType, DictsSonEnum.DEVICE_TYPE_5.getKey())
  114. );
  115. if (deviceInfo == null) {
  116. return true;
  117. }
  118. //发送支付宝设备修改运营状态请求
  119. DeviceChangeStatusDTO deviceChangeStatusDTO = new DeviceChangeStatusDTO()
  120. .setTerminalId(String.valueOf(busySate.getDeviceId()))
  121. .setOperatorId(AuthorizeUtils.getLoginId(String.class))
  122. .setStatus(busySate.getBusyState());
  123. alipayDeviceService.changeStatus(deviceChangeStatusDTO);
  124. return false;
  125. }
  126. }