package com.bosshand.virgo.api.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.bosshand.virgo.api.dao.ReminderDao; import com.bosshand.virgo.api.dao.RemoteReminderDao; import com.bosshand.virgo.api.model.Reminder; import com.bosshand.virgo.api.model.RemoteReminder; import com.bosshand.virgo.core.utils.StringUtil; import com.bosshand.virgo.core.utils.WeChatUtil; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; @Service public class ReminderService { static Logger log = LoggerFactory.getLogger(ReminderService.class); @Autowired ReminderDao reminderDao; @Autowired RemoteReminderDao remoteReminderDao; @Autowired ManagerClient managerClient; public int getTotalCount(Reminder reminder) { return reminderDao.getTotalCount(reminder); } public List getLimit(Reminder reminder, int currPage, int pageSize) { int currIndex = (currPage - 1) * pageSize; return reminderDao.getLimit(reminder, currIndex, pageSize); } public Reminder get(long id) { return reminderDao.get(id); } public Reminder insert(Reminder reminder) { reminderDao.insert(reminder); return reminder; } public Reminder update(Reminder reminder) { reminderDao.update(reminder); return reminder; } public void addMessage(long id) { Reminder reminder = reminderDao.get(id); long userId = reminder.getCreator(); long organizationId = reminder.getOrganizationId(); long projectId = reminder.getProjectId(); JSONObject json = new JSONObject(); json.put("organizationId", organizationId); json.put("projectId", projectId); json.put("dataId", reminder.getId()); List userIds = new ArrayList<>(); userIds.add(userId); String userList = reminder.getUserList(); if (StringUtil.notBlank(userList)) { JSONArray jsonArray = JSONObject.parseArray(userList); if (jsonArray.size() > 0) { for (int i = 0; i < jsonArray.size(); i++) { long uid = jsonArray.getJSONObject(i).getLongValue("id"); userIds.add(uid); } } } JSONArray ja = new JSONArray(); for(Long d : userIds){ JSONObject js = new JSONObject(); js.put("messageType", 7); js.put("sender", userId); js.put("title", "提醒消息"); js.put("message","【提醒消息】"); js.put("userId",d); js.put("viewed", false); js.put("systemMessage", false); js.put("pushed", false); js.put("json", json.toJSONString()); js.put("isCC", false); ja.add(js); } managerClient.pushReminderMessage(ja); } public int updateState(long id) { this.addMessage(id); return reminderDao.updateState(id); } public List getList(Reminder reminder) { return reminderDao.getList(reminder); } public int delete(long id) { return reminderDao.delete(id); } public String getToken() { JSONObject js = new JSONObject(); String userId = "system"; long timestamp = System.currentTimeMillis() + 30000; String yei = "50abd47112ebe8c5a73f4694c96a49ce"; String sign = DigestUtils.md5Hex(userId + timestamp + yei); js.put("userId", userId); js.put("timestamp", timestamp); js.put("sign", sign); String s = WeChatUtil.httpPost("https://www.waywish.com/im/user/token/get", js, null); if (StringUtil.notBlank(s)) { String token = JSONObject.parseObject(s).getJSONObject("data").getString("token"); return token; } return null; } public void send(String token, String userId, String data, String type) { long m = System.currentTimeMillis(); JSONObject js = new JSONObject(); js.put("conversationId", userId); js.put("conversationType", "private"); js.put("from", "system"); js.put("to", userId); js.put("type", "text"); js.put("isRead", 0); js.put("isRevoke", 0); js.put("isDeleted", 0); js.put("status", "unSend"); js.put("time", m); js.put("extra", data); JSONObject jo = new JSONObject(); jo.put("nickname", "系统通知"); jo.put("avatarUrl", "https://file-node.oss-cn-shanghai.aliyuncs.com/youji/3156449b8a1a4874981b2a76d5947721"); js.put("fromUserInfo", jo); JSONObject j = new JSONObject(); j.put("text", type); js.put("body", j); String s = WeChatUtil.httpPost("https://www.waywish.com/im/message/save", js, token); log.info("在" + m + "时间点给userId:" + userId + "发送消息成功."); } //================================================远程提醒设备设置======================================================================== public int getRemoteReminderTotalCount(RemoteReminder remotereminder) { return remoteReminderDao.getTotalCount(remotereminder); } public List getRemoteReminderLimit(RemoteReminder remotereminder, int currPage, int pageSize) { int currIndex = (currPage - 1) * pageSize; return remoteReminderDao.getLimit(remotereminder, currIndex, pageSize); } public RemoteReminder getRemoteReminder(long id) { return remoteReminderDao.get(id); } public RemoteReminder insertRemoteReminder(RemoteReminder remotereminder) { remoteReminderDao.insert(remotereminder); return remotereminder; } public RemoteReminder updateRemoteReminder(RemoteReminder remotereminder) { remoteReminderDao.update(remotereminder); return remotereminder; } public int deleteRemoteReminder(long id) { return remoteReminderDao.delete(id); } public Map projectCount(long projectId) { Map map = reminderDao.projectCount(projectId); int number = remoteReminderDao.projectCount(projectId); map.put("remoteNumber", number); return map; } }