BusinessAnalysisService.java
19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package cn.com.glsx.busassistant.service;
import cn.com.glsx.busassistant.request.businessAnalysis.BusinessAnalysisRequest;
import cn.com.glsx.busassistant.response.businessAnalysis.BusinessAnalysisFlowAnalysisResponse;
import cn.com.glsx.busassistant.response.businessAnalysis.BusinessAnalysisOverviewResponse;
import cn.com.glsx.busassistant.response.businessAnalysis.BusinessAnalysisPurchaseTendencyResponse;
import cn.com.glsx.busassistant.util.CommonUtil;
import cn.com.glsx.busassistant.util.DateUtils;
import cn.com.glsx.dj.smartcarlife.constant.CommonConstants;
import cn.com.glsx.dj.smartcarlife.dto.LifeCustomerCouponDTO;
import cn.com.glsx.dj.smartcarlife.dto.OrderSummaryDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.BusinessAnalysisFlowRankDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.MerchantPurchaseTendencyDiagramDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.NameValueDTO;
import cn.com.glsx.dj.smartcarlife.dto.businessassistant.PurchaseOrderGoodsBaseDTO;
import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum;
import cn.com.glsx.dj.smartcarlife.remote.LifeCustomerCouponRemote;
import cn.com.glsx.dj.smartcarlife.remote.OrderSummaryRemote;
import cn.com.glsx.dj.smartcarlife.remote.PurchaseOrderRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.rop.response.RopResponse;
import com.google.common.collect.Lists;
import com.rop.AbstractRopRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = RpcServiceException.class)
public class BusinessAnalysisService {
private static final Integer SOLAR_FILE_TYPE = 1;
private static final String REFUSAL_REASON1 = "客户体验了,还是不想升级";
private static final String REFUSAL_REASON2 = "价格太高";
private static final String REFUSAL_REASON3 = "客户没来现场,他人代办";
private static final String REFUSAL_REASON4 = "不是本人的车";
private static final String REFUSAL_REASON5 = "其他";
@Autowired
private OrderSummaryRemote orderSummaryRemote;
@Autowired
private LifeCustomerCouponRemote lifeCustomerCouponRemote;
@Autowired
private PurchaseOrderRemote purchaseOrderRemote;
public RopResponse<BusinessAnalysisOverviewResponse> overview(BusinessAnalysisRequest request) {
try {
BusinessAnalysisOverviewResponse businessAnalysisOverviewResponse = new BusinessAnalysisOverviewResponse(0, 0);
//查询所有关联4s店的业务产值数据
setBusinessAnalysisOverviewTotalMoney(businessAnalysisOverviewResponse, request);
//查询所有关联园区店的某月采购支出数据总值
setBusinessAnalysisOverviewPurchaseMoney(businessAnalysisOverviewResponse, request);
return RopResponse.success(businessAnalysisOverviewResponse);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
private void setBusinessAnalysisOverviewTotalMoney(BusinessAnalysisOverviewResponse businessAnalysisOverviewResponse, BusinessAnalysisRequest request) {
List<Integer> merchantIdsByPartnerMerchantId = CommonUtil.getShopMerchantIds(SceneTypeEnum.SCENE1_1.getValue());
OrderSummaryDTO orderSummaryDTO = new OrderSummaryDTO();
orderSummaryDTO.setShopMerchantIdList(merchantIdsByPartnerMerchantId);
orderSummaryDTO.setStartPayTime(request.getStartDate() + CommonConstants.DATE_START_TIME);
orderSummaryDTO.setEndPayTime(request.getEndDate() + CommonConstants.DATE_END_TIME);
log.info("生意助手经营分析,查询overview业务产值数据入参:{}", orderSummaryDTO.toString());
RpcResponse<List<OrderSummaryDTO>> summaryOutputResponse = orderSummaryRemote.getSummaryOutputValueList(orderSummaryDTO);
log.info("生意助手经营分析,查询overview业务产值数据返回结果:{}", summaryOutputResponse.getResult());
if (summaryOutputResponse.hasResult()) {
//太阳膜总产值
BigDecimal solarTotalMoney = summaryOutputResponse.getResult()
.stream().map(OrderSummaryDTO::getTotalSubOrderOrderMoney).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
solarTotalMoney = solarTotalMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
businessAnalysisOverviewResponse.setTotalMoney(solarTotalMoney.intValue());
} else {
log.error("生意助手经营分析,查询overview业务产值数据失败:{}", summaryOutputResponse.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
private void setBusinessAnalysisOverviewPurchaseMoney(BusinessAnalysisOverviewResponse businessAnalysisOverviewResponse, BusinessAnalysisRequest request) {
List<Integer> allMerchantIds = CommonUtil.getAllMerchantIds();
PurchaseOrderGoodsBaseDTO condition = new PurchaseOrderGoodsBaseDTO();
//查询总采购价
condition.setGoodsTypeList(Lists.newArrayList(SOLAR_FILE_TYPE));
condition.setMerchantIdList(allMerchantIds);
condition.setOrderTimeStart(request.getStartDate() + CommonConstants.DATE_START_TIME);
condition.setOrderTimeEnd(request.getEndDate() + CommonConstants.DATE_END_TIME);
log.info("生意助手经营分析,查询overview采购支出数据入参:{}", condition);
RpcResponse<PurchaseOrderGoodsBaseDTO> response = purchaseOrderRemote.getPurchaseOrderGoodsInfo(condition);
log.info("生意助手经营分析,查询overview采购支出数据结果:{}", response.getResult());
if (response.isSuccess()) {
PurchaseOrderGoodsBaseDTO result = response.getResult();
if (Objects.isNull(result)) {
return;
}
BigDecimal purchaseMoney = result.getGoodsTotalPrice();
if (Objects.nonNull(purchaseMoney)) {
purchaseMoney = purchaseMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
businessAnalysisOverviewResponse.setPurchaseMoney(purchaseMoney.intValue());
}
} else {
log.error("生意助手经营分析,查询overview采购支出数据失败:{}", response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
public RopResponse<BusinessAnalysisFlowAnalysisResponse> flowRankInfo(BusinessAnalysisRequest request) {
try {
BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse = new BusinessAnalysisFlowAnalysisResponse();
LifeCustomerCouponDTO condition = buildFlowRankCondition(request);
condition.setLimit(3);
//查询门店引流排行前三数据
setFlowRankList(businessAnalysisFlowAnalysisResponse, condition);
//查询引流分析数据
setPieChartList(businessAnalysisFlowAnalysisResponse, condition);
return RopResponse.success(businessAnalysisFlowAnalysisResponse);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
public LifeCustomerCouponDTO buildFlowRankCondition(BusinessAnalysisRequest request){
LifeCustomerCouponDTO condition = new LifeCustomerCouponDTO();
condition.setMerchantIdList(CommonUtil.getShopMerchantIds(SceneTypeEnum.SCENE1_1.getValue()));
condition.setCouponTypes(Lists.newArrayList(SOLAR_FILE_TYPE.byteValue()));
String startTime = request.getStartDate() + CommonConstants.DATE_START_TIME;
String endTime = request.getEndDate() + CommonConstants.DATE_END_TIME;
//筛选发券时间
condition.setStartDate(startTime);
condition.setEndDate(endTime);
return condition;
}
/**
* 门店排行数据
* @param businessAnalysisFlowAnalysisResponse
* @param condition
*/
private void setFlowRankList(BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse, LifeCustomerCouponDTO condition) {
log.info("生意助手经营分析,查询门店引流排行前三入参:{}", condition);
RpcResponse<List<BusinessAnalysisFlowRankDTO>> response = lifeCustomerCouponRemote.listForCouponRank(condition);
log.info("生意助手经营分析,查询门店引流排行前三结果:{}", response.getResult());
if (response.hasResult()) {
businessAnalysisFlowAnalysisResponse.setFlowRankList(response.getResult());
} else {
log.error("生意助手经营分析,查询门店引流排行前三失败:{}", response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
/**
* 引流分析数据
* @param businessAnalysisFlowAnalysisResponse
* @param condition
*/
private void setPieChartList(BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse, LifeCustomerCouponDTO condition) {
log.info("生意助手经营分析,查询引流分析数据入参:{}", condition);
RpcResponse<List<BusinessAnalysisFlowRankDTO>> response = lifeCustomerCouponRemote.listForCouponNotUpgradeOrder(condition);
List<BusinessAnalysisFlowRankDTO> result = response.getResult();
log.info("生意助手经营分析,查询引流分析数据结果:{}", result);
if (response.hasResult()) {
buildPieChart(result, businessAnalysisFlowAnalysisResponse);
} else {
log.error("生意助手经营分析,查询引流分析数据结果失败:{}", response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
/**
* 构建引流分析饼图
* @param result
* @param businessAnalysisFlowAnalysisResponse
*/
private void buildPieChart(List<BusinessAnalysisFlowRankDTO> result, BusinessAnalysisFlowAnalysisResponse businessAnalysisFlowAnalysisResponse) {
if (CollectionUtils.isEmpty(result)) {
businessAnalysisFlowAnalysisResponse.setPieChartList(Lists.newArrayList());
return;
}
List<NameValueDTO> pieChartList = Lists.newArrayList();
NameValueDTO chart1 = new NameValueDTO(REFUSAL_REASON1, 0);
Integer chart1Value = 0;
NameValueDTO chart2 = new NameValueDTO(REFUSAL_REASON2, 0);
Integer chart2Value = 0;
NameValueDTO chart3 = new NameValueDTO(REFUSAL_REASON3, 0);
Integer chart3Value = 0;
NameValueDTO chart4 = new NameValueDTO(REFUSAL_REASON4, 0);
Integer chart4Value = 0;
NameValueDTO chart5 = new NameValueDTO(REFUSAL_REASON5, 0);
Integer chart5Value = 0;
for (BusinessAnalysisFlowRankDTO businessAnalysisFlowRankDTO : result) {
String refusalReason = businessAnalysisFlowRankDTO.getRefusalReason();
if (StringUtils.isBlank(refusalReason)) {
continue;
}
boolean containsGiveReason = false;
if (refusalReason.indexOf(REFUSAL_REASON1) >= 0) {
chart1Value++;
containsGiveReason = true;
}
if (refusalReason.indexOf(REFUSAL_REASON2) >= 0) {
chart2Value++;
containsGiveReason = true;
}
if (refusalReason.indexOf(REFUSAL_REASON3) >= 0) {
chart3Value++;
containsGiveReason = true;
}
if (refusalReason.indexOf(REFUSAL_REASON4) >= 0) {
chart4Value++;
containsGiveReason = true;
}
// 不包含以上4种拒绝原因,算作其他
if (!containsGiveReason) {
chart5Value++;
}
}
chart1.setValue(chart1Value);
chart2.setValue(chart2Value);
chart3.setValue(chart3Value);
chart4.setValue(chart4Value);
chart5.setValue(chart5Value);
pieChartList.add(chart1);
pieChartList.add(chart2);
pieChartList.add(chart3);
pieChartList.add(chart4);
pieChartList.add(chart5);
businessAnalysisFlowAnalysisResponse.setPieChartList(pieChartList);
}
public RopResponse<RpcPagination<BusinessAnalysisFlowRankDTO>> flowRankPage(BusinessAnalysisRequest businessAnalysisRequest) {
try {
RpcPagination<LifeCustomerCouponDTO> rpcPagination = CommonUtil.getRpcPagination(LifeCustomerCouponDTO.class);
LifeCustomerCouponDTO condition = buildFlowRankCondition(businessAnalysisRequest);
rpcPagination.setCondition(condition);
RpcResponse<RpcPagination<BusinessAnalysisFlowRankDTO>> response = lifeCustomerCouponRemote.listForCouponRankPage(rpcPagination);
if (!response.hasResult()) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
return RopResponse.success(response.getResult());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
public RopResponse<BusinessAnalysisPurchaseTendencyResponse> purchaseTendency(AbstractRopRequest request) {
BusinessAnalysisPurchaseTendencyResponse result = new BusinessAnalysisPurchaseTendencyResponse();
PurchaseOrderGoodsBaseDTO condition = buildPurchaseOrderGoodsBaseDTO();
//查询总采购价、采购数量
setPurchaseTotalInfo(condition, result);
// 查询近半年采购金额趋势数据
setDiagramList(condition, result);
return RopResponse.success(result);
}
private PurchaseOrderGoodsBaseDTO buildPurchaseOrderGoodsBaseDTO() {
List<Integer> allMerchantIds = CommonUtil.getAllMerchantIds();
PurchaseOrderGoodsBaseDTO condition = new PurchaseOrderGoodsBaseDTO();
//查询总采购价
condition.setGoodsTypeList(Lists.newArrayList(SOLAR_FILE_TYPE));
condition.setMerchantIdList(allMerchantIds);
return condition;
}
private void setDiagramList(PurchaseOrderGoodsBaseDTO condition, BusinessAnalysisPurchaseTendencyResponse result) {
//查询近半年的数据
List<MerchantPurchaseTendencyDiagramDTO> diagramList = Lists.newArrayList();
List<String> monthList = DateUtils.getLatestMonthList(12);
for (String month : monthList) {
MerchantPurchaseTendencyDiagramDTO diagramDTO = new MerchantPurchaseTendencyDiagramDTO();
diagramDTO.setAbscissaData(month);
diagramList.add(diagramDTO);
}
condition.setOrderTimeStart(DateUtils.getMonthStartTime(0, 12, true));
condition.setOrderTimeEnd(DateUtils.getMonthEndTime(0, 0, true));
log.info("生意助手经营分析,查询采购金额走势数据入参:{}", condition);
RpcResponse<List<PurchaseOrderGoodsBaseDTO>> response = purchaseOrderRemote.getPurchaseOrderGoodsPurchaseMoneyList(condition);
log.info("生意助手经营分析,查询采购金额走势数据结果:{}", response.getResult());
if (!response.hasResult()) {
log.error("生意助手经营分析,查询门店引流排行前三失败:{}", response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
List<PurchaseOrderGoodsBaseDTO> stockMonthReportDTOList = response.getResult();
for (MerchantPurchaseTendencyDiagramDTO diagramDTO : diagramList) {
for (PurchaseOrderGoodsBaseDTO purchaseOrderGoodsBaseDTO : stockMonthReportDTOList) {
if (Objects.equals(diagramDTO.getAbscissaData(), purchaseOrderGoodsBaseDTO.getDataMonth())) {
BigDecimal purchaseMoney = purchaseOrderGoodsBaseDTO.getGoodsTotalPrice();
if (Objects.nonNull(purchaseMoney)) {
purchaseMoney = purchaseMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
}
diagramDTO.setPurchaseMoney(purchaseMoney.intValue());
}
}
}
result.setDiagramList(diagramList);
}
/**
* 查询总采购价、总采购数量
* @param condition
* @param result
*/
private void setPurchaseTotalInfo(PurchaseOrderGoodsBaseDTO condition, BusinessAnalysisPurchaseTendencyResponse result) {
log.info("生意助手经营分析,查询总采购价、总采购数量入参:{}", condition);
RpcResponse<PurchaseOrderGoodsBaseDTO> response = purchaseOrderRemote.getPurchaseOrderGoodsInfo(condition);
PurchaseOrderGoodsBaseDTO purchaseOrderGoodsBaseDTO = response.getResult();
log.info("生意助手经营分析,查询总采购价、总采购数量结果:{}", purchaseOrderGoodsBaseDTO);
if (response.isSuccess()) {
if (Objects.isNull(purchaseOrderGoodsBaseDTO)) {
return;
}
Integer purchaseNum = purchaseOrderGoodsBaseDTO.getGoodsNumber();
BigDecimal purchaseMoney = purchaseOrderGoodsBaseDTO.getGoodsTotalPrice();
result.setPurchaseNum(purchaseNum);
if (Objects.nonNull(purchaseMoney)) {
purchaseMoney = purchaseMoney.setScale(0, BigDecimal.ROUND_HALF_UP);
result.setPurchaseMoney(purchaseMoney.intValue());
}
} else {
log.error("生意助手经营分析,查询引流分析数据结果失败:{}", response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
public RopResponse<RpcPagination<PurchaseOrderGoodsBaseDTO>> purchasePage(AbstractRopRequest request) {
try {
RpcPagination<PurchaseOrderGoodsBaseDTO> rpcPagination = CommonUtil.getRpcPagination(PurchaseOrderGoodsBaseDTO.class);
PurchaseOrderGoodsBaseDTO condition = buildPurchaseOrderGoodsBaseDTO();
rpcPagination.setCondition(condition);
log.info("生意助手经营分析,采购数量分页查询入参:{}", condition);
RpcResponse<RpcPagination<PurchaseOrderGoodsBaseDTO>> response = purchaseOrderRemote.getPurchaseOrderGoodsPurchaseNumberRankPage(rpcPagination);
log.info("生意助手经营分析,采购数量分页查询结果:{}", response.getResult());
if (!response.hasResult()) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
return RopResponse.success(response.getResult());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
}