DeviceAlgorithmChargingJob.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.xy.job;
  2. import cn.hutool.core.date.DatePattern;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.xxl.job.core.biz.model.ReturnT;
  6. import com.xxl.job.core.handler.annotation.XxlJob;
  7. import com.xy.collections.list.JArrayList;
  8. import com.xy.collections.list.JList;
  9. import com.xy.collections.map.JMap;
  10. import com.xy.dto.MercAccountDto;
  11. import com.xy.dto.MercArrearageConfigDto;
  12. import com.xy.dto.be.MercDto;
  13. import com.xy.entity.DeviceAlgorithmCharging;
  14. import com.xy.entity.DeviceInfo;
  15. import com.xy.service.DeviceAlgorithmChargingServiceImpl;
  16. import com.xy.service.DeviceInfoServiceImpl;
  17. import com.xy.service.MercAccountService;
  18. import com.xy.service.MercArrearageConfigService;
  19. import com.xy.service.be.MercService;
  20. import com.xy.sys.EnumMercCostMsgConfig;
  21. import com.xy.utils.Emptys;
  22. import com.xy.utils.SysDictUtils;
  23. import lombok.AllArgsConstructor;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.springframework.stereotype.Component;
  26. import java.time.LocalDateTime;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.Map;
  31. /**
  32. * 设备算法费job
  33. */
  34. @Slf4j
  35. @Component
  36. @AllArgsConstructor
  37. public class DeviceAlgorithmChargingJob {
  38. private MercAccountService mercAccountService;
  39. private DeviceInfoServiceImpl deviceInfoService;
  40. private DeviceAlgorithmChargingServiceImpl deviceAlgorithmChargingService;
  41. private MercArrearageConfigService mercArrearageConfigService;
  42. private MercService mercService;
  43. /**
  44. * 算法费设备停机
  45. *
  46. * @return
  47. */
  48. @XxlJob("deviceAlgorithmChargingHalt")
  49. public ReturnT<String> deviceAlgorithmChargingHalt() {
  50. //查询余额<=0商户
  51. List<MercAccountDto.Vo> list = mercAccountService.list(new MercAccountDto.SelectList().setEndBalance(0)).getData();
  52. if (!Emptys.check(list)) {
  53. return ReturnT.SUCCESS;
  54. }
  55. //筛选商户欠费天数
  56. int day = SysDictUtils.getValue(EnumMercCostMsgConfig.Code.CODE.getCode(), EnumMercCostMsgConfig.ARREARAGE_DAY.getCode(), Integer.class);
  57. JList<MercAccountDto.Vo> mercAccounts = new JArrayList<>(list).filter().ge(MercAccountDto.Vo::getArrearageDay, day).list();
  58. if (!Emptys.check(mercAccounts)) {
  59. return ReturnT.SUCCESS;
  60. }
  61. //查询白名单
  62. MercArrearageConfigDto.SelectList selectList = new MercArrearageConfigDto.SelectList();
  63. selectList.setType(2);
  64. List<MercArrearageConfigDto.Vo> data = mercArrearageConfigService.list(selectList).getData();
  65. JMap<Long, MercArrearageConfigDto.Vo> mercArrearageConfigJMaps = new JArrayList<>(data).toMap(MercArrearageConfigDto.Vo::getMercId).cover();
  66. //查询是否提现模式
  67. Map<Long, Boolean> isDrawMaps = mercService.isDrawList(new MercDto.IsDrawList().setMercIds(new JArrayList<>(list).getProperty(MercAccountDto.Vo::getMercId))).getData();
  68. //循环处理
  69. mercAccounts.forEach(vo -> {
  70. //验证白名单
  71. MercArrearageConfigDto.Vo mercArrearageConfig = mercArrearageConfigJMaps.get(vo.getMercId());
  72. if (Emptys.check(mercArrearageConfig)) {
  73. if (vo.getArrearageDay() < mercArrearageConfig.getArrearageDay()) {
  74. return;
  75. }
  76. }
  77. //验证是否提现模式
  78. Boolean isDraw = isDrawMaps.get(vo.getMercId());
  79. if (isDraw) {
  80. return;
  81. }
  82. //查询商户未冻结设备
  83. List<DeviceInfo> deviceInfos = deviceInfoService.list(new LambdaQueryWrapper<DeviceInfo>()
  84. .in(DeviceInfo::getDeviceType, Arrays.asList(1, 2))
  85. .eq(DeviceInfo::getMercId, vo.getMercId())
  86. .eq(DeviceInfo::getActiveState, 1)
  87. .eq(DeviceInfo::getFreezeStatus, 1)
  88. );
  89. if (!Emptys.check(deviceInfos)) {
  90. return;
  91. }
  92. JMap<Long, DeviceInfo> deviceInfosJMaps = new JArrayList<>(deviceInfos).toMap(DeviceInfo::getDeviceId).cover();
  93. //分组统计设备可用算法卡包
  94. String algorithmDate = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN);
  95. LambdaQueryWrapper<DeviceAlgorithmCharging> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceAlgorithmCharging>()
  96. .select(DeviceAlgorithmCharging::getDeviceId)
  97. .eq(DeviceAlgorithmCharging::getMercId, vo.getMercId())
  98. .gt(DeviceAlgorithmCharging::getUnusedSize, 0)
  99. .in(DeviceAlgorithmCharging::getDeviceId, deviceInfosJMaps.getKeys())
  100. .and(deviceAlgorithmChargingLambdaQueryWrapper -> deviceAlgorithmChargingLambdaQueryWrapper
  101. .eq(DeviceAlgorithmCharging::getTimeout, -1)
  102. .or(deviceAlgorithmChargingLambdaQueryWrapper1 -> deviceAlgorithmChargingLambdaQueryWrapper1
  103. .le(DeviceAlgorithmCharging::getBeginTime, Integer.valueOf(algorithmDate))
  104. .ge(DeviceAlgorithmCharging::getTimeout, Integer.valueOf(algorithmDate))
  105. )
  106. )
  107. .groupBy(DeviceAlgorithmCharging::getDeviceId);
  108. List<DeviceAlgorithmCharging> deviceAlgorithmChargings = deviceAlgorithmChargingService.list(lambdaQueryWrapper);
  109. JMap<Long, DeviceAlgorithmCharging> deviceAlgorithmChargingsJMaps = new JArrayList<>(deviceAlgorithmChargings).toMap(DeviceAlgorithmCharging::getDeviceId).cover();
  110. //判断是否过期
  111. List<DeviceInfo> updateDeviceInfos = new ArrayList<>();
  112. LocalDateTime now = LocalDateTime.now();
  113. deviceInfosJMaps.forEach((deviceId, deviceInfo) -> {
  114. if (deviceAlgorithmChargingsJMaps.containsKey(deviceId)) {
  115. return;
  116. }
  117. //冻结设备
  118. DeviceInfo updateDeviceInfo = new DeviceInfo()
  119. .setDeviceId(deviceId)
  120. .setFreezeStatus(2)
  121. .setUpdateTime(now);
  122. updateDeviceInfos.add(updateDeviceInfo);
  123. });
  124. if (Emptys.check(updateDeviceInfos)) {
  125. deviceInfoService.updateBatchById(updateDeviceInfos);
  126. }
  127. });
  128. return ReturnT.SUCCESS;
  129. }
  130. }