新集戳功能:
1、member表post_mark 记录当前用户戳的个数,用户每消费一条鱼集一个戳
2、当用户post_mark戳的个数满2、4、8时自动发放A、B、C券,post_mark满8清零。(例如用户之前post_mark为7,当前订单消费鱼2条,给用户发放C优惠券,并且post_mark更新为1)
// 1、一条鱼一个戳 2、满2、4、8条自动发放A、B、C券 3、满8个戳清零
//自第一个戳起,一年内若未集满8个戳,所得鱼戳在第一个鱼戳记录一年后自动清零。重新开启新一轮集戳,每一轮集章均按照第一次集戳时间计算一年有效期。
@Transactional(rollbackFor = Exception.class)
public void newPostMark(long memberId , Integer fishNumber , String serialNo, Date beginDate){
if(StringUtils.isEmpty(serialNo)){
//补戳
postmarkMapper.repairPostMark(memberId , fishNumber);
}else{
//集戳
postmarkMapper.addPostMark(memberId , fishNumber , serialNo );
}
int postMarkCount = memberMapper.selectPostMarkById(memberId); //用户之前戳数
int fishCount = postMarkCount + fishNumber ; ; //当前总戳数
//获取集戳记录
MemberPostMark newestPostMark = postmarkMapper.selectNewestRecordByMemberId(memberId); //最新第一轮参加集戳记录
//处理集戳时间
boolean lastPostTime = false;
if(!StringUtils.isEmpty(newestPostMark)){
lastPostTime = HelpApp.getNextYear(newestPostMark.getChangeTime()).after(beginDate); //上一次集戳时间距是否未超过一年
}
//超过一年集戳清零
try {
if(!StringUtils.isEmpty(newestPostMark) && lastPostTime == false){
fishCount = fishNumber;
logger.info(memberId+"该用户"+postMarkCount+"个戳清零");
}
} catch (Exception e) {
logger.error("集戳清零失败");
}
int postMarkNew = fishCount % 8 ; //member表用户当前post_mark 数值
memberMapper.updatePostMark(postMarkNew , memberId);
int postMarkmultiple = fishCount / 8 ; //满8清0次数
List<String> couponCodes = new ArrayList<>();
//发券 TWO A券 FOUR B券 EIGHT C券
String [] couponList = {
Constant.COUPON_CODE_POSTMARK_TWO , Constant.COUPON_CODE_POSTMARK_FOUR , Constant.COUPON_CODE_POSTMARK_EIGHT};
try {
if(postMarkmultiple == 0){
//总戳数小于8
if(postMarkCount < 2 && 2 <= fishCount && fishCount < 4){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}else if(postMarkCount < 2 && 4 <= fishCount){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}else if(2 <= postMarkCount && postMarkCount < 4 && 4 <= fishCount){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
}else if(postMarkmultiple == 1 && fishCount == 8) {
//总戳数等于8
if(postMarkCount < 4){
//用户之前戳小于4
if(postMarkCount < 2){
//用户之前戳小于2
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
}else if(postMarkmultiple >= 1 && fishCount != 8){
//总戳数大于8
if( postMarkNew < 2){
if(postMarkCount < 2){
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
}
}else if(2<= postMarkCount && postMarkCount < 4){
if(postMarkmultiple -1 != 0){
for(int i = 0 ; i<postMarkmultiple -1 ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}
}
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
}
}else if(4<= postMarkCount && postMarkCount < 8){
if(postMarkmultiple -1 != 0){
for(int i = 0 ; i<postMarkmultiple -1 ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
}
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
}
}
}else if (2 <= postMarkNew && postMarkNew <4){
if(postMarkCount < 2){
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
}
for(int i = 0 ; i<postMarkmultiple + 1 ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}
}else if(2<= postMarkCount && postMarkCount < 4){
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}
}else if(4<= postMarkCount && postMarkCount < 8){
if(postMarkmultiple -1 != 0){
for(int i = 0 ; i<postMarkmultiple -1 ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
}
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}
}
}else if(4<= postMarkNew && postMarkNew < 8){
if(postMarkCount < 2){
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
}
for(int i = 0 ; i<postMarkmultiple + 1 ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
}else if(2<= postMarkCount && postMarkCount < 4){
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
}
for(int i = 0 ; i<postMarkmultiple + 1 ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
}else if(4<= postMarkCount && postMarkCount < 8){
for(int i = 0 ; i<postMarkmultiple ; i++){
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[2]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[0]));
couponCodes.add(sendCouponByMIdAndCId(memberId , couponList[1]));
}
}
}
}
} catch (Exception e) {
logger.error("集戳发券失败"+e);
}
//单张记录所有本次集戳所得优惠券
if(!CollectionUtils.isEmpty(couponCodes)){
logger.info("获得优惠券"+couponCodes);
for(int i = 0; i < couponCodes.size(); i++){
String couponId = mbrCouponMapper.getCouponIdByCouponCode(couponCodes.get(i));
postmarkMapper.consumePostMark(memberId ,couponId , couponCodes.get(i));
}
}
//添加集戳记录
if(StringUtils.isEmpty(newestPostMark)){
//未参加过集戳
//添加第一轮集戳记录
postmarkMapper.addNewRecordPostMark(memberId);
}else{
if (lastPostTime == true && fishCount >= 8) {
//集戳时间未超过1年 并且集戳满8,添加新一轮集戳记录
postmarkMapper.addNewRecordPostMark(memberId);
} else if(lastPostTime == false){
//时间超过一年,集戳清零,添加新一轮集戳记录
postmarkMapper.addNewRecordPostMark(memberId);
}
}
}
// 发券
public String sendCouponByMIdAndCId(long memberId, String couponId){
Map<String, Date> useDateMap = serviceCoupon.couponUseDate(couponId);
String couponCode = (String) redisTemplate.opsForList().leftPop("couponList:" + couponId, 500, TimeUnit.MILLISECONDS);
if (StringUtils.isEmpty(couponCode)) {
logger.error("优惠券:" + couponId + "库存不足");
return "";
}
stockMapper.updateHasGet(couponCode);
mbrCouponMapper.bindCoupon(memberId, couponId, couponCode, useDateMap.get("startDate"), useDateMap.get("endDate"));
return couponCode;
}