Преглед на файлове

格式化insertPlatformBill的SQL

codingliang преди 1 година
родител
ревизия
de028a146b
променени са 1 файла, в които са добавени 138 реда и са изтрити 48 реда
  1. 138 48
      src/main/resources/mapper/reconciliation/PlatformBillMapper.xml

+ 138 - 48
src/main/resources/mapper/reconciliation/PlatformBillMapper.xml

@@ -138,54 +138,144 @@
     </update>
 
     <insert id="insertPlatformBill">
-        INSERT INTO `tcwm2.5`.platform_bill
-        (day_id, user_id, shop_name, `type`, start_money, revenue, revenue_count,
-        shop_payouts, shop_payouts_rates, shop_payouts_count, rider_payouts, rider_payouts_rates,
-        rider_payouts_count, refund_money, refund_count, end_money,sys_gift_amount, platform_rates,total_income,pay_money,create_time,update_time,shop_balance)
-        select #{dayId}, tu.user_id, null,2,0,ifnull(sum(ti.rider_money),0),count(ti.rider_money),null,null,null,
-        ifnull(sum(co.money),0),ifnull(sum(co.rate),0), count(co.money),null,null,ifnull(max(tu.balance),0),
-        ifnull((select sum(money) b from  user_money_details ud1 where ud1.user_id =tu.user_id and ud1.title like '系统赠送%'
-        and ud1.create_time>#{startTime} and ud1.create_time <![CDATA[ <= ]]>#{endTime})
-        -(select sum(money) a from  user_money_details ud2 where ud2.user_id =tu.user_id and ud2.title like '系统扣除%'
-        and ud2.create_time>#{startTime} and ud2.create_time <![CDATA[ <= ]]> #{endTime}),0) as gift_amount,null,
-        ifnull(sum(ti.rider_money),0),null,#{now},#{now},0
-        from tb_user tu USE INDEX(tb_user_rider_open_id_IDX)
-        left join tb_indent ti USE INDEX(tb_indent_finish_time_IDX) on
-        ti.finish_time >#{startTime}
-        and ti.finish_time <![CDATA[ <= ]]> #{endTime}
-        and ti.rider_user_id =tu.user_id
-        and ti.indent_state ='6'
-        left join cash_out co on co.user_id =tu.user_id and co.type =1 and co.state=1
-        and co.out_at >#{startTime}
-        and co.out_at <![CDATA[ <= ]]>#{endTime}
-        where tu.rider_open_id is not null
-        group by tu.user_id
-        union all
-        select #{dayId}, tu.user_id, null,1,0,ifnull(sum(tor.shop_income_money),0),count(tor.shop_income_money),
-               ifnull(sum(co.money),0),ifnull(sum(co.rate),0),count(co.money),null,null,null,ifnull(sum(to1.pay_money),0),
-               count(to1.pay_money),ifnull((select sum(to2.shop_income_money) from tb_order to2 where to2.status =4 and to2.shop_id=gs.shop_id),0),
-               ifnull((select sum(money) b from  user_money_details ud1 where ud1.user_id =tu.user_id and ud1.title like '系统赠送%'
-        and ud1.create_time>#{startTime} and ud1.create_time <![CDATA[ <= ]]>#{endTime})
-        -(select sum(money) a from  user_money_details ud2 where ud2.user_id =tu.user_id and ud2.title like '系统扣除%'
-        and ud2.create_time>#{startTime} and ud2.create_time <![CDATA[ <= ]]>#{endTime}),0) as gift_amount
-        ,ifnull(round(sum(tor.shop_income_money*(1-ifnull(gs.shop_rate,1))/ifnull(gs.shop_rate,1)),2),0) platform_rates,ifnull(sum(tor.shop_income_money),0),
-               ifnull(sum(tor.pay_money),0),#{now},#{now},ifnull(max(um.money),0)
-        from tb_user tu
-        left join goods_shop gs on tu.user_id =gs.user_id
-        left join tb_order  tor USE INDEX(tb_order_update_time_IDX) on gs.shop_id =tor.shop_id
-        and tor.update_time >#{startTime}
-        and tor.update_time <![CDATA[ <= ]]>#{endTime}
-        and tor.status=4
-        left join tb_order  to1 USE INDEX(tb_order_update_time_IDX) on gs.shop_id =to1.shop_id
-        and to1.update_time >#{startTime}
-        and to1.update_time <![CDATA[ <= ]]>#{endTime}
-        and to1.status in(5,8)
-        left join cash_out co on co.user_id =tu.user_id and co.type =2 and co.state=1
-        and co.out_at >#{startTime}
-        and co.out_at <![CDATA[ <= ]]>#{endTime}
-        left join user_money um on um.user_id =tu.user_id
-        where  tu.admin_user_id is not null
-        group by tu.user_id
+        INSERT INTO `tcwm2.5`.platform_bill (
+            day_id, user_id, shop_name, `type`, start_money, revenue, revenue_count,
+            shop_payouts, shop_payouts_rates, shop_payouts_count, rider_payouts, rider_payouts_rates,
+            rider_payouts_count, refund_money, refund_count, end_money, sys_gift_amount, platform_rates,
+            total_income, pay_money, create_time, update_time, shop_balance
+        )
+        -- 第一部分:骑手数据(type=2)
+        SELECT
+            #{dayId},
+            tu.user_id,
+            NULL,
+            2,
+            0,
+            IFNULL(SUM(ti.rider_money), 0),      -- 骑手总收入(revenue)
+            COUNT(ti.rider_money),               -- 收入订单数(revenue_count)
+            NULL, NULL, NULL,                    -- 商户提现字段(未使用)
+            IFNULL(SUM(co.money), 0),            -- 骑手提现金额(rider_payouts)
+            IFNULL(SUM(co.rate), 0),             -- 骑手提现手续费(rider_payouts_rates)
+            COUNT(co.id),                     -- 骑手提现次数(rider_payouts_count)
+            NULL, NULL,                          -- 退款字段(未使用)
+            IFNULL(MAX(tu.balance), 0),          -- 期末余额(end_money)
+            -- 系统赠送金额 - 系统扣除金额(sys_gift_amount):
+            IFNULL(
+                (
+                    SELECT
+                        SUM(money)
+                    FROM
+                        user_money_details ud1
+                    WHERE
+                        ud1.user_id = tu.user_id
+                        AND ud1.title LIKE '系统赠送%'
+                        AND ud1.create_time > #{startTime}
+                        AND ud1.create_time <![CDATA[ <= ]]> #{endTime}
+                )
+                -
+                (
+                    SELECT
+                        SUM(money)
+                    FROM
+                        user_money_details ud2
+                    WHERE
+                        ud2.user_id = tu.user_id
+                        AND ud2.title LIKE '系统扣除%'
+                        AND ud2.create_time > #{startTime}
+                        AND ud2.create_time <![CDATA[ <= ]]> #{endTime}
+                )
+                , 0
+            ) AS gift_amount,
+            NULL,                                -- 平台抽成(未使用)
+            IFNULL(SUM(ti.rider_money), 0),      -- 总收益(total_income)
+            NULL,                                -- 用户支付金额(未使用)
+            #{now}, #{now},                      -- 创建和更新时间
+            0                                    -- 商户余额(shop_balance)
+        FROM
+        tb_user tu USE INDEX (tb_user_rider_open_id_IDX)
+            LEFT JOIN tb_indent ti USE INDEX (tb_indent_finish_time_IDX) ON ti.finish_time > #{startTime} AND ti.finish_time <![CDATA[ <= ]]> #{endTime} AND ti.rider_user_id = tu.user_id AND ti.indent_state = '6'        -- 订单状态:已完成
+            LEFT JOIN cash_out co ON co.user_id = tu.user_id AND co.type = 1 AND co.state = 1 AND co.out_at > #{startTime} AND co.out_at <![CDATA[ <= ]]> #{endTime}
+        WHERE
+            tu.rider_open_id IS NOT NULL         -- 筛选骑手用户
+        GROUP BY
+            tu.user_id
+        UNION ALL
+        -- 第二部分:商家数据(type=1)
+        SELECT
+            #{dayId},
+            tu.user_id,
+            NULL,
+            1,
+            0,
+            IFNULL(SUM(tor.shop_income_money), 0),   -- 商家总收入(revenue)
+            COUNT(tor.shop_income_money),            -- 收入订单数(revenue_count)
+            IFNULL(SUM(co.money), 0),               -- 商家提现金额(shop_payouts)
+            IFNULL(SUM(co.rate), 0),                -- 商家提现手续费(shop_payouts_rates)
+            COUNT(co.id),                        -- 商家提现次数(shop_payouts_count)
+            NULL, NULL, NULL,                       -- 骑手提现字段(未使用)
+            IFNULL(SUM(to1.pay_money), 0),          -- 退款金额(refund_money)
+            COUNT(to1.pay_money),                   -- 退款次数(refund_count)
+            -- 未结算订单金额(end_money):
+            IFNULL(
+                (
+                    SELECT
+                        SUM(to2.shop_income_money)
+                    FROM
+                        tb_order to2
+                    WHERE
+                        to2.status = 4
+                        AND to2.shop_id = gs.shop_id
+                )
+            , 0),
+            -- 系统赠送金额 - 系统扣除金额(sys_gift_amount):
+            IFNULL(
+                (
+                    SELECT
+                        SUM(money)
+                    FROM
+                        user_money_details ud1
+                    WHERE
+                        ud1.user_id = tu.user_id
+                        AND ud1.title LIKE '系统赠送%'
+                        AND ud1.create_time > #{startTime}
+                        AND ud1.create_time <![CDATA[ <= ]]> #{endTime}
+                )
+                -
+                (
+                    SELECT
+                        SUM(money)
+                    FROM
+                        user_money_details ud2
+                    WHERE
+                        ud2.user_id = tu.user_id
+                        AND ud2.title LIKE '系统扣除%'
+                        AND ud2.create_time > #{startTime}
+                        AND ud2.create_time <![CDATA[ <= ]]> #{endTime}
+                )
+            , 0) AS gift_amount,
+            -- 平台抽成金额(platform_rates):
+            IFNULL(ROUND(SUM(tor.shop_income_money * (1 - IFNULL(gs.shop_rate, 1)) / IFNULL(gs.shop_rate, 1)), 2), 0),
+            IFNULL(SUM(tor.shop_income_money), 0),   -- 总收益(total_income)
+            IFNULL(SUM(tor.pay_money), 0),           -- 用户支付金额(pay_money)
+            #{now},
+            #{now},                         -- 创建和更新时间
+            IFNULL(MAX(um.money), 0)                -- 商户余额(shop_balance)
+        FROM
+            tb_user tu
+            LEFT JOIN
+                goods_shop gs ON tu.user_id = gs.user_id          -- 关联商家店铺
+            LEFT JOIN
+                tb_order tor USE INDEX (tb_order_update_time_IDX) ON gs.shop_id = tor.shop_id AND tor.update_time > #{startTime} AND tor.update_time <![CDATA[ <= ]]> #{endTime} AND tor.status = 4                  -- 订单状态:已完成
+            LEFT JOIN
+                    tb_order to1 USE INDEX (tb_order_update_time_IDX) ON gs.shop_id = to1.shop_id AND to1.update_time > #{startTime} AND to1.update_time <![CDATA[ <= ]]> #{endTime} AND to1.status IN (5, 8)            -- 订单状态:退款/异常
+            LEFT JOIN
+                    cash_out co ON co.user_id = tu.user_id AND co.type = 2 AND co.state = 1 AND co.out_at > #{startTime} AND co.out_at <![CDATA[ <= ]]> #{endTime}
+            LEFT JOIN
+                    user_money um ON um.user_id = tu.user_id          -- 用户余额表
+        WHERE
+            tu.admin_user_id IS NOT NULL            -- 筛选商家用户
+        GROUP BY
+            tu.user_id;
     </insert>
 
     <insert id="insertTotalPlatformBill">