BudgetPeriodController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package cn.com.lzt.budget.period.controller;
  2. import cn.com.lzt.budget.period.entity.BudgetPeriodEntity;
  3. import cn.com.lzt.budget.period.service.BudgetPeriodServiceI;
  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-07-01 09:58:28
  69. * @version V1.0
  70. *
  71. */
  72. @Controller
  73. @RequestMapping("/budgetPeriodController")
  74. public class BudgetPeriodController extends BaseController {
  75. /**
  76. * Logger for this class
  77. */
  78. private static final Logger logger = Logger.getLogger(BudgetPeriodController.class);
  79. @Autowired
  80. private BudgetPeriodServiceI budgetPeriodService;
  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/period/budgetPeriodList");
  93. }
  94. /**
  95. * easyui AJAX请求数据
  96. *
  97. * @param request
  98. * @param response
  99. * @param dataGrid
  100. */
  101. @RequestMapping(params = "datagrid")
  102. public void datagrid(BudgetPeriodEntity budgetPeriod,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  103. CriteriaQuery cq = new CriteriaQuery(BudgetPeriodEntity.class, dataGrid);
  104. //查询条件组装器
  105. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, budgetPeriod, request.getParameterMap());
  106. try{
  107. //自定义追加查询条件
  108. }catch (Exception e) {
  109. throw new BusinessException(e.getMessage());
  110. }
  111. cq.add();
  112. this.budgetPeriodService.getDataGridReturn(cq, true);
  113. TagUtil.datagrid(response, dataGrid);
  114. }
  115. /**
  116. * 删除预算周期
  117. *
  118. * @return
  119. */
  120. @RequestMapping(params = "doDel")
  121. @ResponseBody
  122. public AjaxJson doDel(BudgetPeriodEntity budgetPeriod, HttpServletRequest request) {
  123. String message = null;
  124. AjaxJson j = new AjaxJson();
  125. budgetPeriod = systemService.getEntity(BudgetPeriodEntity.class, budgetPeriod.getId());
  126. message = "预算周期删除成功";
  127. try{
  128. budgetPeriodService.delete(budgetPeriod);
  129. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  130. }catch(Exception e){
  131. e.printStackTrace();
  132. message = "预算周期删除失败";
  133. throw new BusinessException(e.getMessage());
  134. }
  135. j.setMsg(message);
  136. return j;
  137. }
  138. /**
  139. * 批量删除预算周期
  140. *
  141. * @return
  142. */
  143. @RequestMapping(params = "doBatchDel")
  144. @ResponseBody
  145. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  146. String message = null;
  147. AjaxJson j = new AjaxJson();
  148. message = "预算周期删除成功";
  149. try{
  150. for(String id:ids.split(",")){
  151. BudgetPeriodEntity budgetPeriod = systemService.getEntity(BudgetPeriodEntity.class,
  152. id
  153. );
  154. budgetPeriodService.delete(budgetPeriod);
  155. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  156. }
  157. }catch(Exception e){
  158. e.printStackTrace();
  159. message = "预算周期删除失败";
  160. throw new BusinessException(e.getMessage());
  161. }
  162. j.setMsg(message);
  163. return j;
  164. }
  165. /**
  166. * 添加预算周期
  167. *
  168. * @return
  169. */
  170. @RequestMapping(params = "doAdd")
  171. @ResponseBody
  172. public AjaxJson doAdd(BudgetPeriodEntity budgetPeriod, HttpServletRequest request) {
  173. if(StringUtils.isNotBlank(budgetPeriod.getId())){
  174. return doUpdate(budgetPeriod,request);
  175. }
  176. String message = null;
  177. AjaxJson j = new AjaxJson();
  178. message = "预算周期添加成功";
  179. try{
  180. budgetPeriodService.save(budgetPeriod);
  181. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  182. }catch(Exception e){
  183. e.printStackTrace();
  184. message = "预算周期添加失败";
  185. throw new BusinessException(e.getMessage());
  186. }
  187. j.setMsg(message);
  188. return j;
  189. }
  190. /**
  191. * 更新预算周期
  192. *
  193. * @return
  194. */
  195. @RequestMapping(params = "doUpdate")
  196. @ResponseBody
  197. public AjaxJson doUpdate(BudgetPeriodEntity budgetPeriod, HttpServletRequest request) {
  198. String message = null;
  199. AjaxJson j = new AjaxJson();
  200. message = "预算周期更新成功";
  201. BudgetPeriodEntity t = budgetPeriodService.get(BudgetPeriodEntity.class, budgetPeriod.getId());
  202. try {
  203. MyBeanUtils.copyBeanNotNull2Bean(budgetPeriod, t);
  204. budgetPeriodService.saveOrUpdate(t);
  205. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  206. } catch (Exception e) {
  207. e.printStackTrace();
  208. message = "预算周期更新失败";
  209. throw new BusinessException(e.getMessage());
  210. }
  211. j.setMsg(message);
  212. return j;
  213. }
  214. /**
  215. * 预算周期新增页面跳转
  216. *
  217. * @return
  218. */
  219. @RequestMapping(params = "goAdd")
  220. public ModelAndView goAdd(BudgetPeriodEntity budgetPeriod, HttpServletRequest req) {
  221. if (StringUtil.isNotEmpty(budgetPeriod.getId())) {
  222. budgetPeriod = budgetPeriodService.getEntity(BudgetPeriodEntity.class, budgetPeriod.getId());
  223. req.setAttribute("budgetPeriodPage", budgetPeriod);
  224. }
  225. return new ModelAndView("cn/com/lzt/budget/period/budgetPeriod-add");
  226. }
  227. /**
  228. * 预算周期编辑页面跳转
  229. *
  230. * @return
  231. */
  232. @RequestMapping(params = "goUpdate")
  233. public ModelAndView goUpdate(BudgetPeriodEntity budgetPeriod, HttpServletRequest req) {
  234. if (StringUtil.isNotEmpty(budgetPeriod.getId())) {
  235. budgetPeriod = budgetPeriodService.getEntity(BudgetPeriodEntity.class, budgetPeriod.getId());
  236. req.setAttribute("entity", budgetPeriod);
  237. }
  238. return new ModelAndView("cn/com/lzt/budget/period/budgetPeriod-add");
  239. }
  240. /**
  241. * 导入功能跳转
  242. *
  243. * @return
  244. */
  245. @RequestMapping(params = "upload")
  246. public ModelAndView upload(HttpServletRequest req) {
  247. req.setAttribute("controller_name","budgetPeriodController");
  248. return new ModelAndView("common/upload/pub_excel_upload");
  249. }
  250. /**
  251. * 导出excel
  252. *
  253. * @param request
  254. * @param response
  255. */
  256. @RequestMapping(params = "exportXls")
  257. public String exportXls(BudgetPeriodEntity budgetPeriod,HttpServletRequest request,HttpServletResponse response
  258. , DataGrid dataGrid,ModelMap modelMap) {
  259. CriteriaQuery cq = new CriteriaQuery(BudgetPeriodEntity.class, dataGrid);
  260. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, budgetPeriod, request.getParameterMap());
  261. List<BudgetPeriodEntity> budgetPeriods = this.budgetPeriodService.getListByCriteriaQuery(cq,false);
  262. modelMap.put(NormalExcelConstants.FILE_NAME,"预算周期");
  263. modelMap.put(NormalExcelConstants.CLASS,BudgetPeriodEntity.class);
  264. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("预算周期列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  265. "导出信息"));
  266. modelMap.put(NormalExcelConstants.DATA_LIST,budgetPeriods);
  267. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  268. }
  269. /**
  270. * 导出excel 使模板
  271. *
  272. * @param request
  273. * @param response
  274. */
  275. @RequestMapping(params = "exportXlsByT")
  276. public String exportXlsByT(BudgetPeriodEntity budgetPeriod,HttpServletRequest request,HttpServletResponse response
  277. , DataGrid dataGrid,ModelMap modelMap) {
  278. modelMap.put(NormalExcelConstants.FILE_NAME,"预算周期");
  279. modelMap.put(NormalExcelConstants.CLASS,BudgetPeriodEntity.class);
  280. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("预算周期列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  281. "导出信息"));
  282. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  283. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  284. }
  285. @SuppressWarnings("unchecked")
  286. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  287. @ResponseBody
  288. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  289. AjaxJson j = new AjaxJson();
  290. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  291. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  292. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  293. MultipartFile file = entity.getValue();// 获取上传文件对象
  294. ImportParams params = new ImportParams();
  295. params.setTitleRows(2);
  296. params.setHeadRows(1);
  297. params.setNeedSave(true);
  298. try {
  299. List<BudgetPeriodEntity> listBudgetPeriodEntitys = ExcelImportUtil.importExcel(file.getInputStream(),BudgetPeriodEntity.class,params);
  300. for (BudgetPeriodEntity budgetPeriod : listBudgetPeriodEntitys) {
  301. budgetPeriodService.save(budgetPeriod);
  302. }
  303. j.setMsg("文件导入成功!");
  304. } catch (Exception e) {
  305. j.setMsg("文件导入失败!");
  306. logger.error(ExceptionUtil.getExceptionMessage(e));
  307. }finally{
  308. try {
  309. file.getInputStream().close();
  310. } catch (IOException e) {
  311. e.printStackTrace();
  312. }
  313. }
  314. }
  315. return j;
  316. }
  317. @RequestMapping(method = RequestMethod.GET)
  318. @ResponseBody
  319. public List<BudgetPeriodEntity> list() {
  320. List<BudgetPeriodEntity> listBudgetPeriods=budgetPeriodService.getList(BudgetPeriodEntity.class);
  321. return listBudgetPeriods;
  322. }
  323. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  324. @ResponseBody
  325. public ResponseEntity<?> get(@PathVariable("id") String id) {
  326. BudgetPeriodEntity task = budgetPeriodService.get(BudgetPeriodEntity.class, id);
  327. if (task == null) {
  328. return new ResponseEntity(HttpStatus.NOT_FOUND);
  329. }
  330. return new ResponseEntity(task, HttpStatus.OK);
  331. }
  332. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  333. @ResponseBody
  334. public ResponseEntity<?> create(@RequestBody BudgetPeriodEntity budgetPeriod, UriComponentsBuilder uriBuilder) {
  335. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  336. Set<ConstraintViolation<BudgetPeriodEntity>> failures = validator.validate(budgetPeriod);
  337. if (!failures.isEmpty()) {
  338. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  339. }
  340. //保存
  341. try{
  342. budgetPeriodService.save(budgetPeriod);
  343. } catch (Exception e) {
  344. e.printStackTrace();
  345. return new ResponseEntity(HttpStatus.NO_CONTENT);
  346. }
  347. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  348. String id = budgetPeriod.getId();
  349. URI uri = uriBuilder.path("/rest/budgetPeriodController/" + id).build().toUri();
  350. HttpHeaders headers = new HttpHeaders();
  351. headers.setLocation(uri);
  352. return new ResponseEntity(headers, HttpStatus.CREATED);
  353. }
  354. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  355. public ResponseEntity<?> update(@RequestBody BudgetPeriodEntity budgetPeriod) {
  356. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  357. Set<ConstraintViolation<BudgetPeriodEntity>> failures = validator.validate(budgetPeriod);
  358. if (!failures.isEmpty()) {
  359. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  360. }
  361. //保存
  362. try{
  363. budgetPeriodService.saveOrUpdate(budgetPeriod);
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. return new ResponseEntity(HttpStatus.NO_CONTENT);
  367. }
  368. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  369. return new ResponseEntity(HttpStatus.NO_CONTENT);
  370. }
  371. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  372. @ResponseStatus(HttpStatus.NO_CONTENT)
  373. public void delete(@PathVariable("id") String id) {
  374. budgetPeriodService.deleteEntityById(BudgetPeriodEntity.class, id);
  375. }
  376. @RequestMapping(params = "checkDup")
  377. @ResponseBody
  378. public AjaxJson checkDup(BudgetPeriodEntity budgetPeriod, HttpServletRequest request) {
  379. String message = null;
  380. AjaxJson j = new AjaxJson();
  381. List<BudgetPeriodEntity> list = systemService.findByProperty(BudgetPeriodEntity.class,"periodYear",budgetPeriod.getPeriodYear());
  382. if(list.isEmpty()){
  383. j.setSuccess(false);
  384. }else {
  385. BudgetPeriodEntity entity = list.get(0);
  386. if (StringUtils.equals(budgetPeriod.getId(), entity.getId())) j.setSuccess(false);
  387. }
  388. return j;
  389. }
  390. }