From b994a52f138b563d89b8b4d89e5b9d2af42288b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Mon, 14 Oct 2024 14:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=BD=92=E9=9B=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ss/device/service/DeviceService.java | 21 +++----- .../service/impl/DeviceServiceImpl.java | 52 +++++++++++++++++-- .../controller/app/AppDeviceController.java | 11 ++-- .../web/controller/ss/SmDeviceController.java | 4 +- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceService.java b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceService.java index 3baa90c8..4f911241 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceService.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceService.java @@ -142,22 +142,12 @@ public interface DeviceService */ DeviceVO selectByMac(String mac); - /** - * 归零电表,并关闭订单 - * - * @param deviceId 设备id - */ - default int resetWithBill(Long deviceId) { - return resetWithBill(deviceId, true); - } - /** * 归零电表,并关闭订单 * * @param deviceId 设备id - * @param requiredIot 是否操作设备 */ - int resetWithBill(Long deviceId, boolean requiredIot); + int resetWithBill(Long deviceId); /** * 设备是否已经被绑定 @@ -316,11 +306,12 @@ public interface DeviceService int resetEle(Long deviceId, boolean required); /** - * 电量归零,并关闭订单 + * 时长归零,并关闭订单 + * @param deviceId + * @param requiredIot + * @return */ - default int resetEleWithBill(Long deviceId) { - return resetEleWithBill(deviceId, true); - } + int resetTimeWithBill(Long deviceId, boolean requiredIot); /** * 电量归零,并关闭订单 diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java index 466389cd..f355c52a 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceServiceImpl.java @@ -68,6 +68,8 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; +import static net.sf.jsqlparser.parser.feature.Feature.reset; + /** * 设备Service业务层处理 * @@ -555,6 +557,38 @@ public class DeviceServiceImpl implements DeviceService return resetEle(device, required); } + @Override + public int resetTimeWithBill(Long deviceId, boolean requiredIot) { + if (deviceId == null) { + return 0; + } + // 拉取设备信息 + pullDeviceInfo(deviceId); + + DeviceVO device = selectById(deviceId); + ServiceUtil.assertion(device == null, "设备不存在"); + + // 尝试关闭该设备未结束的所有订单 + TransactionBillQuery query = new TransactionBillQuery(); + query.setStatusList(TransactionBillStatus.canClose()); + query.setDeviceId(device.getDeviceId()); + query.setIsFinished(false); // 未结束的订单都会被关闭 + query.setSuitFeeTypes(SuitFeeType.rechargeTimeList()); + List billList = transactionBillService.selectSmTransactionBillList(query); + try { + int closeCount = transactionBillService.batchCloseBillByDevice(billList, false, device); + ServiceUtil.assertion(closeCount != billList.size(), "关闭订单失败:closeCount =" + closeCount); + } catch (Exception e) { + log.error("关闭订单失败:{}" , e.getMessage()); + } + + // 发送命令 + int reset = this.resetTime(device, requiredIot); + ServiceUtil.assertion(reset != 1, "设备时长归零失败"); + + return 1; + } + @Override public int resetEleWithBill(Long deviceId, boolean requiredIot) { if (deviceId == null) { @@ -1088,7 +1122,7 @@ public class DeviceServiceImpl implements DeviceService } @Override - public int resetWithBill(Long deviceId, boolean requiredIot) { + public int resetWithBill(Long deviceId) { ServiceUtil.assertion(deviceId == null, "设备id不能为空"); // 拉取最新设备信息 @@ -1120,10 +1154,20 @@ public class DeviceServiceImpl implements DeviceService ServiceUtil.assertion(eleUpdate != 1, "修改设备电量失败"); // 发送命令 - int reset = iotService.setTimeAndEle(device, 0L, BigDecimal.ZERO); - ServiceUtil.assertion(reset != 1, "归零失败"); +// int reset = iotService.setTimeAndEle(device, 0L, BigDecimal.ZERO); +// ServiceUtil.assertion(reset != 1, "归零失败"); + CommandResponse res1 = iotService.setTime(device, 0L); + ServiceUtil.assertion(!res1.isSuccess(), "设备时长归零失败:" + res1.getMsg()); + try { + Thread.sleep(1000); + } catch (Exception e) { + log.error("设备归零等待时间异常:{}", e.getMessage()); + throw new ServiceException(e.getMessage()); + } + CommandResponse res2 = iotService.setEle(device, BigDecimal.ZERO); + ServiceUtil.assertion(!res2.isSuccess(), "设备电量归零失败:" + res2.getMsg()); - return reset; + return 1; }); if (result != null && result == 1) { diff --git a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppDeviceController.java b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppDeviceController.java index 10ac598a..58d392a2 100644 --- a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppDeviceController.java +++ b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppDeviceController.java @@ -9,6 +9,7 @@ import com.ruoyi.common.core.domain.ValidGroup; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.enums.OperatorType; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.ServiceUtil; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.iot.service.IotService; @@ -37,6 +38,8 @@ import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; import java.util.Collections; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * 设备Controller @@ -63,6 +66,9 @@ public class AppDeviceController extends BaseController { @Autowired private IotService iotService; + @Autowired + private ScheduledExecutorService scheduledExecutorService; + @Log(title = "商户修改设备信息", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE) @ApiOperation("商户修改设备信息") @PutMapping @@ -139,9 +145,8 @@ public class AppDeviceController extends BaseController { @Log(title = "设备归零", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE) @ApiOperation("设备归零") @PutMapping("{deviceId}/reset") - public AjaxResult reset(@PathVariable @ApiParam("设备id") Long deviceId, - @RequestParam(required = false, defaultValue = "true") Boolean requiredIot) { - return success(smDeviceService.resetWithBill(deviceId, requiredIot)); + public AjaxResult reset(@PathVariable @ApiParam("设备id") Long deviceId) { + return toAjax(smDeviceService.resetWithBill(deviceId)); } @ApiOperation("获取设备用电量分析") diff --git a/smart-switch-web/src/main/java/com/ruoyi/web/controller/ss/SmDeviceController.java b/smart-switch-web/src/main/java/com/ruoyi/web/controller/ss/SmDeviceController.java index 1bfe4bef..de72a2d6 100644 --- a/smart-switch-web/src/main/java/com/ruoyi/web/controller/ss/SmDeviceController.java +++ b/smart-switch-web/src/main/java/com/ruoyi/web/controller/ss/SmDeviceController.java @@ -176,14 +176,14 @@ public class SmDeviceController extends BaseController @Log(title = "设备时长归零", businessType = BusinessType.OTHER) @PutMapping("/{deviceId}/reset") public AjaxResult reset(@PathVariable @ApiParam("设备id") Long deviceId) { - return toAjax(deviceService.resetWithBill(deviceId)); + return toAjax(deviceService.resetTimeWithBill(deviceId, true)); } @PreAuthorize("@ss.hasPermi('system:device:resetEle')") @Log(title = "设备清空电量", businessType = BusinessType.OTHER) @PutMapping("/{deviceId}/resetEle") public AjaxResult resetEle(@PathVariable @ApiParam("设备id") Long deviceId) { - return toAjax(deviceService.resetEleWithBill(deviceId)); + return toAjax(deviceService.resetEleWithBill(deviceId, true)); } @ApiOperation("设备开关")