ContractMapper.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 ifnull(sum(cc.amount), 0) amount,
  167. bc.name corporationName
  168. FROM contract cc
  169. LEFT JOIN bytesailing_customer.customer bc ON cc.buy_corporation_id = bc.id
  170. ${ew.customSqlSegment}
  171. </select>
  172. <select id="salesmanSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  173. SELECT
  174. SUM(cc.amount) amount,
  175. su.nick_name userName
  176. FROM
  177. contract cc
  178. LEFT JOIN bytesailing_base.sys_user su ON cc.create_user = su.user_id
  179. ${ew.customSqlSegment}
  180. </select>
  181. <select id="productSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  182. SELECT
  183. SUM(cp.amount) amount,
  184. pi.name productName
  185. FROM
  186. contract_product cp
  187. LEFT JOIN bytesailing_item.product_info pi ON cp.product_id = pi.id
  188. ${ew.customSqlSegment}
  189. </select>
  190. <select id="salesVolumeStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
  191. SELECT SUM(cp.quantity) quantity,
  192. pi.name productName
  193. FROM contract_product cp
  194. LEFT JOIN bytesailing_item.product_info pi ON cp.product_id = pi.id
  195. ${ew.customSqlSegment}
  196. </select>
  197. <select id="getAccountRunningWaterByContractId"
  198. resultType="com.fjhx.account.entity.account.vo.AccountRunningWaterVo">
  199. SELECT arw.id,
  200. arw.create_time,
  201. ap.type,
  202. arw.currency,
  203. pad.money amount,
  204. pad.remark remarks,
  205. am.`name` accountManagementName,
  206. am.account_opening accountManagementOpening,
  207. arw.`name`,
  208. arw.status
  209. FROM contract c
  210. JOIN bytesailing_purchase.purchase p ON p.data_resource_id = c.id AND p.del_flag = 0
  211. JOIN bytesailing_purchase.pay_detail `pad` ON pad.purchase_id = p.id AND pad.del_flag = 0
  212. JOIN bytesailing_purchase.pay pa ON pad.pay_id = pa.id AND pa.del_flag = 0
  213. JOIN bytesailing_account.account_payment ap ON ap.business_id = pa.id AND ap.del_flag = 0
  214. JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id AND arw.del_flag = 0
  215. LEFT JOIN bytesailing_account.account_management am
  216. ON arw.account_management_id = am.id AND am.del_flag = 0
  217. WHERE c.id = #{contractId}
  218. UNION ALL
  219. SELECT arw.id,
  220. arw.create_time,
  221. ap.type,
  222. arw.currency,
  223. arfd.amount,
  224. arfd.remarks,
  225. am.`name` accountManagementName,
  226. am.account_opening accountManagementOpening,
  227. arw.`name`,
  228. arw.status
  229. FROM contract c
  230. JOIN bytesailing_account.account_request_funds_detail arfd
  231. ON arfd.contract_id = c.id AND arfd.del_flag = 0
  232. JOIN bytesailing_account.account_payment ap
  233. ON ap.business_id = arfd.account_request_funds_id AND ap.del_flag = 0
  234. JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id AND arw.del_flag = 0
  235. LEFT JOIN bytesailing_account.account_management am
  236. ON arw.account_management_id = am.id AND am.del_flag = 0
  237. WHERE c.id = #{contractId}
  238. group by arfd.id
  239. UNION ALL
  240. SELECT arw.id,
  241. arw.create_time,
  242. 30 type,
  243. arw.currency,
  244. clc.money amount,
  245. arw.remarks,
  246. am.`name` accountManagementName,
  247. am.account_opening accountManagementOpening,
  248. arw.`name`,
  249. arw.status
  250. FROM bytesailing_account.account_running_water arw
  251. JOIN claim cl ON cl.business_id = arw.id
  252. JOIN claim_contract clc ON clc.claim_id = cl.id
  253. JOIN contract c ON clc.contract_id = c.id
  254. LEFT JOIN bytesailing_account.account_management am
  255. ON arw.account_management_id = am.id AND am.del_flag = 0
  256. WHERE arw.del_flag = 0
  257. AND c.id = #{contractId}
  258. UNION ALL
  259. SELECT arw.id,
  260. arw.create_time,
  261. 40 AS type,
  262. arw.currency,
  263. trd.amount,
  264. arw.remarks,
  265. am.`name` accountManagementName,
  266. am.account_opening accountManagementOpening,
  267. arw.`name`,
  268. arw.STATUS
  269. FROM contract c
  270. JOIN bytesailing_account.tax_refund_details trd ON trd.contract_id = c.id
  271. AND trd.del_flag = 0
  272. JOIN bytesailing_account.account_running_water arw ON trd.account_running_water_id = arw.id
  273. AND arw.del_flag = 0
  274. LEFT JOIN bytesailing_account.account_management am ON arw.account_management_id = am.id
  275. AND am.del_flag = 0
  276. WHERE c.id = #{contractId}
  277. </select>
  278. <select id="getCustomerMoney" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
  279. SELECT
  280. t1.sell_corporation_id AS sellCorporationId,
  281. IFNULL( SUM( t1.amount * t1.rate ), 0 ) AS sumAmount,
  282. IFNULL( count( 1 ), 0 ) AS sumQuantity,
  283. (
  284. SELECT
  285. IFNULL( SUM( IFNULL( cc.money, 0 ) * IFNULL( cc.rate, 1 )), 0 )
  286. FROM
  287. claim_contract cc
  288. WHERE
  289. FIND_IN_SET( cc.contract_id, GROUP_CONCAT( t1.id ) )
  290. ) AS sumClaimMoney
  291. FROM
  292. (
  293. SELECT
  294. t1.*,
  295. IFNULL( t1.amount, 0 ) * IFNULL( t1.rate, 0 ) AS amountCNY,
  296. CASE
  297. WHEN ( IFNULL( t1.amount, 0 ) * IFNULL( t1.rate, 0 ) - t1.sumClaimMoney ) &lt;= 0 THEN
  298. 20
  299. WHEN t1.sumClaimMoney = 0 THEN
  300. 0 ELSE 10
  301. END AS refundStatusNew
  302. FROM
  303. (
  304. SELECT
  305. t1.*,
  306. (
  307. SELECT
  308. IFNULL( SUM( IFNULL( cc.money, 0 ) * IFNULL( arw.rate, 1 )), 0 )
  309. FROM
  310. claim_contract cc
  311. LEFT JOIN claim c ON cc.claim_id = c.id
  312. LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
  313. WHERE
  314. cc.contract_id = t1.id
  315. ) AS sumClaimMoney
  316. FROM
  317. contract t1
  318. ) t1
  319. ) t1
  320. ${ew.customSqlSegment}
  321. </select>
  322. <update id="updateContract" parameterType="com.fjhx.sale.entity.contract.po.Contract">
  323. update contract
  324. <set>
  325. <if test="id != null">id = #{id},</if>
  326. <if test="status != null">status = #{status},</if>
  327. <if test="isChange != null">is_change = #{isChange},</if>
  328. <if test="isShow != null">is_show = #{isShow},</if>
  329. <if test="oldContractId != null">old_contract_id = #{oldContractId},</if>
  330. <if test="approvedDate != null">approved_date = DATE_FORMAT(NOW(),'%Y-%m-%d %H:%m:%s'),</if>
  331. update_time = sysdate()
  332. </set>
  333. where id = #{upId}
  334. </update>
  335. </mapper>