新增、编辑、删除费用后,都会根据费用明细重新汇总订单状态:

- 全部未出账:UNBILLED
- 部分已出账、部分未出账:PARTIAL
- 全部已出账:BILLED
3. 应收账单生成
   - 预览、统计、分页生成支持 UNBILLED 和 PARTIAL;
   - PARTIAL 订单只读取未出账费用;
   - 生成成功后,只将本次实际生成的费用更新为 BILLED;
   - 不会重复带入历史已出账费用;
   - 账单金额只统计本次未出账费用。
4. 作废、取消、失败清理
   - 只恢复当前账单关联的费用;
   - 如果费用仍被其他有效账单使用,则继续保持 BILLED;
   - 根据费用状态重新汇总订单状态;
   - 大账单清理使用批量 SQL,不组装几十万条费用 ID。
This commit is contained in:
caojia
2026-09-22 17:20:27 +08:00
parent cb50d2a258
commit 221d4721ee
11 changed files with 457 additions and 106 deletions
@@ -39,6 +39,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
order by bill_id, field(fee_type, 'BASIC_FREIGHT', 'REGISTRATION_FEE', 'SURCHARGE'), fee_id
</select>
<select id="selectUnbilledByBillIds" resultMap="PrcIncomeBillFeeResult">
select fee_id, bill_id, income_bill_id, fee_type, fee_name, sc_id, amount, currency, round_mode, remark,
ar_bill_status, manual_flag, create_time
from prc_income_bill_fee
where ar_bill_status = #{arBillStatus}
and bill_id in
<foreach collection="billIds" item="billId" open="(" separator="," close=")">
#{billId}
</foreach>
order by bill_id, field(fee_type, 'BASIC_FREIGHT', 'REGISTRATION_FEE', 'SURCHARGE'), fee_id
</select>
<select id="selectByFeeId" parameterType="String" resultMap="PrcIncomeBillFeeResult">
select fee_id, bill_id, income_bill_id, fee_type, fee_name, sc_id, amount, currency, round_mode, remark,
ar_bill_status, manual_flag, create_time
@@ -98,6 +110,52 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach>
</update>
<update id="updateArBillStatusByFeeIds">
update prc_income_bill_fee
set ar_bill_status = #{toStatus}
where ar_bill_status = #{fromStatus}
and fee_id in
<foreach collection="feeIds" item="feeId" open="(" separator="," close=")">
#{feeId}
</foreach>
</update>
<update id="restoreUnbilledIfNoActiveArBill">
update prc_income_bill_fee f
set f.ar_bill_status = 'UNBILLED'
where f.ar_bill_status = 'BILLED'
and f.fee_id in
<foreach collection="feeIds" item="feeId" open="(" separator="," close=")">
#{feeId}
</foreach>
and not exists (
select 1
from ar_bill_detail_fee df
inner join ar_bill_detail d on d.detail_id = df.detail_id
inner join ar_bill b on b.ar_bill_id = d.ar_bill_id
where df.prc_income_bill_fee_id = f.fee_id
and b.status &lt;&gt; 'VOID'
)
</update>
<update id="restoreUnbilledIfNoActiveArBillByArBillId">
update prc_income_bill_fee f
inner join ar_bill_detail_fee current_df on current_df.prc_income_bill_fee_id = f.fee_id
inner join ar_bill_detail current_d on current_d.detail_id = current_df.detail_id
and current_d.ar_bill_id = #{arBillId}
set f.ar_bill_status = 'UNBILLED'
where f.ar_bill_status = 'BILLED'
and not exists (
select 1
from ar_bill_detail_fee other_df
inner join ar_bill_detail other_d on other_d.detail_id = other_df.detail_id
inner join ar_bill other_b on other_b.ar_bill_id = other_d.ar_bill_id
where other_df.prc_income_bill_fee_id = f.fee_id
and other_b.status &lt;&gt; 'VOID'
and other_b.ar_bill_id &lt;&gt; #{arBillId}
)
</update>
<delete id="deleteByFeeId" parameterType="String">
delete from prc_income_bill_fee where fee_id = #{feeId}
</delete>
@@ -92,6 +92,29 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
from prc_order_bill
</sql>
<!-- 应收生成专用来源快照:income_total 只汇总当前仍未出账的费用。 -->
<sql id="selectArBillSourceVo">
select p.bill_id, p.bill_key, p.order_no, p.tracking_no, p.transfer_no,
p.belong_saler, p.belong_saler_code, p.settlement_man, p.settlement_man_code, p.service_man,
p.customer_id, p.customer_code, p.customer_name,
p.pieces, p.actual_weight, p.volume, p.volume_weight, p.pkg_length, p.pkg_width, p.pkg_height,
p.shipper_name, p.consignee_name, p.dest_postcode, p.warehouse_code, p.order_time, p.order_remark,
p.product_code, p.product_name, p.country_code, p.country_name, p.weight, p.biz_date, p.tms_id,
p.match_source, p.match_message, p.price_set_id, p.price_set_code, p.price_set_name,
p.set_type, p.bind_priority, p.customer_priceset_id, p.psv_id, p.psd_id, p.product_country_id,
p.weight_min, p.weight_max, p.weight_label, p.effective_date, p.charge_weight,
p.freight_rate, p.base_freight_rate, p.discount_percent, p.registration_fee,
p.freight_amount, p.surcharge_amount,
(select coalesce(sum(f.amount), 0)
from prc_income_bill_fee f
where f.bill_id = p.bill_id
and f.ar_bill_status = #{feeArBillStatus}) as income_total,
p.pay_total, p.currency, p.ar_bill_status,
p.customer_active_status, p.credit_arrears_status, p.round_mode,
p.created_by, p.create_time, p.updated_by, p.update_time
from prc_order_bill p
</sql>
<select id="selectPrcOrderBillByOrderNo" parameterType="String" resultMap="PrcOrderBillResult">
<include refid="selectPrcOrderBillVo"/>
where order_no = #{orderNo}
@@ -245,85 +268,106 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<!-- rename_bill_tables.sql 已将需求文档中的 prc_bill 映射为本项目的 prc_order_bill。 -->
<!-- 生成弹窗预览:按客户、截止日、币种和订单号/运单号/转单号 OR 条件分页查询未入账订单。 -->
<select id="selectPrcOrderBillByCustomerPeriod" resultMap="PrcOrderBillResult">
<include refid="selectPrcOrderBillVo"/>
where customer_id = #{customerId}
<include refid="selectArBillSourceVo"/>
where p.customer_id = #{customerId}
<if test="customerCode != null and customerCode != ''">
and customer_code like concat('%', #{customerCode}, '%')
and p.customer_code like concat('%', #{customerCode}, '%')
</if>
<if test="orderOrTrackingNo != null and orderOrTrackingNo != ''">
and (order_no like concat('%', #{orderOrTrackingNo}, '%')
or tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
and (p.order_no like concat('%', #{orderOrTrackingNo}, '%')
or p.tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or p.transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
</if>
<if test="excludeOrderOrTrackingNo != null and excludeOrderOrTrackingNo != ''">
and (order_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or tracking_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or transfer_no like concat('%', #{excludeOrderOrTrackingNo}, '%'))
and (p.order_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or p.tracking_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or p.transfer_no like concat('%', #{excludeOrderOrTrackingNo}, '%'))
</if>
and biz_date &lt;= #{cutoffDate}
and ar_bill_status = #{arBillStatus}
and p.biz_date &lt;= #{cutoffDate}
and p.ar_bill_status in
<foreach collection="arBillStatuses" item="status" open="(" separator="," close=")">
#{status}
</foreach>
and exists (
select 1 from prc_income_bill_fee f
where f.bill_id = p.bill_id
and f.ar_bill_status = #{feeArBillStatus}
)
<if test="currency != null and currency != ''">
and currency = #{currency}
and p.currency = #{currency}
</if>
<if test="excludedBillIds != null and excludedBillIds.size() > 0">
and bill_id not in
and p.bill_id not in
<foreach collection="excludedBillIds" item="excludedBillId" open="(" separator="," close=")">
#{excludedBillId}
</foreach>
</if>
order by biz_date, order_no, bill_id
order by p.biz_date, p.order_no, p.bill_id
</select>
<select id="sumPrcOrderBillIncomeByCustomerPeriod" resultType="java.math.BigDecimal">
select coalesce(sum(income_total), 0)
from prc_order_bill
where customer_id = #{customerId}
select coalesce(sum(f.amount), 0)
from prc_order_bill p
inner join prc_income_bill_fee f on f.bill_id = p.bill_id
and f.ar_bill_status = #{feeArBillStatus}
where p.customer_id = #{customerId}
<if test="customerCode != null and customerCode != ''">
and customer_code like concat('%', #{customerCode}, '%')
and p.customer_code like concat('%', #{customerCode}, '%')
</if>
<if test="orderOrTrackingNo != null and orderOrTrackingNo != ''">
and (order_no like concat('%', #{orderOrTrackingNo}, '%')
or tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
and (p.order_no like concat('%', #{orderOrTrackingNo}, '%')
or p.tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or p.transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
</if>
<if test="excludeOrderOrTrackingNo != null and excludeOrderOrTrackingNo != ''">
and (order_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or tracking_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or transfer_no like concat('%', #{excludeOrderOrTrackingNo}, '%'))
and (p.order_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or p.tracking_no like concat('%', #{excludeOrderOrTrackingNo}, '%')
or p.transfer_no like concat('%', #{excludeOrderOrTrackingNo}, '%'))
</if>
and biz_date &lt;= #{cutoffDate}
and ar_bill_status = #{arBillStatus}
and p.biz_date &lt;= #{cutoffDate}
and p.ar_bill_status in
<foreach collection="arBillStatuses" item="status" open="(" separator="," close=")">
#{status}
</foreach>
<if test="currency != null and currency != ''">
and currency = #{currency}
and p.currency = #{currency}
</if>
</select>
<sql id="customerPeriodConditions">
where customer_id = #{customerId}
where p.customer_id = #{customerId}
<if test="customerCode != null and customerCode != ''">
and customer_code like concat('%', #{customerCode}, '%')
and p.customer_code like concat('%', #{customerCode}, '%')
</if>
<if test="orderOrTrackingNo != null and orderOrTrackingNo != ''">
and (order_no like concat('%', #{orderOrTrackingNo}, '%')
or tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
and (p.order_no like concat('%', #{orderOrTrackingNo}, '%')
or p.tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or p.transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
</if>
and biz_date &lt;= #{cutoffDate}
and ar_bill_status = #{arBillStatus}
and p.biz_date &lt;= #{cutoffDate}
and p.ar_bill_status in
<foreach collection="arBillStatuses" item="status" open="(" separator="," close=")">
#{status}
</foreach>
and exists (
select 1 from prc_income_bill_fee f
where f.bill_id = p.bill_id
and f.ar_bill_status = #{feeArBillStatus}
)
<if test="currency != null and currency != ''">
and currency = #{currency}
and p.currency = #{currency}
</if>
<if test="snapshotCreateTime != null">
and (create_time is null or create_time &lt;= #{snapshotCreateTime})
and (p.create_time is null or p.create_time &lt;= #{snapshotCreateTime})
</if>
<if test="billIds != null and billIds.size() > 0">
and bill_id in
and p.bill_id in
<foreach collection="billIds" item="billId" open="(" separator="," close=")">
#{billId}
</foreach>
</if>
<if test="excludedBillIds != null and excludedBillIds.size() > 0">
and bill_id not in
and p.bill_id not in
<foreach collection="excludedBillIds" item="excludedBillId" open="(" separator="," close=")">
#{excludedBillId}
</foreach>
@@ -332,43 +376,51 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<!-- 异步生成使用 bill_id 游标批量读取,避免深分页和一次加载全量订单。 -->
<select id="selectPrcOrderBillByCustomerPeriodCursor" resultMap="PrcOrderBillResult">
<include refid="selectPrcOrderBillVo"/>
<include refid="selectArBillSourceVo"/>
<include refid="customerPeriodConditions"/>
<if test="lastSourceBillId != null and lastSourceBillId != ''">
and bill_id &gt; #{lastSourceBillId}
and p.bill_id &gt; #{lastSourceBillId}
</if>
order by bill_id
order by p.bill_id
limit #{limit}
</select>
<!-- 全选模式先统计总数,用于任务进度和空范围校验。 -->
<select id="countPrcOrderBillByCustomerPeriod" resultType="int">
select count(1)
from prc_order_bill
from prc_order_bill p
<include refid="customerPeriodConditions"/>
</select>
<select id="selectPrcOrderBillByCustomerCutoffAndIds" resultMap="PrcOrderBillResult">
<include refid="selectPrcOrderBillVo"/>
where customer_id = #{customerId}
and biz_date &lt;= #{cutoffDate}
and ar_bill_status = #{arBillStatus}
and bill_id in
<include refid="selectArBillSourceVo"/>
where p.customer_id = #{customerId}
and p.biz_date &lt;= #{cutoffDate}
and p.ar_bill_status in
<foreach collection="arBillStatuses" item="status" open="(" separator="," close=")">
#{status}
</foreach>
and exists (
select 1 from prc_income_bill_fee f
where f.bill_id = p.bill_id
and f.ar_bill_status = #{feeArBillStatus}
)
and p.bill_id in
<foreach collection="billIds" item="billId" open="(" separator="," close=")">
#{billId}
</foreach>
<if test="customerCode != null and customerCode != ''">
and customer_code like concat('%', #{customerCode}, '%')
and p.customer_code like concat('%', #{customerCode}, '%')
</if>
<if test="orderOrTrackingNo != null and orderOrTrackingNo != ''">
and (order_no like concat('%', #{orderOrTrackingNo}, '%')
or tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
and (p.order_no like concat('%', #{orderOrTrackingNo}, '%')
or p.tracking_no like concat('%', #{orderOrTrackingNo}, '%')
or p.transfer_no like concat('%', #{orderOrTrackingNo}, '%'))
</if>
<if test="currency != null and currency != ''">
and currency = #{currency}
and p.currency = #{currency}
</if>
order by biz_date, order_no, bill_id
order by p.biz_date, p.order_no, p.bill_id
</select>
<insert id="insertPrcOrderBill" parameterType="PrcOrderBill">
@@ -479,11 +531,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where bill_id = #{billId}
</update>
<!-- 抢占来源订单:只有未入账订单可以变更为已入账,防止重复生成。 -->
<!-- 抢占来源订单:未出账或部分出账订单均可进入本次生成事务。 -->
<update id="claimArBillOrders">
update prc_order_bill
set ar_bill_status = #{toStatus}, updated_by = #{operator}, update_time = sysdate()
where ar_bill_status = #{fromStatus}
where ar_bill_status in
<foreach collection="arBillStatuses" item="status" open="(" separator="," close=")">
#{status}
</foreach>
and bill_id in
<foreach collection="billIds" item="billId" open="(" separator="," close=")">
#{billId}
@@ -507,4 +562,63 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where d.ar_bill_id = #{arBillId}
and p.ar_bill_status = #{fromStatus}
</update>
<!-- 订单状态由来源费用状态汇总,避免补录或删除费用后状态失真。 -->
<update id="refreshArBillStatusByBillIds">
update prc_order_bill o
left join (
select bill_id,
count(1) as fee_count,
sum(case when ar_bill_status = 'BILLED' then 1 else 0 end) as billed_count,
sum(case when ar_bill_status = 'UNBILLED' then 1 else 0 end) as unbilled_count
from prc_income_bill_fee
where bill_id in
<foreach collection="billIds" item="billId" open="(" separator="," close=")">
#{billId}
</foreach>
group by bill_id
) f on f.bill_id = o.bill_id
set o.ar_bill_status = case
when f.bill_id is null then 'UNBILLED'
when f.unbilled_count > 0 and f.billed_count > 0 then 'PARTIAL'
when f.unbilled_count > 0 then 'UNBILLED'
when f.billed_count = f.fee_count then 'BILLED'
else 'UNBILLED'
end,
o.update_time = sysdate()
where o.bill_id in
<foreach collection="billIds" item="billId" open="(" separator="," close=")">
#{billId}
</foreach>
</update>
<update id="refreshArBillStatusByArBillId">
update prc_order_bill o
inner join (
select distinct d.prc_order_bill_id
from ar_bill_detail d
where d.ar_bill_id = #{arBillId}
) source_order on source_order.prc_order_bill_id = o.bill_id
left join (
select f.bill_id,
count(1) as fee_count,
sum(case when f.ar_bill_status = 'BILLED' then 1 else 0 end) as billed_count,
sum(case when f.ar_bill_status = 'UNBILLED' then 1 else 0 end) as unbilled_count
from prc_income_bill_fee f
inner join (
select distinct d2.prc_order_bill_id
from ar_bill_detail d2
where d2.ar_bill_id = #{arBillId}
) source_order2 on source_order2.prc_order_bill_id = f.bill_id
group by f.bill_id
) fee_summary on fee_summary.bill_id = o.bill_id
set o.ar_bill_status = case
when fee_summary.bill_id is null then 'UNBILLED'
when fee_summary.unbilled_count > 0 and fee_summary.billed_count > 0 then 'PARTIAL'
when fee_summary.unbilled_count > 0 then 'UNBILLED'
when fee_summary.billed_count = fee_summary.fee_count then 'BILLED'
else 'UNBILLED'
end,
o.update_time = sysdate()
</update>
</mapper>