ExtAttributeController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package cn.com.lzt.extattribute.controller;
  2. import cn.com.lzt.extattribute.entity.ExtAttributeEntity;
  3. import cn.com.lzt.extattribute.page.ExtAttributePage;
  4. import cn.com.lzt.extattribute.service.ExtAttributeServiceI;
  5. import cn.com.lzt.extattributedefset.entity.ExtAttributeDefsetEntity;
  6. import org.apache.log4j.Logger;
  7. import org.jeecgframework.core.beanvalidator.BeanValidators;
  8. import org.jeecgframework.core.common.controller.BaseController;
  9. import org.jeecgframework.core.common.exception.BusinessException;
  10. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  11. import org.jeecgframework.core.common.model.json.AjaxJson;
  12. import org.jeecgframework.core.common.model.json.DataGrid;
  13. import org.jeecgframework.core.constant.Globals;
  14. import org.jeecgframework.core.util.ExceptionUtil;
  15. import org.jeecgframework.core.util.MyBeanUtils;
  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.ArrayList;
  43. import java.util.List;
  44. import java.util.Map;
  45. import java.util.Set;
  46. /**
  47. * @Title: Controller
  48. * @Description: 扩展属性设置
  49. * @author onlineGenerator
  50. * @date 2019-05-29 16:47:12
  51. * @version V1.0
  52. *
  53. */
  54. @Controller
  55. @RequestMapping("/extAttributeController")
  56. public class ExtAttributeController extends BaseController {
  57. /**
  58. * Logger for this class
  59. */
  60. private static final Logger logger = Logger.getLogger(ExtAttributeController.class);
  61. @Autowired
  62. private ExtAttributeServiceI extAttributeService;
  63. @Autowired
  64. private SystemService systemService;
  65. @Autowired
  66. private Validator validator;
  67. /**
  68. * 扩展属性设置列表 页面跳转
  69. *
  70. * @return
  71. */
  72. @RequestMapping(params = "list")
  73. public ModelAndView list(HttpServletRequest request) {
  74. return new ModelAndView("cn/com/lzt/extattribute/extAttributeList");
  75. }
  76. /**
  77. * easyui AJAX请求数据
  78. *
  79. * @param request
  80. * @param response
  81. * @param dataGrid
  82. * @param user
  83. */
  84. @RequestMapping(params = "datagrid")
  85. public void datagrid(ExtAttributeEntity extAttribute,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  86. CriteriaQuery cq = new CriteriaQuery(ExtAttributeEntity.class, dataGrid);
  87. //查询条件组装器
  88. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, extAttribute);
  89. try{
  90. //自定义追加查询条件
  91. }catch (Exception e) {
  92. throw new BusinessException(e.getMessage());
  93. }
  94. cq.add();
  95. this.extAttributeService.getDataGridReturn(cq, true);
  96. TagUtil.datagrid(response, dataGrid);
  97. }
  98. /**
  99. * 删除扩展属性设置
  100. *
  101. * @return
  102. */
  103. @RequestMapping(params = "doDel")
  104. @ResponseBody
  105. public AjaxJson doDel(ExtAttributeEntity extAttribute, HttpServletRequest request) {
  106. AjaxJson j = new AjaxJson();
  107. extAttribute = systemService.getEntity(ExtAttributeEntity.class, extAttribute.getId());
  108. String message = "扩展属性设置删除成功";
  109. try{
  110. extAttributeService.delMain(extAttribute);
  111. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  112. }catch(Exception e){
  113. e.printStackTrace();
  114. message = "扩展属性设置删除失败";
  115. throw new BusinessException(e.getMessage());
  116. }
  117. j.setMsg(message);
  118. return j;
  119. }
  120. /**
  121. * 批量删除扩展属性设置
  122. *
  123. * @return
  124. */
  125. @RequestMapping(params = "doBatchDel")
  126. @ResponseBody
  127. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  128. AjaxJson j = new AjaxJson();
  129. String message = "扩展属性设置删除成功";
  130. try{
  131. for(String id:ids.split(",")){
  132. ExtAttributeEntity extAttribute = systemService.getEntity(ExtAttributeEntity.class,
  133. id
  134. );
  135. extAttributeService.delMain(extAttribute);
  136. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  137. }
  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. * @param ids
  150. * @return
  151. */
  152. @RequestMapping(params = "doAdd")
  153. @ResponseBody
  154. public AjaxJson doAdd(ExtAttributeEntity extAttribute,ExtAttributePage extAttributePage, HttpServletRequest request) {
  155. List<ExtAttributeDefsetEntity> extAttributeDefsetList = extAttributePage.getExtAttributeDefsetList();
  156. AjaxJson j = new AjaxJson();
  157. String message = "添加成功";
  158. try{
  159. if(!checkTableExsit(extAttributePage.getTablename())) {
  160. j.setSuccess(false);
  161. message = "扩展属性设置添加失败,数据库表:"+extAttributePage.getTablename()+"不存在,请重新填写【单据档案表名】";
  162. }else if(!checkTableSettingExsit(extAttributePage.getTablename())){
  163. j.setSuccess(false);
  164. message = "扩展属性设置添加失败,数据库表:"+extAttributePage.getTablename()+"的扩展属性设置已经存在,请不要重复设置";
  165. }else{
  166. extAttributeService.addMain(extAttribute, extAttributeDefsetList);
  167. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  168. }
  169. }catch(Exception e){
  170. e.printStackTrace();
  171. message = "扩展属性设置添加失败";
  172. throw new BusinessException(e.getMessage());
  173. }
  174. j.setMsg(message);
  175. return j;
  176. }
  177. //校验成功,返回true
  178. private boolean checkTableExsit(String tablename) {
  179. boolean success = true;
  180. try {
  181. String sql = "SELECT COUNT(1) as count FROM information_schema.tables WHERE table_name = ?";
  182. List<Map<String, Object>> resList = systemService.findForJdbc(sql,tablename);
  183. if(resList.get(0).get("count").toString().equals("0"))
  184. success = false;
  185. }catch(Exception e) {
  186. success = false;
  187. }
  188. return success;
  189. }
  190. //校验成功,返回true
  191. private boolean checkTableSettingExsit(String tablename) {
  192. boolean success = true;
  193. try {
  194. String sql = "SELECT COUNT(1) as count FROM t_s_ext_attribute WHERE tablename = ?";
  195. List<Map<String, Object>> resList = systemService.findForJdbc(sql,tablename);
  196. if(Integer.parseInt(resList.get(0).get("count").toString()) > 0)
  197. success = false;
  198. }catch(Exception e) {
  199. logger.error(e);
  200. success = false;
  201. }
  202. return success;
  203. }
  204. /**
  205. * 更新扩展属性设置
  206. *
  207. * @param ids
  208. * @return
  209. */
  210. @RequestMapping(params = "doUpdate")
  211. @ResponseBody
  212. public AjaxJson doUpdate(ExtAttributeEntity extAttribute,ExtAttributePage extAttributePage, HttpServletRequest request) {
  213. List<ExtAttributeDefsetEntity> extAttributeDefsetList = extAttributePage.getExtAttributeDefsetList();
  214. AjaxJson j = new AjaxJson();
  215. String message = "更新成功";
  216. try{
  217. extAttributeService.updateMain(extAttribute, extAttributeDefsetList);
  218. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  219. }catch(Exception e){
  220. e.printStackTrace();
  221. message = "更新扩展属性设置失败";
  222. throw new BusinessException(e.getMessage());
  223. }
  224. j.setMsg(message);
  225. return j;
  226. }
  227. /**
  228. * 扩展属性设置新增页面跳转
  229. *
  230. * @return
  231. */
  232. @RequestMapping(params = "goAdd")
  233. public ModelAndView goAdd(ExtAttributeEntity extAttribute, HttpServletRequest req) {
  234. if (StringUtil.isNotEmpty(extAttribute.getId())) {
  235. extAttribute = extAttributeService.getEntity(ExtAttributeEntity.class, extAttribute.getId());
  236. req.setAttribute("extAttributePage", extAttribute);
  237. }
  238. return new ModelAndView("cn/com/lzt/extattribute/extAttribute-add");
  239. }
  240. /**
  241. * 扩展属性设置编辑页面跳转
  242. *
  243. * @return
  244. */
  245. @RequestMapping(params = "goUpdate")
  246. public ModelAndView goUpdate(ExtAttributeEntity extAttribute, HttpServletRequest req) {
  247. if (StringUtil.isNotEmpty(extAttribute.getId())) {
  248. extAttribute = extAttributeService.getEntity(ExtAttributeEntity.class, extAttribute.getId());
  249. req.setAttribute("extAttributePage", extAttribute);
  250. }
  251. return new ModelAndView("cn/com/lzt/extattribute/extAttribute-update");
  252. }
  253. /**
  254. * 加载明细列表[扩展属性设置明细]
  255. *
  256. * @return
  257. */
  258. @RequestMapping(params = "extAttributeDefsetNumList")
  259. public ModelAndView extAttributeDefsetNumList(ExtAttributeEntity extAttribute, HttpServletRequest req) {
  260. String datatype = "num";
  261. //===================================================================================
  262. //获取参数
  263. Object id0 = extAttribute.getId();
  264. //===================================================================================
  265. //查询-扩展属性设置明细
  266. String hql0 = "from ExtAttributeDefsetEntity where 1 = 1 AND eXTATTRIBUTEID =? and datatype=? ";
  267. try{
  268. List<ExtAttributeDefsetEntity> extAttributeDefsetEntityList = systemService.findHql(hql0,id0,datatype);
  269. if(extAttributeDefsetEntityList.size()==0) {
  270. extAttributeDefsetEntityList = buildNewDefSetList(datatype);
  271. }
  272. req.setAttribute("extAttributeDefsetList", extAttributeDefsetEntityList);
  273. }catch(Exception e){
  274. logger.info(e.getMessage());
  275. }
  276. return new ModelAndView("cn/com/lzt/extattributedefset/extAttributeDefsetNumList");
  277. }
  278. /**
  279. * 加载明细列表[扩展属性设置明细]
  280. *
  281. * @return
  282. */
  283. @RequestMapping(params = "extAttributeDefsetStrList")
  284. public ModelAndView extAttributeDefsetStrList(ExtAttributeEntity extAttribute, HttpServletRequest req) {
  285. String datatype = "str";
  286. //===================================================================================
  287. //获取参数
  288. Object id0 = extAttribute.getId();
  289. //===================================================================================
  290. //查询-扩展属性设置明细
  291. String hql0 = "from ExtAttributeDefsetEntity where 1 = 1 AND eXTATTRIBUTEID =? and datatype=? ";
  292. try{
  293. List<ExtAttributeDefsetEntity> extAttributeDefsetEntityList = systemService.findHql(hql0,id0,datatype);
  294. if(extAttributeDefsetEntityList.size()==0) {
  295. extAttributeDefsetEntityList = buildNewDefSetList(datatype);
  296. }
  297. req.setAttribute("extAttributeDefsetList", extAttributeDefsetEntityList);
  298. }catch(Exception e){
  299. logger.info(e.getMessage());
  300. }
  301. return new ModelAndView("cn/com/lzt/extattributedefset/extAttributeDefsetStrList");
  302. }
  303. private List<ExtAttributeDefsetEntity> buildNewDefSetList(String datatype){
  304. List<ExtAttributeDefsetEntity> setList = new ArrayList<ExtAttributeDefsetEntity>();
  305. for(int i = 1; i <= 10 ; i++) {
  306. ExtAttributeDefsetEntity newAttri = new ExtAttributeDefsetEntity();
  307. newAttri.setTablefieldname("def"+datatype+i);
  308. newAttri.setDatatype(datatype);
  309. setList.add(newAttri);
  310. }
  311. return setList;
  312. }
  313. /**
  314. * 导出excel
  315. *
  316. * @param request
  317. * @param response
  318. */
  319. @RequestMapping(params = "exportXls")
  320. public String exportXls(ExtAttributeEntity extAttribute,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid,ModelMap map) {
  321. CriteriaQuery cq = new CriteriaQuery(ExtAttributeEntity.class, dataGrid);
  322. //查询条件组装器
  323. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, extAttribute);
  324. try{
  325. //自定义追加查询条件
  326. }catch (Exception e) {
  327. throw new BusinessException(e.getMessage());
  328. }
  329. cq.add();
  330. List<ExtAttributeEntity> list=this.extAttributeService.getListByCriteriaQuery(cq, false);
  331. List<ExtAttributePage> pageList=new ArrayList<ExtAttributePage>();
  332. if(list!=null&&list.size()>0){
  333. for(ExtAttributeEntity entity:list){
  334. try{
  335. ExtAttributePage page=new ExtAttributePage();
  336. MyBeanUtils.copyBeanNotNull2Bean(entity,page);
  337. Object id0 = entity.getId();
  338. String hql0 = "from ExtAttributeDefsetEntity where 1 = 1 AND eXTATTRIBUTEID =? ";
  339. List<ExtAttributeDefsetEntity> extAttributeDefsetEntityList = systemService.findHql(hql0,id0);
  340. page.setExtAttributeDefsetList(extAttributeDefsetEntityList);
  341. pageList.add(page);
  342. }catch(Exception e){
  343. logger.info(e.getMessage());
  344. }
  345. }
  346. }
  347. map.put(NormalExcelConstants.FILE_NAME,"扩展属性设置");
  348. map.put(NormalExcelConstants.CLASS,ExtAttributePage.class);
  349. map.put(NormalExcelConstants.PARAMS,new ExportParams("扩展属性设置列表", "导出人:Jeecg",
  350. "导出信息"));
  351. map.put(NormalExcelConstants.DATA_LIST,pageList);
  352. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  353. }
  354. /**
  355. * 通过excel导入数据
  356. * @param request
  357. * @param
  358. * @return
  359. */
  360. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  361. @ResponseBody
  362. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  363. AjaxJson j = new AjaxJson();
  364. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  365. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  366. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  367. MultipartFile file = entity.getValue();// 获取上传文件对象
  368. ImportParams params = new ImportParams();
  369. params.setTitleRows(2);
  370. params.setHeadRows(2);
  371. params.setNeedSave(true);
  372. try {
  373. List<ExtAttributePage> list = ExcelImportUtil.importExcel(file.getInputStream(), ExtAttributePage.class, params);
  374. ExtAttributeEntity entity1=null;
  375. for (ExtAttributePage page : list) {
  376. entity1=new ExtAttributeEntity();
  377. MyBeanUtils.copyBeanNotNull2Bean(page,entity1);
  378. extAttributeService.addMain(entity1, page.getExtAttributeDefsetList());
  379. }
  380. j.setMsg("文件导入成功!");
  381. } catch (Exception e) {
  382. j.setMsg("文件导入失败!");
  383. logger.error(ExceptionUtil.getExceptionMessage(e));
  384. }finally{
  385. try {
  386. file.getInputStream().close();
  387. } catch (IOException e) {
  388. e.printStackTrace();
  389. }
  390. }
  391. }
  392. return j;
  393. }
  394. /**
  395. * 导出excel 使模板
  396. */
  397. @RequestMapping(params = "exportXlsByT")
  398. public String exportXlsByT(ModelMap map) {
  399. map.put(NormalExcelConstants.FILE_NAME,"扩展属性设置");
  400. map.put(NormalExcelConstants.CLASS,ExtAttributePage.class);
  401. map.put(NormalExcelConstants.PARAMS,new ExportParams("扩展属性设置列表", "导出人:"+ ResourceUtil.getSessionUser().getRealName(),
  402. "导出信息"));
  403. map.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  404. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  405. }
  406. /**
  407. * 导入功能跳转
  408. *
  409. * @return
  410. */
  411. @RequestMapping(params = "upload")
  412. public ModelAndView upload(HttpServletRequest req) {
  413. req.setAttribute("controller_name", "extAttributeController");
  414. return new ModelAndView("common/upload/pub_excel_upload");
  415. }
  416. @RequestMapping(method = RequestMethod.GET)
  417. @ResponseBody
  418. public List<ExtAttributeEntity> list() {
  419. List<ExtAttributeEntity> listExtAttributes=extAttributeService.getList(ExtAttributeEntity.class);
  420. return listExtAttributes;
  421. }
  422. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  423. @ResponseBody
  424. public ResponseEntity<?> get(@PathVariable("id") String id) {
  425. ExtAttributeEntity task = extAttributeService.get(ExtAttributeEntity.class, id);
  426. if (task == null) {
  427. return new ResponseEntity(HttpStatus.NOT_FOUND);
  428. }
  429. return new ResponseEntity(task, HttpStatus.OK);
  430. }
  431. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  432. @ResponseBody
  433. public ResponseEntity<?> create(@RequestBody ExtAttributePage extAttributePage, UriComponentsBuilder uriBuilder) {
  434. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  435. Set<ConstraintViolation<ExtAttributePage>> failures = validator.validate(extAttributePage);
  436. if (!failures.isEmpty()) {
  437. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  438. }
  439. //保存
  440. List<ExtAttributeDefsetEntity> extAttributeDefsetList = extAttributePage.getExtAttributeDefsetList();
  441. ExtAttributeEntity extAttribute = new ExtAttributeEntity();
  442. try{
  443. MyBeanUtils.copyBeanNotNull2Bean(extAttribute,extAttributePage);
  444. }catch(Exception e){
  445. logger.info(e.getMessage());
  446. }
  447. extAttributeService.addMain(extAttribute, extAttributeDefsetList);
  448. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  449. String id = extAttributePage.getId();
  450. URI uri = uriBuilder.path("/rest/extAttributeController/" + id).build().toUri();
  451. HttpHeaders headers = new HttpHeaders();
  452. headers.setLocation(uri);
  453. return new ResponseEntity(headers, HttpStatus.CREATED);
  454. }
  455. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  456. public ResponseEntity<?> update(@RequestBody ExtAttributePage extAttributePage) {
  457. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  458. Set<ConstraintViolation<ExtAttributePage>> failures = validator.validate(extAttributePage);
  459. if (!failures.isEmpty()) {
  460. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  461. }
  462. //保存
  463. List<ExtAttributeDefsetEntity> extAttributeDefsetList = extAttributePage.getExtAttributeDefsetList();
  464. ExtAttributeEntity extAttribute = new ExtAttributeEntity();
  465. try{
  466. MyBeanUtils.copyBeanNotNull2Bean(extAttribute,extAttributePage);
  467. }catch(Exception e){
  468. logger.info(e.getMessage());
  469. }
  470. extAttributeService.updateMain(extAttribute, extAttributeDefsetList);
  471. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  472. return new ResponseEntity(HttpStatus.NO_CONTENT);
  473. }
  474. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  475. @ResponseStatus(HttpStatus.NO_CONTENT)
  476. public void delete(@PathVariable("id") String id) {
  477. ExtAttributeEntity extAttribute = extAttributeService.get(ExtAttributeEntity.class, id);
  478. extAttributeService.delMain(extAttribute);
  479. }
  480. }