admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushGoodsServiceImpl.java
@@ -1,14 +1,15 @@
package com.yeshi.fanli.service.impl.push;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.push.PushGoodsMapper;
import com.yeshi.fanli.entity.goods.CommonGoods;
@@ -78,8 +79,25 @@
   @Override
   @Transactional
   public void save(PushGoods record, List<Long> goodsIds) throws Exception {
   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, "预设时间不能为空");
         }
         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.setIsPush(PushGoods.STATE_INIT);
      boolean isAdd = false;
      // 推送id
      Long pushId = record.getId();
@@ -89,14 +107,20 @@
         record.setCreateTime(new Date());
         record.setUpdateTime(new Date());
         pushGoodsMapper.insertSelective(record);
         pushGoodsMapper.insert(record);
         pushId = record.getId();
      } else {
         // 修改
         PushGoods current = pushGoodsMapper.selectByPrimaryKey(pushId);
         if (current == null) {
            throw new PushGoodsException(1, "该记录已不存在");
         }
         record.setCreateTime(current.getCreateTime());
         record.setUpdateTime(new Date());
         pushGoodsMapper.updateByPrimaryKeySelective(record);
         pushGoodsMapper.updateByPrimaryKey(record);
      }
      // 处理商品信息
@@ -214,28 +238,72 @@
      return pushGoodsMapper.countHistoryByPushTime(uid, pushTime);
   }
   @Override
   public void executePush(Long id) throws Exception, PushGoodsException, PushException {
      PushGoods pushGoods = selectByPrimaryKey(id);
      if (pushGoods == null) {
         throw new PushException(1, "推送信息已不存在");
   public List<PushGoods> listTask() {
      return pushGoodsMapper.listTask();
   }
   @Override
   public void taskPush(PushGoods record) {
      String msg = null;
      int state = PushGoods.STATE_FAIL;
      try {
         // 执行推送
         executePush(record);
         state = PushGoods.STATE_SUCCESS;
      } catch (PushGoodsException e) {
         msg = e.getMsg();
      } catch (PushException e) {
         msg = e.getMsg();
      } catch (Exception e) {
         msg = "系统推送失败";
      }
      record.setIsPush(state);
      record.setPushTime(new Date());
      record.setRemark(msg);
      record.setUpdateTime(new Date());
      pushGoodsMapper.updateByPrimaryKey(record);
   }
   @Override
   public void handPush(Long id) throws Exception, PushGoodsException, PushException {
      PushGoods record = selectByPrimaryKey(id);
      if (record == null) {
         throw new PushGoodsException(1, "推送信息已不存在");
      }
      // 执行推送
      executePush(record);
      record.setIsPush(PushGoods.STATE_SUCCESS);
      record.setPushTime(new Date());
      record.setUpdateTime(new Date());
      pushGoodsMapper.updateByPrimaryKey(record);
   }
   @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)) {
         throw new PushException(1, "推送标题及内容不能为空");
         throw new PushGoodsException(1, "推送标题及内容不能为空");
      }
      List<PushGoodsGroup> goodsList = pushGoodsGroupService.getAllInfoByPushId(id);
      if (goodsList == null || goodsList.size() == 0) {
         throw new PushException(1, "推送无商品,请完善数据");
         throw new PushGoodsException(1, "推送无商品,请完善数据");
      }
      
      String versions = pushGoods.getVersions();
      if (versions == null || versions.trim().length() == 0) {
         throw new PushException(1, "推送版本不能为空");
         throw new PushGoodsException(1, "推送版本不能为空");
      }
      
      List<String> listIOS = new ArrayList<String>();
@@ -264,7 +332,7 @@
         PushGoodsGroup pushGoodsGroup = goodsList.get(0);
         CommonGoods commonGoods = pushGoodsGroup.getCommonGoods();
         if (commonGoods == null) {
            throw new PushException(1, "商品详细信息已不存在");
            throw new PushGoodsException(1, "商品详细信息已不存在");
         }
         Long goodsId = commonGoods.getGoodsId();
@@ -276,22 +344,13 @@
         /*   多个商品推送    */
         String url = configService.get("push_goods_details");
         if (StringUtil.isNullOrEmpty(url)) {
            throw new PushException(1, "推送页面链接不存在");
            throw new PushGoodsException(1, "推送页面链接不存在");
         }
         // 生成链接
         url = url + "?id=" + id;
         pushService.pushUrl(pushGoods.getUid(), alertTitle, alertContent, url, listIOS, listAndroid);
      }
      // 已推送
      pushGoods.setPush(true);
      // 推送时间
      pushGoods.setPushTime(new Date());
      // 更新时间
      pushGoods.setUpdateTime(new Date());
      updateByPrimaryKeySelective(pushGoods);
   }
}