ContractMapper.xml 15 KB

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