admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/controller/admin/PushController.java
@@ -12,9 +12,11 @@
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.JsonUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsgTypeEnum;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
@@ -25,15 +27,6 @@
@Controller(value = "adminPushController")
@RequestMapping("admin/new/api/v1/push")
public class PushController {
   public final static int GOODS = 1; // 商品推送
   public final static int URL = 2; // 网页推送
   public final static int DETAIL = 3; // 详情推送
   public final static int ZNX = 4; // 站内推送
   public final static int WEEX = 5; // 站内推送
   public final static int BAICHUAN = 6; // 站内推送
   public final static int WELFARE_CENTER = 7; // 福利中心
   @Resource
   private PushService pushService;
@@ -71,95 +64,6 @@
      }
   }
   /**
    * 商品推送 -(新后台)
    *
    * @param uId
    *            -用户ID 可为空
    * @param url
    *            -商品链接
    * @param title
    *            -推送标题
    * @param content
    *            -推送内容
    * @param out
    * @throws Exception
    */
   @RequestMapping(value = "newPushGoodsAll")
   public void newPushGoodsAll(String callback, String url, String title, String content, String uids, String versions, PrintWriter out)
         throws Exception {
      String auctionId = null;
      if (!StringUtil.isNullOrEmpty(url) && url.contains("id=")) {
         String[] sts = url.split("\\?")[1].split("&");
         for (String st : sts)
            if (st.contains("id=")) {
               auctionId = st.replace("id=", "");
            }
      }
      if (StringUtil.isNullOrEmpty(auctionId)) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("提取商品ID失败"));
         return;
      }
      JSONObject json = IOSPushFactory.createGoodsPush(Long.parseLong(auctionId), title, content);
      if (json.toString().getBytes().length > 256) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("标题或内容过长,请删减后再试"));
         return;
      }
      try {
         List<String> listuid = null;
         if (uids != null && uids.trim().length() > 0) {
            listuid = Arrays.asList(uids.split(","));
            if (listuid == null || listuid.size() == 0) {
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("用户id格式不正确"));
               return;
            }
         }
         List<String> listVersion = null;
         if (versions != null && versions.trim().length() > 0) {
            List<String> list = Arrays.asList(versions.split(","));
            if (list != null && list.size() > 0) {
               listVersion = new ArrayList<String>();
               for (String version: list) {
                  if (version != null && version.trim().length() > 0) {
                     listVersion.add(version.trim());
                  }
               }
            }
            if (listVersion == null || listVersion.size() == 0) {
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("版本号格式不正确"));
               return;
            }
         }
         if (listuid == null) {
            // 全推
            pushService.pushGoods(null, title, content, url, listVersion);
         } else {
            // 部分用户推送
            for (String str_uid: listuid) {
               if (str_uid != null && str_uid.trim().length() > 0) {
                  pushService.pushGoods(Long.parseLong(str_uid), url, title, content, listVersion);
               }
            }
         }
         JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("推送成功"));
      } catch (PushException e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getMsg()));
      } catch (Exception e) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送失败"));
         e.printStackTrace();
      }
   }
   /**
    * 网页推送 -(新后台)
@@ -172,7 +76,7 @@
    */
   @RequestMapping(value = "newPushUrlAll")
   public void newPushUrlAll(String callback, String url, String title, String content, String uids, 
         String versions, PrintWriter out) {
         String arrayIOS, String arrayAndroid, PrintWriter out) {
      if (StringUtil.isNullOrEmpty(url) || StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content)) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请填写链接,标题与内容"));
@@ -196,33 +100,23 @@
            }
         }
         
         List<String> listVersion = null;
         if (versions != null && versions.trim().length() > 0) {
            List<String> list = Arrays.asList(versions.split(","));
            if (list != null && list.size() > 0) {
               listVersion = new ArrayList<String>();
               for (String version: list) {
                  if (version != null && version.trim().length() > 0) {
                     listVersion.add(version.trim());
                  }
               }
            }
            if (listVersion == null || listVersion.size() == 0) {
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("版本号格式不正确"));
               return;
            }
         }
         if ((arrayIOS == null || arrayIOS.trim().length() == 0)
               && (arrayAndroid == null || arrayAndroid.trim().length() == 0)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送版本不能为空"));
            return;
         }
         List<String> listIOS = convertList(arrayIOS);
         List<String> listAndroid = convertList(arrayAndroid);
         
         if (listuid == null) {
            // 全推
            pushService.pushUrl(null, title, content, url, listVersion);
            pushService.pushUrl(null, title, content, url, listIOS, listAndroid);
         } else {
            // 部分推送
            for (String str_uid: listuid) {
               if (str_uid != null && str_uid.trim().length() > 0) {
                  pushService.pushUrl(Long.parseLong(str_uid), url, title, content, listVersion);
                  pushService.pushUrl(Long.parseLong(str_uid), title, content, url, listIOS, listAndroid);
               }
            }
         }
