BudgetPeriodDetailController.java 18 KB

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