CarEventController.java 15 KB

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