DistributorBalanceMapper.xml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.yc.ship.module.trade.dal.mysql.report.DistributorBalanceMapper">
  4. <!-- 分页查询预存款余额表 -->
  5. <!--
  6. 业务说明:
  7. 1. 以分销商(ota_distributor)为主表
  8. 2. 累计充值金额 = 后台充值(ota_distributor_recharge) + 自助充值(ota_distributor_self_recharge) 在日期范围内的成功金额
  9. 3. 累计消费金额 = ota_trade_log 中消费类型(trade_mode=2)在日期范围内的交易金额
  10. 4. 余额 = 分销商当前余额(balance)
  11. -->
  12. <select id="selectDistributorBalancePage"
  13. resultType="com.yc.ship.module.trade.controller.admin.report.vo.DistributorBalanceRespVO">
  14. SELECT * FROM (
  15. SELECT
  16. d.id,
  17. d.name AS travelAgency,
  18. IFNULL(recharge.total_recharge, 0) AS totalRechargeAmount,
  19. IFNULL(topay.actual_amount, 0) AS totalConsumeAmount,
  20. IFNULL(d.balance, 0) AS balance
  21. FROM ota_distributor d
  22. LEFT JOIN (
  23. SELECT
  24. distributor_id,
  25. SUM(recharge_amount) AS total_recharge
  26. FROM (
  27. SELECT
  28. r.distributor_id,
  29. SUM(tl.trade_amount) AS recharge_amount
  30. FROM ota_distributor_recharge r
  31. INNER JOIN ota_trade_log tl ON r.recharge_log_id = tl.id AND tl.deleted = 0 AND tl.trade_type = 1
  32. WHERE r.deleted = 0
  33. AND r.status = 1
  34. AND tl.trade_mode = 1
  35. AND tl.trade_type = 1
  36. <if test="vo.startDate != null">
  37. AND DATE(tl.trade_time) &gt;= #{vo.startDate}
  38. </if>
  39. <if test="vo.endDate != null">
  40. AND DATE(tl.trade_time) &lt;= #{vo.endDate}
  41. </if>
  42. GROUP BY r.distributor_id
  43. UNION ALL
  44. SELECT
  45. sr.distributor_id,
  46. SUM(sr.money) AS recharge_amount
  47. FROM ota_distributor_self_recharge sr
  48. WHERE sr.deleted = 0
  49. AND sr.recharge_state = 1
  50. <if test="vo.startDate != null">
  51. AND DATE(sr.create_time) &gt;= #{vo.startDate}
  52. </if>
  53. <if test="vo.endDate != null">
  54. AND DATE(sr.create_time) &lt;= #{vo.endDate}
  55. </if>
  56. GROUP BY sr.distributor_id
  57. ) t
  58. GROUP BY distributor_id
  59. ) recharge ON d.id = recharge.distributor_id
  60. LEFT JOIN (SELECT
  61. td.source_id,
  62. SUM( tp.pay_amount ) AS actual_amount
  63. FROM
  64. trade_order_pay tp
  65. LEFT JOIN trade_order td ON td.id = tp.order_id
  66. AND td.deleted = 0
  67. where
  68. tp.pay_status = 1
  69. AND tp.deleted = 0
  70. <if test="vo.startDate != null">
  71. AND DATE(tp.payment_date) &gt;= #{vo.startDate}
  72. </if>
  73. <if test="vo.endDate != null">
  74. AND DATE(tp.payment_date) &lt;= #{vo.endDate}
  75. </if>
  76. GROUP BY td.source_id) topay ON d.id = topay.source_id
  77. WHERE d.deleted = 0
  78. <if test="vo.travelAgency != null and vo.travelAgency != ''">
  79. AND d.name LIKE CONCAT('%', #{vo.travelAgency}, '%')
  80. </if>
  81. ) temp
  82. WHERE (totalRechargeAmount <![CDATA[>]]> 0 OR totalConsumeAmount <![CDATA[>]]> 0 OR balance <![CDATA[>]]> 0)
  83. ORDER BY id DESC
  84. </select>
  85. <!-- 导出查询(与分页查询条件一致,不分页) -->
  86. <select id="selectDistributorBalanceExportList"
  87. resultType="com.yc.ship.module.trade.controller.admin.report.vo.DistributorBalanceRespVO">
  88. SELECT * FROM (
  89. SELECT
  90. d.id,
  91. d.name AS travelAgency,
  92. IFNULL(recharge.total_recharge, 0) AS totalRechargeAmount,
  93. IFNULL(consume.total_consume, 0) AS totalConsumeAmount,
  94. IFNULL(d.balance, 0) AS balance
  95. FROM ota_distributor d
  96. LEFT JOIN (
  97. SELECT
  98. distributor_id,
  99. SUM(recharge_amount) AS total_recharge
  100. FROM (
  101. SELECT
  102. r.distributor_id,
  103. SUM(tl.trade_amount) AS recharge_amount
  104. FROM ota_distributor_recharge r
  105. INNER JOIN ota_trade_log tl ON r.recharge_log_id = tl.id AND tl.deleted = 0 AND tl.trade_type = 1
  106. WHERE r.deleted = 0
  107. AND r.status = 1
  108. AND tl.trade_mode = 1
  109. AND tl.trade_type = 1
  110. <if test="vo.startDate != null">
  111. AND DATE(tl.trade_time) &gt;= #{vo.startDate}
  112. </if>
  113. <if test="vo.endDate != null">
  114. AND DATE(tl.trade_time) &lt;= #{vo.endDate}
  115. </if>
  116. GROUP BY r.distributor_id
  117. UNION ALL
  118. SELECT
  119. sr.distributor_id,
  120. SUM(sr.money) AS recharge_amount
  121. FROM ota_distributor_self_recharge sr
  122. INNER JOIN ota_trade_log tl ON tl.id = sr.recharge_log_id AND tl.deleted = 0
  123. WHERE sr.deleted = 0
  124. AND sr.recharge_state = 1
  125. AND tl.trade_type = 1
  126. <if test="vo.startDate != null">
  127. AND DATE(tl.trade_time) &gt;= #{vo.startDate}
  128. </if>
  129. <if test="vo.endDate != null">
  130. AND DATE(tl.trade_time) &lt;= #{vo.endDate}
  131. </if>
  132. GROUP BY sr.distributor_id
  133. ) t
  134. GROUP BY distributor_id
  135. ) recharge ON d.id = recharge.distributor_id
  136. LEFT JOIN (
  137. SELECT
  138. tl.distributor_id,
  139. SUM(tl.trade_amount) AS total_consume
  140. FROM ota_trade_log tl
  141. WHERE tl.deleted = 0
  142. AND tl.trade_mode = 2
  143. AND tl.trade_type = 1
  144. <if test="vo.startDate != null">
  145. AND DATE(tl.trade_time) &gt;= #{vo.startDate}
  146. </if>
  147. <if test="vo.endDate != null">
  148. AND DATE(tl.trade_time) &lt;= #{vo.endDate}
  149. </if>
  150. GROUP BY tl.distributor_id
  151. ) consume ON d.id = consume.distributor_id
  152. WHERE d.deleted = 0
  153. <if test="vo.travelAgency != null and vo.travelAgency != ''">
  154. AND d.name LIKE CONCAT('%', #{vo.travelAgency}, '%')
  155. </if>
  156. ) temp
  157. WHERE (totalRechargeAmount <![CDATA[>]]> 0 OR totalConsumeAmount <![CDATA[>]]> 0 OR balance <![CDATA[>]]> 0)
  158. ORDER BY id DESC
  159. </select>
  160. </mapper>