[fix]优化sql

master
hellolujian 6 years ago
parent a78fd1542d
commit d8d6855d13

@ -163,16 +163,15 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
@Override
public List<JSONObject> getTopTradePartners(JSONObject params, int limit) {
return clientAnalysisMapper.listPartnersTradeAmount(params, new PageBounds(1, limit, Order.formString("aud_fee.desc")));
params.put("rankType", "aud_fee");
params.put("limit", limit);
return clientAnalysisMapper.listPartnersTradeAmount(params);
}
@Override
public List<JSONObject> getTopTradePartners(JSONObject params) {
String rankType = "aud_fee";
if (params.getString("rankType")!=null){
rankType = params.getString("rankType");
}
return clientAnalysisMapper.listPartnersTradeAmount(params, new PageBounds(Order.formString(rankType+".desc")));
params.putIfAbsent("rankType", "aud_fee");
return clientAnalysisMapper.listPartnersTradeAmount(params);
}
@Override

@ -33,7 +33,7 @@ public interface ClientAnalysisMapper {
List<JSONObject> countTradePartnersHistory(JSONObject params);
List<JSONObject> listPartnersTradeAmount(JSONObject params, PageBounds pageBounds);
List<JSONObject> listPartnersTradeAmount(JSONObject params);
List<JSONObject> countClientsTypes(JSONObject params);

@ -75,11 +75,11 @@ public class DashboardController {
@ManagerMapping("/rank_trade_partners")
public List<JSONObject> rankTradePartners(AnalysisBean analysis, @ModelAttribute(CommonConsts.MANAGER_STATUS) JSONObject manager) {
JSONObject params = analysis.toParams(null);
orgManager.checkOrg(manager,params);
orgManager.checkOrg(manager, params);
if (analysis.isOnlyGroup() && ManagerRole.BD_LEADER.hasRole(manager.getIntValue("role"))) {
params.put("bd_group", manager.getString("manager_id"));
if (analysis.getGroup_bd()>0){
params.put("bd_group_bd",analysis.getGroup_bd());
params.put("bd_group_bd", analysis.getGroup_bd());
}
}
return dashboardService.getTopTradePartners(params);

@ -97,30 +97,100 @@
GROUP BY date(o.create_time)
</select>
<!-- 原来的SQL语句
<select id="listPartnersTradeAmount" resultType="com.alibaba.fastjson.JSONObject">
<![CDATA[
SELECT
p.client_moniker,p.short_name,ifnull(sum(if(t.transaction_type='Credit',t.clearing_amount,0)),0)aud_fee,
count(DISTINCT t.order_id) orders,max(t.clearing_amount)max_order,ifnull(sum(if(t.refund_id is not null,if(t.transaction_type='Debit',t.clearing_amount,-t.clearing_amount),0)),0)refund_amount,
ifnull(sum(if(t.refund_id is not null and t.transaction_type='Debit',1,0)),0)refund_orders,max(if(t.refund_id is not null and t.transaction_type='Debit',t.clearing_amount,0))max_refund
SELECT
p.client_moniker,
p.short_name,
ifnull(sum(if(t.transaction_type = 'Credit', t.clearing_amount, 0)), 0) aud_fee,
count(DISTINCT t.order_id) orders,
max(t.clearing_amount) max_order,
ifnull(sum(if(t.refund_id is not null, if(t.transaction_type='Debit', t.clearing_amount, -t.clearing_amount), 0)), 0) refund_amount,
ifnull(sum(if(t.refund_id is not null and t.transaction_type='Debit', 1, 0)), 0)refund_orders, max(if(t.refund_id is not null and t.transaction_type='Debit', t.clearing_amount, 0)) max_refund
FROM sys_clients p
INNER JOIN pmt_transactions t ON t.client_id = p.client_id and (t.transaction_type='Credit' or t.refund_id is not null)
INNER JOIN pmt_orders o ON t.order_id = o.order_id
WHERE o.create_time >= #{begin} AND o.create_time <= #{end}
INNER JOIN pmt_transactions t
ON t.client_id = p.client_id
and (t.transaction_type = 'Credit' or t.refund_id is not null)
INNER JOIN pmt_orders o
ON t.order_id = o.order_id
WHERE o.create_time >= #{begin}
AND o.create_time <= #{end}
]]>
<if test="org_id!=null and org_ids==null">and p.org_id=#{org_id}</if>
<if test="org_ids!=null">and p.org_id in
<foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">#{org_id}</foreach></if>
<if test="bd_group!=null">and p.client_id in
(SELECT b.client_id FROM sys_client_bd b
INNER JOIN financial_bd_config c ON c.manager_id=b.bd_id
where b.is_valid=1 and b.start_date&lt;=now() and (b.end_date is null or b.end_date &gt;= now())
AND (c.bd_group=#{bd_group} or c.manager_id=#{bd_group})
<if test="bd_group_bd">and c.manager_id=#{bd_group_bd}</if>
<if test="org_id != null and org_ids == null">
and p.org_id = #{org_id}
</if>
<if test="org_ids != null">
and p.org_id in
<foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">
#{org_id}
</foreach>
</if>
<if test="bd_group != null">
and p.client_id in (
SELECT b.client_id FROM sys_client_bd b
INNER JOIN financial_bd_config c
ON c.manager_id = b.bd_id
where b.is_valid = 1
and b.start_date &lt;= now()
and (b.end_date is null or b.end_date &gt;= now())
AND (c.bd_group = #{bd_group} or c.manager_id = #{bd_group})
<if test="bd_group_bd">
and c.manager_id = #{bd_group_bd}
</if>
)
</if>
GROUP BY p.client_id
</select>
-->
<!-- 优化后的SQL语句 -->
<select id="listPartnersTradeAmount" resultType="com.alibaba.fastjson.JSONObject">
<![CDATA[
SELECT
p.client_moniker,
p.short_name,
ifnull(sum(if(t.transaction_type = 'Credit', t.clearing_amount, 0)), 0) aud_fee,
count(DISTINCT t.order_id) orders,
max(t.clearing_amount) max_order,
ifnull(sum(if(t.refund_id is not null, if(t.transaction_type='Debit', t.clearing_amount, -t.clearing_amount), 0)), 0) refund_amount,
ifnull(sum(if(t.refund_id is not null and t.transaction_type='Debit', 1, 0)), 0)refund_orders, max(if(t.refund_id is not null and t.transaction_type='Debit', t.clearing_amount, 0)) max_refund
FROM sys_clients p
INNER JOIN pmt_transactions t
ON t.client_id = p.client_id
WHERE t.create_time >= #{begin}
AND t.create_time <= #{end}
]]>
<if test="org_id != null and org_ids == null">
and p.org_id = #{org_id}
</if>
<if test="org_ids != null">
and p.org_id in
<foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">
#{org_id}
</foreach>
</if>
<if test="bd_group != null">
and p.client_id in (
SELECT b.client_id FROM sys_client_bd b
INNER JOIN financial_bd_config c
ON c.manager_id = b.bd_id
where b.is_valid = 1
and b.start_date &lt;= now()
and (b.end_date is null or b.end_date &gt;= now())
AND (c.bd_group = #{bd_group} or c.manager_id = #{bd_group})
<if test="bd_group_bd">
and c.manager_id = #{bd_group_bd}
</if>
)
</if>
GROUP BY p.client_id
ORDER BY #{rankType} DESC
<if test="limit != null">
limit ${limit}
</if>
</select>
<select id="countClientsTypes" resultType="com.alibaba.fastjson.JSONObject">

Loading…
Cancel
Save