浏览代码

refactor(charging): 优化当前充电订单实时费用查询逻辑

- 取消通过充电订单号查询,改为根据当前登录用户ID查询充电订单
- 将原有多步骤数据库访问替换为单次联合查询,减少数据库IO
- 调整接口和服务方法签名,简化调用方式
- 实现MyBatis查询方法并更新对应XML映射配置
- 修改控制器方法,去除参数并调用新的服务接口
- 更新注释和接口文档,明确接口行为及返回情况
SheepHy 1 天之前
父节点
当前提交
00564e99e9

+ 4 - 4
src/main/java/com/zsElectric/boot/business/controller/applet/AppletStationController.java

@@ -68,14 +68,14 @@ public class AppletStationController {
 
     /**
      * 获取当前充电订单实时费用
+     * 根据当前登录用户查询正在充电中的订单
      *
-     * @param chargeOrderNo 充电订单号
-     * @return 实时充电费用信息
+     * @return 实时充电费用信息,如果没有正在充电的订单则返回null
      */
     @Operation(summary = "获取当前充电订单实时费用")
     @GetMapping("/charging-cost")
     @ApiRateLimit(prefix = "applet:charging_cost", limitType = ApiRateLimit.LimitType.IP, count = 2000, time = 60, message = "获取充电费用请求过于频繁,请稍后再试")
-    public Result<AppletChargingCostVO> getCurrentChargingCost(@RequestParam String chargeOrderNo) {
-        return Result.success(appletHomeService.getCurrentChargingCost(chargeOrderNo));
+    public Result<AppletChargingCostVO> getCurrentChargingCost() {
+        return Result.success(appletHomeService.getCurrentChargingCost());
     }
 }

+ 10 - 0
src/main/java/com/zsElectric/boot/business/mapper/ThirdPartyStationInfoMapper.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zsElectric.boot.business.model.vo.AppletStationDetailVO;
 import com.zsElectric.boot.business.model.vo.StationInfoMapVO;
+import com.zsElectric.boot.business.model.vo.applet.AppletChargingCostVO;
 import com.zsElectric.boot.charging.entity.ThirdPartyStationInfo;
 import com.zsElectric.boot.business.model.query.StationInfoQuery;
 import com.zsElectric.boot.business.model.vo.StationInfoVO;
@@ -88,4 +89,13 @@ public interface ThirdPartyStationInfoMapper extends BaseMapper<ThirdPartyStatio
             @Param("currentTime") String currentTime,
             @Param("firmId") Long firmId
     );
+
+    /**
+     * 查询用户当前正在充电中的订单实时费用
+     * 一次查询获取所有需要的信息,减少数据库IO
+     *
+     * @param userId 用户ID
+     * @return 实时充电费用信息
+     */
+    AppletChargingCostVO selectCurrentChargingCost(@Param("userId") Long userId);
 }

+ 3 - 3
src/main/java/com/zsElectric/boot/business/service/AppletHomeService.java

@@ -56,9 +56,9 @@ public interface AppletHomeService {
 
     /***
      * 获取用户当前充电订单费用信息
-     * @param chargeOrderNo 充电订单号
-     * @return 实时充电费用信息
+     * 根据当前登录用户ID查询正在充电中的订单
+     * @return 实时充电费用信息,如果没有正在充电的订单则返回null
      * */
-    AppletChargingCostVO getCurrentChargingCost(String chargeOrderNo);
+    AppletChargingCostVO getCurrentChargingCost();
 
 }

+ 5 - 114
src/main/java/com/zsElectric/boot/business/service/impl/AppletHomeServiceImpl.java

@@ -616,122 +616,13 @@ public class AppletHomeServiceImpl implements AppletHomeService {
     }
 
     @Override
