admin
2019-01-23 e179d8660bf4837455a38c24e74320143e0d2e17
fanli/src/main/java/com/yeshi/fanli/controller/client/UserMsgController.java
@@ -6,7 +6,9 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
@@ -24,6 +26,7 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.lable.QualityFactory;
import com.yeshi.fanli.entity.bus.msg.MsgAccountDetail;
import com.yeshi.fanli.entity.bus.msg.MsgInviteDetail;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail;
@@ -33,22 +36,32 @@
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.goods.RecommendUserGoods;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.exception.goods.CommonGoodsException;
import com.yeshi.fanli.exception.goods.RecommendUserGoodsException;
import com.yeshi.fanli.exception.msg.UserSystemMsgException;
import com.yeshi.fanli.exception.user.UserCustomSettingsException;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.goods.RecommendUserGoodsService;
import com.yeshi.fanli.service.inter.lable.QualityFactoryService;
import com.yeshi.fanli.service.inter.msg.MsgAccountDetailService;
import com.yeshi.fanli.service.inter.msg.MsgInviteDetailService;
import com.yeshi.fanli.service.inter.msg.MsgMoneyDetailService;
import com.yeshi.fanli.service.inter.msg.MsgOrderDetailService;
import com.yeshi.fanli.service.inter.msg.UserMsgReadStateService;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.user.UserCustomSettingsService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.factory.CommonGoodsFactory;
import com.yeshi.fanli.util.factory.msg.UserMsgVOFactory;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import com.yeshi.fanli.vo.msg.UserHomeMsgVO;
import com.yeshi.fanli.vo.msg.UserMsgVO;
import com.yeshi.fanli.vo.msg.UserSystemMsgVO;
import com.yeshi.fanli.vo.user.UserSettingsVO;
import net.sf.json.JSONObject;
@@ -79,6 +92,15 @@
   @Resource
   private UserSystemMsgService userSystemMsgService;
   @Resource
   private CommonGoodsService commonGoodsService;
   @Resource
   private QualityFactoryService qualityFactoryService;
   @Resource
   private UserCustomSettingsService userCustomSettingsService;
   // 消息的类型
   final static String MSG_TYPE_ORDER = "order";
