Files
BMS/bms-back/bms-system/src/main/resources/mapper/ar/ArBillMapper.xml
T

231 lines
10 KiB
XML

<?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.bms.ar.mapper.ArBillMapper">
<!-- 账单头查询、状态流转和金额/生成进度更新。带 ForUpdate 的查询用于核销并发控制。 -->
<resultMap id="ArBillResult" type="ArBill">
<id property="arBillId" column="ar_bill_id"/>
<result property="billNo" column="bill_no"/>
<result property="billName" column="bill_name"/>
<result property="customerId" column="customer_id"/>
<result property="customerCode" column="customer_code"/>
<result property="customerName" column="customer_name"/>
<result property="billPeriodStart" column="bill_period_start"/>
<result property="billPeriodEnd" column="bill_period_end"/>
<result property="billDate" column="bill_date"/>
<result property="currency" column="currency"/>
<result property="orderCount" column="order_count"/>
<result property="totalAmount" column="total_amount"/>
<result property="writtenOffAmount" column="written_off_amount"/>
<result property="unwrittenAmount" column="unwritten_amount"/>
<result property="status" column="status"/>
<result property="generationStatus" column="generation_status"/>
<result property="billVersion" column="bill_version"/>
<result property="bankAccountName" column="bank_account_name"/>
<result property="bankAccountNo" column="bank_account_no"/>
<result property="bankName" column="bank_name"/>
<result property="remark" column="remark"/>
<result property="confirmedBy" column="confirmed_by"/>
<result property="confirmedTime" column="confirmed_time"/>
<result property="createBy" column="create_by"/>
<result property="createUserId" column="create_user_id"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time"/>
</resultMap>
<resultMap id="ArBillSummaryResult" type="com.bms.ar.domain.vo.ArBillSummary">
<result property="totalAmount" column="total_amount"/>
<result property="writtenOffAmount" column="written_off_amount"/>
<result property="unwrittenAmount" column="unwritten_amount"/>
</resultMap>
<sql id="selectArBillColumns">
select ar_bill_id,
bill_no,
bill_name,
customer_id,
customer_code,
customer_name,
bill_period_start,
bill_period_end,
bill_date,
currency,
order_count,
total_amount,
written_off_amount,
unwritten_amount,
status,
generation_status,
bill_version,
bank_account_name,
bank_account_no,
bank_name,
remark,
confirmed_by,
confirmed_time,
create_by,
create_user_id,
create_time,
update_by,
update_time
from ar_bill
</sql>
<sql id="arBillListFilter">
<where>
and (generation_status is null or generation_status = 'READY')
<if test="query.billNo != null and query.billNo != ''">and bill_no like concat('%', #{query.billNo}, '%')</if>
<if test="query.billName != null and query.billName != ''">and bill_name like concat('%', #{query.billName}, '%')</if>
<if test="query.customerCode != null and query.customerCode != ''">and customer_code like concat('%', #{query.customerCode},
'%')
</if>
<if test="query.customerName != null and query.customerName != ''">and customer_name like concat('%', #{query.customerName},
'%')
</if>
<if test="query.customerId != null">and customer_id = #{query.customerId}</if>
<choose>
<when test="query.statuses != null and query.statuses.size() > 0">
and status in
<foreach collection="query.statuses" item="statusValue" open="(" separator="," close=")">
#{statusValue}
</foreach>
</when>
<when test="query.status != null and query.status != ''">and status = #{query.status}</when>
</choose>
<if test="query.currency != null and query.currency != ''">and currency = #{query.currency}</if>
<if test="query.billDateStart != null">and bill_date &gt;= #{query.billDateStart}</if>
<if test="query.billDateEnd != null">and bill_date &lt;= #{query.billDateEnd}</if>
<if test="query.billPeriodEnd != null">and bill_period_end = #{query.billPeriodEnd}</if>
<include refid="com.bms.ar.mapper.ArBillScopeMapper.billScope"/>
</where>
</sql>
<select id="selectArBillList" resultMap="ArBillResult">
select b.* from ar_bill b
<include refid="arBillListFilter"/>
order by create_time desc, ar_bill_id desc
</select>
<select id="selectArBillSummary" resultMap="ArBillSummaryResult">
select coalesce(sum(total_amount), 0) as total_amount,
coalesce(sum(written_off_amount), 0) as written_off_amount,
coalesce(sum(unwritten_amount), 0) as unwritten_amount
from ar_bill b
<include refid="arBillListFilter"/>
and status in ('CONFIRMED', 'PARTIAL_WRITTEN_OFF', 'WRITTEN_OFF')
</select>
<select id="selectArBillById" parameterType="long" resultMap="ArBillResult">
<include refid="selectArBillColumns"/>
where ar_bill_id = #{arBillId}
</select>
<select id="selectArBillByIdScoped" resultMap="ArBillResult">
select b.* from ar_bill b where b.ar_bill_id = #{arBillId}
<include refid="com.bms.ar.mapper.ArBillScopeMapper.billScope"/>
</select>
<select id="selectArBillByBillNo" parameterType="string" resultMap="ArBillResult">
<include refid="selectArBillColumns"/>
where bill_no = #{billNo}
</select>
<select id="selectGeneratingByCustomerPeriod" resultMap="ArBillResult">
<include refid="selectArBillColumns"/>
where customer_id = #{customerId}
and currency = #{currency}
and bill_period_end = #{cutoffDate}
and generation_status = 'GENERATING'
order by ar_bill_id desc
limit 1
</select>
<select id="selectMaxBillVersion" resultType="int">
select bill_version
from ar_bill
where customer_id = #{customerId}
and bill_period_end = #{cutoffDate}
and currency = #{currency}
order by bill_version desc limit 1
for
update
</select>
<select id="selectArBillByIdForUpdate" parameterType="long" resultMap="ArBillResult">
<include refid="selectArBillColumns"/>
where ar_bill_id = #{arBillId} for update
</select>
<select id="selectUnsettledByCustomer" resultMap="ArBillResult">
select b.* from ar_bill b
where customer_id = #{customerId}
and status in ('CONFIRMED', 'PARTIAL_WRITTEN_OFF')
and (generation_status is null or generation_status = 'READY')
and unwritten_amount &gt; 0
<include refid="com.bms.ar.mapper.ArBillScopeMapper.billScope"/>
order by bill_date asc, ar_bill_id asc
</select>
<insert id="insertArBill" parameterType="ArBill" useGeneratedKeys="true" keyProperty="arBillId">
insert into ar_bill (bill_no, bill_name, customer_id, customer_code, customer_name, bill_period_start,
bill_period_end,
bill_date, currency, order_count, total_amount, written_off_amount, unwritten_amount,
status, generation_status, bill_version, bank_account_name, bank_account_no, bank_name,
remark,
confirmed_by, confirmed_time, create_by, create_user_id, create_time, update_by, update_time)
values (#{billNo}, #{billName}, #{customerId}, #{customerCode}, #{customerName}, #{billPeriodStart},
#{billPeriodEnd},
#{billDate}, #{currency}, #{orderCount}, #{totalAmount}, #{writtenOffAmount}, #{unwrittenAmount},
#{status}, #{generationStatus}, #{billVersion}, #{bankAccountName}, #{bankAccountNo}, #{bankName},
#{remark},
#{confirmedBy}, #{confirmedTime}, #{createBy}, #{createUserId}, sysdate(), #{updateBy}, sysdate())
</insert>
<update id="updateArBillStatus">
update ar_bill
set status = #{status},
remark = #{remark},
update_by = #{operator},
update_time = sysdate(),
confirmed_by = case when #{status} = 'CONFIRMED' then #{operator} else confirmed_by end,
confirmed_time = case when #{status} = 'CONFIRMED' then sysdate() else confirmed_time end
where ar_bill_id = #{arBillId}
and status = #{fromStatus}
</update>
<update id="updateArBillAmounts">
update ar_bill
set written_off_amount = #{writtenOffAmount},
unwritten_amount = #{unwrittenAmount},
status = #{status},
update_by = #{operator},
update_time = sysdate()
where ar_bill_id = #{arBillId}
and status = #{fromStatus}
</update>
<update id="updateArBillProgress">
update ar_bill
set order_count = #{orderCount},
total_amount = #{totalAmount},
unwritten_amount = #{unwrittenAmount},
bill_period_start = case
when #{billPeriodStart} &lt; bill_period_start then #{billPeriodStart}
else bill_period_start end,
update_by = #{operator},
update_time = sysdate()
where ar_bill_id = #{arBillId}
</update>
<update id="updateArBillGenerationStatus">
update ar_bill
set generation_status = #{generationStatus},
update_by = #{operator},
update_time = sysdate(),
remark = case when #{remark} is null or #{remark} = '' then remark else #{remark} end
where ar_bill_id = #{arBillId}
</update>
</mapper>