hotfix: 优化用户数查询

master
Yixian 3 years ago
parent b1c04352b6
commit ed58aae8ec

@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit;
* Created by davep on 2016-07-28. * Created by davep on 2016-07-28.
*/ */
@Service @Service
public class DashboardServiceImpl implements DashboardService,DashboardAnalysisTask { public class DashboardServiceImpl implements DashboardService, DashboardAnalysisTask {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
@Resource @Resource
private ClientAnalysisMapper clientAnalysisMapper; private ClientAnalysisMapper clientAnalysisMapper;
@ -64,25 +64,24 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
private MerchantInfoProvider merchantInfoProvider; private MerchantInfoProvider merchantInfoProvider;
@Override @Override
public JSONObject getCommonAnalysis1(JSONObject params) { public JSONObject getCommonAnalysis1(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis1" + params.getString("org_id") + params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis1" + params.getString("org_id") + params.getString("begin")).get();
JSONObject commonAnalysis1 = JSON.parseObject(jsonStr); JSONObject commonAnalysis1 = JSON.parseObject(jsonStr);
if(commonAnalysis1 != null){ if (commonAnalysis1 != null) {
return commonAnalysis1; return commonAnalysis1;
} }
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
if(params.getDate("end").after(new Date())){ if (params.getDate("end").after(new Date())) {
if(params.getDate("begin").compareTo(DateUtils.addDays(params.getDate("end"), -1))==0){ if (params.getDate("begin").compareTo(DateUtils.addDays(params.getDate("end"), -1)) == 0) {
res = transactionAnalysisMapper.getClientTransaction(params); res = transactionAnalysisMapper.getClientTransaction(params);
}else { } else {
res = customerAndOrdersStatisticsMapper.getCommonCount(params); res = customerAndOrdersStatisticsMapper.getCommonCount(params);
} }
}else { } else {
res = customerAndOrdersStatisticsMapper.getCommonCount(params); res = customerAndOrdersStatisticsMapper.getCommonCount(params);
} }
stringRedisTemplate.boundValueOps("org_commonAnalysis1"+params.getString("org_id")+params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("org_commonAnalysis1" + params.getString("org_id") + params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@ -91,7 +90,7 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
public JSONObject getCommonAnalysis3(JSONObject params) { public JSONObject getCommonAnalysis3(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis3" + params.getString("org_id") + params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis3" + params.getString("org_id") + params.getString("begin")).get();
JSONObject commonAnalysis3 = JSON.parseObject(jsonStr); JSONObject commonAnalysis3 = JSON.parseObject(jsonStr);
if(commonAnalysis3 != null){ if (commonAnalysis3 != null) {
return commonAnalysis3; return commonAnalysis3;
} }
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
@ -100,7 +99,7 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
//res.put("total_partners", clientAnalysisMapper.countClients(params)); //res.put("total_partners", clientAnalysisMapper.countClients(params));
// 统计所有的商户,包括禁用的 // 统计所有的商户,包括禁用的
res.put("total_partners", clientAnalysisMapper.countClientsAll(params)); res.put("total_partners", clientAnalysisMapper.countClientsAll(params));
stringRedisTemplate.boundValueOps("org_commonAnalysis3"+params.getString("org_id")+params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("org_commonAnalysis3" + params.getString("org_id") + params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@ -108,25 +107,26 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
public JSONObject getCommonAnalysis2(JSONObject params) { public JSONObject getCommonAnalysis2(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis2" + params.getString("org_id") + params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis2" + params.getString("org_id") + params.getString("begin")).get();
JSONObject commonAnalysis2 = JSON.parseObject(jsonStr); JSONObject commonAnalysis2 = JSON.parseObject(jsonStr);
if(commonAnalysis2 != null){ if (commonAnalysis2 != null) {
return commonAnalysis2; return commonAnalysis2;
} }
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
if(params.getDate("end").after(new Date())){ if (params.getDate("end").after(new Date())) {
if(params.getDate("begin").compareTo(DateUtils.addDays(params.getDate("end"), -1))==0){ if (params.getDate("begin").compareTo(DateUtils.addDays(params.getDate("end"), -1)) == 0) {
res.put("total_customers", transactionAnalysisMapper.countCustomers(params)); res.put("total_customers", transactionAnalysisMapper.countCustomers(params));
res.put("old_customers", transactionAnalysisMapper.countOldCustomers(params)); res.put("old_customers", transactionAnalysisMapper.countOldCustomers(params));
res.put("traded_partners", clientAnalysisMapper.countTradedPartners(params)); res.put("traded_partners", clientAnalysisMapper.countTradedPartners(params));
}else { } else {
res = getYesOrLasMonth(params); res = getYesOrLasMonth(params);
} }
}else { } else {
res = getYesOrLasMonth(params); res = getYesOrLasMonth(params);
} }
stringRedisTemplate.boundValueOps("org_commonAnalysis2"+params.getString("org_id")+params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("org_commonAnalysis2" + params.getString("org_id") + params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
private JSONObject getYesOrLasMonth(JSONObject params){
private JSONObject getYesOrLasMonth(JSONObject params) {
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
res.put("traded_partners", customerAndOrdersStatisticsMapper.countTradedClients(params)); res.put("traded_partners", customerAndOrdersStatisticsMapper.countTradedClients(params));
res.put("total_customers", customerAndOrdersStatisticsMapper.countTotalCustomers(params)); res.put("total_customers", customerAndOrdersStatisticsMapper.countTotalCustomers(params));
@ -138,7 +138,7 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
public JSONObject getCommonAnalysis4(JSONObject params) { public JSONObject getCommonAnalysis4(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis4" + params.getString("org_id") + params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("org_commonAnalysis4" + params.getString("org_id") + params.getString("begin")).get();
JSONObject commonAnalysis4 = JSON.parseObject(jsonStr); JSONObject commonAnalysis4 = JSON.parseObject(jsonStr);
if(commonAnalysis4 != null){ if (commonAnalysis4 != null) {
return commonAnalysis4; return commonAnalysis4;
} }
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
@ -146,22 +146,22 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
if (!topOrders.isEmpty()) { if (!topOrders.isEmpty()) {
res.put("top_amount_order", topOrders.get(0)); res.put("top_amount_order", topOrders.get(0));
} }
stringRedisTemplate.boundValueOps("org_commonAnalysis4"+params.getString("org_id")+params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("org_commonAnalysis4" + params.getString("org_id") + params.getString("begin")).set(res.toJSONString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@Override @Override
public List<JSONObject> getTradeInTypes(JSONObject params) { public List<JSONObject> getTradeInTypes(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("tradeInTypes"+params.getString("org_id")+ params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("tradeInTypes" + params.getString("org_id") + params.getString("begin")).get();
List<JSONObject> tradeInTypes = JSONObject.parseArray(jsonStr,JSONObject.class); List<JSONObject> tradeInTypes = JSONObject.parseArray(jsonStr, JSONObject.class);
if(tradeInTypes != null){ if (tradeInTypes != null) {
return tradeInTypes; return tradeInTypes;
} }
if (params.containsKey("client_id")) { if (params.containsKey("client_id")) {
clientManager.queryModifyClientIds(params.getInteger("client_id"), params); clientManager.queryModifyClientIds(params.getInteger("client_id"), params);
} }
List<JSONObject> res = transactionAnalysisMapper.getTradeAmountInTypes(params); List<JSONObject> res = transactionAnalysisMapper.getTradeAmountInTypes(params);
stringRedisTemplate.boundValueOps("tradeInTypes"+params.getString("org_id")+ params.getString("begin")).set(res.toString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("tradeInTypes" + params.getString("org_id") + params.getString("begin")).set(res.toString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@ -175,14 +175,14 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
@Override @Override
public List<JSONObject> getTopTradePartnersNew(JSONObject params) { public List<JSONObject> getTopTradePartnersNew(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("topTradePartnersNew"+params.getString("org_id")+ params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("topTradePartnersNew" + params.getString("org_id") + params.getString("begin")).get();
List<JSONObject> topTradePartnersNew = JSONObject.parseArray(jsonStr,JSONObject.class); List<JSONObject> topTradePartnersNew = JSONObject.parseArray(jsonStr, JSONObject.class);
if(topTradePartnersNew != null){ if (topTradePartnersNew != null) {
return topTradePartnersNew; return topTradePartnersNew;
} }
params.putIfAbsent("rankType", "aud_fee"); params.putIfAbsent("rankType", "aud_fee");
List<JSONObject> res = clientAnalysisMapper.listPartnersTradeAmountNew(params); List<JSONObject> res = clientAnalysisMapper.listPartnersTradeAmountNew(params);
stringRedisTemplate.boundValueOps("topTradePartnersNew"+params.getString("org_id")+ params.getString("begin")).set(res.toString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("topTradePartnersNew" + params.getString("org_id") + params.getString("begin")).set(res.toString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@ -277,6 +277,7 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
analysis.add(wechatHtml5); analysis.add(wechatHtml5);
return analysis; return analysis;
} }
@Override @Override
public List<JSONObject> getPartnerTradeAnalysisInDays(JSONObject params) { public List<JSONObject> getPartnerTradeAnalysisInDays(JSONObject params) {
int date_range = Integer.parseInt(DurationFormatUtils.formatPeriod(params.getDate("begin").getTime(), params.getDate("end").getTime(), "dd")); int date_range = Integer.parseInt(DurationFormatUtils.formatPeriod(params.getDate("begin").getTime(), params.getDate("end").getTime(), "dd"));
@ -293,16 +294,16 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
@Override @Override
public List<JSONObject> getTradeAnalysisInDays(JSONObject params) { public List<JSONObject> getTradeAnalysisInDays(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("tradeAnalysisInDays"+params.getString("org_id")+ params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("tradeAnalysisInDays" + params.getString("org_id") + params.getString("begin")).get();
List<JSONObject> tradeAnalysisInDays = JSONObject.parseArray(jsonStr,JSONObject.class); List<JSONObject> tradeAnalysisInDays = JSONObject.parseArray(jsonStr, JSONObject.class);
if(tradeAnalysisInDays != null){ if (tradeAnalysisInDays != null) {
return tradeAnalysisInDays; return tradeAnalysisInDays;
} }
if (!params.containsKey("client_ids") && !params.containsKey("org_id")) { if (!params.containsKey("client_ids") && !params.containsKey("org_id")) {
params.put("client_id", 0); params.put("client_id", 0);
} }
List<JSONObject> res = customerAndOrdersStatisticsMapper.getSumCustomersAnalysis(params); List<JSONObject> res = customerAndOrdersStatisticsMapper.getSumCustomersAnalysis(params);
stringRedisTemplate.boundValueOps("tradeAnalysisInDays"+params.getString("org_id")+ params.getString("begin")).set(res.toString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("tradeAnalysisInDays" + params.getString("org_id") + params.getString("begin")).set(res.toString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@ -330,11 +331,11 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
params.put("client_id", partner.getIntValue("client_id")); params.put("client_id", partner.getIntValue("client_id"));
clientManager.queryModifyClientIds(partner.getIntValue("client_id"), params); clientManager.queryModifyClientIds(partner.getIntValue("client_id"), params);
JSONObject today = getTransJSONO(params); JSONObject today = getTransJSONO(params);
params.put("begin",DateUtils.addDays(params.getDate("begin"),-1)); params.put("begin", DateUtils.addDays(params.getDate("begin"), -1));
params.put("end",DateUtils.addDays(params.getDate("end"),-1)); params.put("end", DateUtils.addDays(params.getDate("end"), -1));
JSONObject yes = getTransJSONO(params); JSONObject yes = getTransJSONO(params);
res.put("today",today); res.put("today", today);
res.put("yes",yes); res.put("yes", yes);
// res.put("not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params))); // res.put("not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params)));
// params.put("channel", "system"); // params.put("channel", "system");
// res.put("sys_not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params))); // res.put("sys_not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params)));
@ -342,18 +343,19 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
// res.put("rpaypmt_card_not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params))); // res.put("rpaypmt_card_not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params)));
// params.put("channel", "rpaypmt_dd"); // params.put("channel", "rpaypmt_dd");
// res.put("rpaypmt_dd_not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params))); // res.put("rpaypmt_dd_not_settled", new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params)));
res.put("trade_amount_rise",yes.getBigDecimal("trade_amount").compareTo(BigDecimal.ZERO)>0 ? (today.getBigDecimal("trade_amount").subtract(yes.getBigDecimal("trade_amount"))).divide(yes.getBigDecimal("trade_amount"), 4, RoundingMode.HALF_UP):BigDecimal.ZERO); res.put("trade_amount_rise", yes.getBigDecimal("trade_amount").compareTo(BigDecimal.ZERO) > 0 ? (today.getBigDecimal("trade_amount").subtract(yes.getBigDecimal("trade_amount"))).divide(yes.getBigDecimal("trade_amount"), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO);
res.put("trade_count_rise", yes.getIntValue("trade_count") > 0 ? (today.getBigDecimal("trade_count").subtract(yes.getBigDecimal("trade_count"))).divide(yes.getBigDecimal("trade_count"), 4, RoundingMode.HALF_UP) : 0); res.put("trade_count_rise", yes.getIntValue("trade_count") > 0 ? (today.getBigDecimal("trade_count").subtract(yes.getBigDecimal("trade_count"))).divide(yes.getBigDecimal("trade_count"), 4, RoundingMode.HALF_UP) : 0);
res.put("customers_rise", yes.getIntValue("customers") > 0 ? (today.getBigDecimal("customers").subtract(yes.getBigDecimal("customers"))).divide(yes.getBigDecimal("customers"), 4, RoundingMode.HALF_UP) : 0); res.put("customers_rise", yes.getIntValue("customers") > 0 ? (today.getBigDecimal("customers").subtract(yes.getBigDecimal("customers"))).divide(yes.getBigDecimal("customers"), 4, RoundingMode.HALF_UP) : 0);
params.remove("begin"); params.remove("begin");
params.remove("end"); params.remove("end");
List<JSONObject> logs = transactionMapper.listSettlementLogTotal(params); List<JSONObject> logs = transactionMapper.listSettlementLogTotal(params);
res.put("latest_settle",logs.isEmpty()?BigDecimal.ZERO:logs.get(0).getBigDecimal("income")); res.put("latest_settle", logs.isEmpty() ? BigDecimal.ZERO : logs.get(0).getBigDecimal("income"));
return res; return res;
} }
/** /**
* *
*
* @param partner * @param partner
* @param partnerAnalysisBean * @param partnerAnalysisBean
* @return * @return
@ -374,16 +376,17 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
params.put("hide_sub_mch", mchConfig.getBooleanValue("hide_sub_mch")); params.put("hide_sub_mch", mchConfig.getBooleanValue("hide_sub_mch"));
params.put("client_id", partner.getIntValue("client_id")); params.put("client_id", partner.getIntValue("client_id"));
clientManager.queryModifyClientIds(partner.getIntValue("client_id"), params); clientManager.queryModifyClientIds(partner.getIntValue("client_id"), params);
params.put("begin",DateUtils.addDays(params.getDate("begin"),-1)); params.put("begin", DateUtils.addDays(params.getDate("begin"), -1));
params.put("end",DateUtils.addDays(params.getDate("end"),-1)); params.put("end", DateUtils.addDays(params.getDate("end"), -1));
String key="not_settled"; String key = "not_settled";
if(StringUtils.isNotEmpty(params.getString("channel"))){ if (StringUtils.isNotEmpty(params.getString("channel"))) {
key=params.getString("channel")+"_not_settled"; key = params.getString("channel") + "_not_settled";
} }
res.put(key, new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params))); res.put(key, new BigDecimal(transactionAnalysisMapper.analysisNotSettled(params)));
return res; return res;
} }
private JSONObject getTransJSONO(JSONObject params){
private JSONObject getTransJSONO(JSONObject params) {
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
JSONObject object = transactionAnalysisMapper.getClientTransaction(params); JSONObject object = transactionAnalysisMapper.getClientTransaction(params);
res.put("trade_amount", object.getBigDecimal("trade_amount")); res.put("trade_amount", object.getBigDecimal("trade_amount"));
@ -391,6 +394,7 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
res.put("customers", transactionAnalysisMapper.countCustomers(params)); res.put("customers", transactionAnalysisMapper.countCustomers(params));
return res; return res;
} }
@Override @Override
public List<JSONObject> getTradeAnalysisInHours(JSONObject partner, AnalysisBean partnerAnalysisBean) { public List<JSONObject> getTradeAnalysisInHours(JSONObject partner, AnalysisBean partnerAnalysisBean) {
String timezone = partner.getJSONObject("client").getString("timezone"); String timezone = partner.getJSONObject("client").getString("timezone");
@ -446,22 +450,22 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
throw new ParamInvalidException("end", "error.payment.valid.invalid_time"); throw new ParamInvalidException("end", "error.payment.valid.invalid_time");
} }
List<JSONObject> listChannelRate = new ArrayList<>(); List<JSONObject> listChannelRate = new ArrayList<>();
Map<Date,JSONObject> exchangeRateMap = new TreeMap<>(); Map<Date, JSONObject> exchangeRateMap = new TreeMap<>();
String[] channels = {"Wechat","Alipay"}; String[] channels = {"Wechat", "Alipay"};
for (String channel:channels){ for (String channel : channels) {
channelsExchangeRate(beginDate, endDate, exchangeRateMap, channel); channelsExchangeRate(beginDate, endDate, exchangeRateMap, channel);
} }
for (Date date:exchangeRateMap.keySet()){ for (Date date : exchangeRateMap.keySet()) {
JSONObject total = exchangeRateMap.get(date); JSONObject total = exchangeRateMap.get(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
String time = sdf.format(date); String time = sdf.format(date);
total.put("date",time); total.put("date", time);
for (String channel:channels){ for (String channel : channels) {
if(total.getJSONObject(channel)==null){ if (total.getJSONObject(channel) == null) {
JSONObject mockAnalysis = new JSONObject(); JSONObject mockAnalysis = new JSONObject();
mockAnalysis.put("exchange_rate",0); mockAnalysis.put("exchange_rate", 0);
mockAnalysis.put("date",time); mockAnalysis.put("date", time);
total.put(channel,mockAnalysis); total.put(channel, mockAnalysis);
} }
} }
listChannelRate.add(total); listChannelRate.add(total);
@ -469,17 +473,17 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
return listChannelRate; return listChannelRate;
} }
public void channelsExchangeRate(Date beginDate,Date endDate,Map<Date, JSONObject> exchangeRateMap, String channel){ public void channelsExchangeRate(Date beginDate, Date endDate, Map<Date, JSONObject> exchangeRateMap, String channel) {
List<JSONObject> channelRates = exchangeRateMapper.listExchangeRates(beginDate, endDate,channel); List<JSONObject> channelRates = exchangeRateMapper.listExchangeRates(beginDate, endDate, channel);
for (JSONObject analysisItem:channelRates){ for (JSONObject analysisItem : channelRates) {
Date date = analysisItem.getDate("date"); Date date = analysisItem.getDate("date");
JSONObject dataItem = exchangeRateMap.get(date); JSONObject dataItem = exchangeRateMap.get(date);
if(dataItem==null){ if (dataItem == null) {
dataItem = new JSONObject(); dataItem = new JSONObject();
exchangeRateMap.put(date,dataItem); exchangeRateMap.put(date, dataItem);
} }
dataItem.put(channel,analysisItem); dataItem.put(channel, analysisItem);
} }
} }
@ -502,16 +506,16 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
public JSONObject getNewRecord() { public JSONObject getNewRecord() {
String jsonStr = stringRedisTemplate.boundValueOps("newRecord").get(); String jsonStr = stringRedisTemplate.boundValueOps("newRecord").get();
JSONObject newRecord = JSON.parseObject(jsonStr); JSONObject newRecord = JSON.parseObject(jsonStr);
if(newRecord != null){ if (newRecord != null) {
return newRecord; return newRecord;
} }
JSONObject max_amount = customerAndOrdersStatisticsMapper.getMaxAmount(0); JSONObject max_amount = customerAndOrdersStatisticsMapper.getMaxAmount(0);
JSONObject max_orders = customerAndOrdersStatisticsMapper.getMaxOrders(0); JSONObject max_orders = customerAndOrdersStatisticsMapper.getMaxOrders(0);
JSONObject maxTransactionPartners = customerAndOrdersStatisticsMapper.getMaxTransactionPartners(); JSONObject maxTransactionPartners = customerAndOrdersStatisticsMapper.getMaxTransactionPartners();
JSONObject res = new JSONObject(); JSONObject res = new JSONObject();
res.put("max_amount",max_amount); res.put("max_amount", max_amount);
res.put("max_orders",max_orders); res.put("max_orders", max_orders);
res.put("max_transaction_partners",maxTransactionPartners); res.put("max_transaction_partners", maxTransactionPartners);
stringRedisTemplate.boundValueOps("newRecord").set(res.toJSONString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("newRecord").set(res.toJSONString(), 5, TimeUnit.MINUTES);
return res; return res;
} }
@ -523,29 +527,31 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
} }
@Override @Override
public void generateCustomersAndOrdersStatistics(Date analysisDate){ public void generateCustomersAndOrdersStatistics(Date analysisDate) {
DateFormat format = new SimpleDateFormat("yyyyMMdd"); DateFormat format = new SimpleDateFormat("yyyyMMdd");
String date = format.format(analysisDate); String date = format.format(analysisDate);
List<JSONObject> clients =clientMapper.listClients(); List<JSONObject> clients = clientMapper.listClients();
for (JSONObject client:clients){ for (JSONObject client : clients) {
generateData(format, date, client); generateData(format, date, client);
} }
JSONObject totalObject = new JSONObject(); JSONObject totalObject = new JSONObject();
totalObject.put("client_id",0); totalObject.put("client_id", 0);
generateData(format,date,totalObject); generateData(format, date, totalObject);
logger.info("generate customer and orders statistics on {} done", date);
} }
/** /**
* *
*
* @param analysisDate * @param analysisDate
* @param clientId * @param clientId
*/ */
public void generateCustomersAndOrdersStatisticsByClientMoniker(Date analysisDate,String clientId){ public void generateCustomersAndOrdersStatisticsByClientMoniker(Date analysisDate, String clientId) {
DateFormat format = new SimpleDateFormat("yyyyMMdd"); DateFormat format = new SimpleDateFormat("yyyyMMdd");
String date = format.format(analysisDate); String date = format.format(analysisDate);
List<JSONObject> clients =clientMapper.listClients(); List<JSONObject> clients = clientMapper.listClients();
for (JSONObject client:clients){ for (JSONObject client : clients) {
if(client.getString("client_id").equals(clientId)){ if (client.getString("client_id").equals(clientId)) {
generateData(format, date, client); generateData(format, date, client);
} }
} }
@ -553,8 +559,8 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
private void generateData(DateFormat format, String date, JSONObject client) { private void generateData(DateFormat format, String date, JSONObject client) {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
String timezone =client.getString("timezone"); String timezone = client.getString("timezone");
if (timezone!=null){ if (timezone != null) {
format.setTimeZone(TimeZone.getTimeZone(timezone)); format.setTimeZone(TimeZone.getTimeZone(timezone));
} }
try { try {
@ -563,17 +569,17 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
} catch (ParseException e) { } catch (ParseException e) {
throw new ParamInvalidException("date", "error.payment.valid.invalid_date_format"); throw new ParamInvalidException("date", "error.payment.valid.invalid_date_format");
} }
if (client.getIntValue("client_id")!=0){ if (client.getIntValue("client_id") != 0) {
params.put("client_id",client.getIntValue("client_id")); params.put("client_id", client.getIntValue("client_id"));
} }
List<JSONObject> records = transactionAnalysisMapper.getCustomersAndOrdersInDaysUseInScheduled(params); List<JSONObject> records = transactionAnalysisMapper.getCustomersAndOrdersInDaysUseInScheduled(params);
if (!records.isEmpty()){ if (!records.isEmpty()) {
JSONObject deleteParams = new JSONObject(); JSONObject deleteParams = new JSONObject();
deleteParams.put("begin",params.getDate("begin")); deleteParams.put("begin", params.getDate("begin"));
deleteParams.put("end",params.getDate("end")); deleteParams.put("end", params.getDate("end"));
deleteParams.put("client_id",params.getIntValue("client_id")); deleteParams.put("client_id", params.getIntValue("client_id"));
customerAndOrdersStatisticsMapper.delete(deleteParams); customerAndOrdersStatisticsMapper.delete(deleteParams);
for (JSONObject record:records){ for (JSONObject record : records) {
String sdate = format.format(record.getDate("date")); String sdate = format.format(record.getDate("date"));
try { try {
params.put("begin", format.parse(sdate)); params.put("begin", format.parse(sdate));
@ -581,17 +587,17 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
} catch (ParseException e) { } catch (ParseException e) {
throw new ParamInvalidException("date", "error.payment.valid.invalid_date_format"); throw new ParamInvalidException("date", "error.payment.valid.invalid_date_format");
} }
params.put("channel",record.getString("channel")); params.put("channel", record.getString("channel"));
int newCustomers = 0; int newCustomers = 0;
int oldCustomers = transactionAnalysisMapper.countOldCustomers(params); int oldCustomers = transactionAnalysisMapper.countOldCustomers(params);
if (client.getIntValue("client_id")==0){ if (client.getIntValue("client_id") == 0) {
newCustomers = record.getIntValue("customers")-oldCustomers; newCustomers = record.getIntValue("customers") - oldCustomers;
}else { } else {
newCustomers = transactionAnalysisMapper.countNewCustomers(params); newCustomers = transactionAnalysisMapper.countNewCustomers(params);
} }
record.put("new_customers",newCustomers); record.put("new_customers", newCustomers);
record.put("old_customers",oldCustomers); record.put("old_customers", oldCustomers);
record.put("client_id",client.getIntValue("client_id")); record.put("client_id", client.getIntValue("client_id"));
if (StringUtils.equalsIgnoreCase("rpaypmt_card", record.getString("channel")) || StringUtils.equalsIgnoreCase("rpaypmt_dd", record.getString("channel"))) { if (StringUtils.equalsIgnoreCase("rpaypmt_card", record.getString("channel")) || StringUtils.equalsIgnoreCase("rpaypmt_dd", record.getString("channel"))) {
record.put("single_amount", record.getBigDecimal("total").divide(record.getBigDecimal("orders"), 2, RoundingMode.HALF_UP)); record.put("single_amount", record.getBigDecimal("total").divide(record.getBigDecimal("orders"), 2, RoundingMode.HALF_UP));
} }
@ -599,37 +605,40 @@ public class DashboardServiceImpl implements DashboardService,DashboardAnalysisT
} }
} }
} }
@Override @Override
public JSONObject getPlatformAmount(JSONObject params) { public JSONObject getPlatformAmount(JSONObject params) {
String jsonStr = stringRedisTemplate.boundValueOps("org_ChannelAnalysis" + params.getString("org_id") + params.getString("begin")).get(); String jsonStr = stringRedisTemplate.boundValueOps("org_ChannelAnalysis" + params.getString("org_id") + params.getString("begin")).get();
JSONObject channelAnalysis = JSON.parseObject(jsonStr); JSONObject channelAnalysis = JSON.parseObject(jsonStr);
if(channelAnalysis != null){ if (channelAnalysis != null) {
return channelAnalysis; return channelAnalysis;
}else { } else {
JSONObject resp; JSONObject resp;
if(params.getDate("end").compareTo(new Date()) > 0){ if (params.getDate("end").compareTo(new Date()) > 0) {
if(params.getDate("begin").compareTo(DateUtils.addDays(params.getDate("end"), -1))==0){ if (params.getDate("begin").compareTo(DateUtils.addDays(params.getDate("end"), -1)) == 0) {
resp = getTodayChannelCount(params); resp = getTodayChannelCount(params);
}else { } else {
resp = customerAndOrdersStatisticsMapper.getChannelCommonCount(params); resp = customerAndOrdersStatisticsMapper.getChannelCommonCount(params);
} }
}else { } else {
resp = customerAndOrdersStatisticsMapper.getChannelCommonCount(params); resp = customerAndOrdersStatisticsMapper.getChannelCommonCount(params);
} }
stringRedisTemplate.boundValueOps("org_ChannelAnalysis"+ params.getString("org_id")+params.getString("begin")).set(resp.toJSONString(), 5, TimeUnit.MINUTES); stringRedisTemplate.boundValueOps("org_ChannelAnalysis" + params.getString("org_id") + params.getString("begin")).set(resp.toJSONString(), 5, TimeUnit.MINUTES);
return resp; return resp;
} }
} }
private JSONObject getTodayChannelCount(JSONObject params){
private JSONObject getTodayChannelCount(JSONObject params) {
String[] channels = new String[]{"Wechat", "Alipay", "AlipayOnline", "Rpay", "rpaypmt_card", "rpaypmt_dd"}; String[] channels = new String[]{"Wechat", "Alipay", "AlipayOnline", "Rpay", "rpaypmt_card", "rpaypmt_dd"};
JSONObject resp = new JSONObject(); JSONObject resp = new JSONObject();
for(String channel:channels) { for (String channel : channels) {
params.put("channel", channel); params.put("channel", channel);
resp.put(channel.toLowerCase()+"amount", transactionAnalysisMapper.getPlatformAmount(params).getBigDecimal("amount")); resp.put(channel.toLowerCase() + "amount", transactionAnalysisMapper.getPlatformAmount(params).getBigDecimal("amount"));
resp.put(channel.toLowerCase()+"_order_count", transactionAnalysisMapper.getPlatformAmount(params).getIntValue("order_count")); resp.put(channel.toLowerCase() + "_order_count", transactionAnalysisMapper.getPlatformAmount(params).getIntValue("order_count"));
} }
return resp; return resp;
} }
@Override @Override
public JSONObject getOrderAndCustomerPerDay(JSONObject partner, AnalysisBean partnerAnalysisBean) { public JSONObject getOrderAndCustomerPerDay(JSONObject partner, AnalysisBean partnerAnalysisBean) {
String timezone = partner.getJSONObject("client").getString("timezone"); String timezone = partner.getJSONObject("client").getString("timezone");

@ -441,11 +441,9 @@
</select> </select>
<select id="countOldCustomers" resultType="java.lang.Integer"> <select id="countOldCustomers" resultType="java.lang.Integer">
<![CDATA[ <![CDATA[
SELECT COUNT(DISTINCT customer_id) FROM (SELECT o.customer_id FROM pmt_transactions t SELECT COUNT(customer_id) FROM (
INNER JOIN pmt_orders o ON o.order_id = t.order_id SELECT o.customer_id,min(o.create_time) first_pay,max(o.create_time) last_pay FROM pmt_orders o
]]> WHERE o.create_time < #{end} and o.status>4
<![CDATA[
WHERE o.create_time >= #{begin} AND o.create_time < #{end}
]]> ]]>
<if test="client_ids!=null"> <if test="client_ids!=null">
AND o.client_id IN AND o.client_id IN
@ -456,29 +454,15 @@
<if test="client_ids==null and client_id != null"> <if test="client_ids==null and client_id != null">
and o.client_id=#{client_id} and o.client_id=#{client_id}
</if> </if>
<if test="org_id!=null and org_ids==null">and t.org_id=#{org_id}</if> <if test="org_id!=null and org_ids==null">and o.org_id=#{org_id}</if>
<if test="org_ids!=null">and t.org_id in <if test="org_ids!=null">and o.org_id in
<foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">#{org_id}</foreach> <foreach collection="org_ids" item="org_id" open="(" close=")" separator=",">#{org_id}</foreach>
</if> </if>
<if test="channel !=null">and o.channel = #{channel}</if> <if test="channel !=null">and o.channel = #{channel}</if>
<![CDATA[ <![CDATA[
GROUP BY o.customer_id) p GROUP BY o.customer_id
where p.customer_id IN (SELECT o.customer_id FROM pmt_transactions t INNER JOIN pmt_orders o ON o.order_id = t.order_id having first_pay<#{begin} and last_pay>=#{begin}
]]> ) p
<![CDATA[
WHERE o.create_time < #{begin}
]]>
<if test="client_ids!=null">
AND o.client_id IN
<foreach collection="client_ids" open="(" close=")" separator="," item="client_id">
#{client_id}
</foreach>
</if>
<if test="client_ids==null and client_id != null">
and o.client_id=#{client_id}
</if>
<![CDATA[
)
]]> ]]>
</select> </select>

Loading…
Cancel
Save