BudgetOfficeController.java 15 KB

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