From 0983df3a965acb29e8c4306725d0fb357edb2485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Sat, 28 Sep 2024 14:42:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=88=86=E6=88=90=EF=BC=8C?= =?UTF-8?q?=E8=BF=98=E5=B7=AE=E6=9C=88=E8=B4=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ss/bonus/service/BonusConverter.java | 3 +- .../service/impl/BonusConverterImpl.java | 19 +-- .../bonus/service/impl/BonusServiceImpl.java | 68 ++++++-- .../ruoyi/ss/device/domain/vo/DeviceVO.java | 6 + .../ruoyi/ss/device/mapper/DeviceMapper.xml | 3 +- .../ss/device/service/DeviceAssembler.java | 9 ++ .../ss/device/service/DeviceService.java | 5 + .../service/impl/DeviceAssemblerImpl.java | 44 +++++ .../service/impl/DeviceServiceImpl.java | 61 ++++--- .../service/TransactionBillService.java | 6 - .../impl/TransactionBillServiceImpl.java | 150 ++++++++---------- .../controller/app/AppDeviceController.java | 2 + 12 files changed, 243 insertions(+), 133 deletions(-) diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/BonusConverter.java b/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/BonusConverter.java index ded9e5bc..1e3dd005 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/BonusConverter.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/BonusConverter.java @@ -34,10 +34,9 @@ public interface BonusConverter { * @param agent * @param platform * @param device - * @param channel * @return */ - List genBonusList(SmUserVo mch, SmUserVo agent, SysDept platform, DeviceVO device, ChannelVO channel); + List genBonusList(SmUserVo mch, SmUserVo agent, SysDept platform, DeviceVO device); /** * 旧订单转为分成明细 diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusConverterImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusConverterImpl.java index 05832198..f5203fa4 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusConverterImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusConverterImpl.java @@ -2,16 +2,15 @@ package com.ruoyi.ss.bonus.service.impl; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.entity.SysDept; -import com.ruoyi.common.enums.ServiceType; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.ServiceUtil; import com.ruoyi.ss.bonus.domain.Bonus; import com.ruoyi.ss.bonus.domain.enums.BonusArrivalType; import com.ruoyi.ss.bonus.domain.enums.BonusStatus; import com.ruoyi.ss.bonus.service.BonusConverter; -import com.ruoyi.ss.channel.domain.ChannelVO; import com.ruoyi.ss.device.domain.enums.DeviceServiceMode; import com.ruoyi.ss.device.domain.vo.DeviceVO; +import com.ruoyi.ss.device.service.DeviceAssembler; import com.ruoyi.ss.store.service.StoreService; import com.ruoyi.ss.transactionBill.domain.TransactionBill; import com.ruoyi.ss.transactionBill.domain.bo.RechargeBO; @@ -21,7 +20,6 @@ import com.ruoyi.ss.transactionBill.domain.vo.UserRechargeServiceVO; import com.ruoyi.ss.transactionBill.service.TransactionBillService; import com.ruoyi.ss.user.domain.SmUserVo; import com.ruoyi.ss.user.service.ISmUserService; -import com.ruoyi.system.domain.enums.config.ConfigKey; import com.ruoyi.system.service.ISysConfigService; import com.ruoyi.system.service.ISysDeptService; import org.springframework.beans.factory.annotation.Autowired; @@ -59,6 +57,9 @@ public class BonusConverterImpl implements BonusConverter { @Autowired private ISysConfigService sysConfigService; + @Autowired + private DeviceAssembler deviceAssembler; + /** * 订单转为分成明细 * @@ -75,7 +76,7 @@ public class BonusConverterImpl implements BonusConverter { return Collections.emptyList(); } - List result = genBonusList(bo.getMch(), null, bo.getPlatform(), bo.getDevice(), bo.getChannel()); + List result = genBonusList(bo.getMch(), null, bo.getPlatform(), bo.getDevice()); for (Bonus bonus : result) { bonus.setBillId(bill.getBillId()); @@ -96,11 +97,11 @@ public class BonusConverterImpl implements BonusConverter { return Collections.emptyList(); } - return genBonusList(bo.getMch(), bo.getAgent(), bo.getPlatform(), bo.getDevice(), null); + return genBonusList(bo.getMch(), bo.getAgent(), bo.getPlatform(), bo.getDevice()); } @Override - public List genBonusList(SmUserVo mch, SmUserVo agent, SysDept platform, DeviceVO device, ChannelVO channel) { + public List genBonusList(SmUserVo mch, SmUserVo agent, SysDept platform, DeviceVO device) { List result = new ArrayList<>(); if (mch == null || platform == null || device == null) { return result; @@ -110,11 +111,11 @@ public class BonusConverterImpl implements BonusConverter { // 直营模式 if (DeviceServiceMode.DIRECT.getMode().equals(device.getServiceMode())) { - // 平台收取服务费 - UserRechargeServiceVO userRechargeService = transactionBillService.getMchRechargeService(channel, mch, device); + // 拼接设备服务费费率 + deviceAssembler.assembleRealServiceRate(device); // 按照百分比收取服务费 - BigDecimal serviceRate = userRechargeService.getServiceRate(); // 服务费比例 + BigDecimal serviceRate = device.getRealServiceRate(); // 服务费比例 result.add(this.toPo(platform, serviceRate)); point = point.subtract(serviceRate); diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusServiceImpl.java index 06df411e..18f94907 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/bonus/service/impl/BonusServiceImpl.java @@ -198,7 +198,7 @@ public class BonusServiceImpl implements BonusService @Override public int partBonus(List bonusList, BigDecimal money) { - if (CollectionUtils.isEmptyElement(bonusList)) { + if (CollectionUtils.isEmptyElement(bonusList) || money == null) { return 0; } @@ -216,18 +216,61 @@ public class BonusServiceImpl implements BonusService LocalDateTime now = LocalDateTime.now(); - // TODO 最低分成,假设为 0.05 元,若判断金额小于 0.05 则全部收取,若分成小于 0.05 则收取0.05元 // 获取平台的分成 BonusVO platform = bonusList.stream().filter(bonus -> bonus.getArrivalType().equals(BonusArrivalType.PLATFORM.getType())).findFirst().orElse(null); ServiceUtil.assertion(platform == null, "平台不存在"); + BigDecimal platformAmount = money.multiply(platform.getPoint()).divide(decimal100, 2, RoundingMode.HALF_UP); // 平台预计分成金额 - // 循环遍历,构造分成金额 + // 处理最低分成 + BigDecimal minService = sysConfigService.getBigDecimal(ConfigKey.RECHARGE_MIN_SERVICE); + // 若总金额 < 最低分成,则平台全收 + if (money.compareTo(minService) < 0) { + this.setAmount(platform, money); + dividedAmount = platform.getAmount(); + } + // 若平台分成 < 最低金额,则收取最低金额,其他金额给其他分成方分成 + else if (platformAmount.compareTo(minService) < 0) { + // 平台设置分成为最低金额 + this.setAmount(platform, minService); + dividedAmount = platform.getAmount(); + + // 计算其他分成方分成金额 + BigDecimal remain = money.subtract(platform.getAmount()); // 剩余可分成金额 + BigDecimal remainPoint = decimal100.subtract(platform.getPoint()); // 剩余总百分比 + for (BonusVO bonus : bonusList) { + if (bonus.getArrivalType().equals(BonusArrivalType.PLATFORM.getType())) { + continue; + } + // 计算分成比例占剩余可分成金额的比例 + BigDecimal bonusPoint = bonus.getPoint().multiply(decimal100).divide(remainPoint, 2, RoundingMode.HALF_UP); + BigDecimal amount = remain.multiply(bonusPoint).divide(decimal100, 2, RoundingMode.HALF_UP); + this.setAmount(bonus, amount); + // 增加已分成金额 + dividedAmount = dividedAmount.add(amount); + } + } + // 其余情况,正常收取费用 + else { + // 循环遍历,构造分成金额 + for (BonusVO bonus : bonusList) { + BigDecimal amount = money.multiply(bonus.getPoint()).divide(decimal100, 2, RoundingMode.HALF_UP); + this.setAmount(bonus, amount); + // 增加已分成金额 + dividedAmount = dividedAmount.add(amount); + } + } + + // 若存在误差,则平台吃掉误差 + if (dividedAmount.compareTo(money) != 0) { + BigDecimal subtract = money.subtract(dividedAmount); // 误差值 + this.setAmount(platform, platform.getAmount().add(subtract)); + } + + // 设置预计分成时间等数据 for (BonusVO bonus : bonusList) { - BigDecimal amount = money.multiply(bonus.getPoint()).divide(decimal100, 2, RoundingMode.HALF_UP); - bonus.setAmount(amount); - bonus.setWaitAmount(amount); bonus.setStatus(BonusStatus.WAIT_DIVIDE.getStatus()); - // 预计分成时间 + bonus.setWaitAmount(bonus.getAmount()); + // 设置预计分成时间 if (BonusArrivalType.userList().contains(bonus.getArrivalType())) { SmUserVo user = userList.stream().filter(item -> item.getUserId().equals(bonus.getArrivalId())).findFirst().orElse(null); if (user == null) { @@ -237,12 +280,6 @@ public class BonusServiceImpl implements BonusService } else { bonus.setPrePayTime(now); } - dividedAmount = dividedAmount.add(amount); - } - // 若存在误差,则平台吃掉误差 - if (dividedAmount.compareTo(money) != 0) { - BigDecimal subtract = money.subtract(dividedAmount); // 误差值 - platform.setAmount(platform.getAmount().add(subtract)); } Integer result = transactionTemplate.execute(status -> { @@ -256,6 +293,11 @@ public class BonusServiceImpl implements BonusService return result == null ? 0 : result; } + private void setAmount(BonusVO bonus, BigDecimal amount) { + ServiceUtil.assertion(amount.compareTo(BigDecimal.ZERO) < 0, "分成金额不允许小于0"); + bonus.setAmount(amount); + } + @Override public List> selectCountByArrival(BonusQuery query) { return bonusMapper.selectCountByArrival(query); diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/domain/vo/DeviceVO.java b/smart-switch-service/src/main/java/com/ruoyi/ss/device/domain/vo/DeviceVO.java index 33871f94..a2bd0c91 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/domain/vo/DeviceVO.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/domain/vo/DeviceVO.java @@ -75,6 +75,12 @@ public class DeviceVO extends Device implements IotDevice { @ApiModelProperty("型号产品ID") private String modelProductId; + @ApiModelProperty("商户服务费率") + private BigDecimal userServiceRate; + + @ApiModelProperty("最终生效的服务费率") + private BigDecimal realServiceRate; + @Override public String iotMac1() { return getMac(); diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/mapper/DeviceMapper.xml b/smart-switch-service/src/main/java/com/ruoyi/ss/device/mapper/DeviceMapper.xml index 58d18286..69334bf8 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/mapper/DeviceMapper.xml +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/mapper/DeviceMapper.xml @@ -153,7 +153,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ss.contact_name as store_contact_name, ss.contact_mobile as store_contact_mobile, su.phonenumber as user_mobile, - su.user_name as user_name + su.user_name as user_name, + su.service_rate as user_service_rate from sm_device sd left join sm_model sm on sm.model_id = sd.model_id left join sm_store ss on ss.store_id = sd.store_id diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceAssembler.java b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceAssembler.java index d7314893..d5856bbc 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceAssembler.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/DeviceAssembler.java @@ -27,4 +27,13 @@ public interface DeviceAssembler { */ void assembleOrderCountInfo(List list); + /** + * 拼接真实的服务费率 + */ + void assembleRealServiceRate(DeviceVO device); + + /** + * 拼接真实的服务费率 + */ + void assembleRealServiceRate(List list); } 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 e28737d2..492ed4ac 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 @@ -111,6 +111,11 @@ public interface DeviceService */ void pullDeviceInfo(List deviceIds); + /** + * 拉取设备信息,并保存到数据库 + */ + void pullDeviceInfoList(List list); + /** * 通过SN绑定设备 * diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceAssemblerImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceAssemblerImpl.java index c43a0825..2655c5ba 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceAssemblerImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/device/service/impl/DeviceAssemblerImpl.java @@ -1,5 +1,6 @@ package com.ruoyi.ss.device.service.impl; +import com.ruoyi.common.utils.ServiceUtil; import com.ruoyi.common.utils.collection.CollectionUtils; import com.ruoyi.ss.dashboard.vo.BillCountVo; import com.ruoyi.ss.device.domain.vo.DeviceVO; @@ -15,6 +16,8 @@ import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillGroupBy; import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus; import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType; import com.ruoyi.ss.transactionBill.service.TransactionBillService; +import com.ruoyi.system.domain.enums.config.ConfigKey; +import com.ruoyi.system.service.ISysConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -38,6 +41,9 @@ public class DeviceAssemblerImpl implements DeviceAssembler { @Autowired private DeviceSuitService deviceSuitService; + @Autowired + private ISysConfigService sysConfigService; + /** * 拼接套餐列表 * @@ -113,4 +119,42 @@ public class DeviceAssemblerImpl implements DeviceAssembler { } } + @Override + public void assembleRealServiceRate(DeviceVO device) { + if (device == null) { + return; + } + this.assembleRealServiceRate(Collections.singletonList(device)); + } + + /** + * 拼接真实的服务费率 + * + * @param list + */ + @Override + public void assembleRealServiceRate(List list) { + if (CollectionUtils.isEmptyElement(list)) { + return; + } + + // 系统默认配置 + BigDecimal sysServiceRate = sysConfigService.getBigDecimal(ConfigKey.SERVICE_FEE_RATE); + + for (DeviceVO device : list) { + if (device == null) { + continue; + } + if (device.getServiceRate() != null) { + device.setRealServiceRate(device.getServiceRate()); + } else if (device.getUserServiceRate() != null) { + device.setRealServiceRate(device.getUserServiceRate()); + } else { + ServiceUtil.assertion(sysServiceRate != null, "系统服务费率未配置,请联系管理员"); + device.setRealServiceRate(sysServiceRate); + } + } + + } + } 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 c3d5d47e..eded1fe3 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 @@ -768,7 +768,8 @@ public class DeviceServiceImpl implements DeviceService this.pullDeviceInfoList(list); } - private void pullDeviceInfoList(List list) { + @Override + public void pullDeviceInfoList(List list) { if (CollectionUtils.isEmptyElement(list)) { return; } @@ -783,27 +784,26 @@ public class DeviceServiceImpl implements DeviceService IotDeviceInfo deviceInfo = iotService.getDeviceInfo(device); // 更新设备信息 - Device data = new Device(); - data.setDeviceId(device.getDeviceId()); + device.setDeviceId(device.getDeviceId()); if (deviceInfo != null) { - data.setLastPullTime(deviceInfo.getAt()); - data.setTotalElectriQuantity(deviceInfo.getW()); - data.setPowerStatus(deviceInfo.getS()); - data.setRemainTime(deviceInfo.getTime()); - data.setRealTimePower(deviceInfo.getP()); - data.setVoltage(deviceInfo.getV()); - data.setElectricity(deviceInfo.getA()); + device.setLastPullTime(deviceInfo.getAt()); + device.setTotalElectriQuantity(deviceInfo.getW()); + device.setPowerStatus(deviceInfo.getS()); + device.setRemainTime(deviceInfo.getTime()); + device.setRealTimePower(deviceInfo.getP()); + device.setVoltage(deviceInfo.getV()); + device.setElectricity(deviceInfo.getA()); // 是否有WIFI if (ModelTag.hasTag(device.getModelTags(), ModelTag.WIFI)) { - data.setWifi(deviceInfo.getWifi()); + device.setWifi(deviceInfo.getWifi()); } // 是否有电量 if (ModelTag.hasTag(device.getModelTags(), ModelTag.ELE)) { - data.setSurplusEle(deviceInfo.getM()); + device.setSurplusEle(deviceInfo.getM()); } else { - data.setSurplusEle(BigDecimal.ZERO); + device.setSurplusEle(BigDecimal.ZERO); } } @@ -811,29 +811,50 @@ public class DeviceServiceImpl implements DeviceService // 设备过期时间 > 当前时间,则正在使用 boolean hasTime = device.getExpireTime() != null && device.getExpireTime().isAfter(now); // 若当前设备有电量,则正在使用 - boolean hasEle = data.getSurplusEle() != null && data.getSurplusEle().compareTo(BigDecimal.ZERO) > 0; + boolean hasEle = device.getSurplusEle() != null && device.getSurplusEle().compareTo(BigDecimal.ZERO) > 0; // 若开关开启,则正在使用 - boolean hasOpen = DevicePowerStatus.ON.getStatus().equals(data.getPowerStatus()); + boolean hasOpen = DevicePowerStatus.ON.getStatus().equals(device.getPowerStatus()); if (hasTime || hasEle || hasOpen) { - data.setStatus(DeviceStatus.USING.getStatus()); + device.setStatus(DeviceStatus.USING.getStatus()); } else { - data.setStatus(DeviceStatus.NORMAL.getStatus()); + device.setStatus(DeviceStatus.NORMAL.getStatus()); } // 是否在线 String onlineStatus = iotService.getOnlineStatus(device).getStatus(); - data.setOnlineStatus(onlineStatus); + device.setOnlineStatus(onlineStatus); if (DeviceOnlineStatus.ONLINE.getStatus().equals(onlineStatus)) { - data.setLastOnlineTime(now); + device.setLastOnlineTime(now); } - deviceMapper.updateSmDevice(data); + this.updateIotInfo(device); } catch (Exception e) { log.error("更新设备信息失败,deviceId:{}", device.getDeviceId(), e); } } } + /** + * 更新物联网信息 + */ + private int updateIotInfo(Device device) { + Device data = new Device(); + data.setDeviceId(device.getDeviceId()); + data.setLastPullTime(device.getLastPullTime()); + data.setTotalElectriQuantity(device.getTotalElectriQuantity()); + data.setPowerStatus(device.getPowerStatus()); + data.setRemainTime(device.getRemainTime()); + data.setRealTimePower(device.getRealTimePower()); + data.setVoltage(device.getVoltage()); + data.setElectricity(device.getElectricity()); + data.setWifi(device.getWifi()); + data.setSurplusEle(device.getSurplusEle()); + data.setStatus(device.getStatus()); + data.setOnlineStatus(device.getOnlineStatus()); + data.setLastOnlineTime(device.getLastOnlineTime()); + return deviceMapper.updateSmDevice(device); + } + /** * 异步延迟执行拉取设备信息 * @param deviceIds 设备id列表 diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionBillService.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionBillService.java index 7709f217..4a16302b 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionBillService.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/TransactionBillService.java @@ -1,6 +1,5 @@ package com.ruoyi.ss.transactionBill.service; -import com.ruoyi.ss.channel.domain.ChannelVO; import com.ruoyi.ss.dashboard.vo.BillCountVo; import com.ruoyi.ss.device.domain.vo.DeviceVO; import com.ruoyi.ss.payBill.domain.vo.DoPayVO; @@ -309,11 +308,6 @@ public interface TransactionBillService */ TransactionBillVO selectLastOne(TransactionBillQuery query); - /** - * 获取商户的充值手续费 - */ - UserRechargeServiceVO getMchRechargeService(ChannelVO channel, SmUserVo mch, DeviceVO device); - /** * 修复订单数据 */ diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java index 225858e4..b0064758 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/transactionBill/service/impl/TransactionBillServiceImpl.java @@ -18,7 +18,6 @@ import com.ruoyi.ss.bonus.domain.enums.BonusArrivalType; import com.ruoyi.ss.bonus.domain.enums.BonusStatus; import com.ruoyi.ss.bonus.service.BonusConverter; import com.ruoyi.ss.bonus.service.BonusService; -import com.ruoyi.ss.channel.domain.ChannelVO; import com.ruoyi.ss.channelWithdraw.domain.ChannelWithdrawVO; import com.ruoyi.ss.channelWithdraw.service.ChannelWithdrawService; import com.ruoyi.ss.dashboard.vo.BillCountVo; @@ -255,14 +254,12 @@ public class TransactionBillServiceImpl implements TransactionBillService, After // 拉取设备信息 deviceService.pullDeviceInfo(Collections.singletonList(bo.getDevice().getDeviceId())); - // 下单 + // 生成订单 TransactionBill order = parseToOrder(bo); - // 代理商模式,生成分成列表 - if (DeviceServiceMode.AGENT.getMode().equals(order.getDeviceServiceMode())) { - List bonusList = bonusConverter.toPo(bo); - bo.setBonusList(bonusList); - } + // 生成分成列表 + List bonusList = bonusConverter.toPo(bo); + bo.setBonusList(bonusList); // TODO 收取月费 @@ -272,14 +269,12 @@ public class TransactionBillServiceImpl implements TransactionBillService, After ServiceUtil.assertion(insert != 1, "下单失败"); // 代理商模式,插入分成列表 - if (DeviceServiceMode.AGENT.getMode().equals(order.getDeviceServiceMode())) { - for (Bonus bonus : bo.getBonusList()) { - bonus.setBillId(order.getBillId()); - bonus.setBillNo(order.getBillNo()); - } - int bonusInsert = bonusService.batchInsert(bo.getBonusList()); - ServiceUtil.assertion(bonusInsert != bo.getBonusList().size(), "创建分成失败"); + for (Bonus bonus : bo.getBonusList()) { + bonus.setBillId(order.getBillId()); + bonus.setBillNo(order.getBillNo()); } + int bonusInsert = bonusService.batchInsert(bo.getBonusList()); + ServiceUtil.assertion(bonusInsert != bo.getBonusList().size(), "创建分成失败"); return insert; }); @@ -349,25 +344,6 @@ public class TransactionBillServiceImpl implements TransactionBillService, After } - @Override - public UserRechargeServiceVO getMchRechargeService(ChannelVO channel, SmUserVo mch, DeviceVO device) { - // 优先级: 设备 > 商户 > 渠道 - if (device.getServiceRate() != null && StringUtils.hasText(device.getServiceType())) { - return new UserRechargeServiceVO(device.getServiceType(), device.getServiceRate()); - } - // 商户 - else if (mch.getServiceRate() != null && StringUtils.hasText(mch.getServiceType())) { - return new UserRechargeServiceVO(mch.getServiceType(), mch.getServiceRate()); - } - // 渠道 - else { - ServiceUtil.assertion(channel == null, "支付渠道不存在"); - ServiceUtil.assertion(channel.getServiceRate() == null, "支付渠道服务费未配置,请联系管理员"); - ServiceUtil.assertion(StringUtils.isBlank(channel.getServiceType()), "支付渠道服务费收取方式未配置,请联系管理员"); - return new UserRechargeServiceVO(channel.getServiceType(), channel.getServiceRate()); - } - } - @Override public int fix(Long billId) { TransactionBillVO bill = selectSmTransactionBillByBillId(billId); @@ -1085,10 +1061,13 @@ public class TransactionBillServiceImpl implements TransactionBillService, After } else { throw new ServiceException("计算金额出错,套餐收费类型不支持"); } - // 最低金额 - if (totalAmount.compareTo(BigDecimal.valueOf(0.01)) < 0) { - totalAmount = BigDecimal.valueOf(0.01); + + // 最低金额不允许低于最低服务费 + BigDecimal minService = sysConfigService.getBigDecimal(ConfigKey.RECHARGE_MIN_SERVICE); + if (totalAmount.compareTo(minService) < 0) { + totalAmount = minService; } + return totalAmount; } @@ -1112,9 +1091,6 @@ public class TransactionBillServiceImpl implements TransactionBillService, After // 若结束时间 > 订单结束时间 或者 结束电量 > 订单结束电量,则表示已经结束使用 ServiceUtil.assertion(order.getIsFinished() != null && order.getIsFinished(), "当前订单已结束,无法操作"); - // 计算需要退款的金额 - BigDecimal refundAmount = this.calcRefundAmount(order, endTime, totalEle); - Integer result = transactionTemplate.execute(status -> { // 修改结束使用的时间 TransactionBill data = new TransactionBill(); @@ -1124,28 +1100,30 @@ public class TransactionBillServiceImpl implements TransactionBillService, After query.setBillId(order.getBillId()); query.setStatus(TransactionBillStatus.SUCCESS.getStatus()); query.setIsFinished(false); -// if (SuitFeeType.TIME.getType().equals(order.getSuitFeeType())) { -// query.setStartSuitEndTime(endTime); // 计时的话,需要结束时间在这之后的订单 -// } else if (SuitFeeType.COUNT.getType().equals(order.getSuitFeeType())) { -// query.setStartSuitEndEle(totalEle); // 计量的话,需要结束电量比这个大的订单 -// } int update = this.updateByQuery(data, query); ServiceUtil.assertion(update != 1, "修改订单信息失败,请重试:" + order.getBillNo()); - // 若金额 > 0.01 则申请退款 - if (BigDecimal.valueOf(0.01).compareTo(refundAmount) < 0) { - // 申请退款 - BillRefundDTO refundDto = new BillRefundDTO(); - refundDto.setBillId(order.getBillId()); - refundDto.setRefundAmount(refundAmount); - refundDto.setRefundReason(String.format("充值订单%s智能退款%s元", order.getBillNo(), refundAmount)); - int refund = this.refund(refundDto); - ServiceUtil.assertion(refund != 1, "申请退款失败"); - } - return update; }); + boolean success = result != null && result == 1; // 是否成功 + + if (success) { + // 计算需要退款的金额,若金额 > 0.01 则申请退款 + BigDecimal refundAmount = this.calcRefundAmount(order, endTime, totalEle); + if (BigDecimal.valueOf(0.01).compareTo(refundAmount) < 0) { + scheduledExecutorService.schedule(() -> { + // 申请退款 + BillRefundDTO refundDto = new BillRefundDTO(); + refundDto.setBillId(order.getBillId()); + refundDto.setRefundAmount(refundAmount); + refundDto.setRefundReason(String.format("充值订单%s智能退款%s元", order.getBillNo(), refundAmount)); + int refund = this.refund(refundDto); + ServiceUtil.assertion(refund != 1, "申请退款失败"); + }, 0, TimeUnit.SECONDS); + } + } + // 清零设备 if (resetDevice) { try { @@ -1249,16 +1227,25 @@ public class TransactionBillServiceImpl implements TransactionBillService, After } private BigDecimal calcRefundAmount(TransactionBillVO order, LocalDateTime endTime, BigDecimal totalEle) { + BigDecimal refund = BigDecimal.ZERO; // 智能收费时长计费 if (SuitFeeType.TIME.getType().equals(order.getSuitFeeType())) { - return this.calcTimeRefund(order, endTime); + refund = this.calcTimeRefund(order, endTime); } // 智能收费计量收费 else if (SuitFeeType.COUNT.getType().equals(order.getSuitFeeType())) { - return this.calcCountRefund(order, totalEle); - } else { + refund = this.calcCountRefund(order, totalEle); + } + else { throw new ServiceException("不支持的智能收费方式"); } + + // 退款金额不允许大于订单金额 + if (refund.compareTo(order.getMoney()) > 0) { + refund = order.getMoney(); + } + + return refund; } @Override @@ -1614,13 +1601,12 @@ public class TransactionBillServiceImpl implements TransactionBillService, After // 分成方余额按照比例扣减 // 按比例计算退款金额 BigDecimal refundAmount = dto.getRefundAmount(); // 总退款金额 - int updateRefundBonus = this.updateRefundBonus(bonusList, refundAmount); + int updateRefundBonus = this.updateRefundBonus(bonusList, refundAmount, bill.getMoney()); ServiceUtil.assertion(updateRefundBonus != bonusList.size(), "商户余额更新失败"); - // 修改原订单的退款金额和退款手续费 - Bonus platform = bonusList.stream().filter(bonus -> bonus.getArrivalType().equals(BonusArrivalType.PLATFORM.getType())).findFirst().orElse(null); + // 修改原订单的退款金额 int updateRefundAmount = this.addRefundAmount(bill.getBillId(), refundAmount, BigDecimal.ZERO, BigDecimal.ZERO); - ServiceUtil.assertion(updateRefundAmount != 1, "修改原订单的退款金额和退款手续费失败"); + ServiceUtil.assertion(updateRefundAmount != 1, "修改原订单的退款金额失败"); // 发起退款 PayBillRefundDTO refundDto = new PayBillRefundDTO(); @@ -1638,14 +1624,15 @@ public class TransactionBillServiceImpl implements TransactionBillService, After /** * 更新分成的退款金额 + * @param bonusList 分成列表 + * @param refundAmount 本次退款金额 + * @param billAmount 订单总金额 */ - private int updateRefundBonus(List bonusList, BigDecimal refundAmount) { + private int updateRefundBonus(List bonusList, BigDecimal refundAmount, BigDecimal billAmount) { if (CollectionUtils.isEmptyElement(bonusList)) { return 0; } - BigDecimal decimal100 = BigDecimal.valueOf(100); - // 构建退款列表 List refundList = new ArrayList<>(); // 退款的列表 BigDecimal dividedAmount = BigDecimal.ZERO; // 已分配金额 @@ -1653,14 +1640,21 @@ public class TransactionBillServiceImpl implements TransactionBillService, After if (bonus == null) { continue; } - // 计算扣减金额 - BigDecimal bonusRefundAmount = refundAmount.multiply(bonus.getPoint()).divide(decimal100, 2, RoundingMode.HALF_UP); - bonus.setRefundAmount(bonusRefundAmount); - refundList.add(bonus); + // 计算扣减金额 = 退款金额 * 分配金额 / 总金额 + BigDecimal bonusRefundAmount = refundAmount.multiply(bonus.getAmount()).divide(billAmount, 2, RoundingMode.HALF_UP); + + Bonus refundBonus = new Bonus(); + refundBonus.setId(bonus.getId()); + refundBonus.setStatus(bonus.getStatus()); + refundBonus.setRefundAmount(bonusRefundAmount); + refundBonus.setArrivalId(bonus.getArrivalId()); + refundBonus.setArrivalType(bonus.getArrivalType()); + refundBonus.setBillNo(bonus.getBillNo()); + refundBonus.setBillId(bonus.getBillId()); + refundList.add(refundBonus); dividedAmount = dividedAmount.add(bonusRefundAmount); } - // 平台吃掉误差 Bonus platform = refundList.stream().filter(bonus -> bonus.getArrivalType().equals(BonusArrivalType.PLATFORM.getType())).findFirst().orElse(null); ServiceUtil.assertion(platform == null, "平台不存在"); @@ -1669,6 +1663,10 @@ public class TransactionBillServiceImpl implements TransactionBillService, After platform.setRefundAmount(platform.getRefundAmount().add(subtract)); } + // 校验金额 + for (Bonus bonus : refundList) { + ServiceUtil.assertion(bonus.getRefundAmount().compareTo(BigDecimal.ZERO) < 0, "退款金额不允许小于0"); + } BigDecimal sum = refundList.stream().map(Bonus::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add); ServiceUtil.assertion(sum.compareTo(refundAmount) != 0, "退款金额分配出错"); @@ -1889,12 +1887,6 @@ public class TransactionBillServiceImpl implements TransactionBillService, After ServiceUtil.assertion(!redisLock.lock(RedisLockKey.PREPAY, lockKey), "当前订单正在支付,请稍后重试"); try { - // 直营,计算分成 - if (DeviceServiceMode.DIRECT.getMode().equals(order.getDeviceServiceMode())) { - List bonusList = bonusConverter.toPo(bo); - bo.setBonusList(bonusList); - } - return transactionTemplate.execute(status -> { // 修改订单信息 TransactionBill data = new TransactionBill(); @@ -1906,12 +1898,6 @@ public class TransactionBillServiceImpl implements TransactionBillService, After int paying = this.updateByQuery(data, query); ServiceUtil.assertion(paying != 1, "订单状态已发生变化,更新失败"); - // 直营,新增分成信息 - if (DeviceServiceMode.DIRECT.getMode().equals(order.getDeviceServiceMode())) { - int bonusInsert = bonusService.batchInsert(bo.getBonusList()); - ServiceUtil.assertion(bonusInsert != bo.getBonusList().size(), "新增分成失败"); - } - // 创建支付订单并发起支付 return payBillService.createPayBill(payBillConverter.toPoByRechargeMoney(bo)); }); 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 5df84b45..cf722778 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 @@ -74,6 +74,7 @@ public class AppDeviceController extends BaseController { startPage(); smDevice.setUserId(getUserId()); List list = smDeviceService.selectSmDeviceList(smDevice); + smDeviceService.pullDeviceInfoList(list); return getDataTable(list); } @@ -84,6 +85,7 @@ public class AppDeviceController extends BaseController { startPage(); query.setStoreId(storeId); List list = smDeviceService.selectSmDeviceList(query); + smDeviceService.pullDeviceInfoList(list); deviceAssembler.assembleBusinessTime(list); // 店铺营业时间 return getDataTable(list); }