优化供应商和客户对账单的查询逻辑,兼容高版本sql

This commit is contained in:
jishenghua
2025-03-08 23:05:36 +08:00
parent c5ab90ed7a
commit b26ac076de

View File

@@ -815,7 +815,7 @@
</select> </select>
<select id="getStatementAccount" resultType="com.jsh.erp.datasource.vo.DepotHeadVo4StatementAccount"> <select id="getStatementAccount" resultType="com.jsh.erp.datasource.vo.DepotHeadVo4StatementAccount">
select * from select *, (preBackInMoney+preBackOutMoney) as preBackMoney, (backInMoney+backOutMoney) as backMoney from
(select s.id, s.supplier, s.contacts, s.telephone, s.phone_num, s.email, (select s.id, s.supplier, s.contacts, s.telephone, s.phone_num, s.email,
-- 起始期初金额 -- 起始期初金额
(case when s.type='供应商' then ifnull(s.begin_need_pay,0) else ifnull(s.begin_need_get,0) end) begin_need, (case when s.type='供应商' then ifnull(s.begin_need_pay,0) else ifnull(s.begin_need_get,0) end) begin_need,
@@ -835,12 +835,18 @@
and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack} and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack}
and dh.oper_time &lt;= #{beginTime} and dh.oper_time &lt;= #{beginTime}
and ifnull(dh.delete_flag,'0') !='1') preReturnDebtMoney, and ifnull(dh.delete_flag,'0') !='1') preReturnDebtMoney,
-- 上期收 -- 上期收款
(select (case when ah.type='收款' then ifnull(sum(ai.each_amount),0) else 0-ifnull(sum(ai.each_amount),0) end) from jsh_account_item ai (select ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1' left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type=#{billType} where ah.organ_id=s.id and ah.type='收款'
and ah.bill_time &lt;= #{beginTime} and ah.bill_time &lt;= #{beginTime}
and ifnull(ai.delete_flag,'0') !='1') preBackMoney, and ifnull(ai.delete_flag,'0') !='1') preBackInMoney,
-- 上期付款
(select 0-ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type='付款'
and ah.bill_time &lt;= #{beginTime}
and ifnull(ai.delete_flag,'0') !='1') preBackOutMoney,
-- 本期欠款 -- 本期欠款
(select (select
ifnull(sum(dh.discount_last_money),0)+ifnull(sum(dh.other_money),0)-ifnull(sum(dh.deposit),0)-abs(ifnull(sum(dh.change_amount),0)) ifnull(sum(dh.discount_last_money),0)+ifnull(sum(dh.other_money),0)-ifnull(sum(dh.deposit),0)-abs(ifnull(sum(dh.change_amount),0))
@@ -857,12 +863,18 @@
and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack} and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack}
and dh.oper_time>#{beginTime} and dh.oper_time &lt;= #{endTime} and dh.oper_time>#{beginTime} and dh.oper_time &lt;= #{endTime}
and ifnull(dh.delete_flag,'0') !='1') returnDebtMoney, and ifnull(dh.delete_flag,'0') !='1') returnDebtMoney,
-- 本期收 -- 本期收款
(select (case when ah.type='收款' then ifnull(sum(ai.each_amount),0) else 0-ifnull(sum(ai.each_amount),0) end) from jsh_account_item ai (select ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1' left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type=#{billType} where ah.organ_id=s.id and ah.type='收款'
and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime} and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime}
and ifnull(ai.delete_flag,'0') !='1') backMoney and ifnull(ai.delete_flag,'0') !='1') backInMoney,
-- 本期付款
(select 0-ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type='付款'
and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime}
and ifnull(ai.delete_flag,'0') !='1') backOutMoney
from jsh_supplier s from jsh_supplier s
where s.enabled=1 where s.enabled=1
and s.type=#{supplierType} and s.type=#{supplierType}
@@ -878,12 +890,12 @@
</if> </if>
and ifnull(s.delete_flag,'0') !='1') tb and ifnull(s.delete_flag,'0') !='1') tb
<if test="hasDebt == 1"> <if test="hasDebt == 1">
where begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney!=0 where begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney!=0
</if> </if>
<if test="hasDebt == 0"> <if test="hasDebt == 0">
where begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney=0 where begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney=0
</if> </if>
order by begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney desc order by begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney desc
<if test="offset != null and rows != null"> <if test="offset != null and rows != null">
limit #{offset},#{rows} limit #{offset},#{rows}
</if> </if>
@@ -910,12 +922,18 @@
and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack} and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack}
and dh.oper_time &lt;= #{beginTime} and dh.oper_time &lt;= #{beginTime}
and ifnull(dh.delete_flag,'0') !='1') preReturnDebtMoney, and ifnull(dh.delete_flag,'0') !='1') preReturnDebtMoney,
-- 上期收 -- 上期收款
(select (case when ah.type='收款' then ifnull(sum(ai.each_amount),0) else 0-ifnull(sum(ai.each_amount),0) end) from jsh_account_item ai (select ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1' left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type=#{billType} where ah.organ_id=s.id and ah.type='收款'
and ah.bill_time &lt;= #{beginTime} and ah.bill_time &lt;= #{beginTime}
and ifnull(ai.delete_flag,'0') !='1') preBackMoney, and ifnull(ai.delete_flag,'0') !='1') preBackInMoney,
-- 上期付款
(select 0-ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type='付款'
and ah.bill_time &lt;= #{beginTime}
and ifnull(ai.delete_flag,'0') !='1') preBackOutMoney,
-- 本期欠款 -- 本期欠款
(select (select
ifnull(sum(dh.discount_last_money),0)+ifnull(sum(dh.other_money),0)-ifnull(sum(dh.deposit),0)-abs(ifnull(sum(dh.change_amount),0)) ifnull(sum(dh.discount_last_money),0)+ifnull(sum(dh.other_money),0)-ifnull(sum(dh.deposit),0)-abs(ifnull(sum(dh.change_amount),0))
@@ -932,12 +950,18 @@
and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack} and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack}
and dh.oper_time>#{beginTime} and dh.oper_time &lt;= #{endTime} and dh.oper_time>#{beginTime} and dh.oper_time &lt;= #{endTime}
and ifnull(dh.delete_flag,'0') !='1') returnDebtMoney, and ifnull(dh.delete_flag,'0') !='1') returnDebtMoney,
-- 本期收 -- 本期收款
(select (case when ah.type='收款' then ifnull(sum(ai.each_amount),0) else 0-ifnull(sum(ai.each_amount),0) end) from jsh_account_item ai (select ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1' left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type=#{billType} where ah.organ_id=s.id and ah.type='收款'
and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime} and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime}
and ifnull(ai.delete_flag,'0') !='1') backMoney and ifnull(ai.delete_flag,'0') !='1') backInMoney,
-- 本期付款
(select 0-ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type='付款'
and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime}
and ifnull(ai.delete_flag,'0') !='1') backOutMoney
from jsh_supplier s from jsh_supplier s
where s.enabled=1 where s.enabled=1
and s.type=#{supplierType} and s.type=#{supplierType}
@@ -953,16 +977,16 @@
</if> </if>
and ifnull(s.delete_flag,'0') !='1') tb and ifnull(s.delete_flag,'0') !='1') tb
<if test="hasDebt == 1"> <if test="hasDebt == 1">
where begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney!=0 where begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney!=0
</if> </if>
<if test="hasDebt == 0"> <if test="hasDebt == 0">
where begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney=0 where begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney=0
</if> </if>
</select> </select>
<select id="getStatementAccountTotalPay" resultType="com.jsh.erp.datasource.vo.DepotHeadVo4StatementAccount"> <select id="getStatementAccountTotalPay" resultType="com.jsh.erp.datasource.vo.DepotHeadVo4StatementAccount">
select sum(begin_need) begin_need, sum(preDebtMoney) preDebtMoney, sum(preReturnDebtMoney) preReturnDebtMoney, sum(preBackMoney) preBackMoney, select sum(begin_need) begin_need, sum(preDebtMoney) preDebtMoney, sum(preReturnDebtMoney) preReturnDebtMoney, sum(preBackInMoney+preBackOutMoney) preBackMoney,
sum(debtMoney) debtMoney, sum(returnDebtMoney) returnDebtMoney, sum(backMoney) backMoney from sum(debtMoney) debtMoney, sum(returnDebtMoney) returnDebtMoney, sum(backInMoney+backOutMoney) backMoney from
(select s.id, (select s.id,
(case when s.type='供应商' then ifnull(s.begin_need_pay,0) else ifnull(s.begin_need_get,0) end) begin_need, (case when s.type='供应商' then ifnull(s.begin_need_pay,0) else ifnull(s.begin_need_get,0) end) begin_need,
(select (select
@@ -979,11 +1003,16 @@
and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack} and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack}
and dh.oper_time &lt;= #{beginTime} and dh.oper_time &lt;= #{beginTime}
and ifnull(dh.delete_flag,'0') !='1') preReturnDebtMoney, and ifnull(dh.delete_flag,'0') !='1') preReturnDebtMoney,
(select (case when ah.type='收款' then ifnull(sum(ai.each_amount),0) else 0-ifnull(sum(ai.each_amount),0) end) from jsh_account_item ai (select ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1' left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type=#{billType} where ah.organ_id=s.id and ah.type='收款'
and ah.bill_time &lt;= #{beginTime} and ah.bill_time &lt;= #{beginTime}
and ifnull(ai.delete_flag,'0') !='1') preBackMoney, and ifnull(ai.delete_flag,'0') !='1') preBackInMoney,
(select 0-ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type='付款'
and ah.bill_time &lt;= #{beginTime}
and ifnull(ai.delete_flag,'0') !='1') preBackOutMoney,
(select (select
ifnull(sum(dh.discount_last_money),0)+ifnull(sum(dh.other_money),0)-ifnull(sum(dh.deposit),0)-abs(ifnull(sum(dh.change_amount),0)) ifnull(sum(dh.discount_last_money),0)+ifnull(sum(dh.other_money),0)-ifnull(sum(dh.deposit),0)-abs(ifnull(sum(dh.change_amount),0))
from jsh_depot_head dh from jsh_depot_head dh
@@ -998,11 +1027,16 @@
and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack} and dh.type=#{typeBack} and dh.sub_type=#{subTypeBack}
and dh.oper_time>#{beginTime} and dh.oper_time &lt;= #{endTime} and dh.oper_time>#{beginTime} and dh.oper_time &lt;= #{endTime}
and ifnull(dh.delete_flag,'0') !='1') returnDebtMoney, and ifnull(dh.delete_flag,'0') !='1') returnDebtMoney,
(select (case when ah.type='收款' then ifnull(sum(ai.each_amount),0) else 0-ifnull(sum(ai.each_amount),0) end) from jsh_account_item ai (select ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1' left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type=#{billType} where ah.organ_id=s.id and ah.type='收款'
and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime} and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime}
and ifnull(ai.delete_flag,'0') !='1') backMoney and ifnull(ai.delete_flag,'0') !='1') backInMoney,
(select 0-ifnull(sum(ai.each_amount),0) from jsh_account_item ai
left join jsh_account_head ah on ai.header_id=ah.id and ifnull(ah.delete_flag,'0') !='1'
where ah.organ_id=s.id and ah.type='付款'
and ah.bill_time>#{beginTime} and ah.bill_time &lt;= #{endTime}
and ifnull(ai.delete_flag,'0') !='1') backOutMoney
from jsh_supplier s from jsh_supplier s
where s.enabled=1 where s.enabled=1
and s.type=#{supplierType} and s.type=#{supplierType}
@@ -1018,10 +1052,10 @@
</if> </if>
and ifnull(s.delete_flag,'0') !='1') tb and ifnull(s.delete_flag,'0') !='1') tb
<if test="hasDebt == 1"> <if test="hasDebt == 1">
where begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney!=0 where begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney!=0
</if> </if>
<if test="hasDebt == 0"> <if test="hasDebt == 0">
where begin_need+preDebtMoney-preReturnDebtMoney-preBackMoney+debtMoney-returnDebtMoney-backMoney=0 where begin_need+preDebtMoney-preReturnDebtMoney-preBackInMoney-preBackOutMoney+debtMoney-returnDebtMoney-backInMoney-backOutMoney=0
</if> </if>
</select> </select>