332 lines
18 KiB
XML
332 lines
18 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.price.mapper.PrcProfitReportMapper">
|
|
|
|
<resultMap type="PrcProfitReportExport" id="PrcProfitReportExportResult">
|
|
<id property="exportId" column="export_id"/>
|
|
<result property="dimension" column="dimension"/>
|
|
<result property="fileName" column="file_name"/>
|
|
<result property="storageKey" column="storage_key"/>
|
|
<result property="rowCount" column="row_count"/>
|
|
<result property="exportStatus" column="export_status"/>
|
|
<result property="exportUserId" column="export_user_id"/>
|
|
<result property="exportBy" column="export_by"/>
|
|
<result property="exportTime" column="export_time"/>
|
|
<result property="remark" column="remark"/>
|
|
</resultMap>
|
|
|
|
<sql id="profitReportExportColumns">
|
|
select export_id, dimension, file_name, storage_key, row_count,
|
|
export_status, export_user_id, export_by, export_time, remark
|
|
from prc_profit_report_export
|
|
</sql>
|
|
|
|
<insert id="insertExport" parameterType="PrcProfitReportExport" useGeneratedKeys="true" keyProperty="exportId">
|
|
insert into prc_profit_report_export (
|
|
dimension, file_name, storage_key, row_count, export_status, export_user_id, export_by, export_time, remark
|
|
) values (
|
|
#{dimension}, #{fileName}, #{storageKey}, #{rowCount}, #{exportStatus}, #{exportUserId}, #{exportBy}, sysdate(), #{remark}
|
|
)
|
|
</insert>
|
|
|
|
<update id="markExportSuccess" parameterType="PrcProfitReportExport">
|
|
update prc_profit_report_export
|
|
set file_name = #{fileName}, storage_key = #{storageKey}, row_count = #{rowCount},
|
|
export_status = 'SUCCESS', remark = null
|
|
where export_id = #{exportId} and export_status = 'PROCESSING'
|
|
</update>
|
|
|
|
<update id="markExportFailed">
|
|
update prc_profit_report_export
|
|
set export_status = 'FAILED', remark = #{remark}
|
|
where export_id = #{exportId} and export_status = 'PROCESSING'
|
|
</update>
|
|
|
|
<select id="selectExports" resultMap="PrcProfitReportExportResult">
|
|
<include refid="profitReportExportColumns"/>
|
|
order by export_time desc, export_id desc
|
|
</select>
|
|
|
|
<select id="selectExportById" resultMap="PrcProfitReportExportResult">
|
|
<include refid="profitReportExportColumns"/>
|
|
where export_id = #{exportId}
|
|
</select>
|
|
|
|
<resultMap type="PrcProfitReportOrderRow" id="PrcProfitReportOrderResult">
|
|
<result property="orderNo" column="order_no"/>
|
|
<result property="customerCode" column="customer_code"/>
|
|
<result property="customerName" column="customer_name"/>
|
|
<result property="belongSaler" column="belong_saler"/>
|
|
<result property="belongSalerCode" column="belong_saler_code"/>
|
|
<result property="productCode" column="product_code"/>
|
|
<result property="productName" column="product_name"/>
|
|
<result property="countryCode" column="country_code"/>
|
|
<result property="countryName" column="country_name"/>
|
|
<result property="currency" column="currency"/>
|
|
<result property="bizDate" column="biz_date"/>
|
|
<result property="costAmount" column="cost_amount"/>
|
|
<result property="redlineAmount" column="redline_amount"/>
|
|
<result property="redlineGrossProfit" column="redline_gross_profit"/>
|
|
<result property="redlineGrossMarginRate" column="redline_gross_margin_rate"/>
|
|
<result property="salesAmount" column="sales_amount"/>
|
|
<result property="salesGrossProfit" column="sales_gross_profit"/>
|
|
<result property="salesGrossMarginRate" column="sales_gross_margin_rate"/>
|
|
<!-- 提成结果字段暂停使用。
|
|
<result property="commissionRate" column="commission_rate"/>
|
|
<result property="commissionAmount" column="commission_amount"/>
|
|
-->
|
|
<result property="arBillStatus" column="ar_bill_status"/>
|
|
</resultMap>
|
|
|
|
<resultMap type="PrcProfitReportProductRow" id="PrcProfitReportProductResult">
|
|
<result property="productCode" column="product_code"/>
|
|
<result property="productName" column="product_name"/>
|
|
<result property="countryCode" column="country_code"/>
|
|
<result property="countryName" column="country_name"/>
|
|
<result property="currency" column="currency"/>
|
|
<result property="orderCount" column="order_count"/>
|
|
<result property="costAmount" column="cost_amount"/>
|
|
<result property="redlineAmount" column="redline_amount"/>
|
|
<result property="redlineGrossProfit" column="redline_gross_profit"/>
|
|
<result property="redlineGrossMarginRate" column="redline_gross_margin_rate"/>
|
|
<result property="salesAmount" column="sales_amount"/>
|
|
<result property="salesGrossProfit" column="sales_gross_profit"/>
|
|
<result property="salesGrossMarginRate" column="sales_gross_margin_rate"/>
|
|
<!-- 提成结果字段暂停使用。
|
|
<result property="commissionAmount" column="commission_amount"/>
|
|
<result property="commissionMissingOrderCount" column="commission_missing_order_count"/>
|
|
-->
|
|
</resultMap>
|
|
|
|
<resultMap type="PrcProfitReportSummary" id="PrcProfitReportSummaryResult">
|
|
<result property="orderCount" column="order_count"/>
|
|
<result property="currency" column="currency"/>
|
|
<result property="costAmount" column="cost_amount"/>
|
|
<result property="redlineAmount" column="redline_amount"/>
|
|
<result property="redlineGrossProfit" column="redline_gross_profit"/>
|
|
<result property="redlineGrossMarginRate" column="redline_gross_margin_rate"/>
|
|
<result property="salesAmount" column="sales_amount"/>
|
|
<result property="salesGrossProfit" column="sales_gross_profit"/>
|
|
<result property="salesGrossMarginRate" column="sales_gross_margin_rate"/>
|
|
<!-- 提成汇总字段暂停使用。
|
|
<result property="commissionAmount" column="commission_amount"/>
|
|
<result property="commissionMissingOrderCount" column="commission_missing_order_count"/>
|
|
-->
|
|
</resultMap>
|
|
|
|
<sql id="profitReportFromAndWhere">
|
|
from prc_income_bill i
|
|
inner join prc_order_bill p on p.bill_id = i.bill_id
|
|
<if test="customerArea != null and customerArea != ''">
|
|
left join ctr_customer c on c.customer_id = p.customer_id and c.del_flag = '0'
|
|
</if>
|
|
<!-- 提成表关联暂停使用,毛利报表不再依赖提成配置。
|
|
left join prc_profit_commission_rate cr
|
|
on cr.belong_saler_code = i.belong_saler_code and cr.status = '0'
|
|
-->
|
|
<where>
|
|
<!-- 不将未匹配产品、国家或重量段的计费失败订单纳入毛利报表;保留历史空值(按成功处理)。 -->
|
|
and (p.billing_fail_status is null or p.billing_fail_status not in (1, 2, 3))
|
|
<if test="customerArea != null and customerArea != ''">
|
|
and c.customer_area = #{customerArea}
|
|
</if>
|
|
<if test="beginBizDate != null">
|
|
and p.biz_date >= #{beginBizDate}
|
|
</if>
|
|
<if test="endBizDate != null">
|
|
and p.biz_date <= #{endBizDate}
|
|
</if>
|
|
<choose>
|
|
<when test="orderNos != null and orderNos.size() > 0">
|
|
and (
|
|
<foreach collection="orderNos" item="orderValue" separator=" or ">
|
|
p.order_no like concat('%', #{orderValue}, '%')
|
|
</foreach>
|
|
)
|
|
</when>
|
|
<when test="orderNo != null and orderNo != ''">
|
|
and p.order_no like concat('%', #{orderNo}, '%')
|
|
</when>
|
|
</choose>
|
|
<choose>
|
|
<when test="customerCodes != null and customerCodes.size() > 0">
|
|
and p.customer_code in
|
|
<foreach collection="customerCodes" item="customerValue" open="(" separator="," close=")">
|
|
#{customerValue}
|
|
</foreach>
|
|
</when>
|
|
<when test="customerCode != null and customerCode != ''">
|
|
and p.customer_code = #{customerCode}
|
|
</when>
|
|
</choose>
|
|
<choose>
|
|
<when test="customerNames != null and customerNames.size() > 0">
|
|
and (
|
|
<foreach collection="customerNames" item="customerValue" separator=" or ">
|
|
p.customer_name like concat('%', #{customerValue}, '%')
|
|
</foreach>
|
|
)
|
|
</when>
|
|
<when test="customerName != null and customerName != ''">
|
|
and p.customer_name like concat('%', #{customerName}, '%')
|
|
</when>
|
|
</choose>
|
|
<choose>
|
|
<when test="productDrilldown != null and productDrilldown">
|
|
and p.product_code <=> #{productCode}
|
|
and p.country_code <=> #{countryCode}
|
|
and i.currency <=> #{currency}
|
|
<if test="belongSalerCode != null and belongSalerCode != ''">
|
|
and i.belong_saler_code = #{belongSalerCode}
|
|
</if>
|
|
</when>
|
|
<otherwise>
|
|
<if test="belongSalerCode != null and belongSalerCode != ''">
|
|
and i.belong_saler_code = #{belongSalerCode}
|
|
</if>
|
|
<if test="productCode != null and productCode != ''">
|
|
and p.product_code = #{productCode}
|
|
</if>
|
|
<if test="countryCode != null and countryCode != ''">
|
|
and p.country_code = #{countryCode}
|
|
</if>
|
|
<if test="currency != null and currency != ''">
|
|
and i.currency = #{currency}
|
|
</if>
|
|
</otherwise>
|
|
</choose>
|
|
<if test="productName != null and productName != ''">
|
|
and p.product_name like concat('%', #{productName}, '%')
|
|
</if>
|
|
<if test="countryName != null and countryName != ''">
|
|
and p.country_name like concat('%', #{countryName}, '%')
|
|
</if>
|
|
</where>
|
|
</sql>
|
|
|
|
<!-- 广州缺少有效销售价或红线价时不计算毛利;计费失败留下的双零占位也不参与计算。 -->
|
|
<sql id="guangzhouProfitAvailable">
|
|
i.sales_amount is not null and i.redline_amount is not null
|
|
and not (coalesce(p.billing_fail_status, 0) in (23, 24, 25) and i.sales_amount = 0 and i.redline_amount = 0)
|
|
</sql>
|
|
|
|
<sql id="profitExpressions">
|
|
<choose>
|
|
<when test="profitRule == 'GUANGZHOU'">
|
|
null as cost_amount, i.redline_amount,
|
|
null as redline_gross_profit, null as redline_gross_margin_rate,
|
|
i.sales_amount,
|
|
case when <include refid="guangzhouProfitAvailable"/>
|
|
then i.sales_amount - i.redline_amount else null end as sales_gross_profit,
|
|
case when <include refid="guangzhouProfitAvailable"/> and i.sales_amount != 0
|
|
then round((i.sales_amount - i.redline_amount) * 100 / i.sales_amount, 4)
|
|
else null end as sales_gross_margin_rate
|
|
</when>
|
|
<otherwise>
|
|
i.product_amount as cost_amount,
|
|
i.redline_amount,
|
|
case when i.product_amount is not null and i.redline_amount is not null
|
|
then i.product_amount - i.redline_amount else null end as redline_gross_profit,
|
|
case when i.product_amount is not null and i.redline_amount is not null and i.redline_amount != 0
|
|
then round((i.product_amount - i.redline_amount) * 100 / i.redline_amount, 4) else null end as redline_gross_margin_rate,
|
|
i.sales_amount,
|
|
case when i.product_amount is not null and i.sales_amount is not null
|
|
then i.sales_amount - i.product_amount else null end as sales_gross_profit,
|
|
case when i.product_amount is not null and i.sales_amount is not null and i.sales_amount != 0
|
|
then round((i.sales_amount - i.product_amount) * 100 / i.sales_amount, 4) else null end as sales_gross_margin_rate
|
|
</otherwise>
|
|
</choose>
|
|
</sql>
|
|
|
|
<select id="selectOrderRows" resultMap="PrcProfitReportOrderResult">
|
|
select p.order_no, p.customer_code, p.customer_name,
|
|
i.belong_saler, i.belong_saler_code,
|
|
p.product_code, p.product_name, p.country_code, p.country_name,
|
|
i.currency, p.biz_date,
|
|
<include refid="profitExpressions"/>,
|
|
p.ar_bill_status
|
|
<include refid="profitReportFromAndWhere"/>
|
|
<if test="arBillStatus != null and arBillStatus != '' and productDrilldown != true">
|
|
and p.ar_bill_status = #{arBillStatus}
|
|
</if>
|
|
order by p.biz_date desc, p.bill_id desc
|
|
</select>
|
|
|
|
<!-- 汇总和产品维度使用同一套逐单有效数据计算,毛利率按有效销售额加权。 -->
|
|
<sql id="profitAggregateExpressions">
|
|
<choose>
|
|
<when test="profitRule == 'GUANGZHOU'">
|
|
null as cost_amount,
|
|
sum(i.redline_amount) as redline_amount,
|
|
null as redline_gross_profit, null as redline_gross_margin_rate,
|
|
sum(i.sales_amount) as sales_amount,
|
|
sum(case when <include refid="guangzhouProfitAvailable"/>
|
|
then i.sales_amount - i.redline_amount else null end) as sales_gross_profit,
|
|
case when sum(case when <include refid="guangzhouProfitAvailable"/>
|
|
then i.sales_amount else null end) is null
|
|
or sum(case when <include refid="guangzhouProfitAvailable"/>
|
|
then i.sales_amount else null end) = 0
|
|
then null
|
|
else round(sum(case when <include refid="guangzhouProfitAvailable"/>
|
|
then i.sales_amount - i.redline_amount else null end) * 100 /
|
|
sum(case when <include refid="guangzhouProfitAvailable"/>
|
|
then i.sales_amount else null end), 4)
|
|
end as sales_gross_margin_rate
|
|
</when>
|
|
<otherwise>
|
|
sum(i.product_amount) as cost_amount,
|
|
sum(i.redline_amount) as redline_amount,
|
|
sum(case when i.product_amount is not null and i.redline_amount is not null
|
|
then i.product_amount - i.redline_amount else null end) as redline_gross_profit,
|
|
case when sum(case when i.product_amount is not null and i.redline_amount is not null
|
|
then i.redline_amount else null end) is null
|
|
or sum(case when i.product_amount is not null and i.redline_amount is not null
|
|
then i.redline_amount else null end) = 0
|
|
then null
|
|
else round(
|
|
sum(case when i.product_amount is not null and i.redline_amount is not null
|
|
then i.product_amount - i.redline_amount else null end) * 100 /
|
|
sum(case when i.product_amount is not null and i.redline_amount is not null
|
|
then i.redline_amount else null end), 4)
|
|
end as redline_gross_margin_rate,
|
|
sum(i.sales_amount) as sales_amount,
|
|
sum(case when i.product_amount is not null and i.sales_amount is not null
|
|
then i.sales_amount - i.product_amount else null end) as sales_gross_profit,
|
|
case when sum(case when i.product_amount is not null and i.sales_amount is not null
|
|
then i.sales_amount else null end) is null
|
|
or sum(case when i.product_amount is not null and i.sales_amount is not null
|
|
then i.sales_amount else null end) = 0
|
|
then null
|
|
else round(
|
|
sum(case when i.product_amount is not null and i.sales_amount is not null
|
|
then i.sales_amount - i.product_amount else null end) * 100 /
|
|
sum(case when i.product_amount is not null and i.sales_amount is not null
|
|
then i.sales_amount else null end), 4)
|
|
end as sales_gross_margin_rate
|
|
</otherwise>
|
|
</choose>
|
|
</sql>
|
|
|
|
<select id="selectProductRows" resultMap="PrcProfitReportProductResult">
|
|
select p.product_code, p.product_name, p.country_code, p.country_name,
|
|
i.currency, count(*) as order_count,
|
|
<include refid="profitAggregateExpressions"/>
|
|
<include refid="profitReportFromAndWhere"/>
|
|
group by p.product_code, p.product_name, p.country_code, p.country_name, i.currency
|
|
order by p.product_code, p.country_code, i.currency
|
|
</select>
|
|
|
|
<select id="selectSummary" resultMap="PrcProfitReportSummaryResult">
|
|
select i.currency, count(*) as order_count,
|
|
<include refid="profitAggregateExpressions"/>
|
|
<include refid="profitReportFromAndWhere"/>
|
|
<if test="arBillStatus != null and arBillStatus != ''">
|
|
and p.ar_bill_status = #{arBillStatus}
|
|
</if>
|
|
group by i.currency
|
|
order by i.currency
|
|
</select>
|
|
</mapper>
|