CarMaintainController.java 14 KB

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