ActivityService.java
15.5 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
359
package cn.com.glsx.dj.weapp.service;
import cn.com.glsx.dj.marketing.dto.*;
import cn.com.glsx.dj.marketing.model.ShopActivityCarBrandPriceRefer;
import cn.com.glsx.dj.marketing.model.ShopActivityInfo;
import cn.com.glsx.dj.marketing.model.ShopActivityOperationLog;
import cn.com.glsx.dj.marketing.remote.ShopActivityRemote;
import cn.com.glsx.dj.marketing.remote.WeappActivityRemote;
import cn.com.glsx.dj.marketing.remote.WeappReportRemote;
import cn.com.glsx.dj.marketing.remote.WxShopActivityRemote;
import cn.com.glsx.dj.system.dto.MerchantDTO;
import cn.com.glsx.dj.system.dto.UserDTO;
import cn.com.glsx.dj.system.remote.MerchantRemote;
import cn.com.glsx.dj.system.remote.UserRemote;
import cn.com.glsx.dj.weapp.util.ThreadLocalCache;
import cn.com.glsx.dj.weapp.vo.*;
import cn.com.glsx.dj.wechat.model.WechatUserInfo;
import cn.com.glsx.framework.core.beans.ResultEntity;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import org.oreframework.util.collection.ListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Service
public class ActivityService{
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private WeappActivityRemote weappActivityRemote;
@Autowired
private WxShopActivityRemote wxShopActivityRemote;
@Autowired
private ShopActivityRemote shopActivityRemote;
@Autowired
private WeappReportRemote weappReportRemote;
@Autowired
private MerchantRemote merchantRemote;
@Autowired
private UserRemote userRemote;
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Autowired
private Environment env;
private static final Integer SCAN = 4;
private static final String SCAN_ACTIVITY= "scan_activity_with_accountid_";
private static final String SCAN_VEDIO="scan_vedio_with_accountid_";
/**
* 首页活动列表
* @param vo
* merchantId 商户id
* status null:全部,2:未开始,3:进行中,4:已结束,5:已下架
* @author yangjia
* @date 2020/02/03
* */
public ResultEntity<RpcPagination<ShopActivityInfo>> listActivityByStatus(ActivityVO vo){
logger.info("listActivityByStatus by condition:{}", vo);
if(vo.getPageNum() == null){
vo.setPageNum(1);
}
if(vo.getPageSize() == null){
vo.setPageSize(10);
}
ShopActivityInfo info = new ShopActivityInfo();
info.setMerchantId(vo.getMerchantId());
//activityStatus: 全部:null;进行中:2;已结束:4;已下架:5
info.setActivityStatus(vo.getStatus());
RpcPagination<ShopActivityInfo> pageCondition = new RpcPagination<>();
pageCondition.setCondition(info);
pageCondition.setPageNum(vo.getPageNum());
pageCondition.setPageSize(vo.getPageSize());
RpcResponse<RpcPagination<ShopActivityInfo>> rpcResponse = weappActivityRemote.listActivityByStatus(pageCondition);
return ResultEntity.result(rpcResponse);
}
/**
* @Description: 通过条件查询活动详情
* @param detailVO activityId 活动id
* merchantId 商户id
* userId 用户id dj_system_user
* @return ResultEntity<ShopActivityInfoDTO>
* @author yangjia
* @Date: 2020/02/03
*/
public ResultEntity<ShopActivityInfoVO> detail(MyActivityDetailVO detailVO) {
logger.info("ActivityService.detail by vo:{}", detailVO);
if(detailVO == null || detailVO.getActivityId() == null){
return ResultEntity.error(null,"活动必要参数为空");
}
ResultEntity<ShopActivityInfoVO> resultEntity = new ResultEntity<>();
ShopActivityInfoVO vo = new ShopActivityInfoVO();
Integer accountId = ThreadLocalCache.getAccountId();
String phone = ThreadLocalCache.getMobile();
WxShopActivityInfoDTO dto = new WxShopActivityInfoDTO();
dto.setId(detailVO.getActivityId().longValue());
dto.setAccountId(accountId);
dto.setPhone(phone);
dto.setUserId(detailVO.getUserId());
RpcResponse<ShopActivityInfoDTO> rpcResponse = wxShopActivityRemote.getItemShopActivity(dto);
if(rpcResponse.getResult() != null){
ShopActivityInfoDTO a = rpcResponse.getResult();
Integer carType = 2;
if(a.getFlashSalePrice() == null || carType.equals(a.getCarType())){
List<ShopActivityCarBrandPriceRefer> carBrandPriceRefers = a.getPriceList();
if(ListUtil.isNotEmpty(carBrandPriceRefers)){
BigDecimal p = carBrandPriceRefers.get(0).getPrice();
if(p != null){
a.setFlashSalePrice(p.doubleValue());
}
}
}
//发送用户群体:0全部用户,1精准用户,-1自定义用户
Integer accurateUserType = 1;
if(accurateUserType.equals(a.getSelectionUserType())){
RpcResponse<List<ActivityInfoConditionConfigDTO>> configRpc = shopActivityRemote.
getShopActivitySendConfigById(detailVO.getActivityId());
if(configRpc.getResult() != null){
vo.setConfigList(configRpc.getResult());
}
}
vo.setActivity(a);
//活动是否绑定优惠券
if(a.getBindCoupon() > 0){
RpcResponse<List<WxActivityCouponUserDTO>> relatedCpuopnListResp = wxShopActivityRemote.listShopActivityCoupon(detailVO.getActivityId().longValue());
LinkedHashSet<WxActivityCouponUserDTO> couponList = new LinkedHashSet<>();
List<CouponMapVo> couponMapList = new ArrayList<>();
if(relatedCpuopnListResp.getResult() != null){
List<WxActivityCouponUserDTO> l = relatedCpuopnListResp.getResult();
l.forEach(e -> {
Map<Long, Integer> m = e.getCouponMap();
if(!m.isEmpty()){
CouponMapVo couponMapVo = new CouponMapVo();
m.forEach((k, v) -> {
for(int i = 0;i< v;i++){
couponList.add(e);
}
couponMapVo.setId(k);
couponMapVo.setNum(v);
});
couponMapList.add(couponMapVo);
}
});
}
vo.setCouponList(couponList);
vo.setCouponMapList(couponMapList);
}
//获取活动浏览统计数据
ShopActivityScanDataDTO scanCondition = new ShopActivityScanDataDTO();
scanCondition.setActivityId(detailVO.getActivityId());
scanCondition.setMerchantId(detailVO.getMerchantId());
if(detailVO.getUserId() != null){
scanCondition.setUserId(detailVO.getUserId());
}else{
if(detailVO.getMerchantId() != null){
RpcResponse<MerchantDTO> merchantRpcResponse = merchantRemote.getAllInfoByMerchantId(detailVO.getMerchantId());
if(merchantRpcResponse.isSuccess() && merchantRpcResponse.hasResult()) {
MerchantDTO merchantDTO = merchantRpcResponse.getResult();
scanCondition.setUserId(merchantDTO.getUserId());
}
}
}
scanCondition.setCountType(1);//只查活动
RpcResponse<ShopActivityScanDataDTO> scanDataRpc = weappReportRemote.getScanDataByActivityIdOrVideoId(scanCondition);
logger.info("getScanDataByActivityId result:{}",scanDataRpc);
if(scanDataRpc.getResult() != null){
ShopActivityScanDataDTO scanDataDTO = scanDataRpc.getResult();
if(scanDataDTO.getTodayPayNum() == null){
scanDataDTO.setTodayPayNum(0);
}
if(scanDataDTO.getTodayScanNum() == null){
scanDataDTO.setTodayScanNum(0);
}
if(scanDataDTO.getTotalPayNum() == null){
scanDataDTO.setTotalPayNum(0);
}
if(scanDataDTO.getTotalScanNum() == null){
scanDataDTO.setTotalScanNum(0);
}
vo.setScanData(scanDataDTO);
}
}
logger.info("getItemShopActivity result:{}",vo);
resultEntity.setData(vo);
return ResultEntity.ok(vo);
}
/**
*操作日志记录
*@param dto
* merchantId 商户id
* employeeId 操作人为员工身份时必传
* activityId 活动id
* referActivityId 分享活动不用传,其他必传
* userId 账户id
* wechatName
* wechatLogo
* mobile
* operationType 操作类型
* ACTIVITY_CREATE(1, "创建活动"),
ACTIVITY_SHARE(2, "分享活动"),
ACTIVITY_EDIT(3, "编辑活动"),
SCAN(4, "浏览"),
ORDER_CREATE(5, "生成订单"),
ORDER_CANCEL(6, "取消订单"),
ORDER_PAY(7, "购买成功"),
ORDER_VERIFCATE(8, "核销订单");
CONSULT(9, "咨询");
* orderId
* payPrice 支付金额
* createBy 浏览时传用户手机号
* */
public ResultEntity<Integer> createLogRecord(ShopActivityOperationLogDTO dto){
logger.info("ActivityService.createLogRecord by dto:{}",dto);
if(dto == null){
return ResultEntity.error(null,"入参为空");
}
Integer accountId = ThreadLocalCache.getAccountId();
Date date = new Date();
//浏览商品,3分钟内仅计一条记录
if(SCAN.equals(dto.getOperationType())){
String key = "";
if(null != dto.getActivityId()){
key = SCAN_ACTIVITY.concat(dto.getActivityId().toString());
if(accountId != null){
key = key.concat("_" + accountId);
}
}else if(null != dto.getVideoId()){
key = SCAN_VEDIO.concat(dto.getVideoId().toString());
if(accountId != null){
key = key.concat("_" + accountId);
}
}
boolean has = redisTemplate.hasKey(key);
if (has){
return ResultEntity.error(null,"浏览商品在3分钟内");
}
ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
if(accountId == null){
accountId = 0;
}
valueOps.set(key, accountId);
redisTemplate.expire(key, 180, TimeUnit.SECONDS);
}
if(dto.getUserId() == null){
if(dto.getEmployeeId() == null && dto.getMerchantId() != null){
RpcResponse<MerchantDTO> merchantRpcResponse = merchantRemote.getAllInfoByMerchantId(dto.getMerchantId());
if(merchantRpcResponse.isSuccess() && merchantRpcResponse.hasResult()) {
MerchantDTO merchantDTO = merchantRpcResponse.getResult();
dto.setUserId(merchantDTO.getUserId());
}
}
if(dto.getEmployeeId() != null){
RpcResponse<UserDTO> userRpc = userRemote.getByEmployeeId(dto.getEmployeeId());
if(userRpc.isSuccess() && userRpc.hasResult()){
UserDTO userDTO = userRpc.getResult();
dto.setUserId(userDTO.getId());
}
}
}
WechatUserInfo userInfo = ThreadLocalCache.getWechatUserInfo();
if(userInfo != null){
if(dto.getWechatName() == null){
dto.setWechatName(userInfo.getNickName());
}
if(dto.getWechatLogo() == null){
dto.setWechatLogo(userInfo.getLogo());
}
if(dto.getCreatedBy() == null){
dto.setCreatedBy(userInfo.getPhone());
}
if(dto.getMobile() == null){
dto.setMobile(userInfo.getPhone());
}
}
if(dto.getWechatName() == null){
dto.setWechatName("微信用户");
}
dto.setCreatedDate(date);
dto.setRandomKey(UUID.randomUUID().toString());
RpcResponse<Integer> rpcResponse = weappActivityRemote.createLogRecord(dto);
return ResultEntity.result(rpcResponse);
}
/**
* 获取活动操作日志
* @param vo
* merchantId 商户id
* employeeId 操作人为员工身份时必传
* activityId 活动id
* userId
* @author yangjia
* @date 2020/02/03
* */
public ResultEntity<RpcPagination<ShopActivityOperationLog>> listLogRecord(LogRecordVO vo){
logger.info("ActivityService.listLogRecord by vo:{}",vo);
if(vo.getActivityId() == null){
return ResultEntity.error(null,"活动id为空");
}
if(vo.getPageNum() == null){
vo.setPageNum(1);
}
if(vo.getPageSize() == null){
vo.setPageSize(20);
}
ShopActivityOperationLogDTO dto = new ShopActivityOperationLogDTO();
dto.setMerchantId(vo.getMerchantId());
dto.setEmployeeId(vo.getEmployeeId());
dto.setActivityId(vo.getActivityId().longValue());
if(vo.getUserId() == null){
if(dto.getEmployeeId() != null){
RpcResponse<UserDTO> userRpc = userRemote.getByEmployeeId(dto.getEmployeeId());
if(userRpc.isSuccess() && userRpc.hasResult()){
UserDTO userDTO = userRpc.getResult();
dto.setUserId(userDTO.getId());
}
}else{
RpcResponse<MerchantDTO> merchantRpcResponse = merchantRemote.getAllInfoByMerchantId(dto.getMerchantId());
if(merchantRpcResponse.isSuccess() && merchantRpcResponse.hasResult()) {
MerchantDTO merchantDTO = merchantRpcResponse.getResult();
dto.setUserId(merchantDTO.getUserId());
}
}
}else{
dto.setUserId(vo.getUserId());
}
RpcPagination<ShopActivityOperationLogDTO> pageCondition = new RpcPagination<>();
pageCondition.setPageNum(vo.getPageNum());
pageCondition.setPageSize(vo.getPageSize());
pageCondition.setCondition(dto);
RpcResponse<RpcPagination<ShopActivityOperationLog>> rpcResponse = weappActivityRemote.listLogRecord(pageCondition);
return ResultEntity.result(rpcResponse);
}
public ResultEntity<String> getDoActivityLink(){
String link = env.getProperty("dj.webapp.howtodoactivity.link", "https://mp.weixin.qq.com/s/5Slqep200aP54zw3FHQXdw");
return ResultEntity.ok(link);
}
}