yujian
2019-04-08 5c4ecf9e8b47efbbf5d21c9e109be0a60cb7e26d
fanli/src/main/java/com/yeshi/fanli/controller/client/UserCouponController.java
@@ -17,10 +17,12 @@
import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
import com.yeshi.fanli.entity.bus.user.DeviceLotteryRecord;
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.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.config.SystemCouponService;
@@ -30,6 +32,7 @@
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.DeviceLotteryRecordService;
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.Constant;
import com.yeshi.fanli.vo.user.UserSystemCouponRecordVO;
@@ -79,6 +82,10 @@
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   @Resource
   private UserLotteryRecordService userLotteryRecordService;
   /**
@@ -433,11 +440,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 +469,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 +527,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();
                  UserSystemCoupon.SOURCE_NEWBIES);
            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);
                  userSystemCouponService.insertUserCoupon(uid, prize, UserSystemCoupon.SOURCE_NEWBIES);
               }
            }
            // 剩余次数
            userInfoExtra.setLotteryNewbies(count);
            userInfoExtraService.saveUserInfoExtra(userInfoExtra);
@@ -573,7 +613,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 +621,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 +635,7 @@
         } else {
            prize = rebateCoupon; // 奖励券
         }
      } else { // 大于3次机会
         if (countPrize < 2) { // 不足两个奖品
@@ -613,8 +647,50 @@
            }
         }
      }
      return prize;
   }
   /**
    * 获取抽奖次数- 天天抽奖励券
    *
    * @param callback
    * @param acceptData
    * @param uid
    * @param out
    */
   @RequestMapping(value = "getDailyCount")
   public void getDailyCount(String callback, AcceptData acceptData, Long uid, PrintWriter out) {
      try {
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult( userLotteryRecordService.getLotteryCountDaily(uid)));
      } catch (UserLotteryRecordException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getCode(),e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("信息获取失败"));
         e.printStackTrace();
      }
   }
   /**
    * 获取抽奖结果 - 天天抽奖励券`
    *
    *
    *
    * @param callback
    * @param acceptData
    * @param uid
    * @param out
    */
   @RequestMapping(value = "getDailyResult")
   public void getDailyResult(String callback, AcceptData acceptData, Long uid, PrintWriter out) {
      try {
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult( userLotteryRecordService.executeLotteryDaily(uid)));
      } catch (UserLotteryRecordException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getCode(),e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("抽奖失败"));
         e.printStackTrace();
      }
   }
}