PlatformBillMapper.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.sqx.modules.reconciliation.mapper.PlatformBillMapper">
  4. <select id="riderBill" resultType="com.sqx.modules.reconciliation.model.RiderBillVo">
  5. select pb.*,tu.user_name as userName,tu.phone ,rs.station_name as stationName
  6. from platform_bill pb left join tb_user tu on pb.user_id =tu.user_id
  7. left join rider_station rs on rs.id =tu.rider_station_id
  8. <where>
  9. pb.`type` =2
  10. <if test="params.startDate != null and params.startDate != '' and params.startDate != 'null'">
  11. and pb.day_id >= #{params.startDate}
  12. </if>
  13. <if test="params.endDate != null and params.endDate != '' and params.endDate != 'null'">
  14. and pb.day_id <![CDATA[ <= ]]> #{params.endDate}
  15. </if>
  16. <if test="params.riderStation != null and params.riderStation != ''">
  17. and rs.id = #{params.riderStation}
  18. </if>
  19. <if test="params.riderPhone != null and params.riderPhone != '' and params.riderPhone != 'null'">
  20. and tu.phone like concat('%',#{params.riderPhone},'%')
  21. </if>
  22. <if test="params.riderName != null and params.riderName != '' and params.riderName != 'null'">
  23. and tu.user_name like concat('%',#{params.riderName},'%')
  24. </if>
  25. </where>
  26. order by pb.dayId desc
  27. </select>
  28. <select id="shopBill" resultType="com.sqx.modules.reconciliation.model.ShopBillVo">
  29. select pb.*,gs.shop_name as shopName,gs.phone,gs.shop_id as shopId
  30. from platform_bill pb left join tb_user tu on pb.user_id =tu.user_id
  31. left join goods_shop gs on gs.user_id =tu.user_id
  32. <where>
  33. pb.`type` = 1
  34. <if test="params.startDate != null and params.startDate != '' and params.startDate != 'null'">
  35. and pb.day_id >= #{params.startDate}
  36. </if>
  37. <if test="params.endDate != null and params.endDate != '' and params.endDate != 'null'">
  38. and pb.day_id <![CDATA[ <= ]]> #{params.endDate}
  39. </if>
  40. <if test="params.shopPhone != null and params.shopPhone != '' and params.shopPhone != 'null'">
  41. and gs.phone like concat('%',#{params.shopPhone},'%')
  42. </if>
  43. <if test="params.shopName != null and params.shopName != '' and params.shopName != 'null'">
  44. and gs.shop_name like concat('%',#{params.shopName},'%')
  45. </if>
  46. </where>
  47. order by pb.dayId desc
  48. </select>
  49. <select id="platformBill" resultType="com.sqx.modules.reconciliation.model.PlatformBill">
  50. select pb.*
  51. from platform_bill pb
  52. <where>
  53. pb.`type` = 0
  54. <if test="params.startDate != null and params.startDate != '' and params.startDate != 'null'">
  55. and pb.day_id >= #{params.startDate}
  56. </if>
  57. <if test="params.endDate != null and params.endDate != '' and params.endDate != 'null'">
  58. and pb.day_id <![CDATA[ <= ]]> #{params.endDate}
  59. </if>
  60. </where>
  61. order by pb.dayId desc
  62. </select>
  63. <select id="excelRiderBillList" resultType="com.sqx.modules.reconciliation.model.RiderBillVo">
  64. select pb.*,tu.user_name as userName,tu.phone ,rs.station_name as stationName
  65. from platform_bill pb left join tb_user tu on pb.user_id =tu.user_id
  66. left join rider_station rs on rs.id =tu.rider_station_id
  67. <where>
  68. pb.`type` =2
  69. <if test="params.startDate != null and params.startDate != '' and params.startDate != 'null'">
  70. and pb.day_id >= #{params.startDate}
  71. </if>
  72. <if test="params.endDate != null and params.endDate != '' and params.endDate != 'null'">
  73. and pb.day_id <![CDATA[ <= ]]> #{params.endDate}
  74. </if>
  75. <if test="params.riderStation != null and params.riderStation != ''">
  76. and rs.id = #{params.riderStation}
  77. </if>
  78. <if test="params.riderPhone != null and params.riderPhone != '' and params.riderPhone != 'null'">
  79. and tu.phone like concat('%',#{params.riderPhone},'%')
  80. </if>
  81. <if test="params.riderName != null and params.riderName != '' and params.riderName != 'null'">
  82. and tu.user_name like concat('%',#{params.riderName},'%')
  83. </if>
  84. </where>
  85. order by pb.dayId desc
  86. </select>
  87. <select id="excelShopBillList" resultType="com.sqx.modules.reconciliation.model.ShopBillVo">
  88. select pb.*,gs.shop_name as shopName,gs.phone
  89. from platform_bill pb left join tb_user tu on pb.user_id =tu.user_id
  90. left join goods_shop gs on gs.user_id =tu.user_id
  91. <where>
  92. pb.`type` = 1
  93. <if test="params.startDate != null and params.startDate != '' and params.startDate != 'null'">
  94. and pb.day_id >= #{params.startDate}
  95. </if>
  96. <if test="params.endDate != null and params.endDate != '' and params.endDate != 'null'">
  97. and pb.day_id <![CDATA[ <= ]]> #{params.endDate}
  98. </if>
  99. <if test="params.shopPhone != null and params.shopPhone != '' and params.shopPhone != 'null'">
  100. and gs.phone like concat('%',#{params.shopPhone},'%')
  101. </if>
  102. <if test="params.shopName != null and params.shopName != '' and params.shopName != 'null'">
  103. and gs.shop_name like concat('%',#{params.shopName},'%')
  104. </if>
  105. </where>
  106. order by pb.dayId desc
  107. </select>
  108. <select id="excelPlatformBillList" resultType="com.sqx.modules.reconciliation.model.PlatformBill">
  109. select pb.*
  110. from platform_bill pb
  111. <where>
  112. pb.`type` = 0
  113. <if test="params.startDate != null and params.startDate != '' and params.startDate != 'null'">
  114. and pb.day_id >= #{params.startDate}
  115. </if>
  116. <if test="params.endDate != null and params.endDate != '' and params.endDate != 'null'">
  117. and pb.day_id <![CDATA[ <= ]]> #{params.endDate}
  118. </if>
  119. </where>
  120. order by pb.dayId desc
  121. </select>
  122. <update id="updatePlatFormStartMoney">
  123. update platform_bill a join platform_bill b on a.type =b.type and TO_DAYS(a.day_id)-TO_DAYS(b.day_id)= 1
  124. set a.start_money=b.end_money
  125. where a.day_id =#{dayId} and a.type = 0
  126. </update>
  127. <update id="updateRiderStartMoney">
  128. update platform_bill a join platform_bill b on a.user_id =b.user_id and a.type =b.type and TO_DAYS(a.day_id)-TO_DAYS(b.day_id)= 1
  129. set a.start_money=b.end_money
  130. where a.day_id =#{dayId} and a.type in(1,2)
  131. </update>
  132. <insert id="insertPlatformBill">
  133. INSERT INTO `tcwm2.5`.platform_bill (
  134. day_id, user_id, shop_name, `type`, start_money, revenue, revenue_count,
  135. shop_payouts, shop_payouts_rates, shop_payouts_count, rider_payouts, rider_payouts_rates,
  136. rider_payouts_count, refund_money, refund_count, end_money, sys_gift_amount, platform_rates,
  137. total_income, pay_money, create_time, update_time, shop_balance
  138. )
  139. -- 第一部分:骑手数据(type=2)
  140. SELECT
  141. #{dayId},
  142. tu.user_id,
  143. NULL,
  144. 2,
  145. 0,
  146. IFNULL(SUM(ti.rider_money), 0), -- 骑手总收入(revenue)
  147. COUNT(ti.rider_money), -- 收入订单数(revenue_count)
  148. NULL, NULL, NULL, -- 商户提现字段(未使用)
  149. IFNULL(SUM(co.money), 0), -- 骑手提现金额(rider_payouts)
  150. IFNULL(SUM(co.rate), 0), -- 骑手提现手续费(rider_payouts_rates)
  151. COUNT(co.id), -- 骑手提现次数(rider_payouts_count)
  152. NULL, NULL, -- 退款字段(未使用)
  153. IFNULL(MAX(tu.balance), 0), -- 期末余额(end_money)
  154. -- 系统赠送金额 - 系统扣除金额(sys_gift_amount):
  155. IFNULL(
  156. (
  157. SELECT
  158. SUM(money)
  159. FROM
  160. user_money_details ud1
  161. WHERE
  162. ud1.user_id = tu.user_id
  163. AND ud1.title LIKE '系统赠送%'
  164. AND ud1.create_time > #{startTime}
  165. AND ud1.create_time <![CDATA[ <= ]]> #{endTime}
  166. )
  167. -
  168. (
  169. SELECT
  170. SUM(money)
  171. FROM
  172. user_money_details ud2
  173. WHERE
  174. ud2.user_id = tu.user_id
  175. AND ud2.title LIKE '系统扣除%'
  176. AND ud2.create_time > #{startTime}
  177. AND ud2.create_time <![CDATA[ <= ]]> #{endTime}
  178. )
  179. , 0
  180. ) AS gift_amount,
  181. NULL, -- 平台抽成(未使用)
  182. IFNULL(SUM(ti.rider_money), 0), -- 总收益(total_income)
  183. NULL, -- 用户支付金额(未使用)
  184. #{now}, #{now}, -- 创建和更新时间
  185. 0 -- 商户余额(shop_balance)
  186. FROM
  187. tb_user tu USE INDEX (tb_user_rider_open_id_IDX)
  188. 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' -- 订单状态:已完成
  189. 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}
  190. WHERE
  191. tu.rider_open_id IS NOT NULL -- 筛选骑手用户
  192. GROUP BY
  193. tu.user_id
  194. UNION ALL
  195. -- 第二部分:商家数据(type=1)
  196. SELECT
  197. #{dayId},
  198. tu.user_id,
  199. NULL,
  200. 1,
  201. 0,
  202. IFNULL(SUM(tor.shop_income_money), 0), -- 商家总收入(revenue)
  203. COUNT(tor.shop_income_money), -- 收入订单数(revenue_count)
  204. IFNULL(SUM(co.money), 0), -- 商家提现金额(shop_payouts)
  205. IFNULL(SUM(co.rate), 0), -- 商家提现手续费(shop_payouts_rates)
  206. COUNT(co.id), -- 商家提现次数(shop_payouts_count)
  207. NULL, NULL, NULL, -- 骑手提现字段(未使用)
  208. IFNULL(SUM(to1.pay_money), 0), -- 退款金额(refund_money)
  209. COUNT(to1.pay_money), -- 退款次数(refund_count)
  210. -- 未结算订单金额(end_money):
  211. IFNULL(
  212. (
  213. SELECT
  214. SUM(to2.shop_income_money)
  215. FROM
  216. tb_order to2
  217. WHERE
  218. to2.status = 4
  219. AND to2.shop_id = gs.shop_id
  220. )
  221. , 0),
  222. -- 系统赠送金额 - 系统扣除金额(sys_gift_amount):
  223. IFNULL(
  224. (
  225. SELECT
  226. SUM(money)
  227. FROM
  228. user_money_details ud1
  229. WHERE
  230. ud1.user_id = tu.user_id
  231. AND ud1.title LIKE '系统赠送%'
  232. AND ud1.create_time > #{startTime}
  233. AND ud1.create_time <![CDATA[ <= ]]> #{endTime}
  234. )
  235. -
  236. (
  237. SELECT
  238. SUM(money)
  239. FROM
  240. user_money_details ud2
  241. WHERE
  242. ud2.user_id = tu.user_id
  243. AND ud2.title LIKE '系统扣除%'
  244. AND ud2.create_time > #{startTime}
  245. AND ud2.create_time <![CDATA[ <= ]]> #{endTime}
  246. )
  247. , 0) AS gift_amount,
  248. -- 平台抽成金额(platform_rates):
  249. IFNULL(ROUND(SUM(tor.shop_income_money * (1 - IFNULL(gs.shop_rate, 1)) / IFNULL(gs.shop_rate, 1)), 2), 0),
  250. IFNULL(SUM(tor.shop_income_money), 0), -- 总收益(total_income)
  251. IFNULL(SUM(tor.pay_money), 0), -- 用户支付金额(pay_money)
  252. #{now},
  253. #{now}, -- 创建和更新时间
  254. IFNULL(MAX(um.money), 0) -- 商户余额(shop_balance)
  255. FROM
  256. tb_user tu
  257. LEFT JOIN
  258. goods_shop gs ON tu.user_id = gs.user_id -- 关联商家店铺
  259. LEFT JOIN
  260. 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 -- 订单状态:已完成
  261. LEFT JOIN
  262. 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) -- 订单状态:退款/异常
  263. LEFT JOIN
  264. 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}
  265. LEFT JOIN
  266. user_money um ON um.user_id = tu.user_id -- 用户余额表
  267. WHERE
  268. tu.admin_user_id IS NOT NULL -- 筛选商家用户
  269. GROUP BY
  270. tu.user_id;
  271. </insert>
  272. <insert id="insertTotalPlatformBill">
  273. INSERT INTO `tcwm2.5`.platform_bill
  274. (day_id, user_id, shop_name, `type`, start_money, revenue, revenue_count,
  275. shop_payouts, shop_payouts_rates, shop_payouts_count, rider_payouts, rider_payouts_rates,
  276. rider_payouts_count, refund_money, refund_count, end_money,sys_gift_amount, platform_rates,total_income,pay_money,create_time,update_time)
  277. select
  278. day_id,null,null,0,0,sum(if(type=1, revenue,0)),sum(if(type=1, revenue_count,0)),
  279. sum(if(type=1, shop_payouts ,0)),sum(if(type=1, shop_payouts_rates ,0)),sum(if(type=1, shop_payouts_count ,0)),
  280. sum(if(type=2, rider_payouts ,0)),sum(if(type=2, rider_payouts_rates ,0)),sum(if(type=2, rider_payouts_count ,0)),
  281. sum(if(type=1, refund_money ,0)),sum(if(type=1, refund_count ,0)),(select ifnull(sum(pay_money), 0) from tb_order where status in (3,4,6,7)),null,
  282. sum(if(type=1, platform_rates ,0)),null,null,max(create_time) ,max(update_time)
  283. from platform_bill pb where day_id =#{dayId}
  284. </insert>
  285. <!-- 按商家维度统计订单完成金额、订单完成数量、订单取消金额、订单取消数量 -->
  286. <!-- 3待取餐/派送中 4已完成 6制作中 7商家待接单 5已取消、8商家拒绝接单 -->
  287. <!-- 3,4,6,7状态下用户已经完成付款对账的时候视为已完成订单 5,8状态下表示订单已取消 -->
  288. <select id="getOrderData" resultType="com.sqx.modules.reconciliation.model.OrderDataBO">
  289. SELECT
  290. shop_id,
  291. IFNULL(SUM(pay_money), 0) AS pay_amount, -- 当天用户支付总金额
  292. SUM(IF(status IN (5, 8), pay_money, 0)) AS canceled_amount, -- 已取消订单总金额
  293. COUNT(IF(status IN (5, 8), 1, NULL)) AS canceled_count, -- 已取消订单总数量
  294. SUM(IF(status IN (3, 4, 6, 7), shop_income_money, 0)) AS shop_amount, -- 已完成订单商家总收入
  295. COUNT(IF(status IN (3, 4, 6, 7), 1, NULL)) AS completed_count -- 已完成订单总数量
  296. FROM
  297. tb_order
  298. WHERE
  299. is_pay = 1
  300. AND pay_time BETWEEN #{startTime} AND #{endTime}
  301. GROUP BY
  302. shop_id;
  303. </select>
  304. <select id="getShopMoneyRecord" resultType="com.sqx.modules.reconciliation.model.ShopMoneyRecordBO">
  305. SELECT
  306. shop_id,
  307. SUM(TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(content, '平台服务费:', -1), ',',1), ':', -1))) AS platform_fee
  308. FROM
  309. user_money_details
  310. WHERE
  311. shop_id IS NOT NULL
  312. AND type = 1
  313. AND classify = 3
  314. AND create_time BETWEEN #{startTime} AND #{endTime}
  315. GROUP BY shop_id;
  316. </select>
  317. <select id="getShopCashOutRecord" resultType="com.sqx.modules.reconciliation.model.CashOutRecordBO">
  318. SELECT
  319. user_id,
  320. IFNULL(SUM(money), 0) payouts, -- 提现金额
  321. IFNULL(SUM(rate), 0) payouts_rates, -- 提现手续费
  322. COUNT(id) payouts_count -- 提现次数
  323. FROM
  324. cash_out
  325. WHERE
  326. state != -1 -- 提现成功
  327. AND create_at BETWEEN #{startTime} AND #{endTime}
  328. AND shop_id IS NOT NULL
  329. GROUP BY user_id;
  330. </select>
  331. <select id="getRiderCashOutRecord" resultType="com.sqx.modules.reconciliation.model.CashOutRecordBO">
  332. SELECT
  333. user_id,
  334. IFNULL(SUM(money), 0) payouts, -- 提现金额
  335. IFNULL(SUM(rate), 0) payouts_rates, -- 提现手续费
  336. COUNT(id) payouts_count -- 提现次数
  337. FROM
  338. cash_out
  339. WHERE
  340. type = 1
  341. AND create_at BETWEEN #{startTime} AND #{endTime}
  342. AND shop_id IS NULL
  343. GROUP BY user_id;
  344. </select>
  345. <select id="getUserMoneyBalance" resultType="com.sqx.modules.reconciliation.model.UserMoneyBalanceBO">
  346. SELECT
  347. user_id,
  348. money balance
  349. FROM
  350. user_money
  351. WHERE
  352. user_id IN
  353. <foreach collection="userIds" item="userId" open="(" separator="," close=")">
  354. #{userId}
  355. </foreach>
  356. </select>
  357. <select id="getIndentOrderData" resultType="com.sqx.modules.reconciliation.model.IndentOrderDataBO">
  358. SELECT
  359. rider_user_id user_id,
  360. SUM(rider_money) AS totalAmount,
  361. COUNT(indent_id) AS orderCount
  362. FROM
  363. tb_indent
  364. WHERE
  365. finish_time BETWEEN #{startTime} AND #{endTime}
  366. AND rider_user_id IS NOT NULL
  367. AND indent_state = '6'
  368. GROUP BY rider_user_id;
  369. </select>
  370. <select id="getSysGiftRecord" resultType="com.sqx.modules.reconciliation.model.SysGiftRecordBO">
  371. SELECT
  372. user_id,
  373. SUM(IF(type = 1, money, 0)) - SUM(IF(type = 2, money, 0)) as amount
  374. FROM
  375. user_money_details
  376. WHERE
  377. state = 2
  378. AND classify = 3
  379. AND create_time BETWEEN #{startTime} AND #{endTime}
  380. AND user_id IN
  381. <foreach collection="userIds" item="userId" open="(" separator="," close=")">
  382. #{userId}
  383. </foreach>
  384. AND (title = '系统赠送骑手余额' or title = '系统扣除骑手余额')
  385. GROUP BY user_id;
  386. </select>
  387. </mapper>