ClothingController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package cn.com.lzt.clothing.base.controller;
  2. import cn.com.lzt.clothing.base.entity.ClothingEntity;
  3. import cn.com.lzt.clothing.base.entity.TBClothingRelatedUnitPriceEntity;
  4. import cn.com.lzt.clothing.base.page.TBClothingPage;
  5. import cn.com.lzt.clothing.base.service.ClothingServiceI;
  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.ResourceUtil;
  17. import org.jeecgframework.core.util.StringUtil;
  18. import org.jeecgframework.poi.excel.ExcelImportUtil;
  19. import org.jeecgframework.poi.excel.entity.ExportParams;
  20. import org.jeecgframework.poi.excel.entity.ImportParams;
  21. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  22. import org.jeecgframework.tag.core.easyui.TagUtil;
  23. import org.jeecgframework.web.system.service.SystemService;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.http.HttpHeaders;
  26. import org.springframework.http.HttpStatus;
  27. import org.springframework.http.MediaType;
  28. import org.springframework.http.ResponseEntity;
  29. import org.springframework.stereotype.Controller;
  30. import org.springframework.ui.ModelMap;
  31. import org.springframework.web.bind.annotation.*;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import org.springframework.web.multipart.MultipartHttpServletRequest;
  34. import org.springframework.web.servlet.ModelAndView;
  35. import org.springframework.web.util.UriComponentsBuilder;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. import javax.validation.ConstraintViolation;
  39. import javax.validation.Validator;
  40. import java.io.IOException;
  41. import java.net.URI;
  42. import java.util.*;
  43. /**
  44. * @Title: Controller
  45. * @Description: 服装档案
  46. * @author onlineGenerator
  47. * @date 2019-10-08 09:37:35
  48. * @version V1.0
  49. *
  50. */
  51. @Controller
  52. @RequestMapping("/clothingController")
  53. public class ClothingController extends BaseController {
  54. /**
  55. * Logger for this class
  56. */
  57. private static final Logger logger = Logger.getLogger(ClothingController.class);
  58. @Autowired
  59. private ClothingServiceI clothingService;
  60. @Autowired
  61. private SystemService systemService;
  62. @Autowired
  63. private Validator validator;
  64. /**
  65. * 服装档案列表 页面跳转
  66. *
  67. * @return
  68. */
  69. @RequestMapping(params = "list")
  70. public ModelAndView list(HttpServletRequest request) {
  71. return new ModelAndView("cn/com/lzt/clothing/base/clothingList");
  72. }
  73. /**
  74. * easyui AJAX请求数据
  75. *
  76. * @param request
  77. * @param response
  78. * @param dataGrid
  79. * @param user
  80. */
  81. @RequestMapping(params = "datagrid")
  82. public void datagrid(ClothingEntity clothing,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  83. String ClothingRelatedUnitSql = "select clothingid ,GROUP_CONCAT(relatedunitname) as runame "
  84. + "from t_b_clothing_relatedunit_price group by clothingid ";
  85. List<Map<String,Object>> crList = systemService.findForJdbc(ClothingRelatedUnitSql);
  86. HashMap<String,String> clothingRUname = new HashMap<String, String>();
  87. for (Map<String, Object> crRow : crList) {
  88. clothingRUname.put(crRow.get("clothingid").toString(), crRow.get("runame").toString());
  89. }
  90. CriteriaQuery cq = new CriteriaQuery(ClothingEntity.class, dataGrid);
  91. try{
  92. //自定义追加查询条件
  93. String name = request.getParameter("name");
  94. //查询条件组装器
  95. if(StringUtils.isNotBlank(name)){
  96. //模糊查询
  97. cq.like("name", "%"+name+"%");
  98. clothing.setName(null);
  99. }
  100. //查询条件组装器
  101. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, clothing, request.getParameterMap());
  102. }catch (Exception e) {
  103. throw new BusinessException(e.getMessage());
  104. }
  105. cq.add();
  106. this.clothingService.getDataGridReturn(cq, true);
  107. List<ClothingEntity> clothings = dataGrid.getResults();
  108. for (ClothingEntity cEntity : clothings) {
  109. cEntity.setProviders(clothingRUname.get(cEntity.getId()));
  110. }
  111. TagUtil.datagrid(response, dataGrid);
  112. }
  113. @RequestMapping(params = "datagridbywarehouse")
  114. public void datagridbywarehouse(ClothingEntity clothing,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  115. String warehosueid = request.getParameter("warehouseid");
  116. CriteriaQuery cq = new CriteriaQuery(ClothingEntity.class, dataGrid);
  117. try{
  118. //自定义追加查询条件
  119. String name = request.getParameter("name");
  120. //查询条件组装器
  121. if(StringUtils.isNotBlank(name)){
  122. //模糊查询
  123. cq.like("name", "%"+name+"%");
  124. clothing.setName(null);
  125. }
  126. //查询条件组装器
  127. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, clothing, request.getParameterMap());
  128. }catch (Exception e) {
  129. throw new BusinessException(e.getMessage());
  130. }
  131. cq.add();
  132. this.clothingService.getDataGridReturn(cq, true);
  133. TagUtil.datagrid(response, dataGrid);
  134. }
  135. /**
  136. * 删除服装档案
  137. *
  138. * @return
  139. */
  140. @RequestMapping(params = "doDel")
  141. @ResponseBody
  142. public AjaxJson doDel(ClothingEntity clothing, HttpServletRequest request) {
  143. String message = null;
  144. AjaxJson j = new AjaxJson();
  145. clothing = systemService.getEntity(ClothingEntity.class, clothing.getId());
  146. message = "服装档案删除成功";
  147. try{
  148. clothingService.delete(clothing);
  149. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  150. }catch(Exception e){
  151. e.printStackTrace();
  152. message = "服装档案删除失败";
  153. throw new BusinessException(e.getMessage());
  154. }
  155. j.setMsg(message);
  156. return j;
  157. }
  158. /**
  159. * 批量删除服装档案
  160. *
  161. * @return
  162. */
  163. @RequestMapping(params = "doBatchDel")
  164. @ResponseBody
  165. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  166. String message = null;
  167. AjaxJson j = new AjaxJson();
  168. message = "服装档案删除成功";
  169. try{
  170. for(String id:ids.split(",")){
  171. ClothingEntity clothing = systemService.getEntity(ClothingEntity.class,
  172. id
  173. );
  174. clothingService.delete(clothing);
  175. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  176. }
  177. }catch(Exception e){
  178. e.printStackTrace();
  179. message = "服装档案删除失败";
  180. throw new BusinessException(e.getMessage());
  181. }
  182. j.setMsg(message);
  183. return j;
  184. }
  185. /**
  186. * 添加服装档案
  187. *
  188. * @param ids
  189. * @return
  190. */
  191. @RequestMapping(params = "doAdd")
  192. @ResponseBody
  193. public AjaxJson doAdd(ClothingEntity clothing, TBClothingPage tBClothingPage,HttpServletRequest request) {
  194. List<TBClothingRelatedUnitPriceEntity> tBRelatedUnitPriceInfoList = tBClothingPage.getTBRelatedUnitPriceInfoList();
  195. String message = null;
  196. AjaxJson j = new AjaxJson();
  197. message = "服装档案添加成功";
  198. Map<String,TBClothingRelatedUnitPriceEntity> rumap = new HashMap<String,TBClothingRelatedUnitPriceEntity>();
  199. boolean relatedUnitRepetitionflag = false;
  200. for(int i=0; i<tBRelatedUnitPriceInfoList.size(); i++){
  201. TBClothingRelatedUnitPriceEntity priceInfo = tBRelatedUnitPriceInfoList.get(i);
  202. if(priceInfo.getMeteringUnit() != null && !"".equals(priceInfo.getMeteringUnit())){
  203. rumap.put(priceInfo.getRelatedunitid(), priceInfo);
  204. }else {
  205. relatedUnitRepetitionflag = true;
  206. break;
  207. }
  208. }
  209. if(relatedUnitRepetitionflag){
  210. message = "供应商价格不能重复,请修改后再保存";
  211. j.setMsg(message);
  212. j.setSuccess(false);
  213. return j;
  214. }
  215. try{
  216. clothingService.addMain(clothing, tBRelatedUnitPriceInfoList);
  217. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  218. }catch(Exception e){
  219. e.printStackTrace();
  220. message = "服装档案添加失败";
  221. throw new BusinessException(e.getMessage());
  222. }
  223. j.setMsg(message);
  224. return j;
  225. }
  226. /**
  227. * 更新服装档案
  228. *
  229. * @param ids
  230. * @return
  231. */
  232. @RequestMapping(params = "doUpdate")
  233. @ResponseBody
  234. public AjaxJson doUpdate(ClothingEntity clothing,TBClothingPage tBClothingPage, HttpServletRequest request) {
  235. List<TBClothingRelatedUnitPriceEntity> tBRelatedUnitPriceInfoList = tBClothingPage.getTBRelatedUnitPriceInfoList();
  236. String message = null;
  237. AjaxJson j = new AjaxJson();
  238. message = "服装档案更新成功";
  239. Map<String,TBClothingRelatedUnitPriceEntity> rumap = new HashMap<String,TBClothingRelatedUnitPriceEntity>();
  240. boolean relatedUnitRepetitionflag = false;
  241. for(int i=0; i<tBRelatedUnitPriceInfoList.size(); i++){
  242. TBClothingRelatedUnitPriceEntity priceInfo = tBRelatedUnitPriceInfoList.get(i);
  243. if(priceInfo.getMeteringUnit() != null && !"".equals(priceInfo.getMeteringUnit())){
  244. rumap.put(priceInfo.getRelatedunitid(), priceInfo);
  245. }else {
  246. relatedUnitRepetitionflag = true;
  247. break;
  248. }
  249. }
  250. if(relatedUnitRepetitionflag){
  251. message = "供应商价格不能重复,请修改后再保存";
  252. j.setMsg(message);
  253. j.setSuccess(false);
  254. return j;
  255. }
  256. // ClothingEntity t = clothingService.get(ClothingEntity.class, clothing.getId());
  257. try {
  258. // MyBeanUtils.copyBeanNotNull2Bean(clothing, t);
  259. clothingService.updateMain(clothing, tBRelatedUnitPriceInfoList);;
  260. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  261. } catch (Exception e) {
  262. e.printStackTrace();
  263. message = "服装档案更新失败";
  264. throw new BusinessException(e.getMessage());
  265. }
  266. j.setMsg(message);
  267. return j;
  268. }
  269. /**
  270. * 服装档案新增页面跳转
  271. *
  272. * @return
  273. */
  274. @RequestMapping(params = "goAdd")
  275. public ModelAndView goAdd(ClothingEntity clothing, HttpServletRequest req) {
  276. if (StringUtil.isNotEmpty(clothing.getId())) {
  277. clothing = clothingService.getEntity(ClothingEntity.class, clothing.getId());
  278. req.setAttribute("clothingPage", clothing);
  279. }
  280. return new ModelAndView("cn/com/lzt/clothing/base/clothing-add");
  281. }
  282. /**
  283. * 服装档案编辑页面跳转
  284. *
  285. * @return
  286. */
  287. @RequestMapping(params = "goUpdate")
  288. public ModelAndView goUpdate(ClothingEntity clothing, HttpServletRequest req) {
  289. if (StringUtil.isNotEmpty(clothing.getId())) {
  290. clothing = clothingService.getEntity(ClothingEntity.class, clothing.getId());
  291. req.setAttribute("clothingPage", clothing);
  292. }
  293. return new ModelAndView("cn/com/lzt/clothing/base/clothing-update");
  294. }
  295. /**
  296. * 导入功能跳转
  297. *
  298. * @return
  299. */
  300. @RequestMapping(params = "upload")
  301. public ModelAndView upload(HttpServletRequest req) {
  302. req.setAttribute("controller_name","clothingController");
  303. return new ModelAndView("common/upload/pub_excel_upload");
  304. }
  305. /**
  306. * 导出excel
  307. *
  308. * @param request
  309. * @param response
  310. */
  311. @RequestMapping(params = "exportXls")
  312. public String exportXls(ClothingEntity clothing,HttpServletRequest request,HttpServletResponse response
  313. , DataGrid dataGrid,ModelMap modelMap) {
  314. CriteriaQuery cq = new CriteriaQuery(ClothingEntity.class, dataGrid);
  315. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, clothing, request.getParameterMap());
  316. List<ClothingEntity> clothings = this.clothingService.getListByCriteriaQuery(cq,false);
  317. modelMap.put(NormalExcelConstants.FILE_NAME,"服装档案");
  318. modelMap.put(NormalExcelConstants.CLASS,ClothingEntity.class);
  319. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("服装档案列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  320. "导出信息"));
  321. modelMap.put(NormalExcelConstants.DATA_LIST,clothings);
  322. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  323. }
  324. /**
  325. * 导出excel 使模板
  326. *
  327. * @param request
  328. * @param response
  329. */
  330. @RequestMapping(params = "exportXlsByT")
  331. public String exportXlsByT(ClothingEntity clothing,HttpServletRequest request,HttpServletResponse response
  332. , DataGrid dataGrid,ModelMap modelMap) {
  333. modelMap.put(NormalExcelConstants.FILE_NAME,"服装档案");
  334. modelMap.put(NormalExcelConstants.CLASS,ClothingEntity.class);
  335. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("服装档案列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  336. "导出信息"));
  337. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  338. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  339. }
  340. @SuppressWarnings("unchecked")
  341. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  342. @ResponseBody
  343. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  344. AjaxJson j = new AjaxJson();
  345. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  346. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  347. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  348. MultipartFile file = entity.getValue();// 获取上传文件对象
  349. ImportParams params = new ImportParams();
  350. params.setTitleRows(2);
  351. params.setHeadRows(1);
  352. params.setNeedSave(true);
  353. try {
  354. List<ClothingEntity> listClothingEntitys = ExcelImportUtil.importExcel(file.getInputStream(),ClothingEntity.class,params);
  355. for (ClothingEntity clothing : listClothingEntitys) {
  356. clothingService.save(clothing);
  357. }
  358. j.setMsg("文件导入成功!");
  359. } catch (Exception e) {
  360. j.setMsg("文件导入失败!");
  361. logger.error(ExceptionUtil.getExceptionMessage(e));
  362. }finally{
  363. try {
  364. file.getInputStream().close();
  365. } catch (IOException e) {
  366. e.printStackTrace();
  367. }
  368. }
  369. }
  370. return j;
  371. }
  372. @RequestMapping(method = RequestMethod.GET)
  373. @ResponseBody
  374. public List<ClothingEntity> list() {
  375. List<ClothingEntity> listClothings=clothingService.getList(ClothingEntity.class);
  376. return listClothings;
  377. }
  378. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  379. @ResponseBody
  380. public ResponseEntity<?> get(@PathVariable("id") String id) {
  381. ClothingEntity task = clothingService.get(ClothingEntity.class, id);
  382. if (task == null) {
  383. return new ResponseEntity(HttpStatus.NOT_FOUND);
  384. }
  385. return new ResponseEntity(task, HttpStatus.OK);
  386. }
  387. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  388. @ResponseBody
  389. public ResponseEntity<?> create(@RequestBody ClothingEntity clothing, UriComponentsBuilder uriBuilder) {
  390. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  391. Set<ConstraintViolation<ClothingEntity>> failures = validator.validate(clothing);
  392. if (!failures.isEmpty()) {
  393. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  394. }
  395. //保存
  396. try{
  397. clothingService.save(clothing);
  398. } catch (Exception e) {
  399. e.printStackTrace();
  400. return new ResponseEntity(HttpStatus.NO_CONTENT);
  401. }
  402. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  403. String id = clothing.getId();
  404. URI uri = uriBuilder.path("/rest/clothingController/" + id).build().toUri();
  405. HttpHeaders headers = new HttpHeaders();
  406. headers.setLocation(uri);
  407. return new ResponseEntity(headers, HttpStatus.CREATED);
  408. }
  409. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  410. public ResponseEntity<?> update(@RequestBody ClothingEntity clothing) {
  411. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  412. Set<ConstraintViolation<ClothingEntity>> failures = validator.validate(clothing);
  413. if (!failures.isEmpty()) {
  414. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  415. }
  416. //保存
  417. try{
  418. clothingService.saveOrUpdate(clothing);
  419. } catch (Exception e) {
  420. e.printStackTrace();
  421. return new ResponseEntity(HttpStatus.NO_CONTENT);
  422. }
  423. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  424. return new ResponseEntity(HttpStatus.NO_CONTENT);
  425. }
  426. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  427. @ResponseStatus(HttpStatus.NO_CONTENT)
  428. public void delete(@PathVariable("id") String id) {
  429. clothingService.deleteEntityById(ClothingEntity.class, id);
  430. }
  431. @RequestMapping(params = "switchTask")
  432. public String goView(HttpServletRequest request) {
  433. return "cn/com/lzt/clothing/base/clothing_switch_task";
  434. }
  435. @RequestMapping(params = "tBRelatedUnitPriceInfoList")
  436. public ModelAndView tBRelatedUnitPriceInfoList(ClothingEntity clothing, HttpServletRequest req) {
  437. //===================================================================================
  438. //获取参数
  439. Object id1 = clothing.getId();
  440. //===================================================================================
  441. //查询-价格信息
  442. String hql1 = "from TBClothingRelatedUnitPriceEntity where 1 = 1 AND clothingid =? ";
  443. try{
  444. List<TBClothingRelatedUnitPriceEntity> tBRelatedUnitPriceInfoEntityList = systemService.findHql(hql1,id1);
  445. req.setAttribute("tBRelatedUnitPriceInfoList", tBRelatedUnitPriceInfoEntityList);
  446. }catch(Exception e){
  447. logger.info(e.getMessage());
  448. }
  449. return new ModelAndView("cn/com/lzt/clothing/base/tBRelatedUnitPriceInfoList");
  450. }
  451. }