TBLogisticsController.java 16 KB

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