@@ -112,7 +134,7 @@
      switch (type) {
      case MSG_TYPE_ORDER:
         msgOrderDetailService.readMsgByUid(uid);
         userMsgReadStateService.readOrderMsg(uid);
         List<MsgOrderDetail> detailList = msgOrderDetailService.listMsgOrderDetail(uid, page);
         count = msgOrderDetailService.countMsgOrderDetail(uid);
         if (detailList != null)
@@ -120,7 +142,7 @@
               list.add(UserMsgVOFactory.create(detail));
         break;
      case MSG_TYPE_MONEY:
         msgMoneyDetailService.readMsgByUid(uid);
         userMsgReadStateService.readMoneyMsg(uid);
         List<MsgMoneyDetail> detailList1 = msgMoneyDetailService.listMsgMoneyDetail(uid, page);
         count = msgMoneyDetailService.countMsgMoneyDetail(uid);
         if (detailList1 != null)
@@ -128,7 +150,7 @@
               list.add(UserMsgVOFactory.create(detail));
         break;
      case MSG_TYPE_ACCOUNT:
         msgAccountDetailService.readMsgByUid(uid);
         userMsgReadStateService.readAccountMsg(uid);
         List<MsgAccountDetail> detailList2 = msgAccountDetailService.listMsgAccountDetail(uid, page);
         count = msgAccountDetailService.countMsgAccountDetail(uid);
         if (detailList2 != null)
@@ -136,7 +158,7 @@
               list.add(UserMsgVOFactory.create(detail));
         break;
      case MSG_TYPE_INVITE:
         msgInviteDetailService.readMsgByUid(uid);
         userMsgReadStateService.readInviteMsg(uid);
         List<MsgInviteDetail> detailList3 = msgInviteDetailService.listMsgInviteDetail(uid, page);
         count = msgInviteDetailService.countMsgInviteDetail(uid);
         if (detailList3 != null)
@@ -170,10 +192,17 @@
      JSONObject data = JSONObject.fromObject(JsonUtil.getApiCommonGson().toJson(num));
      data.put("totalCount", num.getTypeAccount() + num.getTypeInvite() + num.getTypeMoney() + num.getTypeOrder()
            + num.getTypeScore() + num.getTypeSystem());
      JSONObject root = new JSONObject();
      root.put("data", data);
      root.put("count", 1);
      out.print(JsonUtil.loadTrueResult(root));
      try {
         UserSettingsVO vo = userCustomSettingsService.getMySettings(uid);
         if (vo != null && vo.getNoNewsRedDot() != null && vo.getNoNewsRedDot() == 1) {
            data.put("totalCount", 0);
         }
      } catch (UserCustomSettingsException e) {
         e.printStackTrace();
      }
      out.print(JsonUtil.loadTrueResult(data));
   }
   private String getTimeDesc(Date nowDate, Date targetDate) {
@@ -215,6 +244,84 @@
      return builder;
   }
   private void addRecommendGoods(Long uid, String imei, String idfa) {
      TaoBaoSearchResult result = TaoKeApiUtil.guessDeviceLike(1, 50, imei, idfa);
      if (result != null && result.getTaoBaoGoodsBriefs() != null) {
         for (int i = 0; i < result.getTaoBaoGoodsBriefs().size(); i++) {
            if (result.getTaoBaoGoodsBriefs().get(i).getCouponAmount() == null
                  || result.getTaoBaoGoodsBriefs().get(i).getCouponAmount().compareTo(new BigDecimal(0)) <= 0) {
               result.getTaoBaoGoodsBriefs().remove(i);
               i--;
            }
         }
         // 随机几个
         int count = (int) (Math.random() * 3) + 1;
         Set<Integer> positionList = new HashSet<>();
         for (int i = 0; i < count; i++)
            if (result.getTaoBaoGoodsBriefs().size() > 0) {
               positionList.add((int) (result.getTaoBaoGoodsBriefs().size() * Math.random()));
            }
         if (positionList != null && positionList.size() > 0) {
            List<CommonGoods> goodsList = new ArrayList<>();
            for (Integer p : positionList) {
               TaoBaoGoodsBrief goodsB = result.getTaoBaoGoodsBriefs().get(p);
               try {
                  CommonGoods cgoods = commonGoodsService
                        .addOrUpdateCommonGoods(CommonGoodsFactory.create(goodsB));
                  goodsList.add(cgoods);
               } catch (CommonGoodsException e) {
                  e.printStackTrace();
               }
            }
            try {
               recommendUserGoodsService.addRecommend(uid, "", goodsList);
            } catch (RecommendUserGoodsException e) {
               e.printStackTrace();
            }
         }
      }
   }
   private void addRecommendGoods(Long uid) {
      List<QualityFactory> qfList = qualityFactoryService.listByMinSalesCountOrderByCreateTimeDesc(10000, 1, 20);
      if (qfList != null && qfList != null) {
         for (int i = 0; i < qfList.size(); i++) {
            if (qfList.get(i).getTaoBaoGoodsBrief().getCouponAmount() == null
                  || qfList.get(i).getTaoBaoGoodsBrief().getCouponAmount().compareTo(new BigDecimal(0)) <= 0) {
               qfList.remove(i);
               i--;
            }
         }
         // 随机几个
         int count = (int) (Math.random() * 3) + 1;
         Set<Integer> positionList = new HashSet<>();
         for (int i = 0; i < count; i++)
            if (qfList.size() > 0) {
               positionList.add((int) (qfList.size() * Math.random()));
            }
         if (positionList != null && positionList.size() > 0) {
            List<CommonGoods> goodsList = new ArrayList<>();
            for (Integer p : positionList) {
               TaoBaoGoodsBrief goodsB = qfList.get(p).getTaoBaoGoodsBrief();
               try {
                  CommonGoods cgoods = commonGoodsService
                        .addOrUpdateCommonGoods(CommonGoodsFactory.create(goodsB));
                  goodsList.add(cgoods);
               } catch (CommonGoodsException e) {
                  e.printStackTrace();
               }
            }
            try {
               recommendUserGoodsService.addRecommend(uid, "", goodsList);
            } catch (RecommendUserGoodsException e) {
               e.printStackTrace();
            }
         }
      }
   }
   /**
    * 获取首页消息
    * 
@@ -228,6 +335,27 @@
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (page == 1) {
         boolean canAdd = false;
         int hour = Calendar.getInstance().get(Calendar.HOUR);
         RecommendUserGoods goods = recommendUserGoodsService.getLatestRecommendUserGoods(uid);
         if (goods == null) {
            canAdd = true;
         } else {
            // 是否和上次间隔时间足够
            if (System.currentTimeMillis() - goods.getCreateTime().getTime() >= 1000 * 60 * 60 * 8L) {
               canAdd = true;
            }
         }
         if (canAdd) {
            if (hour < 12)
               addRecommendGoods(uid);
            else
               addRecommendGoods(uid, acceptData.getImei(), acceptData.getIdfa());
         }
      }
      List<UserHomeMsgVO> volist = new ArrayList<>();
@@ -264,10 +392,12 @@
         }
         vo.setGoodsList(goodsList);
         vo.setTitle("猜你喜欢");
         vo.setIcon("http://img.flqapp.com/resource/msg/icon_msg_guess_like.png");
         volist.add(vo);
      }
      if (volist.size() < Constant.PAGE_SIZE) {
      if (volist.size() > 0 && volist.size() < Constant.PAGE_SIZE) {
         // 添加欢迎语
         UserHomeMsgVO vo = new UserHomeMsgVO();
         vo.setContent("恭喜你,成为返利券的用户\n独家优惠券帮你省钱;\n参与奖金活动帮你赚钱;\n让返利券成为你的省钱助手吧!");
@@ -280,12 +410,14 @@
      JSONObject root = new JSONObject();
      root.put("msgList", new Gson().toJson(volist));
      if (page == 1) {
         UserMsgUnReadNum num = userMsgReadStateService.getReadStateByUid(uid);
         UserSystemMsg usm = userSystemMsgService.getLatestUserSystemMsg(uid);
         if (usm != null)
            root.put("systemMsg", new UserSystemMsgVO(usm.getId(), usm.getType().name(), usm.getSolved(),
                  usm.getTitle(), usm.getContent(),
                  usm.getTimeTag() == 1 ? "http://img.flqapp.com/resource/msg/icon_emergent.png" : "",
                  Constant.systemCommonConfig.getDefaultPortrait(), getTimeDesc(now, usm.getCreateTime())));
            root.put("systemMsg",
                  new UserSystemMsgVO(usm.getId(), usm.getType().name(), usm.getSolved(), "系统消息", usm.getTitle(),
                        usm.getTimeTag() == 1 ? "http://img.flqapp.com/resource/msg/icon_emergent.png" : "",
                        "http://img.flqapp.com/resource/msg/icon_msg_system.png",
                        getTimeDesc(now, usm.getCreateTime()), num.getTypeSystem()));
      }
      root.put("count", count);
      out.print(JsonUtil.loadTrueResult(root));
@@ -313,11 +445,11 @@
            voList.add(new UserSystemMsgVO(usm.getId(), usm.getType().name(), usm.getSolved(), usm.getTitle(),
                  usm.getContent(),
                  usm.getTimeTag() == 1 ? "http://img.flqapp.com/resource/msg/icon_emergent.png" : "", null,
                  usm.getCreateTime().getTime() + ""));
                  usm.getCreateTime().getTime() + "", 0));
         }
      long count = userSystemMsgService.countUserSystemMsg(uid);
      userSystemMsgService.readMsgByUid(uid);
      userMsgReadStateService.readUserSystemMsg(uid);
      JSONObject root = new JSONObject();
      root.put("data", voList);