package com.yeshi.fanli.service.impl.user;
|
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.yeshi.utils.DateUtil;
|
|
import com.yeshi.fanli.dao.mybatis.user.UserLotteryRecordMapper;
|
import com.yeshi.fanli.entity.bus.user.UserLotteryRecord;
|
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
|
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
|
import com.yeshi.fanli.exception.user.UserLotteryRecordException;
|
import com.yeshi.fanli.exception.user.UserSystemCouponException;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
|
import com.yeshi.fanli.service.inter.user.UserLotteryRecordService;
|
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
|
|
@Service
|
public class UserLotteryRecordServiceImpl implements UserLotteryRecordService {
|
|
@Resource
|
private UserLotteryRecordMapper userLotteryRecordMapper;
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private CommonOrderCountService commonOrderCountService;
|
|
@Resource
|
private UserSystemCouponService userSystemCouponService;
|
|
|
public UserLotteryRecord getLotteryByTypeAndUid(Long uid, String type) {
|
int num = 0;
|
UserLotteryRecord record = userLotteryRecordMapper.getByTypeAndUid(uid, type);
|
if (record != null) {
|
// 今日之内是否已抽过奖
|
Date updateTime = record.getUpdateTime();
|
if (updateTime != null && DateUtil.isSameDay(updateTime, new Date())) {
|
num = record.getCount();
|
} else {
|
if (UserLotteryRecord.TYPE_DAILY_REBATE.equals(type)) {
|
// 1、天天送抽奖次数
|
num = UserLotteryRecord.COUNT_DAILY_REBATE;
|
} else if(UserLotteryRecord.TYPE_NEWBIES.equals(type)) {
|
// 2、新人抽奖
|
num = record.getCount();
|
}
|
}
|
} else {
|
if (UserLotteryRecord.TYPE_DAILY_REBATE.equals(type)) {
|
// 1、天天送抽奖次数
|
num = UserLotteryRecord.COUNT_DAILY_REBATE;
|
} else if(UserLotteryRecord.TYPE_NEWBIES.equals(type)) {
|
// 2、新人抽奖
|
boolean hasOrder = commonOrderCountService.hasRebateAndShareOrder(uid);
|
// 新用户存在5次抽奖机会 (无订单:返利、分享订单)
|
if (!hasOrder) {
|
num = UserLotteryRecord.COUNT_NEWBIES;
|
}
|
}
|
|
if (num > 0) {
|
// 插入抽奖记录
|
record = new UserLotteryRecord();
|
record.setUid(uid);
|
record.setType(type);
|
record.setCount(num);
|
record.setCreateTime(new Date());
|
record.setUpdateTime(new Date());
|
userLotteryRecordMapper.insertSelective(record);
|
}
|
}
|
|
if (record == null) {
|
record = new UserLotteryRecord();
|
record.setUid(uid);
|
record.setType(type);
|
record.setCount(num);
|
}
|
|
return record;
|
}
|
|
|
@Override
|
public Map<String, Object> getLotteryCountDaily(Long uid) throws UserLotteryRecordException,Exception {
|
|
if (uid == null || uid == 0) {
|
throw new UserLotteryRecordException(1, "未登录系统");
|
}
|
|
//抽奖次数
|
int count = 0;
|
|
UserLotteryRecord record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_DAILY_REBATE);
|
if (record != null) {
|
count = record.getCount();
|
}
|
|
// 抽奖规则
|
String lotteryRule = configService.get("lottery_rule_newbies");
|
|
Map<String,Object> map = new HashMap<String, Object>();
|
map.put("count", count);
|
map.put("rule", lotteryRule);
|
|
return map;
|
}
|
|
|
@Override
|
public Map<String, Object> executeLotteryDaily(Long uid) throws UserLotteryRecordException,Exception{
|
if (uid == null || uid == 0) {
|
throw new UserLotteryRecordException(1, "未登录系统");
|
}
|
|
UserLotteryRecord record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_DAILY_REBATE);
|
if (record == null) {
|
throw new UserLotteryRecordException(1, "暂无抽奖机会");
|
}
|
|
int count = record.getCount();
|
// 抽奖次数不足
|
if (count == 0) {
|
throw new UserLotteryRecordException(2, "抽奖次数不足");
|
}
|
|
int countPrize = 0;
|
|
|
String prize = getLotteryPrizeDaily(countPrize, count);
|
if (prize == null || prize.trim().length() == 0) {
|
prize = "NoPrize";
|
}
|
|
try {
|
// 插入券信息
|
if ("rebateCoupon".equals(prize)) {
|
String rebateCoupon = CouponTypeEnum.rebatePercentCoupon.name(); // 奖励券
|
userSystemCouponService.insertUserCoupon(uid, rebateCoupon, UserSystemCoupon.SOURCE_NEWBIES);
|
} else if ("doubleCoupon".equals(prize)) {
|
String rebateCoupon = CouponTypeEnum.rebatePercentCoupon.name(); // 奖励券
|
userSystemCouponService.insertUserCoupon(uid, rebateCoupon, UserSystemCoupon.SOURCE_NEWBIES);
|
userSystemCouponService.insertUserCoupon(uid, rebateCoupon, UserSystemCoupon.SOURCE_NEWBIES);
|
}
|
} catch (UserSystemCouponException e) {
|
throw new UserLotteryRecordException(1, "抽奖失败");
|
}
|
|
count--;
|
|
// 更新记录
|
UserLotteryRecord updateRecord = new UserLotteryRecord();
|
updateRecord.setId(record.getId());
|
updateRecord.setCount(count);
|
updateRecord.setUpdateTime(new Date());
|
userLotteryRecordMapper.updateByPrimaryKeySelective(updateRecord);
|
|
Map<String,Object> map = new HashMap<String, Object>();
|
map.put("count", count);
|
map.put("result", prize);
|
return map;
|
}
|
|
|
/**
|
* 奖品抽取
|
*
|
* @param countPrize 已抽次数
|
* @param countUsed 券id
|
* @return
|
*/
|
public String getLotteryPrizeDaily(int countPrize, int count) {
|
|
String prize = null;
|
|
String rebateCoupon = "rebateCoupon"; // 一张奖励券
|
String doubleCoupon = "doubleCoupon"; // 奖励券
|
|
if (countPrize == 0 && count== 1) {
|
// 剩余最后一次 且未中奖则本次必中
|
long result = (1 + Math.round(Math.random() * (9)));
|
if (result <= 7) {
|
prize = rebateCoupon;
|
} else {
|
prize = doubleCoupon;
|
}
|
} else {
|
long result = (1 + Math.round(Math.random() * (9)));
|
if (result <= 2) {
|
prize = rebateCoupon;
|
} else if (result <= 5) {
|
prize = doubleCoupon;
|
}
|
}
|
return prize;
|
}
|
|
}
|