<?xml version="1.0" encoding="UTF-8"?>
|
<!DOCTYPE mapper
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
<mapper namespace="com.ruoyi.account.mapper.TChargingCardMapper">
|
|
<select id="selectCardPage" resultType="com.ruoyi.account.api.model.TChargingCard">
|
SELECT id,
|
card_no,
|
balance,
|
company_id,
|
fleet_id,
|
status,
|
del_flag,
|
create_time,
|
create_by,
|
update_time,
|
update_by
|
FROM t_charging_card
|
WHERE del_flag = 0
|
<if test="query.cardNo != null and query.cardNo != ''">
|
AND card_no LIKE CONCAT('%', #{query.cardNo}, '%')
|
</if>
|
<if test="query.status != null">
|
AND status = #{query.status}
|
</if>
|
<if test="query.companyId != null">
|
AND company_id = #{query.companyId}
|
</if>
|
<if test="query.fleetId != null">
|
AND fleet_id = #{query.fleetId}
|
</if>
|
ORDER BY create_time DESC, id DESC
|
</select>
|
|
<select id="selectExportList" resultType="com.ruoyi.account.api.dto.ChargingCardExcelDTO">
|
SELECT card_no,
|
balance,
|
company_id,
|
fleet_id,
|
status
|
FROM t_charging_card
|
WHERE del_flag = 0
|
<if test="query.cardNo != null and query.cardNo != ''">
|
AND card_no LIKE CONCAT('%', #{query.cardNo}, '%')
|
</if>
|
<if test="query.status != null">
|
AND status = #{query.status}
|
</if>
|
<if test="query.companyId != null">
|
AND company_id = #{query.companyId}
|
</if>
|
<if test="query.fleetId != null">
|
AND fleet_id = #{query.fleetId}
|
</if>
|
ORDER BY create_time DESC, id DESC
|
</select>
|
|
<select id="countByCardNo" resultType="int">
|
SELECT COUNT(1)
|
FROM t_charging_card
|
WHERE card_no = #{cardNo}
|
<if test="excludeId != null">
|
AND id != #{excludeId}
|
</if>
|
</select>
|
|
<!-- 人工调账:仍保持"增加不超过上限、扣减不低过 0"的运营约束,并叠加充电锁定条件兜底(V0.2 定稿) -->
|
<update id="adjustBalance">
|
UPDATE t_charging_card
|
SET balance = balance
|
<choose>
|
<when test="type == 1">+ #{amount}</when>
|
<otherwise>- #{amount}</otherwise>
|
</choose>,
|
update_time = NOW(),
|
update_by = #{updateBy}
|
WHERE del_flag = 0
|
AND charging_flag = 0
|
AND id IN
|
<foreach collection="ids" item="id" open="(" separator="," close=")">
|
#{id}
|
</foreach>
|
<choose>
|
<when test="type == 1">
|
AND balance <= #{maxAmount} - #{amount}
|
</when>
|
<otherwise>
|
AND balance >= #{amount}
|
</otherwise>
|
</choose>
|
</update>
|
|
<insert id="insertBalanceRecord">
|
INSERT INTO t_charging_card_balance_record
|
(card_id, change_type, amount, remark, create_time, create_by)
|
VALUES
|
(#{cardId}, #{type}, #{amount}, #{remark}, NOW(), #{createBy})
|
</insert>
|
|
<!-- 锁定:仅空闲卡可锁定,条件更新防止同卡并发多单 -->
|
<update id="lockCardForCharging">
|
UPDATE t_charging_card
|
SET charging_flag = 1,
|
charging_order_code = #{orderCode},
|
update_time = NOW()
|
WHERE id = #{cardId}
|
AND del_flag = 0
|
AND charging_flag = 0
|
</update>
|
|
<!-- 解锁:带订单号比对,防止误释放其他进行中订单;已解锁(order_code 已清空)时影响行数为 0 -->
|
<update id="unlockCardForCharging">
|
UPDATE t_charging_card
|
SET charging_flag = 0,
|
charging_order_code = NULL,
|
update_time = NOW()
|
WHERE id = #{cardId}
|
AND del_flag = 0
|
AND charging_order_code = #{orderCode}
|
</update>
|
|
<!-- 订单结算原子扣款:只按卡 ID 扣减,禁止带 balance >= amount 下限条件,允许扣成负数 -->
|
<update id="deductBalanceByCardId">
|
UPDATE t_charging_card
|
SET balance = balance - #{amount},
|
update_time = NOW()
|
WHERE id = #{cardId}
|
AND del_flag = 0
|
</update>
|
|
<!-- 订单结算扣款流水:charging_order_code 唯一索引即扣款幂等键(uk_order_code) -->
|
<insert id="insertOrderDeductRecord">
|
INSERT INTO t_charging_card_balance_record
|
(card_id, change_type, amount, remark, charging_order_code, create_time, create_by)
|
VALUES
|
(#{cardId}, 3, #{amount}, #{orderCode}, #{orderCode}, NOW(), #{createBy})
|
</insert>
|
|
<select id="selectDeductedOrderCodes" resultType="java.lang.String">
|
SELECT charging_order_code
|
FROM t_charging_card_balance_record
|
WHERE change_type = 3
|
AND charging_order_code IN
|
<foreach collection="orderCodes" item="orderCode" open="(" separator="," close=")">
|
#{orderCode}
|
</foreach>
|
</select>
|
|
<!-- 原子清零余额:账户模式切换回收专用,仅当余额>0时清零,天然幂等 -->
|
<update id="clearBalance">
|
UPDATE t_charging_card
|
SET balance = 0,
|
update_time = NOW(),
|
update_by = #{updateBy}
|
WHERE id = #{id}
|
AND del_flag = 0
|
AND balance > 0
|
</update>
|
|
</mapper>
|