PlatformBillMapper.xml 20 KB

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