ContractMapper.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.fjhx.sale.mapper.contract.ContractMapper">
  4. <select id="getPage" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  5. SELECT *
  6. FROM
  7. (SELECT t1.*,
  8. IFNULL(t1.amount, 0) * IFNULL(t1.rate, 0) AS amountCNY,
  9. CASE
  10. WHEN ( IFNULL( t1.amount, 0 ) * IFNULL( t1.rate, 0 ) - t1.sumClaimMoney ) &lt;= 0 THEN
  11. 20
  12. WHEN t1.sumClaimMoney = 0 THEN
  13. 0
  14. ELSE 10
  15. END AS refundStatusNew
  16. FROM (SELECT t1.*,
  17. (SELECT IFNULL(SUM(IFNULL(cc.money, 0) * IFNULL(arw.rate, 1)), 0)
  18. FROM claim_contract cc
  19. left join claim c on cc.claim_id = c.id
  20. LEFT JOIN bytesailing_account.account_running_water arw on c.business_id = arw.id
  21. WHERE cc.contract_id = t1.id) AS sumClaimMoney
  22. FROM contract t1) t1) t1
  23. ${ew.customSqlSegment}
  24. </select>
  25. <select id="getProfitSettlement" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  26. SELECT
  27. *
  28. FROM
  29. (
  30. SELECT *
  31. FROM
  32. (SELECT t1.*,
  33. CASE
  34. WHEN ( IFNULL( t1.amount, 0 ) * IFNULL( t1.advance_ratio / 100, 0 ) - t1.sumClaimMoney ) &lt;= 0 THEN
  35. 20
  36. WHEN t1.sumClaimMoney = 0 THEN
  37. 0
  38. ELSE 10
  39. END AS refundStatusNew
  40. FROM (SELECT t1.*,
  41. (SELECT IFNULL(SUM(IFNULL(cc.money, 0) * IFNULL(arw.rate, 1)), 0)
  42. FROM claim_contract cc
  43. left join claim c on cc.claim_id = c.id
  44. LEFT JOIN bytesailing_account.account_running_water arw on c.business_id = arw.id
  45. WHERE cc.contract_id = t1.id) AS sumClaimMoney
  46. FROM contract t1) t1) t1
  47. ) t1
  48. ${ew.customSqlSegment}
  49. </select>
  50. <select id="getProfitClearingPage" resultType="com.fjhx.sale.entity.contract.vo.ContractBudgetVo">
  51. SELECT t1.id AS contractId,
  52. t1.*
  53. FROM contract t1
  54. ${ew.customSqlSegment}
  55. </select>
  56. <select id="getProfitBudgetPage" resultType="com.fjhx.sale.entity.contract.vo.ContractBudgetVo">
  57. SELECT
  58. (select GROUP_CONCAT(id) from contract_product WHERE contract_id = t1.id) AS contractProductIds,
  59. t1.id AS contractId,
  60. t1.`code`,
  61. t1.buy_corporation_id ,
  62. t1.user_name,
  63. t1.amount,
  64. t1.currency,
  65. t1.currency_rate_json,
  66. t2.*
  67. FROM
  68. contract t1
  69. LEFT JOIN contract_budget t2 ON t1.id = t2.contract_id
  70. ${ew.customSqlSegment}
  71. </select>
  72. <select id="getNoPackContractByCustomerId" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  73. SELECT
  74. *
  75. FROM
  76. (
  77. SELECT
  78. t1.*,
  79. t2.quantity AS cpQuantity,
  80. ( SELECT IFNULL( SUM( quantity ), 0 ) FROM pack_detail_product WHERE contract_product_id = t2.id ) AS sumPackQuantity
  81. FROM
  82. contract t1
  83. LEFT JOIN contract_product t2 ON t1.id = t2.contract_id
  84. <where>
  85. <if test="customerId neq null and customerId neq '' ">
  86. buy_corporation_id = #{customerId}
  87. </if>
  88. </where>
  89. ) t1
  90. WHERE t1.sumPackQuantity &lt; t1.cpQuantity
  91. AND t1.`status`=30
  92. GROUP BY t1.id
  93. </select>
  94. <select id="getSalesTotal" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  95. SELECT SUM(amount * rate) amount,
  96. currency,
  97. count(*) contractCount
  98. FROM contract
  99. WHERE buy_corporation_id = #{id}
  100. and status = 30
  101. and del_flag = 0
  102. GROUP BY buy_corporation_id
  103. </select>
  104. <select id="getAmount" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  105. SELECT
  106. SUM(amount) amount,
  107. DATE_FORMAT(create_time,'%Y-%m') time
  108. FROM
  109. contract
  110. WHERE
  111. buy_corporation_id = #{id}
  112. and (DATE_FORMAT(create_time,'%Y-%m') >= #{beginTime} and #{endTime} >= DATE_FORMAT(create_time,'%Y-%m'))
  113. GROUP BY
  114. TIME
  115. </select>
  116. <select id="contractStatistics" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
  117. SELECT
  118. COUNT(*) count,
  119. ifnull(c.`status`,-1) status,
  120. ifnull(DATE_FORMAT(sq.create_time,'%Y-%m'),-1) time
  121. FROM
  122. bytesailing_customer.customer c
  123. left JOIN contract sq ON sq.buy_corporation_id = c.id
  124. ${ew.customSqlSegment}
  125. </select>
  126. <select id="countrySalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  127. SELECT
  128. sum(cc.amount) amount,
  129. COUNT(cc.buy_country_id) count,
  130. ai.name countryName
  131. FROM
  132. contract cc
  133. LEFT JOIN bytesailing_base.customize_area ai ON cc.buy_country_id = ai.id
  134. ${ew.customSqlSegment}
  135. </select>
  136. <select id="customSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  137. SELECT
  138. sum(cc.amount) amount,
  139. bc.name corporationName
  140. FROM
  141. contract cc
  142. LEFT JOIN bytesailing_customer.customer bc ON cc.buy_corporation_id = bc.id
  143. ${ew.customSqlSegment}
  144. </select>
  145. <select id="salesmanSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  146. SELECT
  147. SUM(cc.amount) amount,
  148. su.nick_name userName
  149. FROM
  150. contract cc
  151. LEFT JOIN bytesailing_base.sys_user su ON cc.create_user = su.user_id
  152. ${ew.customSqlSegment}
  153. </select>
  154. <select id="productSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  155. SELECT
  156. SUM(cp.amount) amount,
  157. pi.name productName
  158. FROM
  159. contract_product cp
  160. LEFT JOIN bytesailing_item.product_info pi ON cp.product_id = pi.id
  161. ${ew.customSqlSegment}
  162. </select>
  163. <select id="salesVolumeStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  164. SELECT SUM(cp.quantity) quantity,
  165. pi.name productName
  166. FROM contract_product cp
  167. LEFT JOIN bytesailing_item.product_info pi ON cp.product_id = pi.id
  168. ${ew.customSqlSegment}
  169. </select>
  170. <select id="getAccountRunningWaterByContractId"
  171. resultType="com.fjhx.account.entity.account.vo.AccountRunningWaterVo">
  172. SELECT arw.id,
  173. arw.create_time,
  174. ap.type,
  175. arw.currency,
  176. pad.money amount,
  177. pad.remark remarks,
  178. am.`name` accountManagementName,
  179. am.account_opening accountManagementOpening,
  180. arw.`name`,
  181. arw.status
  182. FROM contract c
  183. JOIN bytesailing_purchase.purchase p ON p.data_resource_id = c.id AND p.del_flag = 0
  184. JOIN bytesailing_purchase.pay_detail `pad` ON pad.purchase_id = p.id AND pad.del_flag = 0
  185. JOIN bytesailing_purchase.pay pa ON pad.pay_id = pa.id AND pa.del_flag = 0
  186. JOIN bytesailing_account.account_payment ap ON ap.business_id = pa.id AND ap.del_flag = 0
  187. JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id AND arw.del_flag = 0
  188. LEFT JOIN bytesailing_account.account_management am
  189. ON arw.account_management_id = am.id AND am.del_flag = 0
  190. WHERE c.id = #{contractId}
  191. UNION ALL
  192. SELECT arw.id,
  193. arw.create_time,
  194. ap.type,
  195. arw.currency,
  196. arfd.amount,
  197. arfd.remarks,
  198. am.`name` accountManagementName,
  199. am.account_opening accountManagementOpening,
  200. arw.`name`,
  201. arw.status
  202. FROM contract c
  203. JOIN bytesailing_account.account_request_funds_detail arfd
  204. ON arfd.contract_id = c.id AND arfd.del_flag = 0
  205. JOIN bytesailing_account.account_payment ap
  206. ON ap.business_id = arfd.account_request_funds_id AND ap.del_flag = 0
  207. JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id AND arw.del_flag = 0
  208. LEFT JOIN bytesailing_account.account_management am
  209. ON arw.account_management_id = am.id AND am.del_flag = 0
  210. WHERE c.id = #{contractId}
  211. UNION ALL
  212. SELECT arw.id,
  213. arw.create_time,
  214. 30 type,
  215. arw.currency,
  216. clc.money,
  217. arw.remarks,
  218. am.`name` accountManagementName,
  219. am.account_opening accountManagementOpening,
  220. arw.`name`,
  221. arw.status
  222. FROM bytesailing_account.account_running_water arw
  223. JOIN claim cl ON cl.business_id = arw.id
  224. JOIN claim_contract clc ON clc.claim_id = cl.id
  225. JOIN contract c ON clc.contract_id = c.id
  226. LEFT JOIN bytesailing_account.account_management am
  227. ON arw.account_management_id = am.id AND am.del_flag = 0
  228. WHERE arw.del_flag = 0
  229. AND c.id = #{contractId}
  230. </select>
  231. <select id="getCustomerMoney" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  232. SELECT
  233. t1.sell_corporation_id AS sellCorporationId,
  234. IFNULL( SUM( t1.amount * t1.rate ), 0 ) AS sumAmount,
  235. IFNULL( count( 1 ), 0 ) AS sumQuantity,
  236. (
  237. SELECT
  238. IFNULL( SUM( IFNULL( cc.money, 0 ) * IFNULL( cc.rate, 1 )), 0 )
  239. FROM
  240. claim_contract cc
  241. WHERE
  242. FIND_IN_SET( cc.contract_id, GROUP_CONCAT( t1.id ) )
  243. ) AS sumClaimMoney
  244. FROM
  245. (
  246. SELECT
  247. t1.*,
  248. IFNULL( t1.amount, 0 ) * IFNULL( t1.rate, 0 ) AS amountCNY,
  249. CASE
  250. WHEN ( IFNULL( t1.amount, 0 ) * IFNULL( t1.rate, 0 ) - t1.sumClaimMoney ) &lt;= 0 THEN
  251. 20
  252. WHEN t1.sumClaimMoney = 0 THEN
  253. 0 ELSE 10
  254. END AS refundStatusNew
  255. FROM
  256. (
  257. SELECT
  258. t1.*,
  259. (
  260. SELECT
  261. IFNULL( SUM( IFNULL( cc.money, 0 ) * IFNULL( arw.rate, 1 )), 0 )
  262. FROM
  263. claim_contract cc
  264. LEFT JOIN claim c ON cc.claim_id = c.id
  265. LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
  266. WHERE
  267. cc.contract_id = t1.id
  268. ) AS sumClaimMoney
  269. FROM
  270. contract t1
  271. ) t1
  272. ) t1
  273. ${ew.customSqlSegment}
  274. </select>
  275. <update id="updateContract" parameterType="com.fjhx.sale.entity.contract.po.Contract">
  276. update contract
  277. <set>
  278. <if test="id != null">id = #{id},</if>
  279. <if test="status != null">status = #{status},</if>
  280. <if test="isChange != null">is_change = #{isChange},</if>
  281. <if test="isShow != null">is_show = #{isShow},</if>
  282. <if test="oldContractId != null">old_contract_id = #{oldContractId},</if>
  283. <if test="approvedDate != null">approved_date = DATE_FORMAT(NOW(),'%Y-%m-%d %H:%m:%s'),</if>
  284. update_time = sysdate()
  285. </set>
  286. where id = #{upId}
  287. </update>
  288. </mapper>