-    public AppletChargingCostVO getCurrentChargingCost(String chargeOrderNo) {
-        // 1. 查询充电状态记录
-        ThirdPartyChargeStatus chargeStatus = thirdPartyChargeStatusMapper.selectOne(
-                new LambdaQueryWrapper<ThirdPartyChargeStatus>()
-                        .eq(ThirdPartyChargeStatus::getStartChargeSeq, chargeOrderNo)
-                        .orderByDesc(ThirdPartyChargeStatus::getUpdateTime)
-                        .last("LIMIT 1")
-        );
-
-        if (chargeStatus == null) {
-            return null;
-        }
-
-        // 2. 通过connector_id查询充电终端信息
-        ThirdPartyConnectorInfo connectorInfo = thirdPartyConnectorInfoMapper.selectOne(
-                new LambdaQueryWrapper<ThirdPartyConnectorInfo>()
-                        .eq(ThirdPartyConnectorInfo::getConnectorId, chargeStatus.getConnectorId())
-                        .last("LIMIT 1")
-        );
-
-        if (connectorInfo == null) {
+    public AppletChargingCostVO getCurrentChargingCost() {
+        // 获取当前登录用户ID
+        Long userId = SecurityUtils.getUserId();
+        if (userId == null) {
             return null;
         }
 
-        // 3. 通过station_id查询站点信息
-        ThirdPartyStationInfo stationInfo = thirdPartyStationInfoMapper.selectOne(
-                new LambdaQueryWrapper<ThirdPartyStationInfo>()
-                        .eq(ThirdPartyStationInfo::getStationId, connectorInfo.getStationId())
-                        .last("LIMIT 1")
-        );
-
-        // 4. 构建返回结果
-        AppletChargingCostVO result = new AppletChargingCostVO();
-        result.setChargeOrderNo(chargeOrderNo);
-        result.setStationName(stationInfo != null ? stationInfo.getStationName() : "");
-        result.setConnectorName(connectorInfo.getConnectorName());
-
-        // 订单状态
-        result.setOrderStatus(chargeStatus.getStartChargeSeqStat());
-        result.setOrderStatusDesc(getOrderStatusDesc(chargeStatus.getStartChargeSeqStat()));
-
-        // 充电时长
-        if (chargeStatus.getStartTime() != null && chargeStatus.getEndTime() != null) {
-            long duration = java.time.Duration.between(
-                    chargeStatus.getStartTime(),
-                    chargeStatus.getEndTime()
-            ).getSeconds();
-            result.setChargingDuration(duration);
-            result.setChargingDurationDesc(formatDuration(duration));
-        }
-
-        // 费用信息
-        result.setTotalPower(chargeStatus.getTotalPower() != null ? chargeStatus.getTotalPower() : BigDecimal.ZERO);
-        result.setElecMoney(chargeStatus.getElecMoney() != null ? chargeStatus.getElecMoney() : BigDecimal.ZERO);
-        result.setServiceMoney(chargeStatus.getServiceMoney() != null ? chargeStatus.getServiceMoney() : BigDecimal.ZERO);
-        result.setTotalMoney(chargeStatus.getTotalMoney() != null ? chargeStatus.getTotalMoney() : BigDecimal.ZERO);
-
-        // SOC
-        result.setSoc(chargeStatus.getSoc());
-
-        // 电压电流(取A相为主,如果没有则取其他相)
-        BigDecimal current = chargeStatus.getCurrentA() != null ? chargeStatus.getCurrentA() :
-                (chargeStatus.getCurrentB() != null ? chargeStatus.getCurrentB() : chargeStatus.getCurrentC());
-        BigDecimal voltage = chargeStatus.getVoltageA() != null ? chargeStatus.getVoltageA() :
-                (chargeStatus.getVoltageB() != null ? chargeStatus.getVoltageB() : chargeStatus.getVoltageC());
-
-        result.setCurrent(current);
-        result.setVoltage(voltage);
-
-        // 功率 = 电压 × 电流 / 1000 (kW)
-        if (current != null && voltage != null) {
-            BigDecimal power = voltage.multiply(current).divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP);
-            result.setPower(power);
-        }
-
-        // 时间信息
-        if (chargeStatus.getStartTime() != null) {
-            result.setStartTime(chargeStatus.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
-        }
-        if (chargeStatus.getUpdateTime() != null) {
-            result.setLastUpdateTime(chargeStatus.getUpdateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
-        }
-
-        return result;
-    }
-
-    /**
-     * 获取订单状态描述
-     */
-    private String getOrderStatusDesc(Integer status) {
-        if (status == null) {
-            return "未知";
-        }
-        switch (status) {
-            case 1:
-                return "启动中";
-            case 2:
-                return "充电中";
-            case 3:
-                return "停止中";
-            case 4:
-                return "已结束";
-            case 5:
-                return "未知";
-            default:
-                return "未知";
-        }
-    }
-
-    /**
-     * 格式化时长(秒)为 HH:mm:ss
-     */
-    private String formatDuration(long seconds) {
-        long hours = seconds / 3600;
-        long minutes = (seconds % 3600) / 60;
-        long secs = seconds % 60;
-        return String.format("%02d:%02d:%02d", hours, minutes, secs);
+        return thirdPartyStationInfoMapper.selectCurrentChargingCost(userId);
     }
 }

+ 71 - 0
src/main/resources/mapper/business/ThirdPartyStationInfoMapper.xml

@@ -569,4 +569,75 @@
             AND tpsi.id = #{stationId}
     </select>
 
+    <!-- 查询用户当前正在充电中的订单实时费用 -->
+    <resultMap id="AppletChargingCostResultMap" type="com.zsElectric.boot.business.model.vo.applet.AppletChargingCostVO">
+        <result property="chargeOrderNo" column="charge_order_no"/>
+        <result property="stationName" column="station_name"/>
+        <result property="connectorName" column="connector_name"/>
+        <result property="orderStatus" column="order_status"/>
+        <result property="orderStatusDesc" column="order_status_desc"/>
+        <result property="chargingDuration" column="charging_duration"/>
+        <result property="chargingDurationDesc" column="charging_duration_desc"/>
+        <result property="totalPower" column="total_power"/>
+        <result property="elecMoney" column="elec_money"/>
+        <result property="serviceMoney" column="service_money"/>
+        <result property="totalMoney" column="total_money"/>
+        <result property="soc" column="soc"/>
+        <result property="current" column="current"/>
+        <result property="voltage" column="voltage"/>
+        <result property="power" column="power"/>
+        <result property="startTime" column="start_time"/>
+        <result property="lastUpdateTime" column="last_update_time"/>
+    </resultMap>
+
+    <select id="selectCurrentChargingCost" resultMap="AppletChargingCostResultMap">
+        SELECT
+            coi.charge_order_no,
+            tpsi.station_name,
+            tpci.connector_name,
+            tcs.start_charge_seq_stat AS order_status,
+            CASE tcs.start_charge_seq_stat
+                WHEN 1 THEN '启动中'
+                WHEN 2 THEN '充电中'
+                WHEN 3 THEN '停止中'
+                WHEN 4 THEN '已结束'
+                WHEN 5 THEN '未知'
+                ELSE '未知'
+            END AS order_status_desc,
+            TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) AS charging_duration,
+            CONCAT(
+                LPAD(FLOOR(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) / 3600), 2, '0'), ':',
+                LPAD(FLOOR((TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) % 3600) / 60), 2, '0'), ':',
+                LPAD(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) % 60, 2, '0')
+            ) AS charging_duration_desc,
+            IFNULL(tcs.total_power, 0) AS total_power,
+            IFNULL(tcs.elec_money, 0) AS elec_money,
+            IFNULL(tcs.service_money, 0) AS service_money,
+            IFNULL(tcs.total_money, 0) AS total_money,
+            tcs.soc,
+            COALESCE(tcs.current_a, tcs.current_b, tcs.current_c) AS current,
+            COALESCE(tcs.voltage_a, tcs.voltage_b, tcs.voltage_c) AS voltage,
+            ROUND(
+                COALESCE(tcs.voltage_a, tcs.voltage_b, tcs.voltage_c) *
+                COALESCE(tcs.current_a, tcs.current_b, tcs.current_c) / 1000,
+                2
+            ) AS power,
+            DATE_FORMAT(tcs.start_time, '%Y-%m-%d %H:%i:%s') AS start_time,
+            DATE_FORMAT(tcs.update_time, '%Y-%m-%d %H:%i:%s') AS last_update_time
+        FROM c_charge_order_info coi
+        INNER JOIN third_party_charge_status tcs
+            ON coi.charge_order_no = tcs.start_charge_seq
+        INNER JOIN third_party_connector_info tpci
+            ON tcs.connector_id = tpci.connector_id
+            AND tpci.is_deleted = 0
+        INNER JOIN third_party_station_info tpsi
+            ON tpci.station_id = tpsi.station_id
+            AND tpsi.is_deleted = 0
+        WHERE coi.user_id = #{userId}
+            AND coi.status = 1
+            AND coi.is_deleted = 0
+        ORDER BY tcs.update_time DESC
+        LIMIT 1
+    </select>
+
 </mapper>