liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.iim.web;
 
import java.text.SimpleDateFormat;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.iim.entity.ChatHistory;
import com.jeeplus.modules.iim.service.ChatHistoryService;
import com.jeeplus.modules.sys.utils.UserUtils;
 
/**
 * 聊天记录Controller
 * @author jeeplus
 * @version 2015-12-29
 */
@Controller
@RequestMapping(value = "${adminPath}/iim/chatHistory")
public class ChatHistoryController extends BaseController {
 
    @Autowired
    private ChatHistoryService chatHistoryService;
    
    @ModelAttribute
    public ChatHistory get(@RequestParam(required=false) String id) {
        ChatHistory entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = chatHistoryService.get(id);
        }
        if (entity == null){
            entity = new ChatHistory();
        }
        return entity;
    }
    
    /**
     * 聊天列表页面
     */
    @RequestMapping(value = {"list", ""})
    public String list(ChatHistory chatHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
        
        Page pg = new Page<ChatHistory>(request, response);
        Page<ChatHistory> page = chatHistoryService.findPage(pg, chatHistory); 
        model.addAttribute("chatHistory", chatHistory);
        model.addAttribute("page", page);
        return "modules/iim/chatHistoryList";
    }
 
    /**
     * 查看,增加,编辑聊天表单页面
     */
    @RequestMapping(value = "form")
    public String form(ChatHistory chatHistory, Model model) {
        model.addAttribute("chatHistory", chatHistory);
        return "modules/iim/chatHistoryForm";
    }
 
    /**
     * 保存聊天
     */
    @RequestMapping(value = "save")
    public String save(ChatHistory chatHistory, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, chatHistory)){
            return form(chatHistory, model);
        }
        chatHistoryService.save(chatHistory);
        addMessage(redirectAttributes, "保存聊天成功");
        return "redirect:"+Global.getAdminPath()+"/iim/chatHistory/?repage";
    }
    
    /**
     * 删除聊天
     */
    @RequestMapping(value = "delete")
    public String delete(ChatHistory chatHistory, RedirectAttributes redirectAttributes) {
        chatHistoryService.delete(chatHistory);
        addMessage(redirectAttributes, "删除聊天成功");
        return "redirect:"+Global.getAdminPath()+"/iim/chatHistory/?repage";
    }
    
    /**
     * 批量删除聊天
     */
    @RequestMapping(value = "deleteAll")
    public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
        String idArray[] =ids.split(",");
        for(String id : idArray){
            chatHistoryService.delete(chatHistoryService.get(id));
        }
        addMessage(redirectAttributes, "删除聊天成功");
        return "redirect:"+Global.getAdminPath()+"/iim/chatHistory/?repage";
    }
    
    /**
     * 导出excel文件
     */
    @RequiresPermissions("iim:chatHistory:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(ChatHistory chatHistory, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "聊天"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<ChatHistory> page = chatHistoryService.findPage(new Page<ChatHistory>(request, response, -1), chatHistory);
            new ExportExcel("聊天", ChatHistory.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出聊天记录失败!失败信息:"+e.getMessage());
        }
        return "redirect:"+Global.getAdminPath()+"/iim/chatHistory/?repage";
    }
 
    /**
     * 获取聊天记录
     */
    @ResponseBody
    @RequestMapping(value = "getChats")
    public AjaxJson getChats(ChatHistory chatHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ChatHistory> page = chatHistoryService.findPage(new Page<ChatHistory>(request, response), chatHistory); 
        List<ChatHistory> list = page.getList();
        for(ChatHistory c : list){
            if(c.getStatus().equals("0")){
                if(c.getUserid2().equals(UserUtils.getUser().getLoginName())){//把发送给我的信息标记为已读
                    c.setStatus("1");//标记为已读
                    chatHistoryService.save(c);
                }
                
            }
        }
        AjaxJson j = new AjaxJson();
        j.setMsg("获取聊天记录成功!");
        j.put("data", page.getList());
        return j;
    }
    
    /**
     * 获取未读条数
     */
    @ResponseBody
    @RequestMapping(value = "findUnReadCount")
    public AjaxJson  findUnReadCount(ChatHistory chatHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
        AjaxJson j = new AjaxJson();
        int size = chatHistoryService.findUnReadCount(chatHistory); 
        j.setMsg("获取未读条数成功!");
        j.put("num", size);
        
        return j;
        
    }
 
    
    /**
     * 发送聊天内容(手机端)
     */
    
    @ResponseBody
    @RequestMapping(value = "sendChats")
    public  AjaxJson  sendChats(ChatHistory chatHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
        AjaxJson j = new AjaxJson();
        j.setMsg("消息发送成功!");
        chatHistory.setStatus("0");//标记未读
        chatHistoryService.save(chatHistory);
        
        return j;
    }
    
    
 
 
}