yujian
2019-03-27 cdcbed9af813b2a02cdc01eefa24db8bec6b51a9
fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java
@@ -433,11 +433,17 @@
   public void getLotteryResult(String callback, AcceptData acceptData, Long uid, PrintWriter out) {
      try {
         // 奖品
         // 剩余抽奖次数
         int count = 0;
         String prize = "";
         String hasPrize = "";
         Long couponId = null;
         // 本次抽中的奖品
         String prize = null;
         // 已使用抽奖次数
         int countUsed = 0;
         // 已拥有奖品数量
         int countPrize = 0;
         // 已有券类型
         String couponType = null;
         if (uid == null || uid == 0) { // 未登录
            int platformType = 0;
@@ -456,46 +462,52 @@
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("设备不存在"));
               return;
            }
            int countPrize = 0;
            List<DeviceLotteryRecord> list = deviceLotteryRecordService.listByPlatformAndDevice(platformType,
                  device);
            if (list != null && list.size() == Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 无抽奖机会
            if (list != null && list.size() >= Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 无抽奖机会
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(2, "抽奖次数不足"));
               return;
            } else if (list != null && list.size() < Constant.MAX_COUNT_LOTTERY_NEWBIES) { // 拥有抽奖机会
            }
            Long couponId = null;
            if (list == null || list.size() == 0) { // 拥有抽奖机会
               count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1;
            } else {
               for (DeviceLotteryRecord deviceLotteryRecord : list) {
                  Long systemCouponId = deviceLotteryRecord.getSystemCouponId();
                  if (systemCouponId != null) {
                     countPrize++;
                     couponId = systemCouponId;
                  }
               }
               if (countPrize == 1 && couponId != null) {
                  SystemCoupon coupon = systemCouponService.selectByPrimaryKey(couponId);
                  hasPrize = coupon.getType().name();
               }
               count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1 - list.size();
            } else {
               count = Constant.MAX_COUNT_LOTTERY_NEWBIES - 1;
            }
            // 奖品
            prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize, list.size(), couponId);
            // 奖品小于2时才抽奖
            if (countPrize < 2) {
               if (couponId != null) {
                  SystemCoupon coupon = systemCouponService.selectByPrimaryKey(couponId);
                  // 已有券
                  couponType = coupon.getType().name();
               }
               // 奖品
               prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize, list.size(), couponType);
            }
            // 抽中
            Long newCouponId = null;
            if (prize != null && prize.trim().length() > 0) {
               // 已存在此券
               if (hasPrize.equals(prize)) {
               if (prize.equals(couponType)) {
                  prize = null;
               } else {
                  SystemCoupon coupon = systemCouponService.getCouponByType(prize);
                  newCouponId = coupon.getId();
                  if (coupon != null) {
                     newCouponId = coupon.getId();
                  }
               }
            }
@@ -508,41 +520,62 @@
            deviceLotteryRecordService.insertSelective(record);
         } else { // 已登录
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            if (userInfoExtra == null) {
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("信息不正确"));
               return;
            }
            // 剩余抽奖次数
            Integer lotteryNewbies = userInfoExtra.getLotteryNewbies();
            if (lotteryNewbies == null || lotteryNewbies < 1) { // 次数已用光
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(2, "抽奖次数不足"));
               return;
            } else {
               count = lotteryNewbies - 1; // 剩余次数
            }
            }
            // 剩余次数
            count = lotteryNewbies - 1;
            // 已使用次数
            countUsed = Constant.MAX_COUNT_LOTTERY_NEWBIES - lotteryNewbies;
            // 已抽中的奖品
            List<UserSystemCoupon> list = userSystemCouponService.getUserCouponBySource(uid,
                  UserSystemCoupon.SOURCE_CHOUJIANG);
            if (list != null && list.size() == 1) {
               couponId = list.get(0).getSystemCoupon().getId();
               SystemCoupon coupon = systemCouponService.selectByPrimaryKey(couponId);
               hasPrize = coupon.getType().name();
            if (list == null || list.size() == 0) {
               // 抽奖
               prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize , countUsed, couponType);
            } 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();
               }
               // 拥有奖品数量
               countPrize = 1;
               // 抽奖
               prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, countPrize, countUsed, couponType);
            }
            // 奖品
            prize = getLotteryPrize(Constant.MAX_COUNT_LOTTERY_NEWBIES, list.size(), Constant.MAX_COUNT_LOTTERY_NEWBIES - lotteryNewbies, couponId);
            // 抽中
            if (prize != null && prize.trim().length() > 0) {
               if (hasPrize.equals(prize)) {
               if (prize.equals(couponType)) {
                  // 已有此券
                  prize = null;
               } else {
                  // 插入券信息
                  userSystemCouponService.insertUserCoupon(uid, prize, UserSystemCoupon.SOURCE_CHOUJIANG);
               }
            }
            // 剩余次数
            userInfoExtra.setLotteryNewbies(count);
            userInfoExtraService.saveUserInfoExtra(userInfoExtra);
@@ -573,7 +606,7 @@
    * @param couponId 券id
    * @return
    */
   public String getLotteryPrize(int maxCount, int countPrize, int record, Long couponId) {
   public String getLotteryPrize(int maxCount, int countPrize, int record, String couponType) {
      String prize = null;
      String freeCoupon = CouponTypeEnum.welfareFreeCoupon.name(); // 福利免费券
@@ -581,16 +614,10 @@
      if (countPrize == 1 && record == maxCount - 1) {
         // 最后一次: 只收到一个奖品
         SystemCoupon systemCoupon = systemCouponService.selectByPrimaryKey(couponId);
         if (systemCoupon != null) {
            CouponTypeEnum type = systemCoupon.getType();
            if (type.name() == freeCoupon) {
               prize = rebateCoupon; // 奖励券
            } else {
               prize = freeCoupon; // 福利免费券
            }
         if (rebateCoupon.equals(couponType)) {
            prize = freeCoupon; // 福利免费券
         } else {
            prize = rebateCoupon; // 奖励券
            prize = rebateCoupon;  // 奖励券
         }
      } else if (countPrize == 0 && record == maxCount - 2) {
@@ -601,7 +628,7 @@
         } else {
            prize = rebateCoupon; // 奖励券
         }
      } else { // 大于3次机会
         if (countPrize < 2) { // 不足两个奖品
@@ -613,7 +640,6 @@
            }
         }
      }
      return prize;
   }