CouponOperationServiceImpl.java
6.91 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
package com.topdraw.business.process.service.impl;
import com.topdraw.business.module.coupon.history.domain.CouponHistory;
import com.topdraw.business.module.coupon.history.service.CouponHistoryService;
import com.topdraw.business.module.coupon.service.CouponService;
import com.topdraw.business.module.member.domain.Member;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.process.domian.TempCoupon;
import com.topdraw.business.process.service.CouponOperationService;
import com.topdraw.business.process.service.RightsOperationService;
import com.topdraw.business.process.service.member.MemberOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
@Service
public class CouponOperationServiceImpl implements CouponOperationService {
private static final Logger LOG = LoggerFactory.getLogger(CouponOperationServiceImpl.class);
@Autowired
CouponService couponService;
@Autowired
CouponHistoryService couponHistoryService;
@Autowired
MemberOperationService memberOperationService;
@Autowired
RightsOperationService rightsOperationService;
@Autowired
MemberService memberService;
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
// 过期阀值(默认一个月)
private static final Integer EXPIRE_FACTOR_DAY = 30;
@Override
public void grantCouponThroughTempCoupon(List<TempCoupon> tempCouponList) {
// 优惠券领取、使用历史记录表
for (TempCoupon tempCoupon : tempCouponList) {
this.refresh(tempCoupon);
}
}
/**
* 手动发放优惠券
* @param tempCouponList
*/
@Override
public void grantCouponByManual(List<TempCoupon> tempCouponList) {
// 优惠券领取、使用历史记录表
for (TempCoupon tempCoupon : tempCouponList) {
this.refresh(tempCoupon);
}
}
/**
* 优惠券领取历史记录表
*
* @param tempCoupon 领取的优惠券
*/
private void refresh(TempCoupon tempCoupon) {
// 1.更新会员优惠券数量
this.refreshMemberCoupon(tempCoupon);
// 2.保存优惠券领取、使用历史记录表
this.doInsertCouponHistory(tempCoupon);
}
/**
* 更新会员优惠券信息
* @param tempCoupon 账号id
*/
private void refreshMemberCoupon(TempCoupon tempCoupon) {
// Long userId = tempCoupon.getUserId();
Long memberId = tempCoupon.getMemberId();
Integer rightsAmount = tempCoupon.getRightsAmount();
try {
// 1.历史总优惠券数量
Long historyCouponCount = this.getTotalHistoryCoupon(memberId);
// 1.当前总优惠券数量
Long totalCouponCount = this.getTotalCoupon(historyCouponCount,rightsAmount);
// 2.获取已过期的优惠券数量
Long expireCouponCount = this.getTotalExpireCoupon(memberId);
// 3.即将过期的优惠券数量
Long expireSoonCouponCount = this.getTotalExpireSoonCoupon(memberId,EXPIRE_FACTOR_DAY);
// 4.当前优惠券数量 = 总优惠券-已过期的优惠券
Long currentCoupon = this.getCurrentCoupon(totalCouponCount,expireCouponCount);
// 5.更新用户信息(优惠券数量、即将过期的优惠券数量)
this.doUpdateMemberInfo(memberId,currentCoupon,expireSoonCouponCount);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private Long getTotalCoupon(Long historyCouponCount, Integer rightsAmount) {
return (Objects.nonNull(historyCouponCount) ? historyCouponCount: 0L) + (Objects.nonNull(rightsAmount) ? rightsAmount: 0L);
}
/**
* 更新当前用户优惠券信息
* @param memberId
* @param currentCoupon
* @param expireSoonCouponCount
*/
private void doUpdateMemberInfo(Long memberId, Long currentCoupon, Long expireSoonCouponCount) {
MemberDTO memberDTO = this.findMemberByMemberId(memberId);
Member member = new Member();
BeanUtils.copyProperties(memberDTO,member);
member.setCouponAmount(currentCoupon);
member.setDueCouponAmount(expireSoonCouponCount);
member.setUpdateTime(LocalDateTime.now());
this.memberOperationService.doUpdateMember(member);
}
private MemberDTO findMemberByMemberId(Long memberId) {
MemberDTO memberDTO = this.memberService.findById(memberId);
return memberDTO;
}
/**
* 当前优惠券数量 = 总优惠券-已过期的优惠券
* @param totalCouponCount 总数
* @param expireCouponCount 已过期总数
* @return
*/
private Long getCurrentCoupon(Long totalCouponCount, Long expireCouponCount) {
return (Objects.nonNull(totalCouponCount)?totalCouponCount:0L)-(Objects.nonNull(expireCouponCount)?expireCouponCount:0L);
}
/**
* 即将过期的优惠券数量
* @param expireFactor
* @return
*/
private Long getTotalExpireSoonCoupon(Long userId, Integer expireFactor) {
LocalDateTime expireTime = LocalDateTime.now().plusDays(expireFactor);
return this.couponHistoryService.countByUserIdAndExpireTimeBetween(userId,LocalDateTime.now(),expireTime);
}
/**
* 获取已过期的优惠券数量
* @param userId
* @return
*/
private Long getTotalExpireCoupon(Long userId) {
return this.couponHistoryService.countByUserIdAndExpireTimeBefore(userId,LocalDateTime.now());
}
/**
* 获取用户领取的总优惠券
* @param userId
* @return
*/
private Long getTotalHistoryCoupon(Long userId) {
return this.couponHistoryService.countByUserId(userId);
}
/**
* 优惠券领取、使用历史记录表
* @param tempCoupon 优惠券
*/
private void doInsertCouponHistory(TempCoupon tempCoupon) {
CouponHistory couponHistory = new CouponHistory();
BeanUtils.copyProperties(tempCoupon,couponHistory);
couponHistory.setId(null);
couponHistory.setCouponId(tempCoupon.getId());
couponHistory.setUserId(tempCoupon.getMemberId());
couponHistory.setCouponCode(tempCoupon.getCode());
couponHistory.setUserNickname(tempCoupon.getUserNickname());
couponHistory.setOrderDetailId(tempCoupon.getOrderId());
couponHistory.setReceiveTime(LocalDateTime.now());
couponHistory.setUseStatus(Objects.nonNull(couponHistory.getUseStatus()) ? couponHistory.getUseStatus():0);
this.couponHistoryService.create(couponHistory);
}
}