yujian
2019-01-22 ca60a2588098250cb68a76dad135fa23d63ea952
fanli/src/main/java/com/yeshi/fanli/controller/client/UserMsgController.java
@@ -4,34 +4,88 @@
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.BigDecimalUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.msg.MsgAccountDetail;
import com.yeshi.fanli.entity.bus.msg.MsgInviteDetail;
import com.yeshi.fanli.entity.bus.msg.MsgInviteDetail.MsgTypeInviteTypeEnum;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail;
import com.yeshi.fanli.entity.bus.msg.MsgOrderDetail;
import com.yeshi.fanli.entity.bus.msg.UserMsgUnReadNum;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
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.exception.msg.UserSystemMsgException;
import com.yeshi.fanli.service.inter.goods.RecommendUserGoodsService;
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.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.msg.UserMsgVOFactory;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import com.yeshi.fanli.vo.msg.CommonMsgItemVO;
import com.yeshi.fanli.vo.msg.UserHomeMsgVO;
import com.yeshi.fanli.vo.msg.UserMsgVO;
import com.yeshi.fanli.vo.msg.UserSystemMsgVO;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("api/v1/user/msg")
public class UserMsgController {
   @Resource
   private MsgOrderDetailService msgOrderDetailService;
   @Resource
   private MsgMoneyDetailService msgMoneyDetailService;
   @Resource
   private MsgInviteDetailService msgInviteDetailService;
   @Resource
   private MsgAccountDetailService msgAccountDetailService;
   @Resource
   private UserMsgReadStateService userMsgReadStateService;
   @Resource
   private RecommendUserGoodsService recommendUserGoodsService;
   @Resource
   private UserInfoService userInfoService;
   @Resource
   private UserSystemMsgService userSystemMsgService;
   // 消息的类型
   final static String MSG_TYPE_ORDER = "order";
   final static String MSG_TYPE_MONEY = "money";
   final static String MSG_TYPE_ACCOUNT = "account";
   final static String MSG_TYPE_INVITE = "invite";
   /**
    * 获取订单消息列表
    * 
@@ -40,116 +94,275 @@
    * @param page
    * @param out
    */
   @RequestMapping(value = "getOrderMsgList", method = RequestMethod.POST)
   public void getOrderMsgList(AcceptData acceptData, Long uid, int page, PrintWriter out) {
   @RequestMapping(value = "getMsgList", method = RequestMethod.POST)
   public void getOrderMsgList(AcceptData acceptData, Long uid, int page, String type, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (StringUtil.isNullOrEmpty(type)) {
         out.print(JsonUtil.loadFalseResult(2, "请上传type"));
         return;
      }
      List<UserMsgVO> list = new ArrayList<>();
      long count = 0;
      List<CommonMsgItemVO> items = new ArrayList<>();
      switch (type) {
      case MSG_TYPE_ORDER:
         userMsgReadStateService.readOrderMsg(uid);
         List<MsgOrderDetail> detailList = msgOrderDetailService.listMsgOrderDetail(uid, page);
         count = msgOrderDetailService.countMsgOrderDetail(uid);
         if (detailList != null)
            for (MsgOrderDetail detail : detailList)
               list.add(UserMsgVOFactory.create(detail));
         break;
      case MSG_TYPE_MONEY:
         userMsgReadStateService.readMoneyMsg(uid);
         List<MsgMoneyDetail> detailList1 = msgMoneyDetailService.listMsgMoneyDetail(uid, page);
         count = msgMoneyDetailService.countMsgMoneyDetail(uid);
         if (detailList1 != null)
            for (MsgMoneyDetail detail : detailList1)
               list.add(UserMsgVOFactory.create(detail));
         break;
      case MSG_TYPE_ACCOUNT:
         userMsgReadStateService.readAccountMsg(uid);
         List<MsgAccountDetail> detailList2 = msgAccountDetailService.listMsgAccountDetail(uid, page);
         count = msgAccountDetailService.countMsgAccountDetail(uid);
         if (detailList2 != null)
            for (MsgAccountDetail detail : detailList2)
               list.add(UserMsgVOFactory.create(detail));
         break;
      case MSG_TYPE_INVITE:
         userMsgReadStateService.readInviteMsg(uid);
         List<MsgInviteDetail> detailList3 = msgInviteDetailService.listMsgInviteDetail(uid, page);
         count = msgInviteDetailService.countMsgInviteDetail(uid);
         if (detailList3 != null)
            for (MsgInviteDetail detail : detailList3)
               list.add(UserMsgVOFactory.create(detail));
         break;
      }
      List<ClientTextStyleVO> contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("206089388856584237", "#000000"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("订单号", "#888888"), contentList));
      contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("共", "#000000"));
      contentList.add(new ClientTextStyleVO("1", "#E5005C"));
      contentList.add(new ClientTextStyleVO("件商品", "#000000"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("商品数量", "#888888"), contentList));
      contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("邀请订单", "#000000"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("订单类型", "#888888"), contentList));
      contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("已付款", "#E5005C"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("订单状态", "#888888"), contentList));
      contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("¥58", "#E5005C"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("付款金额", "#888888"), contentList));
      contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("¥0.36", "#E5005C"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("邀请奖金", "#888888"), contentList));
      contentList = new ArrayList<>();
      contentList.add(new ClientTextStyleVO("无", "#000000"));
      items.add(new CommonMsgItemVO(new ClientTextStyleVO("备注", "#888888"), contentList));
      list.add(new UserMsgVO("http://ec-1255749512.file.myqcloud.com/resource/portrait.png", "订单消息", new Date(),
            items));
      GsonBuilder builder = new GsonBuilder();
      builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
      builder = getTimeDescJsonBuilder(builder);
      JSONObject root = new JSONObject();
      root.put("data", builder.create().toJson(list));
      root.put("count", count);
      out.print(JsonUtil.loadTrueResult(root));
   }
   /**
    * 获取消息未读数
    *
    * @param acceptData
    * @param uid
    * @param out
    */
   @RequestMapping(value = "getUnReadMsgCount", method = RequestMethod.POST)
   public void getUnReadMsgCount(AcceptData acceptData, Long uid, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      UserMsgUnReadNum num = userMsgReadStateService.getReadStateByUid(uid);
      JSONObject data = JSONObject.fromObject(JsonUtil.getApiCommonGson().toJson(num));
      data.put("totalCount", num.getTypeAccount() + num.getTypeInvite() + num.getTypeMoney() + num.getTypeOrder()
            + num.getTypeScore() + num.getTypeSystem());
      out.print(JsonUtil.loadTrueResult(data));
   }
   private String getTimeDesc(Date nowDate, Date targetDate) {
      Calendar now = Calendar.getInstance();
      now.setTime(nowDate);
      if (TimeUtil.getGernalTime(targetDate.getTime(), "yyyy-MM-dd")
            .equalsIgnoreCase(TimeUtil.getGernalTime(now.getTimeInMillis(), "yyyy-MM-dd"))) {
         return "今天 " + TimeUtil.getGernalTime(targetDate.getTime(), "HH:mm");
      } else if (TimeUtil.getGernalTime(targetDate.getTime(), "yyyy-MM-dd")
            .equalsIgnoreCase(TimeUtil.getGernalTime(now.getTimeInMillis() - 1000 * 60 * 60 * 24L, "yyyy-MM-dd"))) {
         return "昨天 " + TimeUtil.getGernalTime(targetDate.getTime(), "HH:mm");
      } else {
         return TimeUtil.getGernalTime(targetDate.getTime(), "yyyy.MM.dd HH:mm");
      }
   }
   private GsonBuilder getTimeDescJsonBuilder(GsonBuilder builder) {
      builder = builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
         @Override
         public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
            if (value == null) {
               return new JsonPrimitive("");
            } else {
               return new JsonPrimitive("今天 10:25");
               Calendar now = Calendar.getInstance();
               return new JsonPrimitive(getTimeDesc(new Date(now.getTimeInMillis()), value));
            }
         }
      }).registerTypeAdapter(BigDecimal.class, new JsonSerializer<BigDecimal>() {
         @Override
         public JsonElement serialize(BigDecimal value, Type theType, JsonSerializationContext context) {
            if (value == null) {
               return new JsonPrimitive("0");
            } else {
               return new JsonPrimitive(BigDecimalUtil.getWithNoZera(value).toString());
            }
         }
      });
      JSONObject root = new JSONObject();
      root.put("data", builder.create().toJson(list));
      root.put("count", 1);
      out.print(JsonUtil.loadTrueResult(root));
      return builder;
   }
   /**
    * 邀请消息
    * 获取首页消息
    * 
    * @param acceptData
    * @param uid
    * @param page
    * @param out
    */
   @RequestMapping(value = "getInviteMsgList", method = RequestMethod.POST)
   public void getInviteMsgList(AcceptData acceptData, Long uid, int page, PrintWriter out) {
   @RequestMapping(value = "getHomeMsgList", method = RequestMethod.POST)
   public void getHomeMsgList(AcceptData acceptData, Long uid, int page, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      List<UserMsgVO> list = new ArrayList<>();
      List<UserHomeMsgVO> volist = new ArrayList<>();
      List<RecommendUserGoods> list = recommendUserGoodsService.listRecommend(uid, page, Constant.PAGE_SIZE);
      long count = recommendUserGoodsService.countRecommend(uid) + 1;// 计算欢迎语的数量
      for (int i = 0; i < 20; i++) {
         MsgInviteDetail detail = new MsgInviteDetail();
         detail.setCreateTime(new Date());
         ThreeSale ts = new ThreeSale();
         UserInfo worker = new UserInfo(438965L);
         worker.setPortrait("https://g2.ykimg.com/051400005A38ADC2ADBAC3A4BB0C648E");
         worker.setNickName("呵呵");
         ts.setWorker(worker);
         detail.setInviteUser(ts);
         detail.setDesc("恭喜你,有新队员加入你的队列,若对方60天内未激活将会与你脱离邀请关系");
         detail.setBeiZhu("无");
         detail.setMsgType(MsgTypeInviteTypeEnum.invite);
         list.add(UserMsgVOFactory.create(detail));
      Date now = new Date();
      for (RecommendUserGoods goods : list) {
         UserHomeMsgVO vo = new UserHomeMsgVO();
         vo.setContent(goods.getRecommendDesc());
         vo.setCreateTime(getTimeDesc(now, goods.getCreateTime()));
         if (goods.getGoodsList().size() == 1)
            vo.setType(UserHomeMsgVO.TYPE_GOODS_1);
         else if (goods.getGoodsList().size() == 2)
            vo.setType(UserHomeMsgVO.TYPE_GOODS_2);
         else if (goods.getGoodsList().size() == 3)
            vo.setType(UserHomeMsgVO.TYPE_GOODS_3);
         List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
         for (CommonGoods cg : goods.getGoodsList()) {
            if (goods.getGoodsList().size() == 1)
               cg.setPicture(TbImgUtil.getTBSizeImg(cg.getPicture(), 400));
            else if (goods.getGoodsList().size() == 2)
               cg.setPicture(TbImgUtil.getTBSizeImg(cg.getPicture(), 320));
            else if (goods.getGoodsList().size() == 3)
               cg.setPicture(TbImgUtil.getTBSizeImg(cg.getPicture(), 220));
            TaoBaoGoodsBrief tbGoods = new TaoBaoGoodsBrief();
            if (cg.getCouponAmount() == null)
               tbGoods.setCouponAmount(new BigDecimal(0));
            else
               tbGoods.setCouponAmount(cg.getCouponAmount());
            tbGoods.setAuctionId(cg.getGoodsId());
            tbGoods.setPictUrl(cg.getPicture());
            goodsList.add(tbGoods);
         }
         vo.setGoodsList(goodsList);
         vo.setTitle("猜你喜欢");
         vo.setIcon("http://img.flqapp.com/resource/msg/icon_msg_guess_like.png");
         volist.add(vo);
      }
      GsonBuilder builder = new GsonBuilder();
      builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
         @Override
         public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
            if (value == null) {
               return new JsonPrimitive("");
            } else {
               return new JsonPrimitive("今天 10:25");
            }
         }
      });
      if (volist.size() > 0 && volist.size() < Constant.PAGE_SIZE) {
         // 添加欢迎语
         UserHomeMsgVO vo = new UserHomeMsgVO();
         vo.setContent("恭喜你,成为返利券的用户\n独家优惠券帮你省钱;\n参与奖金活动帮你赚钱;\n让返利券成为你的省钱助手吧!");
         vo.setTitle("新人欢迎");
         vo.setCreateTime(getTimeDesc(now, new Date(userInfoService.getUserById(uid).getCreatetime())));
         vo.setType(UserHomeMsgVO.TYPE_WELCOME);
         volist.add(vo);
      }
      JSONObject root = new JSONObject();
      root.put("data", builder.create().toJson(list));
      root.put("count", 1);
      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()),
                        num.getTypeSystem()));
      }
      root.put("count", count);
      out.print(JsonUtil.loadTrueResult(root));
   }
   /**
    * 获取系统消息列表
    *
    * @param acceptData
    * @param uid
    * @param page
    * @param out
    */
   @RequestMapping(value = "getSystemMsgList", method = RequestMethod.POST)
   public void getSystemMsgList(AcceptData acceptData, Long uid, int page, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      List<UserSystemMsgVO> voList = new ArrayList<>();
      List<UserSystemMsg> list = userSystemMsgService.listUserSystemMsg(uid, page, Constant.PAGE_SIZE);
      if (list != null)
         for (UserSystemMsg usm : list) {
            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() + "", 0));
         }
      long count = userSystemMsgService.countUserSystemMsg(uid);
      userMsgReadStateService.readUserSystemMsg(uid);
      JSONObject root = new JSONObject();
      root.put("data", voList);
      root.put("count", count);
      out.print(JsonUtil.loadTrueResult(root));
   }
   /**
    * 设置问题已经解决
    *
    * @param acceptData
    * @param uid
    * @param id
    * @param out
    */
   @RequestMapping(value = "setSystemMsgSolved", method = RequestMethod.POST)
   public void setSystemMsgSolved(AcceptData acceptData, Long uid, Long id, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      if (id == null) {
         out.print(JsonUtil.loadFalseResult(2, "消息ID为空"));
         return;
      }
      try {
         userSystemMsgService.setSystemMsgSolved(uid, id);
         out.print(JsonUtil.loadTrueResult(""));
      } catch (UserSystemMsgException e) {
         out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
      }
   }
   @RequestMapping(value = "setAllMsgRead", method = RequestMethod.POST)
   public void setAllMsgRead(AcceptData acceptData, Long uid, PrintWriter out) {
      if (uid == null) {
         out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
         return;
      }
      userMsgReadStateService.setAllMsgRead(uid);
      out.print(JsonUtil.loadTrueResult(""));
   }
}