CarFleetController.java 14 KB

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