admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java
@@ -12,13 +12,19 @@
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.push.PushGoodsMapper;
import com.yeshi.fanli.entity.dynamic.GoodsEvaluate;
import com.yeshi.fanli.entity.dynamic.GoodsEvaluate.EvaluateEnum;
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.PushGoods;
import com.yeshi.fanli.entity.push.PushGoodsGroup;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.exception.push.PushGoodsException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.dynamic.GoodsEvaluateService;
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.push.PushGoodsGroupService;
import com.yeshi.fanli.service.inter.push.PushGoodsService;
@@ -46,6 +52,11 @@
   @Resource
   private PushGoodsGroupService pushGoodsGroupService;
   @Resource
   private GoodsEvaluateService goodsEvaluateService;
   @Override
   public int deleteByPrimaryKey(Long id) throws PushGoodsException {
@@ -63,7 +74,7 @@
   }
   @Override
   public PushGoods selectByPrimaryKey(Long id) throws PushGoodsException {
   public PushGoods selectByPrimaryKey(Long id) {
      return pushGoodsMapper.selectByPrimaryKey(id);
   }
@@ -78,25 +89,16 @@
   }
   @Override
   @Transactional
   @Transactional(rollbackFor=Exception.class)
   public void save(PushGoods record, List<Long> goodsIds) throws PushGoodsException, Exception {
      // 定时时间
      Boolean timeTask = record.isTimeTask();
      if (timeTask != null && timeTask) {
         String controlTime_str = record.getControlTime_str();
         if (controlTime_str == null || controlTime_str.trim().length() == 0) {
            throw new PushGoodsException(1, "预设时间不能为空");
         }
      if (StringUtil.isNullOrEmpty(record.getControlTime_str())) {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
         controlTime_str = controlTime_str.replaceAll("T", " ");
         record.setControlTime(format.parse(controlTime_str));
      } else {
         record.setControlTime(null);
         record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " ")));
      }
      record.setIsPush(PushGoods.STATE_INIT);
      record.setType(PushGoods.TYPE_GOODS);
      
      boolean isAdd = false;
      // 推送id
