Răsfoiți Sursa

Merge remote-tracking branch 'origin/test' into test

谭斌 2 ani în urmă
părinte
comite
3aa7ea4028

+ 10 - 0
device-api-cloud/src/main/java/com/xy/feign/DeviceChargingFeign.java

@@ -0,0 +1,10 @@
+package com.xy.feign;
+
+import com.xy.FeignInterceptor;
+import com.xy.consts.ServiceConsts;
+import com.xy.service.DeviceChargingService;
+import org.springframework.cloud.openfeign.FeignClient;
+
+@FeignClient(value = ServiceConsts.SERVICE_NAME, configuration = FeignInterceptor.class)
+public interface DeviceChargingFeign extends DeviceChargingService {
+}

+ 6 - 1
device-api-service/src/main/java/com/xy/entity/DeviceCharging.java

@@ -33,13 +33,18 @@ public class DeviceCharging implements Serializable {
     @ApiModelProperty(value = "累计续费金额")
     private Integer chargingSumMoney;
 
-    @ApiModelProperty(value = "试用剩余天")
+    @ApiModelProperty(value = "管理费剩余天数,权重最高")
     private Integer chargingX;
 
+    @ApiModelProperty(value = "算法识别剩余笔数,权重最高")
+    private Integer algorithmX;
+
     @ApiModelProperty(value = "过期时间")
     private LocalDateTime timeout;
 
     @ApiModelProperty(value = "创建时间")
     private LocalDateTime createTime;
 
+    @ApiModelProperty(value = "是否需要定时计费")
+    private Boolean isJob;
 }

+ 14 - 2
device-api-service/src/main/java/com/xy/entity/DeviceChargingHistory.java

@@ -26,19 +26,31 @@ public class DeviceChargingHistory extends BaseEntity<DeviceChargingHistory> imp
 
     private static final long serialVersionUID = 1L;
 
+    @ApiModelProperty(value = "订单id")
+    private String orderId;
+
     @ApiModelProperty(value = "设备id")
     private Long deviceId;
 
     @ApiModelProperty(value = "续费金额")
     private Integer chargingMoney;
 
+    @ApiModelProperty(value = "续费数量")
+    private Integer chargingSize;
+
     @ApiModelProperty(value = "续费方式")
     private Integer chargingType;
 
-    @ApiModelProperty(value = "续费有效期")
-    private LocalDateTime chargingDateTime;
+    @ApiModelProperty(value = "计费标准 单位:分/年")
+    private Integer standard;
+
+    @ApiModelProperty(value = "状态")
+    private Integer status;
 
     @ApiModelProperty(value = "创建时间")
     private LocalDateTime createTime;
 
+    @ApiModelProperty(value = "更新时间")
+    private LocalDateTime updateTime;
+
 }

+ 19 - 69
device-api-service/src/main/java/com/xy/job/DeviceChargingJob.java

@@ -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);
         }
     }
 

+ 103 - 11
device-api-service/src/main/java/com/xy/service/DeviceChargingServiceImpl.java

@@ -3,14 +3,17 @@ package com.xy.service;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+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.device.EnumDeviceChargingHistoryStatus;
 import com.xy.dto.DeviceChargingDto;
 import com.xy.dto.DeviceChargingHistoryDto;
 import com.xy.dto.DeviceInfoDto;
 import com.xy.entity.DeviceCharging;
 import com.xy.entity.DeviceChargingHistory;
 import com.xy.entity.SysDictRedis;
-import com.xy.job.DeviceChargingJob;
 import com.xy.mapper.DeviceChargingMapper;
 import com.xy.utils.*;
 import io.swagger.annotations.Api;
@@ -18,10 +21,10 @@ import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
-import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 
+import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Map;
 
@@ -41,17 +44,9 @@ import static com.xy.utils.PlusBeans.*;
 @AllArgsConstructor(onConstructor_ = @Lazy)
 public class DeviceChargingServiceImpl extends ServiceImpl<DeviceChargingMapper, DeviceCharging> implements DeviceChargingService {
 
-    private DeviceChargingJob deviceChargingJob;
-
     private DeviceChargingHistoryServiceImpl deviceChargingHistoryService;
 
-    @PostMapping("charging")
-    @ApiOperation("手动计费")
-    public R charging(@RequestBody @Validated DeviceChargingDto.Charging charging) {
-        List<Long> deviceIds = charging.getDeviceIds();
-        deviceChargingJob.deviceCharging(1, deviceIds.size(), deviceIds);
-        return R.ok();
-    }
+    private DeviceInfoServiceImpl deviceInfoService;
 
     @PostMapping("page")
     @ApiOperation("分页查询")
@@ -86,4 +81,101 @@ public class DeviceChargingServiceImpl extends ServiceImpl<DeviceChargingMapper,
         }
         return R.ok(toPageBean(iPage));
     }
