Browse Source

sql优化

yzc 10 months ago
parent
commit
8a15c3567b

+ 1 - 1
hx-sale/src/main/java/com/fjhx/sale/mapper/contract/ContractMapper.java

@@ -118,7 +118,7 @@ public interface ContractMapper extends BaseMapper<Contract> {
     List<AccountRunningWaterVo> getAccountRunningWaterByContractId(@Param("contractId") Long contractId);
     List<AccountRunningWaterVo> getAccountRunningWaterByContractId(@Param("contractId") Long contractId);
 
 
 
 
-    Page<ContractVo> getContractAndSamplePagez(@Param("page") Page<Object> page, @Param("ew") IWrapper<Contract> wrapper);
+    Page<ContractVo> getContractAndSamplePage(@Param("page") Page<Object> page, @Param("ew") IWrapper<Contract> wrapper);
 
 
     void updateContract(Contract contract);
     void updateContract(Contract contract);
 
 

+ 1 - 1
hx-sale/src/main/java/com/fjhx/sale/service/contract/impl/ContractServiceImpl.java

@@ -221,7 +221,7 @@ public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract>
             );
             );
         }
         }
         wrapper.orderByDesc("t1.create_time");
         wrapper.orderByDesc("t1.create_time");
-        Page<ContractVo> contractAndSamplePage = baseMapper.getContractAndSamplePagez(dto.getPage(), wrapper);
+        Page<ContractVo> contractAndSamplePage = baseMapper.getContractAndSamplePage(dto.getPage(), wrapper);
         List<ContractVo> records = contractAndSamplePage.getRecords();
         List<ContractVo> records = contractAndSamplePage.getRecords();
         //赋值原卖方公司名称 多公司
         //赋值原卖方公司名称 多公司
         DynamicDataSourceContextHolder.push(SourceConstant.COMMON);
         DynamicDataSourceContextHolder.push(SourceConstant.COMMON);

+ 49 - 67
hx-sale/src/main/resources/mapper/contract/ContractMapper.xml

@@ -448,73 +448,55 @@
         WHERE arw.del_flag = 0
         WHERE arw.del_flag = 0
           AND c.id = #{contractId}
           AND c.id = #{contractId}
     </select>
     </select>
-    <select id="getContractAndSamplePagez" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
-        SELECT
-        *
-        FROM
-        (
-        SELECT
-            t1.id,
-            t1.sell_corporation_id,
-            t1.contract_type,
-            1 AS dataType,
-            t1.`code`,
-            t1.buy_corporation_id,
-            t1.version,
-            t1.amount,
-            t1.create_user,
-            t1.create_time,
-            t1.refund_status,
-            t1.`status`,
-            (SELECT IFNULL(SUM(IFNULL(cc.money, 0) * IFNULL(arw.rate, 1)), 0)
-             FROM claim_contract cc
-                      LEFT JOIN claim c ON cc.claim_id = c.id
-                      LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
-             WHERE cc.contract_id = t1.id) AS sumClaimMoney,
-            (SELECT IFNULL(SUM(cc.contract_money), 0)
-             FROM claim_contract cc
-                      LEFT JOIN claim c ON cc.claim_id = c.id
-                      LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
-             WHERE cc.contract_id = t1.id) AS sumContractClaimMoney,
-            t1.currency
-        FROM contract t1
-        WHERE t1.`status` >= 30
-          AND t1.`status` &lt; 88
-          AND t1.is_show != 1
-        AND t1.is_change = 0
-        UNION
-        SELECT
-            t1.id, t1.sell_corporation_id, 3 AS contract_type, 2 AS dataType, t1.`code`, t1.buy_corporation_id, t1.versions AS version, t1.amount, t1.create_user, t1.create_time, IF( sumClaimMoney >= t1.amount, 20, IF ( sumClaimMoney != 0, 10, 0 ) ) AS refund_status, t1.`status`, t1.sumClaimMoney, t1.sumContractClaimMoney, t1.currency
-        FROM
-            (
-            SELECT
-            t1.*, (
-            SELECT
-            IFNULL( SUM ( IFNULL( cc.money, 0 ) * IFNULL( arw.rate, 1 )), 0 )
-            FROM
-            claim_contract cc
-            LEFT JOIN claim c ON cc.claim_id = c.id
-            LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
-            WHERE
-            cc.contract_id = t1.id
-            ) AS sumClaimMoney, (
-            SELECT
-            IFNULL( SUM (cc.contract_money), 0 )
-            FROM
-            claim_contract cc
-            LEFT JOIN claim c ON cc.claim_id = c.id
-            LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
-            WHERE
-            cc.contract_id = t1.id
-            ) AS sumContractClaimMoney
-            FROM
-            sample t1
-            WHERE
-            t1.`status` >= 30
-            AND t1.`status` &lt; 88
-            AND t1.is_show != 1
-            AND t1.is_change = 0
-            ) AS t1) t1
+    <select id="getContractAndSamplePage" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
+        SELECT *
+        FROM (SELECT t1.*,
+                     IFNULL(cc1.contract_money, 0)             AS sumContractClaimMoney,
+                     t1.amount - IFNULL(cc1.contract_money, 0) AS sumContractNotClaimMoney,
+                     CASE
+                         WHEN IFNULL(cc1.contract_money, 0) >= t1.amount THEN 20
+                         WHEN IFNULL(cc1.contract_money, 0) != 0 THEN 10
+                         ELSE 0 END                            AS refund_status
+              FROM (SELECT t1.id,
+                           t1.sell_corporation_id,
+                           t1.contract_type,
+                           1 AS dataType,
+                           t1.`code`,
+                           t1.buy_corporation_id,
+                           t1.version,
+                           t1.amount,
+                           t1.create_user,
+                           t1.create_time,
+                           t1.`status`,
+                           t1.currency,
+                           t1.is_show,
+                           t1.is_change
+                    FROM contract t1
+                    UNION ALL
+                    SELECT t1.id,
+                           t1.sell_corporation_id,
+                           3           AS contract_type,
+                           2           AS dataType,
+                           t1.`code`,
+                           t1.buy_corporation_id,
+                           t1.versions AS version,
+                           t1.amount,
+                           t1.create_user,
+                           t1.create_time,
+                           t1.`status`,
+                           t1.currency,
+                           t1.is_show,
+                           t1.is_change
+                    FROM sample t1) t1
+                       LEFT JOIN (SELECT cc.contract_id,
+                                         SUM(IFNULL(cc.contract_money, 0)) AS contract_money
+                                  FROM claim_contract cc
+                                           JOIN claim c ON cc.claim_id = c.id
+                                           JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
+                                  GROUP BY cc.contract_id) cc1 ON cc1.contract_id = t1.id
+              WHERE t1.`status` IN (30, 60)
+                AND t1.is_show != 1
+	AND t1.is_change = 0) t1
             ${ew.customSqlSegment}
             ${ew.customSqlSegment}
     </select>
     </select>