|
@@ -0,0 +1,174 @@
|
|
|
+package com.xy.job;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.xxl.job.core.biz.model.ReturnT;
|
|
|
+import com.xxl.job.core.handler.annotation.XxlJob;
|
|
|
+import com.xy.collections.list.JArrayList;
|
|
|
+import com.xy.collections.list.JList;
|
|
|
+import com.xy.collections.map.JMap;
|
|
|
+import com.xy.dto.DeviceInfoDto;
|
|
|
+import com.xy.dto.MercAccountDto;
|
|
|
+import com.xy.entity.DeviceCharging;
|
|
|
+import com.xy.entity.DeviceChargingHistory;
|
|
|
+import com.xy.entity.DeviceInfo;
|
|
|
+import com.xy.entity.SysDictRedis;
|
|
|
+import com.xy.mapper.DeviceInfoMapper;
|
|
|
+import com.xy.service.DeviceChargingHistoryServiceImpl;
|
|
|
+import com.xy.service.DeviceChargingServiceImpl;
|
|
|
+import com.xy.service.DeviceInfoServiceImpl;
|
|
|
+import com.xy.service.MercAccountService;
|
|
|
+import com.xy.utils.*;
|
|
|
+import com.xy.utils.enums.DictEnum;
|
|
|
+import com.xy.utils.enums.DictSonEnum;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设备计费job
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@AllArgsConstructor
|
|
|
+public class DeviceChargingJob {
|
|
|
+
|
|
|
+ private DeviceChargingServiceImpl deviceChargingService;
|
|
|
+
|
|
|
+ private DeviceChargingHistoryServiceImpl deviceChargingHistoryService;
|
|
|
+
|
|
|
+ private DeviceInfoServiceImpl deviceInfoService;
|
|
|
+
|
|
|
+ private MercAccountService mercAccountService;
|
|
|
+
|
|
|
+ private DeviceInfoMapper deviceInfoMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 扣除设备服务费试用天数
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @XxlJob("deviceChargingX")
|
|
|
+ public ReturnT<String> deviceChargingX() {
|
|
|
+ deviceInfoMapper.updateChargingX(LocalDateTime.now());
|
|
|
+ log.info("设备试用时间扣除完成~");
|
|
|
+ return ReturnT.SUCCESS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设备计费
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @XxlJob("deviceCharging")
|
|
|
+ public ReturnT<String> deviceCharging() {
|
|
|
+ deviceCharging(1, 20, null);
|
|
|
+ log.info("设备计费完成~");
|
|
|
+ return ReturnT.SUCCESS;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归执行
|
|
|
+ *
|
|
|
+ * @param current
|
|
|
+ * @param size
|
|
|
+ * @param deviceIds
|
|
|
+ */
|
|
|
+ public void deviceCharging(int current, int size, List<Long> deviceIds) {
|
|
|
+ log.info("设备计费开始第{}页", current);
|
|
|
+ List<DeviceCharging> updateDeviceChargings = new ArrayList<>();
|
|
|
+ List<DeviceChargingHistory> deviceChargingHistories = new ArrayList<>();
|
|
|
+ List<DeviceInfoDto.Update> updateDeviceInfos = new ArrayList<>();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ //查询已过期计费记录
|
|
|
+ PageBean<DeviceCharging> pageBean = new PageBean().setCurrent(current).setSize(size);
|
|
|
+ IPage<DeviceCharging> iPage = deviceChargingService.page(PlusBeans.toIPage(pageBean), new LambdaQueryWrapper<DeviceCharging>()
|
|
|
+ .le(DeviceCharging::getTimeout, now)
|
|
|
+ .in(Emptys.check(deviceIds), DeviceCharging::getDeviceId, deviceIds)
|
|
|
+ );
|
|
|
+ List<DeviceCharging> records = iPage.getRecords();
|
|
|
+ if (!Emptys.check(records)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //剔除试用期的设备
|
|
|
+ JList<DeviceCharging> deviceChargings = new JArrayList<>(records);
|
|
|
+ JMap<Long, DeviceCharging> deviceChargingsJMaps = deviceChargings.toMap(DeviceCharging::getDeviceId).cover();
|
|
|
+ List<DeviceInfo> deviceInfos = deviceInfoService.list(new LambdaQueryWrapper<DeviceInfo>()
|
|
|
+ .in(DeviceInfo::getDeviceId, deviceChargings.getProperty(DeviceCharging::getDeviceId))
|
|
|
+ .gt(DeviceInfo::getChargingX, 0)
|
|
|
+ );
|
|
|
+ deviceInfos.forEach(deviceInfo -> deviceChargingsJMaps.remove(deviceInfo.getDeviceId()));
|
|
|
+ JMap<Long, DeviceInfo> deviceInfosJMaps = new JArrayList<>(deviceInfos).toMap(DeviceInfo::getDeviceId).cover();
|
|
|
+ //字典
|
|
|
+ Map<String, SysDictRedis> stringSysDictRedisMap = SysDictUtils.get(DictEnum.DEVICE_CHARGING.getKey());
|
|
|
+ SysDictRedis sysDictRedisY = stringSysDictRedisMap.get(DictSonEnum.DEVICE_CHARGING_Y.getKey());
|
|
|
+ Integer y = Integer.valueOf(sysDictRedisY.getValue());
|
|
|
+ //开始计费
|
|
|
+ deviceChargingsJMaps.forEach((deviceId, deviceCharging) -> {
|
|
|
+ deviceCharging.setUpdateTime(now);
|
|
|
+ //发起扣款
|
|
|
+ DeviceInfo deviceInfo = deviceInfosJMaps.get(deviceId);
|
|
|
+ SysDictRedis sysDictRedisBalance = stringSysDictRedisMap.get(String.valueOf(deviceInfo.getDeviceType()));
|
|
|
+ Integer balance = Integer.valueOf(sysDictRedisBalance.getValue());
|
|
|
+ R r = mercAccountService.updateBalance(new MercAccountDto.UpdateBalance()
|
|
|
+ .setMercId(deviceInfo.getMercId())
|
|
|
+ .setBalance(balance)
|
|
|
+ .setResean("设备管理费")
|
|
|
+ );
|
|
|
+ //扣款成功
|
|
|
+ if (r.getCode() == R.Enum.SUCCESS.getCode()) {
|
|
|
+ LocalDateTime newTimeOut = DataTime.toLocal(DataTime.getStringAround(1, 0, 0, 0, 0, 0, DataTime.toString(deviceCharging.getTimeout())));
|
|
|
+ deviceCharging.setChargingMoney(balance)
|
|
|
+ .setChargingSumMoney(deviceCharging.getChargingSumMoney() + balance)
|
|
|
+ .setChargingType(DictSonEnum.DEVICE_CHARGING_100.getKeyInt())
|
|
|
+ .setStatus(true)
|
|
|
+ .setTimeout(newTimeOut);
|
|
|
+ DeviceChargingHistory deviceChargingHistory = new DeviceChargingHistory()
|
|
|
+ .setDeviceId(deviceId)
|
|
|
+ .setDeviceType(deviceCharging.getDeviceType())
|
|
|
+ .setChargingMoney(balance)
|
|
|
+ .setChargingType(DictSonEnum.DEVICE_CHARGING_100.getKeyInt())
|
|
|
+ .setStatus(true)
|
|
|
+ .setCreateTime(now)
|
|
|
+ .setTimeout(newTimeOut);
|
|
|
+ deviceChargingHistories.add(deviceChargingHistory);
|
|
|
+ }
|
|
|
+ //扣款失败
|
|
|
+ if (r.getCode() == R.Enum.FAIL.getCode()) {
|
|
|
+ deviceCharging.setStatus(false);
|
|
|
+ //当前时间 - 过期时间 > y天 则冻结设备
|
|
|
+ long day = DataTime.diff(now, deviceCharging.getTimeout(), "d");
|
|
|
+ if (day > y) {
|
|
|
+ DeviceInfoDto.Update update = new DeviceInfoDto.Update()
|
|
|
+ .setDeviceId(deviceId);
|
|
|
+ update.setFreezeStatus(2);
|
|
|
+ updateDeviceInfos.add(update);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ updateDeviceChargings.add(deviceCharging);
|
|
|
+ });
|
|
|
+ //修改设备计费记录
|
|
|
+ deviceChargingService.updateBatchById(updateDeviceChargings);
|
|
|
+ //添加设备计费记录历史
|
|
|
+ if (Emptys.check(deviceChargingHistories)) {
|
|
|
+ deviceChargingHistoryService.saveBatch(deviceChargingHistories);
|
|
|
+ }
|
|
|
+ //冻结设备
|
|
|
+ if (Emptys.check(updateDeviceInfos)) {
|
|
|
+ deviceInfoService.updateBatch(updateDeviceInfos);
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断是否继续下一页
|
|
|
+ if (iPage.getPages() <= current) {
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ deviceCharging(current + 1, size, deviceIds);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|