MMessageReplyController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package cn.com.lzt.message.reply.controller;
  2. import cn.com.lzt.message.reply.entity.MMessageReplyEntity;
  3. import cn.com.lzt.message.reply.service.MMessageReplyServiceI;
  4. import org.apache.log4j.Logger;
  5. import org.jeecgframework.core.beanvalidator.BeanValidators;
  6. import org.jeecgframework.core.common.controller.BaseController;
  7. import org.jeecgframework.core.common.exception.BusinessException;
  8. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  9. import org.jeecgframework.core.common.model.json.AjaxJson;
  10. import org.jeecgframework.core.common.model.json.DataGrid;
  11. import org.jeecgframework.core.constant.Globals;
  12. import org.jeecgframework.core.util.ExceptionUtil;
  13. import org.jeecgframework.core.util.MyBeanUtils;
  14. import org.jeecgframework.core.util.ResourceUtil;
  15. import org.jeecgframework.core.util.StringUtil;
  16. import org.jeecgframework.poi.excel.ExcelImportUtil;
  17. import org.jeecgframework.poi.excel.entity.ExportParams;
  18. import org.jeecgframework.poi.excel.entity.ImportParams;
  19. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  20. import org.jeecgframework.tag.core.easyui.TagUtil;
  21. import org.jeecgframework.web.system.pojo.base.TSBaseUser;
  22. import org.jeecgframework.web.system.service.SystemService;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.http.HttpStatus;
  26. import org.springframework.http.MediaType;
  27. import org.springframework.http.ResponseEntity;
  28. import org.springframework.stereotype.Controller;
  29. import org.springframework.ui.ModelMap;
  30. import org.springframework.web.bind.annotation.*;
  31. import org.springframework.web.multipart.MultipartFile;
  32. import org.springframework.web.multipart.MultipartHttpServletRequest;
  33. import org.springframework.web.servlet.ModelAndView;
  34. import org.springframework.web.util.UriComponentsBuilder;
  35. import javax.servlet.http.HttpServletRequest;
  36. import javax.servlet.http.HttpServletResponse;
  37. import javax.validation.ConstraintViolation;
  38. import javax.validation.Validator;
  39. import java.io.IOException;
  40. import java.net.URI;
  41. import java.util.*;
  42. /**
  43. * @Title: Controller
  44. * @Description: 消息回复
  45. * @author onlineGenerator
  46. * @date 2020-02-04 17:59:33
  47. * @version V1.0
  48. *
  49. */
  50. @Controller
  51. @RequestMapping("/mMessageReplyController")
  52. public class MMessageReplyController extends BaseController {
  53. /**
  54. * Logger for this class
  55. */
  56. private static final Logger logger = Logger.getLogger(MMessageReplyController.class);
  57. @Autowired
  58. private MMessageReplyServiceI mMessageReplyService;
  59. @Autowired
  60. private SystemService systemService;
  61. @Autowired
  62. private Validator validator;
  63. /**
  64. * 消息回复列表 页面跳转
  65. *
  66. * @return
  67. */
  68. @RequestMapping(params = "list")
  69. public ModelAndView list(HttpServletRequest request) {
  70. return new ModelAndView("cn/com/lzt/message/reply/mMessageReplyList");
  71. }
  72. /**
  73. * easyui AJAX请求数据
  74. *
  75. * @param request
  76. * @param response
  77. * @param dataGrid
  78. */
  79. @RequestMapping(params = "datagrid")
  80. public void datagrid(MMessageReplyEntity mMessageReply, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  81. CriteriaQuery cq = new CriteriaQuery(MMessageReplyEntity.class, dataGrid);
  82. //查询条件组装器
  83. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mMessageReply, request.getParameterMap());
  84. try{
  85. //自定义追加查询条件
  86. }catch (Exception e) {
  87. throw new BusinessException(e.getMessage());
  88. }
  89. cq.add();
  90. this.mMessageReplyService.getDataGridReturn(cq, true);
  91. TagUtil.datagrid(response, dataGrid);
  92. }
  93. /**
  94. * 删除消息回复
  95. *
  96. * @return
  97. */
  98. @RequestMapping(params = "doDel")
  99. @ResponseBody
  100. public AjaxJson doDel(MMessageReplyEntity mMessageReply, HttpServletRequest request) {
  101. String message = null;
  102. AjaxJson j = new AjaxJson();
  103. mMessageReply = systemService.getEntity(MMessageReplyEntity.class, mMessageReply.getId());
  104. message = "消息回复删除成功";
  105. try{
  106. mMessageReplyService.delete(mMessageReply);
  107. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  108. }catch(Exception e){
  109. e.printStackTrace();
  110. message = "消息回复删除失败";
  111. throw new BusinessException(e.getMessage());
  112. }
  113. j.setMsg(message);
  114. return j;
  115. }
  116. /**
  117. * 批量删除消息回复
  118. *
  119. * @return
  120. */
  121. @RequestMapping(params = "doBatchDel")
  122. @ResponseBody
  123. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  124. String message = null;
  125. AjaxJson j = new AjaxJson();
  126. message = "消息回复删除成功";
  127. try{
  128. for(String id:ids.split(",")){
  129. MMessageReplyEntity mMessageReply = systemService.getEntity(MMessageReplyEntity.class,
  130. id
  131. );
  132. mMessageReplyService.delete(mMessageReply);
  133. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  134. }
  135. }catch(Exception e){
  136. e.printStackTrace();
  137. message = "消息回复删除失败";
  138. throw new BusinessException(e.getMessage());
  139. }
  140. j.setMsg(message);
  141. return j;
  142. }
  143. /**
  144. * 添加消息回复
  145. *
  146. * @return
  147. */
  148. @RequestMapping(params = "doAdd")
  149. @ResponseBody
  150. public AjaxJson doAdd(MMessageReplyEntity mMessageReply, HttpServletRequest request) {
  151. String message = null;
  152. AjaxJson j = new AjaxJson();
  153. message = "消息回复添加成功";
  154. try{
  155. mMessageReply.setCreateDate(new Date());
  156. String userId = mMessageReply.getUserId();
  157. //只记录每个用户回复的最后一次内容
  158. List<MMessageReplyEntity> msgs = systemService.findHql(" FROM MMessageReplyEntity where userId =? and messageId = ? ", userId,mMessageReply.getMessageId());
  159. if(msgs == null || msgs.size() == 0) {
  160. TSBaseUser user = systemService.getEntity(TSBaseUser.class,userId);
  161. mMessageReply.setRealName(user.getRealName());
  162. mMessageReplyService.save(mMessageReply);
  163. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  164. }else {
  165. MMessageReplyEntity replay = msgs.get(0);
  166. replay.setReply(mMessageReply.getReply());
  167. replay.setCreateDate(new Date());
  168. mMessageReplyService.saveOrUpdate(replay);
  169. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  170. }
  171. }catch(Exception e){
  172. e.printStackTrace();
  173. message = "消息回复添加失败";
  174. throw new BusinessException(e.getMessage());
  175. }
  176. j.setMsg(message);
  177. return j;
  178. }
  179. /**
  180. * 更新消息回复
  181. *
  182. * @return
  183. */
  184. @RequestMapping(params = "doUpdate")
  185. @ResponseBody
  186. public AjaxJson doUpdate(MMessageReplyEntity mMessageReply, HttpServletRequest request) {
  187. String message = null;
  188. AjaxJson j = new AjaxJson();
  189. message = "消息回复更新成功";
  190. MMessageReplyEntity t = mMessageReplyService.get(MMessageReplyEntity.class, mMessageReply.getId());
  191. try {
  192. MyBeanUtils.copyBeanNotNull2Bean(mMessageReply, t);
  193. mMessageReplyService.saveOrUpdate(t);
  194. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  195. } catch (Exception e) {
  196. e.printStackTrace();
  197. message = "消息回复更新失败";
  198. throw new BusinessException(e.getMessage());
  199. }
  200. j.setMsg(message);
  201. return j;
  202. }
  203. /**
  204. * 消息回复新增页面跳转
  205. *
  206. * @return
  207. */
  208. @RequestMapping(params = "goAdd")
  209. public ModelAndView goAdd(MMessageReplyEntity mMessageReply, HttpServletRequest req) {
  210. if (StringUtil.isNotEmpty(mMessageReply.getId())) {
  211. mMessageReply = mMessageReplyService.getEntity(MMessageReplyEntity.class, mMessageReply.getId());
  212. req.setAttribute("mMessageReplyPage", mMessageReply);
  213. }
  214. return new ModelAndView("cn/com/lzt/reply/mMessageReply-add");
  215. }
  216. /**
  217. * 消息回复编辑页面跳转
  218. *
  219. * @return
  220. */
  221. @RequestMapping(params = "goUpdate")
  222. public ModelAndView goUpdate(MMessageReplyEntity mMessageReply, HttpServletRequest req) {
  223. if (StringUtil.isNotEmpty(mMessageReply.getId())) {
  224. mMessageReply = mMessageReplyService.getEntity(MMessageReplyEntity.class, mMessageReply.getId());
  225. req.setAttribute("mMessageReplyPage", mMessageReply);
  226. }
  227. return new ModelAndView("cn/com/lzt/reply/mMessageReply-update");
  228. }
  229. /**
  230. * 导入功能跳转
  231. *
  232. * @return
  233. */
  234. @RequestMapping(params = "upload")
  235. public ModelAndView upload(HttpServletRequest req) {
  236. req.setAttribute("controller_name","mMessageReplyController");
  237. return new ModelAndView("common/upload/pub_excel_upload");
  238. }
  239. /**
  240. * 导出excel
  241. *
  242. * @param request
  243. * @param response
  244. */
  245. @RequestMapping(params = "exportXls")
  246. public String exportXls(MMessageReplyEntity mMessageReply,HttpServletRequest request,HttpServletResponse response
  247. , DataGrid dataGrid,ModelMap modelMap) {
  248. CriteriaQuery cq = new CriteriaQuery(MMessageReplyEntity.class, dataGrid);
  249. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mMessageReply, request.getParameterMap());
  250. List<MMessageReplyEntity> mMessageReplys = this.mMessageReplyService.getListByCriteriaQuery(cq,false);
  251. modelMap.put(NormalExcelConstants.FILE_NAME,"消息回复");
  252. modelMap.put(NormalExcelConstants.CLASS,MMessageReplyEntity.class);
  253. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("消息回复列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  254. "导出信息"));
  255. modelMap.put(NormalExcelConstants.DATA_LIST,mMessageReplys);
  256. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  257. }
  258. /**
  259. * 导出excel 使模板
  260. *
  261. * @param request
  262. * @param response
  263. */
  264. @RequestMapping(params = "exportXlsByT")
  265. public String exportXlsByT(MMessageReplyEntity mMessageReply,HttpServletRequest request,HttpServletResponse response
  266. , DataGrid dataGrid,ModelMap modelMap) {
  267. modelMap.put(NormalExcelConstants.FILE_NAME,"消息回复");
  268. modelMap.put(NormalExcelConstants.CLASS,MMessageReplyEntity.class);
  269. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("消息回复列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  270. "导出信息"));
  271. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  272. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  273. }
  274. @SuppressWarnings("unchecked")
  275. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  276. @ResponseBody
  277. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  278. AjaxJson j = new AjaxJson();
  279. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  280. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  281. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  282. MultipartFile file = entity.getValue();// 获取上传文件对象
  283. ImportParams params = new ImportParams();
  284. params.setTitleRows(2);
  285. params.setHeadRows(1);
  286. params.setNeedSave(true);
  287. try {
  288. List<MMessageReplyEntity> listMMessageReplyEntitys = ExcelImportUtil.importExcel(file.getInputStream(),MMessageReplyEntity.class,params);
  289. for (MMessageReplyEntity mMessageReply : listMMessageReplyEntitys) {
  290. mMessageReplyService.save(mMessageReply);
  291. }
  292. j.setMsg("文件导入成功!");
  293. } catch (Exception e) {
  294. j.setMsg("文件导入失败!");
  295. logger.error(ExceptionUtil.getExceptionMessage(e));
  296. }finally{
  297. try {
  298. file.getInputStream().close();
  299. } catch (IOException e) {
  300. e.printStackTrace();
  301. }
  302. }
  303. }
  304. return j;
  305. }
  306. @RequestMapping(method = RequestMethod.GET)
  307. @ResponseBody
  308. public List<MMessageReplyEntity> list() {
  309. List<MMessageReplyEntity> listMMessageReplys=mMessageReplyService.getList(MMessageReplyEntity.class);
  310. return listMMessageReplys;
  311. }
  312. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  313. @ResponseBody
  314. public ResponseEntity<?> get(@PathVariable("id") String id) {
  315. MMessageReplyEntity task = mMessageReplyService.get(MMessageReplyEntity.class, id);
  316. if (task == null) {
  317. return new ResponseEntity(HttpStatus.NOT_FOUND);
  318. }
  319. return new ResponseEntity(task, HttpStatus.OK);
  320. }
  321. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  322. @ResponseBody
  323. public ResponseEntity<?> create(@RequestBody MMessageReplyEntity mMessageReply, UriComponentsBuilder uriBuilder) {
  324. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  325. Set<ConstraintViolation<MMessageReplyEntity>> failures = validator.validate(mMessageReply);
  326. if (!failures.isEmpty()) {
  327. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  328. }
  329. //保存
  330. try{
  331. mMessageReplyService.save(mMessageReply);
  332. } catch (Exception e) {
  333. e.printStackTrace();
  334. return new ResponseEntity(HttpStatus.NO_CONTENT);
  335. }
  336. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  337. String id = mMessageReply.getId();
  338. URI uri = uriBuilder.path("/rest/mMessageReplyController/" + id).build().toUri();
  339. HttpHeaders headers = new HttpHeaders();
  340. headers.setLocation(uri);
  341. return new ResponseEntity(headers, HttpStatus.CREATED);
  342. }
  343. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  344. public ResponseEntity<?> update(@RequestBody MMessageReplyEntity mMessageReply) {
  345. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  346. Set<ConstraintViolation<MMessageReplyEntity>> failures = validator.validate(mMessageReply);
  347. if (!failures.isEmpty()) {
  348. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  349. }
  350. //保存
  351. try{
  352. mMessageReplyService.saveOrUpdate(mMessageReply);
  353. } catch (Exception e) {
  354. e.printStackTrace();
  355. return new ResponseEntity(HttpStatus.NO_CONTENT);
  356. }
  357. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  358. return new ResponseEntity(HttpStatus.NO_CONTENT);
  359. }
  360. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  361. @ResponseStatus(HttpStatus.NO_CONTENT)
  362. public void delete(@PathVariable("id") String id) {
  363. mMessageReplyService.deleteEntityById(MMessageReplyEntity.class, id);
  364. }
  365. }