|
@@ -5,20 +5,14 @@ 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.device.EnumDeviceCharging;
|
|
|
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.DeviceChargingMapper;
|
|
|
-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 lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -39,12 +33,8 @@ public class DeviceChargingJob {
|
|
|
|
|
|
private DeviceChargingServiceImpl deviceChargingService;
|
|
|
|
|
|
- private DeviceChargingHistoryServiceImpl deviceChargingHistoryService;
|
|
|
-
|
|
|
private DeviceInfoServiceImpl deviceInfoService;
|
|
|
|
|
|
- private MercAccountService mercAccountService;
|
|
|
-
|
|
|
private DeviceChargingMapper deviceChargingMapper;
|
|
|
|
|
|
/**
|
|
@@ -66,7 +56,7 @@ public class DeviceChargingJob {
|
|
|
*/
|
|
|
@XxlJob("deviceCharging")
|
|
|
public ReturnT<String> deviceCharging() {
|
|
|
- deviceCharging(1, 20, null);
|
|
|
+ deviceCharging(1, 50, LocalDateTime.now());
|
|
|
log.info("设备计费完成~");
|
|
|
return ReturnT.SUCCESS;
|
|
|
}
|
|
@@ -76,94 +66,54 @@ public class DeviceChargingJob {
|
|
|
*
|
|
|
* @param current
|
|
|
* @param size
|
|
|
- * @param deviceIds
|
|
|
+ * @param thisDateTime
|
|
|
*/
|
|
|
- public void deviceCharging(int current, int size, List<Long> deviceIds) {
|
|
|
+ public void deviceCharging(int current, int size, LocalDateTime thisDateTime) {
|
|
|
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)
|
|
|
+ IPage<DeviceCharging> iPage = deviceChargingService.page(PlusBeans.toIPage(new PageBean().setCurrent(current).setSize(size)), new LambdaQueryWrapper<DeviceCharging>()
|
|
|
+ .le(DeviceCharging::getTimeout, thisDateTime)
|
|
|
+ .eq(DeviceCharging::getIsJob, true)
|
|
|
+ .le(DeviceCharging::getChargingX, 0)
|
|
|
);
|
|
|
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))
|
|
|
- );
|
|
|
- JMap<Long, DeviceInfo> deviceInfosJMaps = new JArrayList<>(deviceInfos).toMap(DeviceInfo::getDeviceId).cover();
|
|
|
+ JMap<Long, DeviceCharging> deviceChargingsJMaps = new JArrayList<>(records).toMap(DeviceCharging::getDeviceId).cover();
|
|
|
//字典
|
|
|
Map<String, SysDictRedis> stringSysDictRedisMap = SysDictUtils.get(EnumDeviceCharging.Code.CODE.getCode());
|
|
|
SysDictRedis sysDictRedisY = stringSysDictRedisMap.get(EnumDeviceCharging.N_Y.getCode());
|
|
|
Integer y = Integer.valueOf(sysDictRedisY.getValue());
|
|
|
//开始计费
|
|
|
deviceChargingsJMaps.forEach((deviceId, deviceCharging) -> {
|
|
|
- //发起扣款
|
|
|
- DeviceInfo deviceInfo = deviceInfosJMaps.get(deviceId);
|
|
|
- if (deviceInfo.getMercId() == -1) {
|
|
|
- return;
|
|
|
- }
|
|
|
- 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.setChargingSumMoney(deviceCharging.getChargingSumMoney() + balance)
|
|
|
- .setTimeout(newTimeOut);
|
|
|
- DeviceChargingHistory deviceChargingHistory = new DeviceChargingHistory()
|
|
|
- .createId()
|
|
|
- .setDeviceId(deviceId)
|
|
|
- .setChargingMoney(balance)
|
|
|
- .setChargingType(Integer.valueOf(stringSysDictRedisMap.get(EnumDeviceCharging.N_100.getCode()).getValue()))
|
|
|
- .setChargingDateTime(newTimeOut)
|
|
|
- .setCreateTime(now);
|
|
|
- deviceChargingHistories.add(deviceChargingHistory);
|
|
|
+ //当前时间 - 过期时间 > y
|
|
|
+ long day = DataTime.diff(thisDateTime, deviceCharging.getTimeout(), "d");
|
|
|
+ if (day > y) {
|
|
|
+ DeviceInfoDto.Update update = new DeviceInfoDto.Update()
|
|
|
+ .setDeviceId(deviceId);
|
|
|
+ update.setFreezeStatus(2);
|
|
|
+ update.setBusyState(2);
|
|
|
+ deviceCharging.setIsJob(false);
|
|
|
+ updateDeviceChargings.add(deviceCharging);
|
|
|
+ updateDeviceInfos.add(update);
|
|
|
}
|
|
|
- //扣款失败
|
|
|
- if (r.getCode() == R.Enum.FAIL.getCode()) {
|
|
|
- //当前时间 - 过期时间 > y && 试用期 <= 0 则冻结设备
|
|
|
- long day = DataTime.diff(now, deviceCharging.getTimeout(), "d");
|
|
|
- if (day > y && deviceCharging.getChargingX() <= 0) {
|
|
|
- DeviceInfoDto.Update update = new DeviceInfoDto.Update()
|
|
|
- .setDeviceId(deviceId);
|
|
|
- update.setFreezeStatus(2);
|
|
|
- updateDeviceInfos.add(update);
|
|
|
- }
|
|
|
- }
|
|
|
- updateDeviceChargings.add(deviceCharging);
|
|
|
});
|
|
|
//修改设备计费记录
|
|
|
if (Emptys.check(updateDeviceChargings)) {
|
|
|
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);
|
|
|
+ deviceCharging(current + 1, size, thisDateTime);
|
|
|
}
|
|
|
}
|
|
|
|