|  | @ -0,0 +1,169 @@
 | 
												
													
														
															|  | 
 |  | package com.yihu.jw.message.service;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.entity.base.message.BaseMessageDO;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.message.dao.MessageDao;
 | 
												
													
														
															|  | 
 |  | import com.yihu.mysql.query.BaseJpaService;
 | 
												
													
														
															|  | 
 |  | import org.apache.commons.collections.map.HashedMap;
 | 
												
													
														
															|  | 
 |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
												
													
														
															|  | 
 |  | import org.springframework.data.domain.Sort;
 | 
												
													
														
															|  | 
 |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
												
													
														
															|  | 
 |  | import org.springframework.stereotype.Service;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import java.util.LinkedList;
 | 
												
													
														
															|  | 
 |  | import java.util.List;
 | 
												
													
														
															|  | 
 |  | import java.util.Map;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * @author liuwenbin on 2018/11/28.
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | @Service
 | 
												
													
														
															|  | 
 |  | public class MessageService extends BaseJpaService<BaseMessageDO, MessageDao> {
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private MessageDao messageDao;
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private JdbcTemplate jdbcTemplate;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 消息列表
 | 
												
													
														
															|  | 
 |  |      * @param platform
 | 
												
													
														
															|  | 
 |  |      * @param code
 | 
												
													
														
															|  | 
 |  |      * @param saasId
 | 
												
													
														
															|  | 
 |  |      * @param page
 | 
												
													
														
															|  | 
 |  |      * @param pageSize
 | 
												
													
														
															|  | 
 |  |      * @return
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     public List<Map<String,Object>> messageList(Integer platform,String code,String saasId,Integer page,Integer pageSize){
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         if(page==null){
 | 
												
													
														
															|  | 
 |  |             page = 0;
 | 
												
													
														
															|  | 
 |  |         }else if(page > 0)
 | 
												
													
														
															|  | 
 |  |         {
 | 
												
													
														
															|  | 
 |  |             page = page -1;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         if (pageSize <= 0) {
 | 
												
													
														
															|  | 
 |  |             pageSize = 10;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         // 排序
 | 
												
													
														
															|  | 
 |  |         Sort sort = new Sort(Sort.Direction.DESC, "createTime");
 | 
												
													
														
															|  | 
 |  |         // 分页信息
 | 
												
													
														
															|  | 
 |  |         List<BaseMessageDO> applyList = messageDao.messageList(platform,code,saasId,"00%");
 | 
												
													
														
															|  | 
 |  |         Integer applyReadCount = messageDao.countMessage(platform,code,saasId,"00%");
 | 
												
													
														
															|  | 
 |  |         List<BaseMessageDO> systemList = messageDao.messageList(platform,code,saasId,"01%");
 | 
												
													
														
															|  | 
 |  |         Integer systemReadCount = messageDao.countMessage(platform,code,saasId,"01%");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         List<Map<String,Object>> resultList = new LinkedList<>();
 | 
												
													
														
															|  | 
 |  |         Map<String,Object> applyMap = new HashedMap();
 | 
												
													
														
															|  | 
 |  |         applyMap.put("name","应用消息");
 | 
												
													
														
															|  | 
 |  |         applyMap.put("count",applyReadCount);
 | 
												
													
														
															|  | 
 |  |         applyMap.put("message",applyList.size()>0?applyList.get(0).getMsgContent():null);
 | 
												
													
														
															|  | 
 |  |         resultList.add(applyMap);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         Map<String,Object> systemMap = new HashedMap();
 | 
												
													
														
															|  | 
 |  |         systemMap.put("name","系统消息");
 | 
												
													
														
															|  | 
 |  |         systemMap.put("count",systemReadCount);
 | 
												
													
														
															|  | 
 |  |         systemMap.put("message",systemList.size()>0?systemList.get(0).getMsgContent():null);
 | 
												
													
														
															|  | 
 |  |         resultList.add(systemMap);
 | 
												
													
														
															|  | 
 |  |         Integer unReadPageSize = pageSize;
 | 
												
													
														
															|  | 
 |  |         if(page==0){
 | 
												
													
														
															|  | 
 |  |             unReadPageSize=unReadPageSize-2;
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         //1、未读消息(置顶)
 | 
												
													
														
															|  | 
 |  |         List<Map<String, Object>> unReadResult = messageListSql(platform,code,saasId,0,page,unReadPageSize);
 | 
												
													
														
															|  | 
 |  |         for(Map<String, Object> one:unReadResult){
 | 
												
													
														
															|  | 
 |  |             Map<String,Object> unReadMap = new HashedMap();
 | 
												
													
														
															|  | 
 |  |             unReadMap.put("name",one.get("sender_name"));
 | 
												
													
														
															|  | 
 |  |             unReadMap.put("count",one.get("num"));
 | 
												
													
														
															|  | 
 |  |             unReadMap.put("message",one.get("msg_content"));
 | 
												
													
														
															|  | 
 |  |             resultList.add(unReadMap);
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |         if(unReadResult.size()<unReadPageSize){
 | 
												
													
														
															|  | 
 |  |             page = messageCountSql(platform,code,saasId,1,page,pageSize);
 | 
												
													
														
															|  | 
 |  |             //2、已读消息
 | 
												
													
														
															|  | 
 |  |             List<Map<String, Object>> readResult = messageListSql(platform,code,saasId,1,page,unReadPageSize);
 | 
												
													
														
															|  | 
 |  |             for(Map<String, Object> one:readResult){
 | 
												
													
														
															|  | 
 |  |                 Map<String,Object> readMap = new HashedMap();
 | 
												
													
														
															|  | 
 |  |                 readMap.put("name",one.get("sender_name"));
 | 
												
													
														
															|  | 
 |  |                 readMap.put("count",one.get("num"));
 | 
												
													
														
															|  | 
 |  |                 readMap.put("message",one.get("msg_content"));
 | 
												
													
														
															|  | 
 |  |                 resultList.add(readMap);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         return resultList;
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     public BaseMessageDO create(BaseMessageDO messageDO){
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         return messageDao.save(messageDO);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private List<Map<String, Object>> messageListSql(Integer platform,String code,String saasId,Integer readState,Integer page,Integer pageSize){
 | 
												
													
														
															|  | 
 |  |         String sql =" SELECT " +
 | 
												
													
														
															|  | 
 |  |                 " a.*, b.msg_content " +
 | 
												
													
														
															|  | 
 |  |                 " FROM " +
 | 
												
													
														
															|  | 
 |  |                 " ( " +
 | 
												
													
														
															|  | 
 |  |                 "  SELECT " +
 | 
												
													
														
															|  | 
 |  |                 "   m.sender, " +
 | 
												
													
														
															|  | 
 |  |                 "   m.sender_name, " +
 | 
												
													
														
															|  | 
 |  |                 "   count(*) as num, " +
 | 
												
													
														
															|  | 
 |  |                 "   max(m.create_time) AS create_time " +
 | 
												
													
														
															|  | 
 |  |                 "  FROM " +
 | 
												
													
														
															|  | 
 |  |                 "   base_message m " +
 | 
												
													
														
															|  | 
 |  |                 "  WHERE " +
 | 
												
													
														
															|  | 
 |  |                 "   m.platform = '"+platform+"' " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.receiver = '"+code+"' " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.saas_id = '"+saasId+"' " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.del = 1 " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.read_state = "+readState+" " +
 | 
												
													
														
															|  | 
 |  |                 "  GROUP BY " +
 | 
												
													
														
															|  | 
 |  |                 "   m.sender, " +
 | 
												
													
														
															|  | 
 |  |                 "   m.sender_name " +
 | 
												
													
														
															|  | 
 |  |                 "  LIMIT "+page+", " +
 | 
												
													
														
															|  | 
 |  |                 "  "+pageSize+" " +
 | 
												
													
														
															|  | 
 |  |                 " ) a " +
 | 
												
													
														
															|  | 
 |  |                 " LEFT JOIN base_message b ON a.sender = b.sender " +
 | 
												
													
														
															|  | 
 |  |                 " AND a.create_time = b.create_time " +
 | 
												
													
														
															|  | 
 |  |                 " AND b.platform = '"+platform+"' " +
 | 
												
													
														
															|  | 
 |  |                 " AND b.receiver = '"+code+"' " +
 | 
												
													
														
															|  | 
 |  |                 " AND b.saas_id = '"+saasId+"' " +
 | 
												
													
														
															|  | 
 |  |                 " AND b.del = 1 " +
 | 
												
													
														
															|  | 
 |  |                 " AND m.read_state = "+readState;
 | 
												
													
														
															|  | 
 |  |         return jdbcTemplate.queryForList(sql);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private Integer messageCountSql(Integer platform,String code,String saasId,Integer readState,Integer page,Integer originalPageSize){
 | 
												
													
														
															|  | 
 |  |         String sql ="  SELECT count(*) as num " +
 | 
												
													
														
															|  | 
 |  |                 "  FROM " +
 | 
												
													
														
															|  | 
 |  |                 "   base_message m " +
 | 
												
													
														
															|  | 
 |  |                 "  WHERE " +
 | 
												
													
														
															|  | 
 |  |                 "   m.platform = '"+platform+"' " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.receiver = '"+code+"' " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.saas_id = '"+saasId+"' " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.del = 1 " +
 | 
												
													
														
															|  | 
 |  |                 "  AND m.read_state = "+readState+" " +
 | 
												
													
														
															|  | 
 |  |                 "  GROUP BY " +
 | 
												
													
														
															|  | 
 |  |                 "   m.sender, " +
 | 
												
													
														
															|  | 
 |  |                 "   m.sender_name " ;
 | 
												
													
														
															|  | 
 |  |         Integer count  = jdbcTemplate.queryForObject(sql,Integer.class);
 | 
												
													
														
															|  | 
 |  |         if(count<=(originalPageSize-2)){
 | 
												
													
														
															|  | 
 |  |             return page-1;
 | 
												
													
														
															|  | 
 |  |         }else{
 | 
												
													
														
															|  | 
 |  |             Integer s = (count-(originalPageSize-2))/originalPageSize;
 | 
												
													
														
															|  | 
 |  |             Integer y = (count-(originalPageSize-2))%originalPageSize;
 | 
												
													
														
															|  | 
 |  |             if(y>0){
 | 
												
													
														
															|  | 
 |  |                 return page-(s+2);
 | 
												
													
														
															|  | 
 |  |             }else{
 | 
												
													
														
															|  | 
 |  |                 return page-(s+1);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | }
 |