|
@@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.github.yitter.idgen.YitIdHelper;
|
|
|
import com.xy.collections.list.JArrayList;
|
|
|
import com.xy.collections.list.JList;
|
|
|
+import com.xy.collections.map.JHashMap;
|
|
|
+import com.xy.collections.map.JMap;
|
|
|
import com.xy.device.EnumAlgorithmPayConfig;
|
|
|
import com.xy.device.EnumDeviceAlgorithmChargingHistoryStatus;
|
|
|
import com.xy.device.EnumDeviceAlgorithmChargingType;
|
|
@@ -25,6 +27,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -191,4 +194,85 @@ public class DeviceAlgorithmChargingServiceImpl extends ServiceImpl<DeviceAlgori
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @ApiOperation("扣费")
|
|
|
+ public R<Map<Long, Integer>> charging(List<DeviceAlgorithmChargingDto.Charging> chargings) {
|
|
|
+ //封装默认返回值
|
|
|
+ Map<Long, Integer> map = new JHashMap<>(chargings.size());
|
|
|
+ chargings.forEach(charging -> map.put(charging.getMercDeviceAlgorithmChargingId(), charging.getSize()));
|
|
|
+ //查询数据
|
|
|
+ JList<DeviceAlgorithmChargingDto.Charging> list = new JArrayList<>(chargings);
|
|
|
+ LambdaQueryWrapper<DeviceAlgorithmCharging> lambdaQueryWrapper = new LambdaQueryWrapper<DeviceAlgorithmCharging>()
|
|
|
+ .in(DeviceAlgorithmCharging::getDeviceId, list.getProperty(DeviceAlgorithmChargingDto.Charging::getDeviceId))
|
|
|
+ .in(DeviceAlgorithmCharging::getAlgorithmId, list.getProperty(DeviceAlgorithmChargingDto.Charging::getAlgorithmId))
|
|
|
+ .in(DeviceAlgorithmCharging::getTimeout, list.getProperty(DeviceAlgorithmChargingDto.Charging::getAlgorithmDate).set(-1));
|
|
|
+ List<DeviceAlgorithmCharging> deviceAlgorithmChargingsList = list(lambdaQueryWrapper);
|
|
|
+ if (!Emptys.check(deviceAlgorithmChargingsList)) {
|
|
|
+ return R.ok(map);
|
|
|
+ }
|
|
|
+ JList<DeviceAlgorithmCharging> deviceAlgorithmChargings = new JArrayList<>(deviceAlgorithmChargingsList);
|
|
|
+ //查询计费类型字典
|
|
|
+ Map<String, SysDictRedis> deviceAlgorithmChargingTypeMap = SysDictUtils.get(EnumDeviceAlgorithmChargingType.Code.CODE.getCode());
|
|
|
+ SysDictRedis sysDictRedis1 = deviceAlgorithmChargingTypeMap.get(EnumDeviceAlgorithmChargingType.N_1.getCode());
|
|
|
+ SysDictRedis sysDictRedis2 = deviceAlgorithmChargingTypeMap.get(EnumDeviceAlgorithmChargingType.N_2.getCode());
|
|
|
+ SysDictRedis sysDictRedis3 = deviceAlgorithmChargingTypeMap.get(EnumDeviceAlgorithmChargingType.N_3.getCode());
|
|
|
+ //循环扣费
|
|
|
+ chargings.forEach(charging -> {
|
|
|
+ //应扣数量
|
|
|
+ int size = charging.getSize();
|
|
|
+ //查找对应算法
|
|
|
+ JList<DeviceAlgorithmCharging> deviceAlgorithmChargingJlist = deviceAlgorithmChargings.filter()
|
|
|
+ .eq(DeviceAlgorithmCharging::getDeviceId, charging.getDeviceId())
|
|
|
+ .eq(DeviceAlgorithmCharging::getAlgorithmId, charging.getAlgorithmId())
|
|
|
+ .in(DeviceAlgorithmCharging::getTimeout, Arrays.asList(charging.getAlgorithmDate(), -1))
|
|
|
+ .list();
|
|
|
+ if (!Emptys.check(deviceAlgorithmChargingJlist)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JMap<Integer, DeviceAlgorithmCharging> deviceAlgorithmChargingJMap = deviceAlgorithmChargingJlist.toMap(DeviceAlgorithmCharging::getType).cover();
|
|
|
+ //递归扣费
|
|
|
+ int size2 = charging(size, deviceAlgorithmChargingJMap, new JArrayList<SysDictRedis>().set(sysDictRedis3).set(sysDictRedis1).set(sysDictRedis2));
|
|
|
+ map.put(charging.getMercDeviceAlgorithmChargingId(), size2);
|
|
|
+ });
|
|
|
+ return R.ok(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归扣费
|
|
|
+ *
|
|
|
+ * @return 未扣条数
|
|
|
+ */
|
|
|
+ private int charging(int size, JMap<Integer, DeviceAlgorithmCharging> deviceAlgorithmChargingJMap, List<SysDictRedis> sysDictRediss) {
|
|
|
+ if (size > 0 && Emptys.check(sysDictRediss)) {
|
|
|
+ SysDictRedis sysDictRedis = sysDictRediss.remove(0);
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ DeviceAlgorithmCharging deviceAlgorithmCharging = deviceAlgorithmChargingJMap.get(Integer.valueOf(sysDictRedis.getValue()));
|
|
|
+ if (deviceAlgorithmCharging != null) {
|
|
|
+ int unusedSize = deviceAlgorithmCharging.getUnusedSize();
|
|
|
+ if (unusedSize > 0) {
|
|
|
+ int newUnusedSize = unusedSize - size;
|
|
|
+ int makeSize;
|
|
|
+ //扣除后=负数则算法条数不足
|
|
|
+ if (newUnusedSize < 0) {
|
|
|
+ size = (~(newUnusedSize - 1));
|
|
|
+ newUnusedSize = 0;
|
|
|
+ makeSize = unusedSize;
|
|
|
+ } else {
|
|
|
+ makeSize = size;
|
|
|
+ size = 0;
|
|
|
+ }
|
|
|
+ DeviceAlgorithmCharging updateDeviceAlgorithmCharging = new DeviceAlgorithmCharging()
|
|
|
+ .setId(deviceAlgorithmCharging.getId())
|
|
|
+ .setUnusedSize(newUnusedSize)
|
|
|
+ .setMakeSize(deviceAlgorithmCharging.getMakeSize() + makeSize)
|
|
|
+ .setUpdateTime(now);
|
|
|
+ updateById(updateDeviceAlgorithmCharging);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (size > 0 && Emptys.check(sysDictRediss)) {
|
|
|
+ return charging(size, deviceAlgorithmChargingJMap, sysDictRediss);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
}
|