admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/controller/client/v1/PushController.java
@@ -1,11 +1,13 @@
package com.yeshi.fanli.controller.client.v1;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -13,24 +15,33 @@
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.activity.ActivityUser;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.entity.dynamic.GoodsEvaluate;
import com.yeshi.fanli.entity.dynamic.ImgInfo;
import com.yeshi.fanli.entity.dynamic.ImgInfo.ImgEnum;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.push.DeviceActive;
import com.yeshi.fanli.entity.push.DeviceTokenOPPO;
import com.yeshi.fanli.entity.push.DeviceTokenVIVO;
import com.yeshi.fanli.entity.push.DeviceTokenXM;
import com.yeshi.fanli.entity.push.PushGoods;
import com.yeshi.fanli.entity.push.PushGoodsGroup;
import com.yeshi.fanli.entity.push.PushGoodsRecord;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.push.PushGoodsGroupException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService;
import com.yeshi.fanli.service.inter.push.DeviceActiveService;
import com.yeshi.fanli.service.inter.push.DeviceTokenHWService;
import com.yeshi.fanli.service.inter.push.DeviceTokenOPPOService;
import com.yeshi.fanli.service.inter.push.DeviceTokenVIVOService;
import com.yeshi.fanli.service.inter.push.DeviceTokenXMService;
import com.yeshi.fanli.service.inter.push.IOSPushService;
import com.yeshi.fanli.service.inter.push.PushGoodsGroupService;
import com.yeshi.fanli.service.inter.push.PushGoodsRecordService;
@@ -39,8 +50,11 @@
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.annotation.RequestSerializableByKey;
import com.yeshi.fanli.util.factory.JumpDetailParamsFactory;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import com.yeshi.fanli.vo.push.PushRecommendVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -89,6 +103,13 @@
   @Resource
   private DeviceTokenVIVOService deviceTokenVIVOService;
   @Resource
   private DeviceTokenXMService deviceTokenXMService;
   @Resource
   private GoodsEvaluateService goodsEvaluateService;
   @RequestMapping(value = "callback", method = RequestMethod.POST)
   public void callback(AcceptData acceptData, String pushId, PrintWriter out) {
@@ -193,127 +214,138 @@
    */
   @RequestMapping(value = "/gethistory", method = RequestMethod.POST)
   public void getHistory(AcceptData acceptData, Long uid, String deviceToken, int page, PrintWriter out) {
      long count = 0;
      List<PushGoods> list = null;
      JSONArray resultList = new JSONArray();
      DeviceActive deviceActive = deviceActiveService.getDeviceByDeviceAndPlatform(acceptData.getDevice(),
            deviceToken, acceptData.getPlatform());
      if (deviceActive != null) {
         // 设备注册时间
         Date createTime = deviceActive.getCreateTime();
         count = pushGoodsService.countHistoryByPushTime(uid, createTime);
         int pageSize = Constant.PAGE_SIZE;
         list = pushGoodsService.listHistoryByPushTime((page - 1) * pageSize, pageSize, uid, createTime);
         if (list != null && list.size() > 0) {
            for (PushGoods pushGoods : list) {
               JSONObject result = new JSONObject();
               Long pushId = pushGoods.getId();
               String picture = pushGoods.getPicture();
               String alertContent = pushGoods.getAlertContent();
               Date pushTime = pushGoods.getPushTime();
               result.put("content", alertContent);
               result.put("pushtime", pushTime.getTime());
               // 统计商品数量
               long totalgoods = 0;
               // 商品id
               CommonGoods commonGoods = null;
               if (StringUtil.isNullOrEmpty(picture)) {
                  try {
                     List<PushGoodsGroup> listGroup = pushGoodsGroupService.getAllInfoByPushId(pushId);
                     if (listGroup != null && listGroup.size() > 0) {
                        totalgoods = listGroup.size();
                        PushGoodsGroup pushGoodsGroup = listGroup.get(0);
                        if (pushGoodsGroup != null) {
                           commonGoods = pushGoodsGroup.getCommonGoods();
                           if (commonGoods != null) {
                              picture = commonGoods.getPicture();
                           }
                        }
                     }
                  } catch (PushGoodsGroupException e) {
                     e.printStackTrace();
                  }
               }
               result.put("picture", TbImgUtil.getTBSize320Img(picture));
               String fontColor1 = "#666666";
               String fontColor2 = "#E5005C";
               JSONArray array = new JSONArray();
               JSONObject contentJson1 = new JSONObject();
               contentJson1.put("color", fontColor1);
               contentJson1.put("content", "本次推荐共");
               JSONObject contentJson2 = new JSONObject();
               contentJson2.put("color", fontColor2);
               contentJson2.put("content", totalgoods);
               JSONObject contentJson3 = new JSONObject();
               contentJson3.put("color", fontColor1);
               contentJson3.put("content", "个商品");
               array.add(contentJson1);
               array.add(contentJson2);
               array.add(contentJson3);
               result.put("totalwords", array);
               String params = "";
               JumpDetailV2 jumpDetail = null;
               if (totalgoods == 1) {
                  params = JumpDetailParamsFactory.createGoodsParams(commonGoods.getGoodsId(),
                        commonGoods.getGoodsType());
                  // 单个商品跳转商品详情
                  jumpDetail = jumpDetailV2Service.getByTypeCache("goodsdetail",
                        Constant.getPlatformCode(acceptData.getPlatform()),
                        Integer.parseInt(acceptData.getVersion()));
               } else {
                  String url = configService.get("push_goods_details");
                  if (url == null) {
                     url = "";
                  }
                  url = url + "?id=" + pushId;
                  params = JumpDetailParamsFactory.createWEBParams(url);
                  // 跳转推荐详情
                  jumpDetail = jumpDetailV2Service.getByTypeCache("web",
                        Constant.getPlatformCode(acceptData.getPlatform()),
                        Integer.parseInt(acceptData.getVersion()));
               }
               result.put("params", params);
               result.put("jumpDetail", jumpDetail);
               resultList.add(result);
            }
         }
      if (deviceActive == null) {
         JSONObject resultJson = new JSONObject();
         resultJson.put("count", count);
         resultJson.put("result_list", resultList);
         out.print(JsonUtil.loadTrueResult(resultJson));
         return;
      }
      // 设备注册时间
      Date createTime = deviceActive.getCreateTime();
      count = pushGoodsService.countHistoryByPushTime(uid, createTime);
      int pageSize = Constant.PAGE_SIZE;
      List<PushGoods> list = pushGoodsService.listHistoryByPushTime((page - 1) * pageSize, pageSize, uid, createTime);
      if (list == null)
         list = new ArrayList<>();
      for (PushGoods pushGoods : list) {
         Long id = pushGoods.getId();
         PushRecommendVO vo = new PushRecommendVO();
         vo.setTitle(pushGoods.getTitle());
         vo.setContent(pushGoods.getContent());
         vo.setPicture(pushGoods.getPicture());
         vo.setPushtime(pushGoods.getPushTime().getTime());
         Integer scanNum = pushGoods.getScanNum();
         if (scanNum == null) {
//            vo.setScanNum("0");
         } else if (scanNum >= 10000) {
            double num = scanNum;
            String numMidea = String.format("%.1f", num / 10000);
            vo.setScanNum(numMidea + "万");
         } else {
            vo.setScanNum(scanNum +"");
         }
         String params = "";
         String nickName = "";
         JumpDetailV2 jumpDetail = null;
         List<ClientTextStyleVO> totalwords = new ArrayList<ClientTextStyleVO>();
         Integer type = pushGoods.getType();
         if (type == null || type == PushGoods.TYPE_GOODS) {
            long totalgoods = 0;
            CommonGoods goods = null;
            try {
               List<PushGoodsGroup> listGroup = pushGoodsGroupService.getAllInfoByPushId(id);
               if (listGroup != null && listGroup.size() > 0) {
                  totalgoods = listGroup.size();
                  PushGoodsGroup pushGoodsGroup = listGroup.get(0);
                  if (pushGoodsGroup != null) {
                     goods = pushGoodsGroup.getCommonGoods();
                  }
               }
            } catch (PushGoodsGroupException e) {
               e.printStackTrace();
            }
            if (goods != null && StringUtil.isNullOrEmpty(vo.getPicture())) {
               vo.setPicture(TbImgUtil.getTBSize320Img(goods.getPicture()));
            }
            if (totalgoods == 1) {
               params = JumpDetailParamsFactory.createGoodsParams(goods.getGoodsId(), goods.getGoodsType());
               jumpDetail = jumpDetailV2Service.getByTypeCache("goodsdetail",Constant.getPlatformCode(acceptData.getPlatform()),
                     Integer.parseInt(acceptData.getVersion()));
            } else {
               String url = configService.get(ConfigKeyEnum.pushGoodsDetails.getKey());
               if (url == null) {
                  url = "";
               }
               url = url + "?id=" + id;
               params = JumpDetailParamsFactory.createWEBParams(url);
               jumpDetail = jumpDetailV2Service.getByTypeCache("web",Constant.getPlatformCode(acceptData.getPlatform()),
                     Integer.parseInt(acceptData.getVersion()));
            }
            totalwords.add(new ClientTextStyleVO("本次推荐共", "#666666"));
            totalwords.add(new ClientTextStyleVO(totalgoods + "", "#E5005C"));
            totalwords.add(new ClientTextStyleVO("个商品", "#666666"));
         } else if (type == PushGoods.TYPE_ACTIVITY) {
            GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(pushGoods.getOtherId());
            if (goodsEvaluate == null) {
               continue;
            }
            ActivityUser user = goodsEvaluate.getUser();
            if (user != null) {
               nickName = user.getNickName();
            }
            if (StringUtil.isNullOrEmpty(vo.getPicture()) && goodsEvaluate.getImgList() != null &&
                  goodsEvaluate.getImgList().size() > 0) {
               for (ImgInfo tmgInfo: goodsEvaluate.getImgList()) {
                  if (tmgInfo.getType() == ImgEnum.img || tmgInfo.getType() == ImgEnum.activity) {
                     vo.setPicture(tmgInfo.getUrl());
                  }
               }
            }
            String url = configService.get(ConfigKeyEnum.pushActivityLink.getKey());
            if (url == null) {
               url = "";
            }
            url = url + "?id=" + id;
            params = JumpDetailParamsFactory.createWEBParams(url);
            jumpDetail = jumpDetailV2Service.getByTypeCache("web",
                  Constant.getPlatformCode(acceptData.getPlatform()), Integer.parseInt(acceptData.getVersion()));
         } else {
            continue;
         }
         vo.setNickName(nickName);
         vo.setParams(params);
         vo.setJumpDetail(jumpDetail);
         vo.setTotalwords(totalwords);
         resultList.add(vo);
      }
      JSONObject resultJson = new JSONObject();
      resultJson.put("count", count);
      resultJson.put("result_list", resultList);
      out.print(JsonUtil.loadTrueResult(resultJson));
      // 列表参数、设备参数信息不为空
      if (list != null && list.size() > 0 && deviceActive != null) {
      if (list != null && list.size() > 0 && deviceActive != null) {
         final List<PushGoods> listPush = list;
         // 记录访问信息
         executor.execute(new Runnable() {
@@ -321,7 +353,6 @@
            public void run() {
               try {
                  Long deviceId = deviceActive.getId();
                  for (PushGoods pushGoods : listPush) {
                     List<PushGoodsRecord> listRecord = pushGoodsRecordService
                           .listByPushIdAndDeviceId(pushGoods.getId(), deviceId);
@@ -336,11 +367,9 @@
                     pushGoodsRecord.setDeviceActive(deviceActive);
                     pushGoodsRecordService.insert(pushGoodsRecord);
                  }
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }
         });
      }
@@ -372,4 +401,96 @@
      out.print(JsonUtil.loadTrueResult(""));
   }
   @RequestMapping("registerXM")
   public void registerXM(AcceptData acceptData, String regId, Long uid, PrintWriter out) {
      DeviceActive deviceActive = deviceActiveService.getFirstActiveInfo(acceptData.getDevice());
      if (deviceActive != null) {
         DeviceTokenXM xm = new DeviceTokenXM();
         xm.setDeviceActiveId(deviceActive.getId());
         xm.setRegisterId(regId);
         xm.setUid(uid);
         deviceTokenXMService.addDeviceToken(xm);
      }
      out.print(JsonUtil.loadTrueResult(""));
   }
   /**
    * 获取推送活动详情
    * @param callback
    * @param acceptData
    * @param uid
    * @param id
    * @param out
    */
   @RequestMapping("getPushDetail")
   public void getPushDetail(String callback, AcceptData acceptData, Long uid, Long id, PrintWriter out) {
      if (uid == null || id == null) {
         JsonUtil.printMode(out, callback,JsonUtil.loadFalseResult("参数不完整"));
         return;
      }
      PushGoods pushGoods = pushGoodsService.selectByPrimaryKey(id);
      if (pushGoods == null || StringUtil.isNullOrEmpty(pushGoods.getOtherId())) {
         JsonUtil.printMode(out, callback,JsonUtil.loadFalseResult("该活动已下架"));
         return;
      }
      GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(pushGoods.getOtherId());
      if (goodsEvaluate == null) {
         JsonUtil.printMode(out, callback,JsonUtil.loadFalseResult("该活动已下架"));
         return;
      }
      String scanResult = null;
      Integer scanNum = pushGoods.getScanNum();
      if (scanNum == null) {
      } else if (scanNum >= 10000) {
         double num = scanNum;
         String numMidea = String.format("%.1f", num / 10000);
         scanResult = numMidea + "万";
      } else {
         scanResult = scanNum +"";
      }
      String jumpLink = configService.get(ConfigKeyEnum.activityDetailLink.getKey()) + "?type=%s&id=%s";
      jumpLink = String.format(jumpLink, "circle", goodsEvaluate.getId());
      if (!StringUtil.isNullOrEmpty(goodsEvaluate.getJumpLink())) {
         goodsEvaluate.setJumpLink(jumpLink);
      }
      List<ImgInfo> imgListNew = new ArrayList<>();
      List<ImgInfo> imgList = goodsEvaluate.getImgList();
      if (imgList != null && imgList.size() > 0) {
         for (ImgInfo imgInfo: imgList) {
            if (imgInfo.getType() != ImgEnum.img || imgInfo.getType() == ImgEnum.activity) {
               ImgInfo imgInfoNew = new ImgInfo();
               try {
                  PropertyUtils.copyProperties(imgInfoNew, imgInfo);
               } catch (Exception e) {
                  e.printStackTrace();
                  continue;
               }
               if (!StringUtil.isNullOrEmpty(imgInfoNew.getActivityUrl())) {
                  imgInfoNew.setActivityUrl(jumpLink);
               }
               imgListNew.add(imgInfoNew);
               continue;
            }
            imgListNew.add(imgInfo);
         }
      }
      goodsEvaluate.setImgList(imgListNew);
      GsonBuilder gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
      JSONObject json = new JSONObject();
      json.put("title", pushGoods.getTitle());
      json.put("content", pushGoods.getContent());
      json.put("pushTime", TimeUtil.formatDate(pushGoods.getPushTime()));
      json.put("scanNum", scanResult);
      json.put("detailInfo", gsonBuilder.create().toJson(goodsEvaluate));
      JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(json));
   }
}