admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/controller/client/v2/RedPackControllerV2.java
@@ -2,8 +2,10 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@@ -26,27 +28,43 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.entity.common.Config;
import com.yeshi.fanli.entity.redpack.RedPackDetail.RedPackDetailTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackExchange;
import com.yeshi.fanli.entity.redpack.RedPackWinInvite;
import com.yeshi.fanli.entity.redpack.RedPackWinInvite.RedPackWinInviteTypeEnum;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.redpack.RedPackExchangeException;
import com.yeshi.fanli.exception.redpack.RedPackGiveRecordException;
import com.yeshi.fanli.service.AdminUserService;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
import com.yeshi.fanli.service.inter.redpack.RedPackExchangeService;
import com.yeshi.fanli.service.inter.redpack.RedPackForbidService;
import com.yeshi.fanli.service.inter.redpack.RedPackGiveRecordService;
import com.yeshi.fanli.service.inter.redpack.RedPackWinInviteService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserSystemCouponService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.util.AESUtil;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.UserInviteUtil;
import com.yeshi.fanli.util.email.MailSenderUtil;
import com.yeshi.fanli.vo.redpack.RedPackDetailVO;
import com.yeshi.fanli.vo.redpack.RedPackGiveVO;
import com.yeshi.fanli.vo.redpack.RedPackWinInviteVO;
import com.yeshi.fanli.vo.redpack.RedPackWinProgressVO;
import net.sf.json.JSONObject;
@@ -62,22 +80,42 @@
   @Resource
   private RedPackDetailService redPackDetailService;
   @Resource
   @Resource
   private RedPackGiveRecordService redPackGiveRecordService;
   @Resource
   @Resource
   private RedPackExchangeService redPackExchangeService;
   @Resource
   @Resource
   private RedPackWinInviteService redPackWinInviteService;
   @Resource
   @Resource
   private UserInfoService userInfoService;
   @Resource
   @Resource
   private UserInfoExtraService userInfoExtraService;
   @Resource
   private JumpDetailV2Service jumpDetailV2Service;
   @Resource
   private SwiperPictureService swiperPictureService;
   @Resource
   private UserSystemCouponService userSystemCouponService;
   @Resource
   private RedPackForbidService redPackForbidService;
   @Resource
   private ThreeSaleSerivce threeSaleSerivce;
   
   @Resource
   private ConfigService configService;
   @Resource
   private AdminUserService adminUserService;
   /**
    * 统计
@@ -103,6 +141,7 @@
      BigDecimal thisMonthUse = zero;
      BigDecimal lastMonthUse = zero;
      BigDecimal balance = redPackBalanceService.getBalance(uid);
      ;
      if (balance == null) {
         balance = zero;
      } else {
@@ -137,10 +176,10 @@
      tips.add("你也可以选择使用红包,享受给力折扣。");
      String giveMin = redPackConfigService.getValueByKey("give_money_min");
      String giveMax = redPackConfigService.getValueByKey("give_money_max");
      // 正在提现金额
      BigDecimal extractingMoney = redPackExchangeService.countMoneyByUidAndState(uid, RedPackExchange.STATE_INIT);
      JSONObject data = new JSONObject();
      data.put("balance", balance.setScale(2).toString());
      data.put("helpLink", redPackConfigService.getValueByKey("desc_help_link"));
@@ -148,16 +187,17 @@
      data.put("giveMax", giveMax);
      data.put("giveDesc", "注:红包赠送金额至少" + giveMin + "元至多" + giveMax + "元");
      if (extractingMoney != null && extractingMoney.compareTo(zero) > 0)
      if (extractingMoney != null && extractingMoney.compareTo(zero) > 0)
         data.put("extractingInfo", "提现中:¥" + extractingMoney.setScale(2));
      data.put("extractShortage", tips);
      data.put("extractMin", redPackConfigService.getValueByKey("extract_money_min"));
      data.put("extractMax", redPackConfigService.getValueByKey("extract_money_max"));
      data.put("extractBanlenMin", redPackConfigService.getValueByKey("extract_banlen_min"));
      data.put("extractDesc", "注:3个工作日内完成审核,红包提现成功后将会进入到余额。");
      data.put("giveTotal", redPackGiveRecordService.countGiveList(uid));
      data.put("useLink", redPackConfigService.getValueByKey("use_link"));
      data.put("winLink", redPackConfigService.getValueByKey("invite_win_redpack_link"));
      data.put("todayWin", todayWin.setScale(2).toString());
      data.put("yesterdayWin", yesterdayWin.setScale(2).toString());
@@ -243,7 +283,6 @@
         }
      });
      Gson gson = gsonBuilder.create();
      JSONObject data = new JSONObject();
@@ -275,10 +314,35 @@
      }
   }
   @RequestMapping(value = "getGiveList", method = RequestMethod.POST)
   public void getGiveList(AcceptData acceptData, Integer page, Long uid, PrintWriter out) {
      if (uid == null || uid <= 0) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (page == null || page <= 0)
         page = 1;
      try {
         List<RedPackGiveVO> list = redPackGiveRecordService.getGiveList(page, Constant.PAGE_SIZE, uid);
         GsonBuilder gsonBuilder = new GsonBuilder();
         Gson gson = gsonBuilder.excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy.MM.dd HH:mm").create();
         JSONObject data = new JSONObject();
         data.put("list", gson.toJson(list));
         data.put("count", redPackGiveRecordService.countGiveList(uid));
         out.print(JsonUtil.loadTrueResult(data));
      } catch (Exception e) {
         e.printStackTrace();
         out.print(JsonUtil.loadFalseResult(1, "查询失败"));
      }
   }
   /**
    * 转换红包为真实金钱
    *
    * @param acceptData
    * @param uid
    * @param amount
@@ -294,36 +358,66 @@
      try {
         redPackExchangeService.exchangeCash(uid, amount);
         out.print(JsonUtil.loadTrueResult("提现申请成功"));
         ThreadUtil.run(new Runnable() {
            @Override
            public void run() {
               JSONObject json = new JSONObject();
               json.put("timeStamp", java.lang.System.currentTimeMillis());
               json.put("adminId", 4L);
               String sign = AESUtil.encrypt(json.toString(), Constant.ADMINH5_AESKEY);
               String url = "";
               try {
                  url = String.format("%s?from=emailh5&sign=%s",
                        Constant.systemCommonConfig.getExtractNotifyUrl(), URLEncoder.encode(sign, "UTF-8"));
               } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
               }
               long[] targetUids = new long[] { 3L, 4L };
               Config config = configService.getConfig(ConfigKeyEnum.extractCodeEmailFrom.getKey());
               String[] sts = config.getValue().split(",");
               String account = sts[0];
               String pwd = sts[1];
               for (long adminId : targetUids) {
                  AdminUser adminUser = adminUserService.selectByPrimaryKey(adminId);
                  String msg = "有用户采用红包兑换现金";
                  boolean isS = MailSenderUtil.sendEmail(adminUser.getEmail(), account, pwd, "红包兑换现金提醒", msg);
               }
            }
         });
      } catch (RedPackExchangeException e) {
         out.print(JsonUtil.loadFalseResult(1, e.getMsg()));
      }
   }
   /**
    * 红包邀请进度
    *
    * @param acceptData
    * @param page
    * @param uid
    * @param out
    */
   @RequestMapping(value = "getRewardList", method = RequestMethod.POST)
   public void getRewardList(AcceptData acceptData, Integer page, Long uid, PrintWriter out) {
   @RequestMapping(value = "getRewardList")
   public void getRewardList(AcceptData acceptData, Integer page, Long uid, String callback, PrintWriter out) {
      if (uid == null || uid <= 0) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (page == null || page < 1) {
         out.print(JsonUtil.loadFalseResult(1, "页码不正确"));
         return;
      }
      List<RedPackWinInviteVO> list = new ArrayList<RedPackWinInviteVO>();
      List<RedPackWinInvite> listRecord = redPackWinInviteService.getRewardList((page - 1) * Constant.PAGE_SIZE, Constant.PAGE_SIZE, uid);
      List<RedPackWinInvite> listRecord = redPackWinInviteService.getRewardList((page - 1) * Constant.PAGE_SIZE,
            Constant.PAGE_SIZE, uid);
      if (listRecord != null && !listRecord.isEmpty()) {
         for (RedPackWinInvite record: listRecord) {
         for (RedPackWinInvite record : listRecord) {
            UserInfo user = null;
            RedPackWinInviteVO winVO = new RedPackWinInviteVO();
            RedPackWinInviteTypeEnum type = record.getType();
@@ -339,36 +433,130 @@
            list.add(winVO);
         }
      }
      JSONObject data = new JSONObject();
      data.put("winMoney", redPackWinInviteService.getRewardMoney(uid).setScale(2));
      data.put("count", redPackWinInviteService.countRewardRecord(uid));
       data.put("list", JsonUtil.getApiCommonGson().toJson(list));
       out.print(JsonUtil.loadTrueResult(data));
      data.put("list", JsonUtil.getApiCommonGson().toJson(list));
      if (!StringUtil.isNullOrEmpty(callback)) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } else {
         out.print(JsonUtil.loadTrueResult(data));
      }
   }
   /**
    * 用户基础信息
    *
    * @param acceptData
    * @param uid
    * @param out
    */
   @RequestMapping(value = "getBasicInfo", method = RequestMethod.POST)
   public void getBasicInfo(AcceptData acceptData, Long uid, PrintWriter out) {
   @RequestMapping(value = "getBasicInfo")
   public void getBasicInfo(AcceptData acceptData, Long uid, String callback, PrintWriter out) {
      if (uid == null || uid <= 0) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      JSONObject data = new JSONObject();
      data.put("balance", redPackBalanceService.getBalance(uid));
      data.put("ruleLink", redPackConfigService.getValueByKey("invite_reward_rule_link"));
      UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
      if (userInfoExtra != null && !StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
         data.put("inviteCode", userInfoExtra.getInviteCode());
      data.put("shareLink", UserInviteUtil.getShareUrl(uid));
      data.put("redPackLock", redPackForbidService.verifyForbid(uid));
      String inviteCode = userInfoExtraService.getInviteCodeByUid(uid);
      if (!StringUtil.isNullOrEmpty(inviteCode)) {
         data.put("inviteCode", inviteCode);
      } else {
         data.put("inviteCode", "");
      }
       out.print(JsonUtil.loadTrueResult(data));
      // 赠送免单券
      long freeCouponCount = userSystemCouponService.countGiveFreeCoupon(uid);
      // 奖励券
      long rewardCouponCount = userSystemCouponService.countUsableRewardCoupon(uid);
      data.put("freeCouponCount", freeCouponCount);
      data.put("rewardCouponCount", rewardCouponCount);
      if (!StringUtil.isNullOrEmpty(callback)) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } else {
         out.print(JsonUtil.loadTrueResult(data));
      }
   }
   /**
    *  该用户邀请奖金榜
    *
    * @param acceptData
    * @param uid  下线UID
    * @param out
    */
   @RequestMapping(value = "getInviteReward")
   public void getInviteReward(AcceptData acceptData, Integer page, Long uid, String callback, PrintWriter out) {
      if (uid == null || uid <= 0) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (page == null || page < 1)
         page = 1;
      List<RedPackWinProgressVO> listVO = new ArrayList<RedPackWinProgressVO>();
      List<RedPackWinInvite> list = redPackWinInviteService.getWinTopListByBossId(page, Constant.PAGE_SIZE, uid);
      if (list != null) {
         for (RedPackWinInvite win : list) {
            ThreeSale threeSale = threeSaleSerivce.getNearRelationByBossIdAndWorkerId(uid, win.getTeamUid());
            if (threeSale == null)
               continue;
            UserInfo userInfo = userInfoService.selectByPKey(win.getTeamUid());
            if (userInfo == null)
               continue;
            RedPackWinProgressVO progressVO = new RedPackWinProgressVO();
            progressVO.setUid(userInfo.getId());
            progressVO.setName(userInfo.getNickName());
            progressVO.setPortrait(userInfo.getPortrait());
            progressVO.setMoney(win.getMoney().setScale(2).toString());
            progressVO.setTime("邀请:" + TimeUtil.formatDate(threeSale.getSucceedTime()));
            listVO.add(progressVO);
         }
      }
      JSONObject data = new JSONObject();
      data.put("list", listVO);
      data.put("count", redPackWinInviteService.countWinTopListByBossId(uid));
      if (!StringUtil.isNullOrEmpty(callback)) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } else {
         out.print(JsonUtil.loadTrueResult(data));
      }
   }
   /**
    *  该用户邀请进度
    *
    * @param acceptData
    * @param tid  下线UID
    * @param out
    */
   @RequestMapping(value = "getInviteProgress")
   public void getInviteProgress(AcceptData acceptData, Long uid, Long tid, String callback, PrintWriter out) {
      if (uid == null || uid <= 0 || tid == null || tid <= 0) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "传递参数缺失"));
         return;
      }
      JSONObject data = new JSONObject();
      data.put("list", redPackWinInviteService.getInviteProgressByWorkerId(uid, tid));
      data.put("num", redPackWinInviteService.countTeamNumByTid(uid, tid));
      if (!StringUtil.isNullOrEmpty(callback)) {
         out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
      } else {
         out.print(JsonUtil.loadTrueResult(data));
      }
   }
}