@@ -127,9 +129,8 @@
      saveGoodsInfo(isAdd, pushId, goodsIds);
   }
   @Transactional
   @Transactional(rollbackFor=Exception.class)
   public void saveGoodsInfo(boolean isAdd, Long pushId, List<Long> goodsIds) throws Exception {
      if (goodsIds == null || goodsIds.size() == 0) {
         if (!isAdd) {
            pushGoodsGroupService.deleteByPushId(pushId);
@@ -141,26 +142,26 @@
      List<Long> listCommonId = new ArrayList<Long>();
      for (Long auctionId : goodsIds) {
         // 获取商品详情
         TaoBaoGoodsBrief goodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
         // 转换简版商品信息
         CommonGoods commonGoods = CommonGoodsFactory.create(goodsBrief);
         if (commonGoods != null) {
            commonGoodsService.addOrUpdateCommonGoods(commonGoods);
            Long cid = commonGoods.getId();
            if (cid != null) {
               listCommonId.add(cid);
         try {
            TaoBaoGoodsBrief goodsBrief = TaoKeApiUtil.searchGoodsDetail(auctionId);
            // 转换简版商品信息
            CommonGoods commonGoods = CommonGoodsFactory.create(goodsBrief);
            if (commonGoods != null) {
               commonGoodsService.addOrUpdateCommonGoods(commonGoods);
               Long cid = commonGoods.getId();
               if (cid != null) {
                  listCommonId.add(cid);
               }
            }
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
      /* 修改时删除 部分 */
      if (!isAdd) {
         List<Long> listdelete = new ArrayList<Long>();
         List<PushGoodsGroup> listGroup = pushGoodsGroupService.selectByPushId(pushId);
         if (listGroup != null && listGroup.size() > 0) {
            for (PushGoodsGroup group : listGroup) {
               Long groupId = group.getId();
@@ -194,14 +195,91 @@
            pushGoodsGroup.setCommonGoods(new CommonGoods(cid));
            listAdd.add(pushGoodsGroup);
         }
         pushGoodsGroupService.insertBatch(listAdd);
      }
   }
   @Override
   @Transactional(rollbackFor=Exception.class)
   public void saveInfo(PushGoods record) throws PushGoodsException, Exception {
      if (StringUtil.isNullOrEmpty(record.getOtherId())) {
         throw new PushGoodsException(1, "相关内容ID不能为空");
      }
      
      if (StringUtil.isNullOrEmpty(record.getTitle())) {
         throw new PushGoodsException(1, "标题不能为空");
      }
      if (StringUtil.isNullOrEmpty(record.getContent())) {
         throw new PushGoodsException(1, "内容不能为空");
      }
      if (record.getType() == null) {
         throw new PushGoodsException(1, "推送类型不能为空");
      }
      if (record.getType() == PushGoods.TYPE_ACTIVITY) {
         GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(record.getOtherId());
         if (goodsEvaluate == null) {
            throw new PushGoodsException(1, "发圈活动信息不存在");
         }
         if (goodsEvaluate.getType() != EvaluateEnum.activity) {
            throw new PushGoodsException(1, "该内容非发圈活动信息");
         }
         String picUrl = null;
         List<ImgInfo> imgList = goodsEvaluate.getImgList();
         if (imgList != null && imgList.size() > 0) {
            for (ImgInfo imgInfo: imgList) {
               if (!StringUtil.isNullOrEmpty(imgInfo.getUrl())  && StringUtil.isNullOrEmpty(picUrl)
                     && (imgInfo.getType() == ImgEnum.img || imgInfo.getType() == ImgEnum.activity)) {
                  picUrl = imgInfo.getUrl();
                  break;
               }
            }
         }
         if (StringUtil.isNullOrEmpty(picUrl)) {
            throw new PushGoodsException(1, "该非发圈活动无图片信息");
         }
         record.setPicture(picUrl);
      }
      // 定时时间
      if (!StringUtil.isNullOrEmpty(record.getControlTime_str())) {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
         record.setControlTime(format.parse(record.getControlTime_str().replaceAll("T", " ")));
      }
      // 浏览数量随机
      if (record.getScanNum() == null || record.getScanNum()  <= 0)
         record.setScanNum((int) (Math.random() * 5000) + 1000);
      record.setIsPush(PushGoods.STATE_INIT);
      record.setAlertTitle(record.getTitle());
      record.setAlertContent(record.getContent());
      Long id = record.getId();
      if (id == null) {
         record.setCreateTime(new Date());
         pushGoodsMapper.insert(record);
      } else {
         PushGoods current = pushGoodsMapper.selectByPrimaryKey(id);
         if (current == null) {
            throw new PushGoodsException(1, "该记录已不存在");
         }
         record.setCreateTime(current.getCreateTime());
         record.setScanNum(current.getScanNum());
         record.setUpdateTime(new Date());
         pushGoodsMapper.updateByPrimaryKey(record);
      }
   }
   @Override
   @Transactional
   @Transactional(rollbackFor=Exception.class)
   public int deleteBatchByPrimaryKey(List<Long> list) throws Exception {
      /*List<PushGoods> lisState = pushGoodsMapper.listByPushState(list);
@@ -287,15 +365,101 @@
   
   @Override
   public void executePush(PushGoods pushGoods) throws Exception, PushGoodsException, PushException {
      Long id = pushGoods.getId();
      String alertTitle = pushGoods.getAlertTitle();
      String alertContent = pushGoods.getAlertContent();
      if (StringUtil.isNullOrEmpty(alertTitle) || StringUtil.isNullOrEmpty(alertContent)) {
      if (StringUtil.isNullOrEmpty(pushGoods.getTitle()) || StringUtil.isNullOrEmpty(pushGoods.getContent())) {
         throw new PushGoodsException(1, "推送标题及内容不能为空");
      }
      if (StringUtil.isNullOrEmpty(pushGoods.getVersions())) {
         throw new PushGoodsException(1, "推送版本不能为空");
      }
      if (pushGoods.getType() == null || pushGoods.getType() == PushGoods.TYPE_GOODS) {
         executePushGoods(pushGoods); // 推送商品
      } else if (pushGoods.getType() == PushGoods.TYPE_ACTIVITY) {
         executePushActivity(pushGoods); // 推送活动
      }
   }
   /**
    * 推送活动
    * @param pushGoods
    * @throws Exception
    * @throws PushGoodsException
    * @throws PushException
    */
   private void executePushActivity(PushGoods pushGoods) throws Exception, PushGoodsException, PushException {
      String otherId = pushGoods.getOtherId();
      if (StringUtil.isNullOrEmpty(otherId)) {
         throw new PushGoodsException(1, "活动id不能为空");
      }
      GoodsEvaluate goodsEvaluate = goodsEvaluateService.getById(otherId);
      if (goodsEvaluate == null) {
         throw new PushGoodsException(1, "发圈活动信息不存在");
      }
      if (goodsEvaluate.getType() != EvaluateEnum.activity) {
         throw new PushGoodsException(1, "该内容非发圈活动信息");
      }
      String picUrl = null;
      List<ImgInfo> imgList = goodsEvaluate.getImgList();
      if (imgList != null && imgList.size() > 0) {
         for (ImgInfo imgInfo: imgList) {
            if (imgInfo.getType() == ImgEnum.img && !StringUtil.isNullOrEmpty(imgInfo.getUrl())) {
               picUrl = imgInfo.getUrl();
            }
         }
      }
      if (StringUtil.isNullOrEmpty(picUrl)) {
         throw new PushGoodsException(1, "该非发圈活动无图片信息");
      }
      String versions = pushGoods.getVersions();
      JSONObject json = JSONObject.fromObject(versions);
      String versionsIOS = json.getString("IOS");
      List<String> listIOS = new ArrayList<String>();
      if (versionsIOS != null && versionsIOS.trim().length() > 0) {
         if (versionsIOS.contains("全推")) {
            listIOS = null;
         } else {
            listIOS = Arrays.asList(versionsIOS.split(","));
         }
      }
      List<String> listAndroid = new ArrayList<String>();
      String versionsAndroid  = json.getString("Android");
      if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
         if (versionsAndroid.contains("全推")) {
            listAndroid = null;
         } else {
            listAndroid = Arrays.asList(versionsAndroid.split(","));
         }
      }
      String url = configService.get(ConfigKeyEnum.pushActivityLink.getKey());
      if (StringUtil.isNullOrEmpty(url)) {
         throw new PushGoodsException(1, "推送页面链接不存在");
      }
      url = url + "?id=" + pushGoods.getId();
      // h活动全推
      pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid);
   }
   /**
    * 推送商品
    * @param pushGoods
    * @throws Exception
    * @throws PushGoodsException
    * @throws PushException
    */
   private void executePushGoods(PushGoods pushGoods) throws Exception, PushGoodsException, PushException {
      Long id = pushGoods.getId();
      List<PushGoodsGroup> goodsList = pushGoodsGroupService.getAllInfoByPushId(id);
      if (goodsList == null || goodsList.size() == 0) {
         throw new PushGoodsException(1, "推送无商品,请完善数据");
@@ -337,20 +501,18 @@
         Long goodsId = commonGoods.getGoodsId();
         String url = "https://item.taobao.com/item.htm?id=" + goodsId;
         pushService.pushGoods(pushGoods.getUid(), alertTitle, alertContent, url,listIOS, listAndroid);
         pushService.pushGoods(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url,listIOS, listAndroid);
      } else {
         /*   多个商品推送    */
         String url = configService.get("push_goods_details");
         String url = configService.get(ConfigKeyEnum.pushGoodsDetails.getKey());
         if (StringUtil.isNullOrEmpty(url)) {
            throw new PushGoodsException(1, "推送页面链接不存在");
         }
         // 生成链接
         url = url + "?id=" + id;
         pushService.pushUrl(pushGoods.getUid(), alertTitle, alertContent, url, listIOS, listAndroid);
         pushService.pushUrl(pushGoods.getUid(), pushGoods.getTitle(), pushGoods.getContent(), url, listIOS, listAndroid);
      }
   }
   
}