admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/push/PushInfoServiceImpl.java
@@ -1,377 +1,383 @@
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dao.mybatis.push.PushInfoMapper;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsgTypeEnum;
import com.yeshi.fanli.entity.push.PushInfo;
import com.yeshi.fanli.entity.push.PushInfo.PushTypeEnum;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.exception.push.PushInfoException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.push.PushInfoService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
import net.sf.json.JSONObject;
@Service
public class PushInfoServiceImpl implements PushInfoService {
   @Resource
   private PushService pushService;
   @Resource
   private UserSystemMsgService userSystemMsgService;
   @Resource
   private ConfigService configService;
   @Resource
   private PushInfoMapper pushInfoMapper;
   @Override
   public void save(PushInfo record) throws PushInfoException, Exception {
      if (record == null) {
         throw new PushInfoException(1, "参数不正确");
      }
      PushTypeEnum type = record.getType();
      if (type == null) {
         throw new PushInfoException(1, "推送类型不能为空");
      }
      String title = record.getTitle();
      if (title == null || title.trim().length() == 0) {
         throw new PushInfoException(1, "标题不能为空");
      }
      String arrayAndroid = record.getArrayAndroid();
      String arrayIOS = record.getArrayIOS();
      if (StringUtil.isNullOrEmpty(arrayIOS) && StringUtil.isNullOrEmpty(arrayAndroid)) {
         throw new PushInfoException(1, "推送版本不能为空");
      }
      // 定时时间
      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 PushInfoException(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);
      }
      String url = record.getUrl();
      String content = record.getContent();
      if (StringUtil.isNullOrEmpty(url) && StringUtil.isNullOrEmpty(content)) {
         throw new PushInfoException(1, "推送内容或URL不能为空");
      }
      if (url != null && (url.trim().length() == 0 || url.equalsIgnoreCase("null"))) {
         record.setUrl(null);
      }
      if (content != null && (content.trim().length() == 0 || content.equalsIgnoreCase("null"))) {
         record.setContent(null);
      }
      String uids = record.getUids();
      if (uids != null && (uids.trim().length() == 0 || uids.equalsIgnoreCase("null"))) {
         record.setUids(null);
      }
      // 数据转换json
      convertJson(record);
      record.setState(PushInfo.STATE_INIT);
      Long id = record.getId();
      if (id == null) {
         record.setCreateTime(new Date());
         record.setUpdateTime(new Date());
         pushInfoMapper.insert(record);
      } else {
         // 修改
         PushInfo current = pushInfoMapper.selectByPrimaryKey(id);
         if (current == null) {
            throw new PushInfoException(1, "该记录已不存在");
         }
         if (PushInfo.STATE_SUCCESS == current.getState()) {
            throw new PushInfoException(1, "已推送成功的信息不能修改");
         }
         record.setCreateTime(current.getCreateTime());
         record.setUpdateTime(new Date());
         pushInfoMapper.updateByPrimaryKey(record);
      }
   }
   /**
    * 转换json
    *
    * @param pushGoods
    */
   public void convertJson(PushInfo record) {
      JSONObject json = new JSONObject();
      String url = record.getUrl();
      if (StringUtil.isNullOrEmpty(url)) {
         url = "";
      }
      json.put("url", url);
      json.put("ios", convertVersion(record.getArrayIOS()));
      json.put("android", convertVersion(record.getArrayAndroid()));
      record.setJsonData(json.toString());
   }
   /**
    * 转换list
    *
    * @param pushGoods
    */
   public String convertVersion(String array) {
      String versions = "";
      if (array != null && array.trim().length() > 0) {
         Gson gson = new Gson();
         List<String> list = gson.fromJson(array, new TypeToken<ArrayList<String>>() {
         }.getType());
         if (list != null && list.size() > 0) {
            for (String version: list) {
               versions += version + ",";
            }
            if (versions.endsWith(",")) {
               versions = versions.substring(0, versions.length() - 1);
            }
         }
      }
      return versions;
   }
   @Override
   public void deleteBatchByPrimaryKey(List<Long> list) {
      pushInfoMapper.deleteBatchByPrimaryKey(list);
   }
   @Override
   public List<PushInfo> listQuery(long start, int count, String key, Integer keyType, Integer state, String type) {
      List<PushInfo> list = pushInfoMapper.listQuery(start, count, key, keyType, state, type);
      if (list == null || list.size() == 0) {
         return list;
      }
      for (PushInfo pushInfo: list) {
         Date controlTime = pushInfo.getControlTime();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
         if (controlTime == null) {
            pushInfo.setTimeTask(false);
            pushInfo.setControlTime_str("");
         } else {
            pushInfo.setTimeTask(true);
            pushInfo.setControlTime_str(sdf.format(controlTime));
         }
         String jsonData = pushInfo.getJsonData();
         JSONObject json = JSONObject.fromObject(jsonData);
         String url = json.getString("url");
         pushInfo.setUrl(url);
         List<String> listIOS = new ArrayList<String>();
         String versionsIOS = json.getString("ios");
         if (versionsIOS != null && versionsIOS.trim().length() > 0) {
            listIOS = Arrays.asList(versionsIOS.split(","));
         }
         pushInfo.setListIOS(listIOS);
         List<String> listAndroid = new ArrayList<String>();
         String versionsAndroid = json.getString("android");
         if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
            listAndroid = Arrays.asList(versionsAndroid.split(","));
         }
         pushInfo.setListAndroid(listAndroid);
      }
      return list;
   }
   @Override
   public long countQuery(String key, Integer keyType, Integer state, String type) {
      return pushInfoMapper.countQuery(key, keyType, state, type);
   }
   @Override
   public List<PushInfo> listTask() {
      return pushInfoMapper.listTask();
   }
   @Override
   public void handPush(Long id) throws Exception, PushInfoException, PushException {
      PushInfo record = pushInfoMapper.selectByPrimaryKey(id);
      if (record == null) {
         throw new PushInfoException(1, "推送信息已不存在");
      }
      // 执行推送
      executePush(record);
      record.setState(PushInfo.STATE_SUCCESS);
      record.setPushTime(new Date());
      record.setUpdateTime(new Date());
      pushInfoMapper.updateByPrimaryKey(record);
   }
   @Override
   public void taskPush(PushInfo record) {
      String msg = null;
      int state = PushInfo.STATE_FAIL;
      try {
         // 执行推送
         executePush(record);
         state = PushInfo.STATE_SUCCESS;
      } catch (PushInfoException e) {
         msg = e.getMsg();
      } catch (PushException e) {
         msg = e.getMsg();
      } catch (Exception e) {
         msg = "系统推送失败";
      }
      record.setState(state);
      record.setPushTime(new Date());
      record.setRemark(msg);
      record.setUpdateTime(new Date());
      pushInfoMapper.updateByPrimaryKey(record);
   }
   @Override
   @Transactional
   public void executePush(PushInfo record) throws Exception, PushInfoException, PushException {
      if (PushInfo.STATE_SUCCESS == record.getState()) {
         throw new PushInfoException(1, "该信息不能重复推送");
      }
      String title = record.getTitle();
      if (title == null || title.trim().length() == 0) {
         throw new PushInfoException(1, "标题不能为空");
      }
      String jsonData = record.getJsonData();
      if (StringUtil.isNullOrEmpty(jsonData) && StringUtil.isNullOrEmpty(jsonData)) {
         throw new PushInfoException(1, "推送版本不能为空");
      }
      JSONObject json = JSONObject.fromObject(jsonData);
      String url = json.getString("url");
      String content = record.getContent();
      if (StringUtil.isNullOrEmpty(url) && StringUtil.isNullOrEmpty(content)) {
         throw new PushInfoException(1, "推送内容或URL不能为空");
      }
      List<String> listIOS = new ArrayList<String>();
      String versionsIOS = json.getString("ios");
      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(","));
         }
      }
      List<String> listuid = null;
      String uids = record.getUids();
      if (uids != null && uids.trim().length() > 0) {
         listuid = Arrays.asList(uids.split(","));
         if (listuid == null || listuid.size() == 0) {
            throw new PushInfoException(1, "用户id格式不正确");
         }
      }
      PushTypeEnum type = record.getType();
      if (type == null) {
         throw new PushInfoException(1, "推送类型不能为空");
      }
      int pushWay = 0;
      if (PushTypeEnum.ZNX == type) {
         pushWay = 1;
      } else if (PushTypeEnum.URL == type) {
         pushWay = 2;
      } else if (PushTypeEnum.BAICHUAN == type) {
         pushWay = 3;
      } else {
         throw new PushInfoException(1, "推送类型不匹配");
      }
      if (listuid == null) { // 全推
         switch (pushWay) {
         case 1: // 站内信
            pushService.pushZNX(null, title, content, listIOS, listAndroid);
            break;
         case 2: // 网页推送
            pushService.pushUrl(null, title, content, url, listIOS, listAndroid);
            break;
         case 3: // 百川
            pushService.pushBaiChuanUrl(null, title, content, url, listIOS, listAndroid);
            break;
         default:
            throw new PushInfoException(1, "推送类型不匹配");
         }
      } else {
         for (String str_uid : listuid) {
            if (str_uid != null && str_uid.trim().length() > 0) {
               long uid = Long.parseLong(str_uid);
               switch (pushWay) {
               case 1: // 站内信
                  pushService.pushZNX(uid, title, content, listIOS, listAndroid);
                  userSystemMsgService.addUserSystemMsg(uid, UserSystemMsgTypeEnum.question, title, content,
                        UserSystemMsg.TIME_TAG_EMERGENT, null);
                  break;
               case 2: // 网页推送
                  pushService.pushUrl(uid, title, content, url, listIOS, listAndroid);
                  break;
               case 3: // 百川
                  pushService.pushBaiChuanUrl(uid, title, content, url, listIOS, listAndroid);
                  break;
               default:
                  throw new PushInfoException(1, "推送类型不匹配");
               }
            }
         }
      }
   }
}
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 com.yeshi.fanli.entity.SystemEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dao.mybatis.push.PushInfoMapper;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsgTypeEnum;
import com.yeshi.fanli.entity.push.PushInfo;
import com.yeshi.fanli.entity.push.PushInfo.PushTypeEnum;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.exception.push.PushInfoException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.push.PushInfoService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
import net.sf.json.JSONObject;
@Service
public class PushInfoServiceImpl implements PushInfoService {
    @Resource
    private PushService pushService;
    @Resource
    private UserSystemMsgService userSystemMsgService;
    @Resource
    private PushInfoMapper pushInfoMapper;
    private Logger logger = LoggerFactory.getLogger("debugLog");
    @Override
    public void save(PushInfo record) throws PushInfoException, Exception {
        if (record == null) {
            throw new PushInfoException(1, "参数不正确");
        }
        PushTypeEnum type = record.getType();
        if (type == null) {
            throw new PushInfoException(1, "推送类型不能为空");
        }
        String title = record.getTitle();
        if (title == null || title.trim().length() == 0) {
            throw new PushInfoException(1, "标题不能为空");
        }
        String arrayAndroid = record.getArrayAndroid();
        String arrayIOS = record.getArrayIOS();
        if (StringUtil.isNullOrEmpty(arrayIOS) && StringUtil.isNullOrEmpty(arrayAndroid)) {
            throw new PushInfoException(1, "推送版本不能为空");
        }
        // 定时时间
        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 PushInfoException(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);
        }
        String url = record.getUrl();
        String content = record.getContent();
        if (StringUtil.isNullOrEmpty(url) && StringUtil.isNullOrEmpty(content)) {
            throw new PushInfoException(1, "推送内容或URL不能为空");
        }
        if (url != null && (url.trim().length() == 0 || url.equalsIgnoreCase("null"))) {
            record.setUrl(null);
        }
        if (content != null && (content.trim().length() == 0 || content.equalsIgnoreCase("null"))) {
            record.setContent(null);
        }
        String uids = record.getUids();
        if (uids != null && (uids.trim().length() == 0 || uids.equalsIgnoreCase("null"))) {
            record.setUids(null);
        }
        // 数据转换json
        convertJson(record);
        record.setState(PushInfo.STATE_INIT);
        Long id = record.getId();
        if (id == null) {
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            pushInfoMapper.insert(record);
        } else {
            // 修改
            PushInfo current = pushInfoMapper.selectByPrimaryKey(id);
            if (current == null) {
                throw new PushInfoException(1, "该记录已不存在");
            }
            if (PushInfo.STATE_SUCCESS == current.getState()) {
                throw new PushInfoException(1, "已推送成功的信息不能修改");
            }
            record.setCreateTime(current.getCreateTime());
            record.setUpdateTime(new Date());
            pushInfoMapper.updateByPrimaryKey(record);
        }
    }
    /**
     * 转换json
     */
    public void convertJson(PushInfo record) {
        JSONObject json = new JSONObject();
        String url = record.getUrl();
        if (StringUtil.isNullOrEmpty(url)) {
            url = "";
        }
        json.put("url", url);
        json.put("ios", convertVersion(record.getArrayIOS()));
        json.put("android", convertVersion(record.getArrayAndroid()));
        record.setJsonData(json.toString());
    }
    /**
     * 转换list
     */
    public String convertVersion(String array) {
        String versions = "";
        if (array != null && array.trim().length() > 0) {
            Gson gson = new Gson();
            List<String> list = gson.fromJson(array, new TypeToken<ArrayList<String>>() {
            }.getType());
            if (list != null && list.size() > 0) {
                for (String version : list) {
                    versions += version + ",";
                }
                if (versions.endsWith(",")) {
                    versions = versions.substring(0, versions.length() - 1);
                }
            }
        }
        return versions;
    }
    @Override
    public void deleteBatchByPrimaryKey(List<Long> list) {
        pushInfoMapper.deleteBatchByPrimaryKey(list);
    }
    @Override
    public List<PushInfo> listQuery(long start, int count, String key, Integer keyType, Integer state, String type, SystemEnum system) {
        List<PushInfo> list = pushInfoMapper.listQuery(start, count, key, keyType, state, type, system);
        if (list == null || list.size() == 0) {
            return list;
        }
        for (PushInfo pushInfo : list) {
            Date controlTime = pushInfo.getControlTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            if (controlTime == null) {
                pushInfo.setTimeTask(false);
                pushInfo.setControlTime_str("");
            } else {
                pushInfo.setTimeTask(true);
                pushInfo.setControlTime_str(sdf.format(controlTime));
            }
            String jsonData = pushInfo.getJsonData();
            JSONObject json = JSONObject.fromObject(jsonData);
            String url = json.getString("url");
            pushInfo.setUrl(url);
            List<String> listIOS = new ArrayList<String>();
            String versionsIOS = json.getString("ios");
            if (versionsIOS != null && versionsIOS.trim().length() > 0) {
                listIOS = Arrays.asList(versionsIOS.split(","));
            }
            pushInfo.setListIOS(listIOS);
            List<String> listAndroid = new ArrayList<String>();
            String versionsAndroid = json.getString("android");
            if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
                listAndroid = Arrays.asList(versionsAndroid.split(","));
            }
            pushInfo.setListAndroid(listAndroid);
        }
        return list;
    }
    @Override
    public long countQuery(String key, Integer keyType, Integer state, String type, SystemEnum system) {
        return pushInfoMapper.countQuery(key, keyType, state, type, system);
    }
    @Override
    public List<PushInfo> listTask(SystemEnum system) {
        return pushInfoMapper.listTask(system);
    }
    @Override
    public void handPush(Long id) throws Exception, PushInfoException, PushException {
        PushInfo record = pushInfoMapper.selectByPrimaryKey(id);
        if (record == null) {
            throw new PushInfoException(1, "推送信息已不存在");
        }
        // 执行推送
        executePush(record);
        record.setState(PushInfo.STATE_SUCCESS);
        record.setPushTime(new Date());
        record.setUpdateTime(new Date());
        pushInfoMapper.updateByPrimaryKey(record);
    }
    @Override
    public void taskPush(PushInfo record) {
        String msg = null;
        int state = PushInfo.STATE_FAIL;
        try {
            // 执行推送
            executePush(record);
            state = PushInfo.STATE_SUCCESS;
        } catch (PushInfoException e) {
            msg = e.getMsg();
        } catch (PushException e) {
            msg = e.getMsg();
        } catch (Exception e) {
            msg = "系统推送失败";
            logger.error("系统推送失败",e);
        }
        record.setState(state);
        record.setPushTime(new Date());
        record.setRemark(msg);
        record.setUpdateTime(new Date());
        pushInfoMapper.updateByPrimaryKey(record);
    }
    @Override
    public PushInfo selectByPrimaryKey(Long id) {
        return pushInfoMapper.selectByPrimaryKey(id);
    }
    @Override
    public void updateSelectiveByPrimaryKey(PushInfo info) {
        pushInfoMapper.updateByPrimaryKeySelective(info);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executePush(PushInfo record) throws Exception, PushInfoException, PushException {
        if (PushInfo.STATE_SUCCESS == record.getState()) {
            throw new PushInfoException(1, "该信息不能重复推送");
        }
        String title = record.getTitle();
        if (title == null || title.trim().length() == 0) {
            throw new PushInfoException(1, "标题不能为空");
        }
        String jsonData = record.getJsonData();
        if (StringUtil.isNullOrEmpty(jsonData) && StringUtil.isNullOrEmpty(jsonData)) {
            throw new PushInfoException(1, "推送版本不能为空");
        }
        JSONObject json = JSONObject.fromObject(jsonData);
        String url = json.getString("url");
        String content = record.getContent();
        if (StringUtil.isNullOrEmpty(url) && StringUtil.isNullOrEmpty(content)) {
            throw new PushInfoException(1, "推送内容或URL不能为空");
        }
        List<String> listIOS = new ArrayList<String>();
        String versionsIOS = json.getString("ios");
        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(","));
            }
        }
        List<String> listuid = null;
        String uids = record.getUids();
        if (uids != null && uids.trim().length() > 0) {
            listuid = Arrays.asList(uids.split(","));
            if (listuid == null || listuid.size() == 0) {
                throw new PushInfoException(1, "用户id格式不正确");
            }
        }
        PushTypeEnum type = record.getType();
        if (type == null) {
            throw new PushInfoException(1, "推送类型不能为空");
        }
        int pushWay = 0;
        if (PushTypeEnum.ZNX == type) {
            pushWay = 1;
        } else if (PushTypeEnum.URL == type) {
            pushWay = 2;
        } else if (PushTypeEnum.BAICHUAN == type) {
            pushWay = 3;
        } else {
            throw new PushInfoException(1, "推送类型不匹配");
        }
        if (listuid == null) { // 全推
            switch (pushWay) {
                case 1: // 站内信
                    pushService.pushZNX(null, title, content, listIOS, listAndroid, record.getSystem());
                    break;
                case 2: // 网页推送
                    pushService.pushUrl(null, title, content, url, listIOS, listAndroid, record.getSystem());
                    break;
                case 3: // 百川
                    pushService.pushBaiChuanUrl(null, title, content, url, listIOS, listAndroid, record.getSystem());
                    break;
                default:
                    throw new PushInfoException(1, "推送类型不匹配");
            }
        } else {
            for (String str_uid : listuid) {
                if (str_uid != null && str_uid.trim().length() > 0) {
                    long uid = Long.parseLong(str_uid);
                    switch (pushWay) {
                        case 1: // 站内信
                            pushService.pushZNX(uid, title, content, listIOS, listAndroid, record.getSystem());
                            userSystemMsgService.addUserSystemMsg(uid, UserSystemMsgTypeEnum.question, title, content,
                                    UserSystemMsg.TIME_TAG_COMMON, null);
                            break;
                        case 2: // 网页推送
                            pushService.pushUrl(uid, title, content, url, listIOS, listAndroid, record.getSystem());
                            break;
                        case 3: // 百川
                            pushService.pushBaiChuanUrl(uid, title, content, url, listIOS, listAndroid, record.getSystem());
                            break;
                        default:
                            throw new PushInfoException(1, "推送类型不匹配");
                    }
                }
            }
        }
    }
}