ClothingOnhandController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package cn.com.lzt.clothing.onhand.controller;
  2. import cn.com.lzt.clothing.onhand.entity.ClothingOnhandEntity;
  3. import cn.com.lzt.clothing.onhand.service.ClothingOnhandServiceI;
  4. import cn.com.lzt.common.util.UserUtil;
  5. import com.xcgl.utils.XcglDateUtils;
  6. import org.apache.commons.lang.xwork.StringUtils;
  7. import org.apache.log4j.Logger;
  8. import org.jeecgframework.core.beanvalidator.BeanValidators;
  9. import org.jeecgframework.core.common.controller.BaseController;
  10. import org.jeecgframework.core.common.exception.BusinessException;
  11. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  12. import org.jeecgframework.core.common.model.json.AjaxJson;
  13. import org.jeecgframework.core.common.model.json.DataGrid;
  14. import org.jeecgframework.core.constant.Globals;
  15. import org.jeecgframework.core.util.ExceptionUtil;
  16. import org.jeecgframework.core.util.MyBeanUtils;
  17. import org.jeecgframework.core.util.ResourceUtil;
  18. import org.jeecgframework.core.util.StringUtil;
  19. import org.jeecgframework.poi.excel.ExcelImportUtil;
  20. import org.jeecgframework.poi.excel.entity.ExportParams;
  21. import org.jeecgframework.poi.excel.entity.ImportParams;
  22. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  23. import org.jeecgframework.tag.core.easyui.TagUtil;
  24. import org.jeecgframework.web.system.pojo.base.TSUser;
  25. import org.jeecgframework.web.system.service.SystemService;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.http.HttpHeaders;
  28. import org.springframework.http.HttpStatus;
  29. import org.springframework.http.MediaType;
  30. import org.springframework.http.ResponseEntity;
  31. import org.springframework.stereotype.Controller;
  32. import org.springframework.ui.ModelMap;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.web.multipart.MultipartFile;
  35. import org.springframework.web.multipart.MultipartHttpServletRequest;
  36. import org.springframework.web.servlet.ModelAndView;
  37. import org.springframework.web.util.UriComponentsBuilder;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import javax.validation.ConstraintViolation;
  41. import javax.validation.Validator;
  42. import java.io.IOException;
  43. import java.net.URI;
  44. import java.text.SimpleDateFormat;
  45. import java.util.ArrayList;
  46. import java.util.List;
  47. import java.util.Map;
  48. import java.util.Set;
  49. /**
  50. * @Title: Controller
  51. * @Description: 项目服装存量
  52. * @author onlineGenerator
  53. * @date 2019-10-08 09:38:06
  54. * @version V1.0
  55. *
  56. */
  57. @Controller
  58. @RequestMapping("/clothingOnhandController")
  59. public class ClothingOnhandController extends BaseController {
  60. /**
  61. * Logger for this class
  62. */
  63. private static final Logger logger = Logger.getLogger(ClothingOnhandController.class);
  64. @Autowired
  65. private ClothingOnhandServiceI clothingOnhandService;
  66. @Autowired
  67. private SystemService systemService;
  68. @Autowired
  69. private Validator validator;
  70. /**
  71. * 项目服装存量列表 页面跳转
  72. *
  73. * @return
  74. */
  75. @RequestMapping(params = "list")
  76. public ModelAndView list(HttpServletRequest request) {
  77. return new ModelAndView("cn/com/lzt/clothing/onhand/clothingOnhandList");
  78. }
  79. /**
  80. * easyui AJAX请求数据
  81. *
  82. * @param request
  83. * @param response
  84. * @param dataGrid
  85. * @param user
  86. */
  87. @RequestMapping(params = "datagrid")
  88. public void datagrid(ClothingOnhandEntity clothingOnhand,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  89. CriteriaQuery cq = new CriteriaQuery(ClothingOnhandEntity.class, dataGrid);
  90. TSUser user = ResourceUtil.getSessionUser();
  91. boolean isproject = UserUtil.isProjectUser(user.getId());
  92. if(isproject) {
  93. //查询所属组织对应的领料点
  94. String sql = " select id from t_b_warehouse where responsible_person = '"+user.getId()+"' ";
  95. List<String> ids = systemService.findListbySql(sql);
  96. if(ids.size() > 0) {
  97. cq.in("warehouseid", ids.toArray());
  98. //设置查询的状态为闲置
  99. //clothingOnhand.setUsingstatus("0");
  100. }else {
  101. //无权限,则不允许查询到
  102. cq.eq("warehouseid", "1");
  103. }
  104. }
  105. try{
  106. //自定义追加查询条件
  107. String query_startdate_begin = request.getParameter("startdate_begin");
  108. String query_startdate_end = request.getParameter("startdate_end");
  109. String clothingname = request.getParameter("clothingname");
  110. String warehousename = request.getParameter("warehousename");
  111. //查询条件组装器
  112. if(StringUtils.isNotBlank(clothingname)){
  113. //模糊查询
  114. cq.like("clothingname", "%"+clothingname+"%");
  115. clothingOnhand.setClothingname(null);
  116. }
  117. if(StringUtils.isNotBlank(warehousename)){
  118. //模糊查询
  119. cq.like("warehousename", "%"+warehousename+"%");
  120. clothingOnhand.setWarehousename(null);
  121. }
  122. if(StringUtil.isNotEmpty(query_startdate_begin)){
  123. cq.ge("startdate", new SimpleDateFormat("yyyy-MM-dd").parse(query_startdate_begin));
  124. }
  125. if(StringUtil.isNotEmpty(query_startdate_end)){
  126. cq.le("startdate", new SimpleDateFormat("yyyy-MM-dd").parse(query_startdate_end));
  127. }
  128. //查询条件组装器
  129. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, clothingOnhand, request.getParameterMap());
  130. }catch (Exception e) {
  131. throw new BusinessException(e.getMessage());
  132. }
  133. cq.add();
  134. this.clothingOnhandService.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(ClothingOnhandEntity clothingOnhand, HttpServletRequest request) {
  145. String message = null;
  146. AjaxJson j = new AjaxJson();
  147. clothingOnhand = systemService.getEntity(ClothingOnhandEntity.class, clothingOnhand.getId());
  148. message = "项目服装存量删除成功";
  149. try{
  150. clothingOnhandService.delete(clothingOnhand);
  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. ClothingOnhandEntity clothingOnhand = systemService.getEntity(ClothingOnhandEntity.class,
  174. id
  175. );
  176. clothingOnhandService.delete(clothingOnhand);
  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(ClothingOnhandEntity clothingOnhand, HttpServletRequest request) {
  196. String message = null;
  197. AjaxJson j = new AjaxJson();
  198. message = "项目服装存量添加成功";
  199. try{
  200. List<ClothingOnhandEntity> existsEntityList = systemService.findHql(
  201. " from ClothingOnhandEntity where clothingid =? and warehouseid =? and startdate = ? ",
  202. clothingOnhand.getClothingid(), clothingOnhand.getWarehouseid(),clothingOnhand.getStartdate());
  203. if(existsEntityList.size() > 0) {
  204. j.setSuccess(false);
  205. message = "项目服装存量添加失败,服装【"+clothingOnhand.getClothingname()+"】已经在领料点【"+clothingOnhand.getWarehousename()+"】有存量信息,请不要重复录入";
  206. }else {
  207. clothingOnhand.setExchangedate(XcglDateUtils.addDateDay(clothingOnhand.getStartdate(), 365*2));
  208. clothingOnhandService.save(clothingOnhand);
  209. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  210. }
  211. }catch(Exception e){
  212. e.printStackTrace();
  213. message = "项目服装存量添加失败";
  214. throw new BusinessException(e.getMessage());
  215. }
  216. j.setMsg(message);
  217. return j;
  218. }
  219. /**
  220. * 更新项目服装存量
  221. *
  222. * @param ids
  223. * @return
  224. */
  225. @RequestMapping(params = "doUpdate")
  226. @ResponseBody
  227. public AjaxJson doUpdate(ClothingOnhandEntity clothingOnhand, HttpServletRequest request) {
  228. String message = null;
  229. AjaxJson j = new AjaxJson();
  230. message = "项目服装存量更新成功";
  231. ClothingOnhandEntity t = clothingOnhandService.get(ClothingOnhandEntity.class, clothingOnhand.getId());
  232. try {
  233. List<ClothingOnhandEntity> existsEntityList = systemService.findHql(" from ClothingOnhandEntity where clothingid = ? "
  234. + " and warehouseid =? and id <> ? and startdate =? and usingstatus = ? ",
  235. clothingOnhand.getClothingid(), clothingOnhand.getWarehouseid(),clothingOnhand.getId(),clothingOnhand.getStartdate(),clothingOnhand.getUsingstatus());
  236. if(existsEntityList.size() > 0) {
  237. j.setSuccess(false);
  238. message = "项目服装存量添加失败<br>服装【"+clothingOnhand.getClothingname()+"】已经在领料点【"+clothingOnhand.getWarehousename()+"】有存量信息,请不要重复录入";
  239. }else {
  240. MyBeanUtils.copyBeanNotNull2Bean(clothingOnhand, t);
  241. clothingOnhandService.saveOrUpdate(t);
  242. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  243. }
  244. } catch (Exception e) {
  245. e.printStackTrace();
  246. message = "项目服装存量更新失败";
  247. throw new BusinessException(e.getMessage());
  248. }
  249. j.setMsg(message);
  250. return j;
  251. }
  252. /**
  253. * 项目服装存量新增页面跳转
  254. *
  255. * @return
  256. */
  257. @RequestMapping(params = "goAdd")
  258. public ModelAndView goAdd(ClothingOnhandEntity clothingOnhand, HttpServletRequest req) {
  259. if (StringUtil.isNotEmpty(clothingOnhand.getId())) {
  260. clothingOnhand = clothingOnhandService.getEntity(ClothingOnhandEntity.class, clothingOnhand.getId());
  261. req.setAttribute("clothingOnhandPage", clothingOnhand);
  262. }
  263. return new ModelAndView("cn/com/lzt/clothing/onhand/clothingOnhand-add");
  264. }
  265. /**
  266. * 项目服装存量编辑页面跳转
  267. *
  268. * @return
  269. */
  270. @RequestMapping(params = "goUpdate")
  271. public ModelAndView goUpdate(ClothingOnhandEntity clothingOnhand, HttpServletRequest req) {
  272. if (StringUtil.isNotEmpty(clothingOnhand.getId())) {
  273. clothingOnhand = clothingOnhandService.getEntity(ClothingOnhandEntity.class, clothingOnhand.getId());
  274. req.setAttribute("clothingOnhandPage", clothingOnhand);
  275. }
  276. return new ModelAndView("cn/com/lzt/clothing/onhand/clothingOnhand-update");
  277. }
  278. /**
  279. * 导入功能跳转
  280. *
  281. * @return
  282. */
  283. @RequestMapping(params = "upload")
  284. public ModelAndView upload(HttpServletRequest req) {
  285. req.setAttribute("controller_name","clothingOnhandController");
  286. return new ModelAndView("common/upload/pub_excel_upload");
  287. }
  288. /**
  289. * 导出excel
  290. *
  291. * @param request
  292. * @param response
  293. */
  294. @RequestMapping(params = "exportXls")
  295. public String exportXls(ClothingOnhandEntity clothingOnhand,HttpServletRequest request,HttpServletResponse response
  296. , DataGrid dataGrid,ModelMap modelMap) {
  297. CriteriaQuery cq = new CriteriaQuery(ClothingOnhandEntity.class, dataGrid);
  298. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, clothingOnhand, request.getParameterMap());
  299. List<ClothingOnhandEntity> clothingOnhands = this.clothingOnhandService.getListByCriteriaQuery(cq,false);
  300. modelMap.put(NormalExcelConstants.FILE_NAME,"项目服装存量");
  301. modelMap.put(NormalExcelConstants.CLASS,ClothingOnhandEntity.class);
  302. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("项目服装存量列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  303. "导出信息"));
  304. modelMap.put(NormalExcelConstants.DATA_LIST,clothingOnhands);
  305. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  306. }
  307. /**
  308. * 导出excel 使模板
  309. *
  310. * @param request
  311. * @param response
  312. */
  313. @RequestMapping(params = "exportXlsByT")
  314. public String exportXlsByT(ClothingOnhandEntity clothingOnhand,HttpServletRequest request,HttpServletResponse response
  315. , DataGrid dataGrid,ModelMap modelMap) {
  316. modelMap.put(NormalExcelConstants.FILE_NAME,"项目服装存量");
  317. modelMap.put(NormalExcelConstants.CLASS,ClothingOnhandEntity.class);
  318. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("项目服装存量列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  319. "导出信息"));
  320. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  321. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  322. }
  323. @SuppressWarnings("unchecked")
  324. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  325. @ResponseBody
  326. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  327. AjaxJson j = new AjaxJson();
  328. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  329. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  330. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  331. MultipartFile file = entity.getValue();// 获取上传文件对象
  332. ImportParams params = new ImportParams();
  333. params.setTitleRows(2);
  334. params.setHeadRows(1);
  335. params.setNeedSave(true);
  336. try {
  337. List<ClothingOnhandEntity> listClothingOnhandEntitys = ExcelImportUtil.importExcel(file.getInputStream(),ClothingOnhandEntity.class,params);
  338. for (ClothingOnhandEntity clothingOnhand : listClothingOnhandEntitys) {
  339. clothingOnhandService.save(clothingOnhand);
  340. }
  341. j.setMsg("文件导入成功!");
  342. } catch (Exception e) {
  343. j.setMsg("文件导入失败!");
  344. logger.error(ExceptionUtil.getExceptionMessage(e));
  345. }finally{
  346. try {
  347. file.getInputStream().close();
  348. } catch (IOException e) {
  349. e.printStackTrace();
  350. }
  351. }
  352. }
  353. return j;
  354. }
  355. @RequestMapping(method = RequestMethod.GET)
  356. @ResponseBody
  357. public List<ClothingOnhandEntity> list() {
  358. List<ClothingOnhandEntity> listClothingOnhands=clothingOnhandService.getList(ClothingOnhandEntity.class);
  359. return listClothingOnhands;
  360. }
  361. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  362. @ResponseBody
  363. public ResponseEntity<?> get(@PathVariable("id") String id) {
  364. ClothingOnhandEntity task = clothingOnhandService.get(ClothingOnhandEntity.class, id);
  365. if (task == null) {
  366. return new ResponseEntity(HttpStatus.NOT_FOUND);
  367. }
  368. return new ResponseEntity(task, HttpStatus.OK);
  369. }
  370. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  371. @ResponseBody
  372. public ResponseEntity<?> create(@RequestBody ClothingOnhandEntity clothingOnhand, UriComponentsBuilder uriBuilder) {
  373. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  374. Set<ConstraintViolation<ClothingOnhandEntity>> failures = validator.validate(clothingOnhand);
  375. if (!failures.isEmpty()) {
  376. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  377. }
  378. //保存
  379. try{
  380. clothingOnhandService.save(clothingOnhand);
  381. } catch (Exception e) {
  382. e.printStackTrace();
  383. return new ResponseEntity(HttpStatus.NO_CONTENT);
  384. }
  385. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  386. String id = clothingOnhand.getId();
  387. URI uri = uriBuilder.path("/rest/clothingOnhandController/" + id).build().toUri();
  388. HttpHeaders headers = new HttpHeaders();
  389. headers.setLocation(uri);
  390. return new ResponseEntity(headers, HttpStatus.CREATED);
  391. }
  392. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  393. public ResponseEntity<?> update(@RequestBody ClothingOnhandEntity clothingOnhand) {
  394. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  395. Set<ConstraintViolation<ClothingOnhandEntity>> failures = validator.validate(clothingOnhand);
  396. if (!failures.isEmpty()) {
  397. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  398. }
  399. //保存
  400. try{
  401. clothingOnhandService.saveOrUpdate(clothingOnhand);
  402. } catch (Exception e) {
  403. e.printStackTrace();
  404. return new ResponseEntity(HttpStatus.NO_CONTENT);
  405. }
  406. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  407. return new ResponseEntity(HttpStatus.NO_CONTENT);
  408. }
  409. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  410. @ResponseStatus(HttpStatus.NO_CONTENT)
  411. public void delete(@PathVariable("id") String id) {
  412. clothingOnhandService.deleteEntityById(ClothingOnhandEntity.class, id);
  413. }
  414. }