+
+    @Override
+    @ApiOperation("购买")
+    public R pay(DeviceChargingDto.Pay pay) {
+        JList<DeviceChargingDto.Pay.DeviceCharging> deviceChargings = new JArrayList<>(pay.getDeviceChargings());
+        Integer payType = pay.getPayType();
+        LocalDateTime now = LocalDateTime.now();
+        JList<DeviceChargingHistory> deviceChargingHistories = new JArrayList<>();
+        deviceChargings.forEach(deviceCharging -> {
+            DeviceChargingHistory deviceChargingHistory = new DeviceChargingHistory()
+                    .createId()
+                    .setOrderId(pay.getOrderId())
+                    .setDeviceId(deviceCharging.getDeviceId())
+                    .setChargingSize(deviceCharging.getNum())
+                    .setChargingMoney(deviceCharging.getChargingMoney())
+                    .setChargingType(payType)
+                    .setStandard(deviceCharging.getStandard())
+                    .setCreateTime(now)
+                    .setUpdateTime(now);
+            deviceChargingHistories.add(deviceChargingHistory);
+        });
+        if (!Emptys.check(deviceChargingHistories)) {
+            return R.fail();
+        }
+        //添加设备计费历史表
+        deviceChargingHistoryService.saveBatch(deviceChargingHistories);
+        return R.ok();
+    }
+
+    @Override
+    @ApiOperation("购买回调")
+    public R payNotice(DeviceChargingDto.PayNotice payNotice) {
+        //查询设备计费历史表
+        List<DeviceChargingHistory> list = deviceChargingHistoryService.list(new LambdaQueryWrapper<DeviceChargingHistory>().eq(DeviceChargingHistory::getOrderId, payNotice.getOrderId()));
+        if (!Emptys.check(list)) {
+            return R.ok();
+        }
+        JList<DeviceChargingHistory> deviceChargingHistories = new JArrayList<>(list);
+        LocalDateTime now = LocalDateTime.now();
+        //查询设备计费表
+        List<DeviceCharging> deviceChargings = list(new LambdaQueryWrapper<DeviceCharging>().in(DeviceCharging::getDeviceId, deviceChargingHistories.getProperty(DeviceChargingHistory::getDeviceId)));
+        JMap<Long, DeviceCharging> deviceChargingsJMaps = new JArrayList<>(deviceChargings).toMap(DeviceCharging::getDeviceId).cover();
+        //获取已支付字典
+        Integer status = SysDictUtils.getValue(EnumDeviceChargingHistoryStatus.Code.CODE.getCode(), EnumDeviceChargingHistoryStatus.N_2.getCode(), Integer.class);
+        JList<DeviceCharging> updateDeviceCharging = new JArrayList<>();
+        JList<DeviceInfoDto.Update> updateDeviceInfos = new JArrayList<>();
+        JList<DeviceChargingHistory> updateDeviceChargingHistory = new JArrayList<>();
+        for (DeviceChargingHistory deviceChargingHistory : deviceChargingHistories) {
+            DeviceCharging deviceCharging = deviceChargingsJMaps.get(deviceChargingHistory.getDeviceId());
+            Integer chargingSize = deviceChargingHistory.getChargingSize();
+            boolean fal = false;
+            //设备计费历史完成
+            deviceChargingHistory.setStatus(status);
+            if (deviceChargingHistory.getChargingType() == 100) {
+                //赠送天数
+                deviceCharging.setChargingX(deviceCharging.getChargingX() + chargingSize);
+                fal = true;
+            } else {
+                String timeout = DataTime.getStringAround(chargingSize, 0, 0, 0, 0, 0, DataTime.toString(deviceCharging.getTimeout()));
+                deviceCharging.setChargingSumMoney(deviceCharging.getChargingSumMoney() + deviceChargingHistory.getChargingMoney())
+                        .setTimeout(DataTime.toLocal(timeout));
+                //过期时间大于当前时间
+                if (DataTime.stringContrast(timeout, DataTime.toString(now)) > 0) {
+                    fal = true;
+                }
+            }
+            if (fal) {
+                //设备重新计费
+                deviceCharging.setIsJob(true);
+                //解冻设备
+                DeviceInfoDto.Update deviceInfo = new DeviceInfoDto.Update()
+                        .setDeviceId(deviceChargingHistory.getDeviceId());
+                deviceInfo.setFreezeStatus(1);
+                deviceInfo.setBusyState(1);
+                updateDeviceInfos.add(deviceInfo);
+            }
+            updateDeviceCharging.add(deviceCharging);
+            updateDeviceChargingHistory.add(deviceChargingHistory);
+        }
+        int size = 50;
+        //修改设备计费记录
+        if (Emptys.check(updateDeviceCharging)) {
+            JList<JList<DeviceCharging>> partition = updateDeviceCharging.partition(size);
+            partition.forEach(data -> updateBatchById(data));
+        }
+        //修改设备计费历史记录
+        if (Emptys.check(updateDeviceChargingHistory)) {
+            JList<JList<DeviceChargingHistory>> partition = updateDeviceChargingHistory.partition(size);
+            partition.forEach(data -> deviceChargingHistoryService.updateBatchById(data));
+        }
+        //修改设备
+        if (Emptys.check(updateDeviceInfos)) {
+            JList<JList<DeviceInfoDto.Update>> partition = updateDeviceInfos.partition(size);
+            partition.forEach(data -> deviceInfoService.updateBatch(data));
+        }
+        return R.ok();
+    }
 }

