admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserLotteryRecordServiceImpl.java
@@ -2,6 +2,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
@@ -10,15 +11,23 @@
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.order.CommonOrderCountService;
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 {
@@ -30,10 +39,13 @@
   private ConfigService configService;
   @Resource
   private CommonOrderCountService commonOrderCountService;
   private UserSystemCouponService userSystemCouponService;
   
   @Resource
   private UserSystemCouponService userSystemCouponService;
   private SystemCouponService systemCouponService;
   @Resource
   private UserInfoExtraService userInfoExtraService;
   
   public UserLotteryRecord getLotteryByTypeAndUid(Long uid, String type) {
@@ -58,10 +70,9 @@
            // 1、天天送抽奖次数
            num = UserLotteryRecord.COUNT_DAILY_REBATE;
         } else if(UserLotteryRecord.TYPE_NEWBIES.equals(type))  {
            // 2、新人抽奖
            boolean hasOrder = commonOrderCountService.hasRebateAndShareOrder(uid);
            // 新用户存在5次抽奖机会 (无订单:返利、分享订单)
            if (!hasOrder) {
            // 2、新人抽奖 : 15天之内首次登录系统
            boolean isNewUser = userInfoExtraService.isNewUser(uid);
            if (isNewUser) {
               num = UserLotteryRecord.COUNT_NEWBIES;
            }
         }
@@ -83,14 +94,187 @@
         record.setUid(uid);
         record.setType(type);
         record.setCount(num);
      } else {
         record.setCount(num);
      }
      return record;
   }
   
   
   @Override
   public Map<String, Object> getLotteryCountDaily(Long uid) throws UserLotteryRecordException,Exception {
   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);
         } 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, "未登录系统");
@@ -105,18 +289,17 @@
      }
      
      // 抽奖规则
      String lotteryRule = configService.get("lottery_rule_newbies");
      String lotteryRule = configService.get("lottery_rule_daily_rebate");
               
      Map<String,Object> map = new HashMap<String, Object>();
      map.put("count", count);
      map.put("rule", lotteryRule);
      return map;
      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{
   public Map<String, Object> executeLotteryDaily(Long uid) throws UserLotteryRecordException, Exception{
      if (uid == null || uid == 0) {
          throw new UserLotteryRecordException(1, "未登录系统");
      }
@@ -132,28 +315,33 @@
         throw new UserLotteryRecordException(2,  "抽奖次数不足");
      } 
      
      int countPrize = 0;
      String prize = null;
      // 今日天天抽奖-已抽中拥有券数量
      int todayHas = userSystemCouponService.countTodatyUserCouponBySource(uid, UserSystemCoupon.SOURCE_DAILY_REBATE);
      
      String prize = getLotteryPrizeDaily(countPrize, count);
      if (prize == null || prize.trim().length() == 0) {
         prize = "NoPrize";
      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, "抽奖失败");
         }
      }
      
      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--;
      
      // 更新记录
@@ -162,6 +350,12 @@
      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);
@@ -173,19 +367,18 @@
   /**
    *  奖品抽取
    * 
    * @param countPrize   已抽次数
    * @param countUsed 券id
    * @param count 剩余次数
    * @return
    */
   public String getLotteryPrizeDaily(int countPrize, int count) {
   public String getLotteryPrizeDaily(int count) {
      
      String prize = null;
      
      String rebateCoupon = "rebateCoupon"; // 一张奖励券
      String doubleCoupon = "doubleCoupon"; // 奖励券
      String doubleCoupon = "doubleCoupon"; // 两张奖励券
      
      if (countPrize == 0 && count== 1) {
         // 剩余最后一次 且未中奖则本次必中
      if (count == 1) {
         // 剩余最后一次必中
         long result = (1 + Math.round(Math.random() * (9)));
         if (result <= 7) {
            prize = rebateCoupon;
@@ -194,7 +387,7 @@
         }
      } else {
         long result = (1 + Math.round(Math.random() * (9)));
         if (result <= 2) {
         if (result <= 3) {
            prize = rebateCoupon;
         } else if (result <= 5) {
            prize = doubleCoupon;