@@ -246,7 +140,8 @@
    * @throws Exception
    */
   @RequestMapping(value = "newPushFanZNX")
   public void newPushFanZNX(String callback, String uids, String title, String content, String versions, PrintWriter out) {
   public void newPushFanZNX(String callback, String uids, String title, String content, String arrayIOS,
         String arrayAndroid, PrintWriter out) {
      if (StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content)) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请填写标题与内容"));
@@ -264,33 +159,24 @@
            }
         }
         
         List<String> listVersion = null;
         if (versions != null && versions.trim().length() > 0) {
            List<String> list = Arrays.asList(versions.split(","));
            if (list != null && list.size() > 0) {
               listVersion = new ArrayList<String>();
               for (String version: list) {
                  if (version != null && version.trim().length() > 0) {
                     listVersion.add(version.trim());
                  }
               }
            }
            if (listVersion == null || listVersion.size() == 0) {
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("版本号格式不正确"));
               return;
            }
         }
         if ((arrayIOS == null || arrayIOS.trim().length() == 0)
               && (arrayAndroid == null || arrayAndroid.trim().length() == 0)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送版本不能为空"));
            return;
         }
         List<String> listIOS = convertList(arrayIOS);
         List<String> listAndroid = convertList(arrayAndroid);
         
         if (listuid == null) {
            // 全推
            pushService.pushZNX(null, title, content, listVersion);
            pushService.pushZNX(null, title, content, listIOS, listAndroid);
         } else {
            // 部分推送
            for (String str_uid: listuid) {
               if (str_uid != null && str_uid.trim().length() > 0) {
                  pushService.pushZNX(Long.parseLong(str_uid), title, content, listVersion);
                  pushService.pushZNX(Long.parseLong(str_uid), title, content, listIOS, listAndroid);
                  userSystemMsgService.addUserSystemMsg(Long.parseLong(str_uid), UserSystemMsgTypeEnum.question,
                        title, content, UserSystemMsg.TIME_TAG_EMERGENT, null);
@@ -316,7 +202,8 @@
    * @throws Exception
    */
   @RequestMapping(value = "pushBaiChuan")
   public void pushBaiChuan(String callback, String uids, String title, String content, String url, String versions, PrintWriter out) {
   public void pushBaiChuan(String callback, String uids, String title, String content, String url, String arrayIOS,
         String arrayAndroid, PrintWriter out) {
      if (StringUtil.isNullOrEmpty(title) || StringUtil.isNullOrEmpty(content)) {
         JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请填写标题与内容"));
@@ -334,33 +221,23 @@
            }
         }
         
         List<String> listVersion = null;
         if (versions != null && versions.trim().length() > 0) {
            List<String> list = Arrays.asList(versions.split(","));
            if (list != null && list.size() > 0) {
               listVersion = new ArrayList<String>();
               for (String version: list) {
                  if (version != null && version.trim().length() > 0) {
                     listVersion.add(version.trim());
                  }
               }
            }
            if (listVersion == null || listVersion.size() == 0) {
               JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("版本号格式不正确"));
               return;
            }
         }
         if ((arrayIOS == null || arrayIOS.trim().length() == 0)
               && (arrayAndroid == null || arrayAndroid.trim().length() == 0)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("推送版本不能为空"));
            return;
         }
         List<String> listIOS = convertList(arrayIOS);
         List<String> listAndroid = convertList(arrayAndroid);
         
         if (listuid == null) {
            // 全推
            pushService.pushBaiChuanUrl(null, title, content, url, listVersion);
            pushService.pushBaiChuanUrl(null, title, content, url, listIOS, listAndroid);
         } else {
            // 部分推送
            for (String str_uid: listuid) {
               if (str_uid != null && str_uid.trim().length() > 0) {
                  pushService.pushBaiChuanUrl(Long.parseLong(str_uid), title, content, url, listVersion);
                  pushService.pushBaiChuanUrl(Long.parseLong(str_uid), title, content, url, listIOS, listAndroid);
               }
            }
         }
@@ -374,4 +251,23 @@
      }
   }
   /**
    * 版本处理
    * @param array
    * @return
    */
   public List<String> convertList (String array) {
      Gson gson = new Gson();
      List<String> list = null;
      if (array == null || array.trim().length() == 0) {
         list = new ArrayList<String>(); // 长度为0 不推送
      } else {
         list = gson.fromJson(array, new TypeToken<ArrayList<String>>() {}.getType());
         if (list != null && list.size() > 0 && list.contains("全推")) {
            list = null; // 全推
         }
      }
      return list;
   }
}