+ 1 - 1
device-api-service/src/main/java/com/xy/service/DeviceStatusServiceImpl.java

@@ -79,7 +79,7 @@ public class DeviceStatusServiceImpl extends ServiceImpl<DeviceStatusMapper, Dev
             Integer tempMin = deviceConfig.getTempMin();
             //温度有异常
             String key = String.format("device_temp_error:%s:%s", deviceStatus.getDeviceId(), deviceStatus.getTempValue() >= tempMax ? "max" : "min");
-            if (deviceStatus.getTempValue() >= tempMax || deviceStatus.getTempValue() <= tempMin) {
+            if (deviceStatus.getTempValue() > tempMax || deviceStatus.getTempValue() < tempMin) {
                 //校验异常次数
                 Integer errorSize = SysDictUtils.getValue(EnumDeviceTempConfig.Code.CODE.getCode(), EnumDeviceTempConfig.ERROR_SIZE.getCode(), Integer.class);
                 boolean fal = false;

+ 45 - 0
device-api/src/main/java/com/xy/dto/DeviceChargingDto.java

@@ -6,7 +6,9 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.experimental.Accessors;
 
+import javax.validation.constraints.NotBlank;
 import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
 import java.time.LocalDateTime;
 import java.util.List;
 
@@ -49,6 +51,49 @@ public class DeviceChargingDto {
         private LocalDateTime thisTime = LocalDateTime.now();
     }
 
+    @Data
+    @Accessors(chain = true)
+    public static class Pay {
+
+        @NotBlank(message = "orderId不能为空")
+        @ApiModelProperty("订单号")
+        private String orderId;
+
+        @NotEmpty(message = "deviceChargings不能为空")
+        @ApiModelProperty("续费信息")
+        private List<DeviceCharging> deviceChargings;
+
+        @NotNull(message = "payType不能为空")
+        @ApiModelProperty(value = "支付类型 2=支付宝 3=微信 100=赠送")
+        private Integer payType;
+
+        @Data
+        @Accessors(chain = true)
+        public static class DeviceCharging {
+
+            @ApiModelProperty("设备id")
+            private Long deviceId;
+
+            @ApiModelProperty("购买年数/赠送天数")
+            private Integer num;
+
+            @ApiModelProperty(value = "计费标准 单位:分/年")
+            private Integer standard;
+
+            @ApiModelProperty("续费金额")
+            private Integer chargingMoney;
+        }
+    }
+
+    @Data
+    @Accessors(chain = true)
+    public static class PayNotice {
+
+        @NotBlank(message = "orderId不能为空")
+        private String orderId;
+
+    }
+
     @Data
     @Accessors(chain = true)
     public static class Vo {

+ 14 - 2
device-api/src/main/java/com/xy/dto/DeviceChargingHistoryDto.java

@@ -25,21 +25,33 @@ public class DeviceChargingHistoryDto {
         @ApiModelProperty(value = "id")
         private Long id;
 
+        @ApiModelProperty(value = "订单id")
+        private String orderId;
+
         @ApiModelProperty(value = "设备id")
         private Long deviceId;
 
         @ApiModelProperty(value = "续费金额")
         private Integer chargingMoney;
 
+        @ApiModelProperty(value = "续费数量")
+        private Integer chargingSize;
+
         @ApiModelProperty(value = "续费方式")
         private Integer chargingType;
 
-        @ApiModelProperty(value = "续费有效期")
-        private LocalDateTime chargingDateTime;
+        @ApiModelProperty(value = "计费标准 单位:分/年")
+        private Integer standard;
+
+        @ApiModelProperty(value = "状态")
+        private Integer status;
 
         @ApiModelProperty(value = "创建时间")
         private LocalDateTime createTime;
 
+        @ApiModelProperty(value = "更新时间")
+        private LocalDateTime updateTime;
+
     }
 
     @Data

+ 21 - 0
device-api/src/main/java/com/xy/service/DeviceChargingService.java

@@ -1,6 +1,10 @@
 package com.xy.service;
 
 import com.xy.annotate.RestMappingController;
+import com.xy.dto.DeviceChargingDto;
+import com.xy.utils.R;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 
 /**
  * <p>
@@ -13,4 +17,21 @@ import com.xy.annotate.RestMappingController;
 @RestMappingController("/device-charging")
 public interface DeviceChargingService {
 
+    /**
+     * 购买
+     *
+     * @param pay
+     * @return
+     */
+    @PostMapping("pay")
+    R pay(@RequestBody DeviceChargingDto.Pay pay);
+
+    /**
+     * 购买回调
+     *
+     * @param payNotice
+     * @return
+     */
+    @PostMapping("payNotice")
+    R payNotice(@RequestBody DeviceChargingDto.PayNotice payNotice);
 }