package com.yeshi.fanli.service.impl.user;
|
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
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.UserInfoExtra;
|
import com.yeshi.fanli.entity.bus.user.UserLotteryRecord;
|
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
|
import com.yeshi.fanli.entity.system.SystemCoupon;
|
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
|
import com.yeshi.fanli.exception.user.UserInfoExtraException;
|
import com.yeshi.fanli.exception.user.UserLotteryRecordException;
|
import com.yeshi.fanli.exception.user.UserSystemCouponException;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.service.inter.config.ConfigService;
|
import com.yeshi.fanli.service.inter.config.SystemCouponService;
|
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
|
import com.yeshi.fanli.service.inter.user.UserLotteryRecordService;
|
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONObject;
|
|
@Service
|
public class UserLotteryRecordServiceImpl implements UserLotteryRecordService {
|
|
@Resource
|
private UserLotteryRecordMapper userLotteryRecordMapper;
|
|
@Resource
|
private ConfigService configService;
|
|
@Resource
|
private UserSystemCouponService userSystemCouponService;
|
|
@Resource
|
private SystemCouponService systemCouponService;
|
|
@Resource
|
private UserInfoExtraService userInfoExtraService;
|
|
|
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、新人抽奖 : 15天之内首次登录系统
|
boolean isNewUser = userInfoExtraService.isNewUser(uid);
|
if (isNewUser) {
|
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);
|
} else {
|
record.setCount(num);
|
}
|
return record;
|
}
|
|
|
@Override
|
public JSONObject getLotteryCountNewbies(Long uid) throws UserLotteryRecordException,Exception {
|
|
if (uid == null || uid == 0) {
|
throw new UserLotteryRecordException(1, "未登录系统");
|
}
|
|
|
// 兼容1.5.3之前版本
|
UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
|
if (userInfoExtra == null) {
|
throw new UserLotteryRecordException(1, "未登录系统");
|
}
|
|
//抽奖次数
|
int count = 0;
|
Integer lotteryNewbies = userInfoExtra.getLotteryNewbies();
|
if (lotteryNewbies != null) {
|
count = lotteryNewbies;
|
} else {
|
UserLotteryRecord record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_NEWBIES);
|
if (record != null) {
|
count = record.getCount();
|
}
|
}
|
|
// 抽奖规则
|
String lotteryRule = configService.get("lottery_rule_newbies");
|
|
JSONObject data = new JSONObject();
|
data.put("count", count);
|
data.put("rule", lotteryRule);
|
return data;
|
}
|
|
|
@Override
|
public Map<String, Object> executeLotteryNewbies(Long uid) throws UserLotteryRecordException, Exception{
|
if (uid == null || uid == 0) {
|
throw new UserLotteryRecordException(1, "未登录系统");
|
}
|
|
UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
|
if (userInfoExtra == null) {
|
throw new UserLotteryRecordException(1, "未登录系统");
|
}
|
|
int count = 0;
|
boolean isold = true;
|
UserLotteryRecord record = null;
|
|
Integer lotteryNewbies = userInfoExtra.getLotteryNewbies();
|
if (lotteryNewbies != null) {
|
// 老版的次数 兼容1.5.3之前版本
|
count = lotteryNewbies;
|
} else {
|
isold = false; // 新版
|
record = getLotteryByTypeAndUid(uid, UserLotteryRecord.TYPE_NEWBIES);
|
if (record != null) {
|
count = record.getCount();
|
}
|
}
|
|
if (count <= 0) {
|
throw new UserLotteryRecordException(2, "抽奖次数不足");
|
}
|
|
String prize = null;
|
String couponType = null;
|
// 新人抽奖 -已抽中奖品
|
List<UserSystemCoupon> list = userSystemCouponService.getUserCouponBySource(uid, UserSystemCoupon.SOURCE_NEWBIES);
|
|
if (list == null || list.size() == 0) {
|
prize = getLotteryPrizeNewbies(count, 0, null);
|
} else if (list.size() == 1) {
|
|
UserSystemCoupon userSystemCoupon = list.get(0);
|
SystemCoupon systemCoupon = userSystemCoupon.getSystemCoupon();
|
if (systemCoupon != null) {
|
SystemCoupon coupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
|
couponType = coupon.getType().name();
|
}
|
|
if (couponType != null && couponType.trim().length() > 0) {
|
prize = getLotteryPrizeNewbies(count, 1, couponType);
|
}
|
}
|
|
if (prize == null || prize.trim().length() == 0) {
|
prize = "NoPrize";
|
} else if (prize.equals(couponType)) {
|
// 已存在此券
|
prize = "NoPrize";
|
} else {
|
try {
|
userSystemCouponService.insertUserCoupon(uid, prize, UserSystemCoupon.SOURCE_NEWBIES, null,true);
|
} catch (UserSystemCouponException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
// 减少次数
|
count--;
|
|
// 更新记录
|
if (isold) {
|
UserInfoExtra extra = new UserInfoExtra();
|
extra.setId(userInfoExtra.getId());
|
extra.setLotteryNewbies(count);
|
try {
|
userInfoExtraService.saveUserInfoExtra(extra);
|
} catch (UserInfoExtraException e) {
|
e.printStackTrace();
|
}
|
} else {
|
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 count
|
* @param countPrize
|
* @param couponType
|
* @return
|
*/
|
@Override
|
public String getLotteryPrizeNewbies(int count, int countPrize, String couponType) {
|
String prize = null;
|
String freeCoupon = CouponTypeEnum.welfareFreeCoupon.name(); // 福利免费券
|
String rebateCoupon = CouponTypeEnum.rebatePercentCoupon.name(); // 奖励券
|
|
if (countPrize == 1 && count == 1) {
|
// 最后一次: 只收到一个奖品
|
if (rebateCoupon.equals(couponType)) {
|
prize = freeCoupon; // 福利免费券
|
} else {
|
prize = rebateCoupon; // 奖励券
|
}
|
} else if (countPrize == 0 && count == 2) {
|
// 后面两次必中
|
long result = (1 + Math.round(Math.random() * (9)));
|
if (result <= 5) {
|
prize = freeCoupon; // 福利免费券
|
} else {
|
prize = rebateCoupon; // 奖励券
|
}
|
} else {
|
long result = (1 + Math.round(Math.random() * (9)));
|
if (result <= 3) {
|
prize = freeCoupon; // 福利免费券
|
} else if (result <= 6) {
|
prize = rebateCoupon; // 奖励券
|
}
|
}
|
return prize;
|
}
|
|
|
|
@Override
|
public JSONObject 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_daily_rebate");
|
|
JSONObject data = new JSONObject();
|
data.put("count", count);
|
data.put("rule", lotteryRule);
|
return data;
|
}
|
|
|
@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, "抽奖次数不足");
|
}
|
|
String prize = null;
|
// 今日天天抽奖-已抽中拥有券数量
|
int todayHas = userSystemCouponService.countTodatyUserCouponBySource(uid, UserSystemCoupon.SOURCE_DAILY_REBATE);
|
|
if (todayHas == 0) {
|
// 抽奖
|
prize = getLotteryPrizeDaily(count);
|
LogHelper.test(uid + "中奖结果:" + prize);
|
try {
|
int num = 0;
|
if ("rebateCoupon".equals(prize)) {
|
num = 1;
|
} else if ("doubleCoupon".equals(prize)) {
|
num = 2;
|
}
|
|
// 插入奖励券
|
if (num > 0) {
|
userSystemCouponService.randomRewardCoupon(num, uid, UserSystemCoupon.SOURCE_DAILY_REBATE);
|
}
|
|
} 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);
|
|
LogHelper.test(uid + "返回H5中奖结果:" + prize);
|
if (StringUtil.isNullOrEmpty(prize)) {
|
prize = "NoPrize";
|
}
|
LogHelper.test(uid + "返回H5中奖结果处理空值:" + prize);
|
|
Map<String,Object> map = new HashMap<String, Object>();
|
map.put("count", count);
|
map.put("result", prize);
|
return map;
|
}
|
|
|
/**
|
* 奖品抽取
|
*
|
* @param count 剩余次数
|
* @return
|
*/
|
public String getLotteryPrizeDaily(int count) {
|
|
String prize = null;
|
|
String rebateCoupon = "rebateCoupon"; // 一张奖励券
|
String doubleCoupon = "doubleCoupon"; // 两张奖励券
|
|
if (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 <= 3) {
|
prize = rebateCoupon;
|
} else if (result <= 5) {
|
prize = doubleCoupon;
|
}
|
}
|
return prize;
|
}
|
|
}
|