WarehouseController.java 20 KB

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