BudgetControlController.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package cn.com.lzt.budget.control.controller;
  2. import cn.com.lzt.budget.control.entity.BudgetControlPublishEntity;
  3. import cn.com.lzt.budget.control.entity.BudgetControlEntity;
  4. import cn.com.lzt.budget.control.service.BudgetControlServiceI;
  5. import cn.com.lzt.budget.control.page.BudgetControlPage;
  6. import cn.com.lzt.budget.control.entity.BudgetControlDetailEntity;
  7. import java.util.*;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import cn.com.lzt.budget.measure.entity.BudgetMeasureEntity;
  11. import org.apache.commons.lang.xwork.StringUtils;
  12. import org.apache.log4j.Logger;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.ResponseBody;
  18. import org.springframework.web.servlet.ModelAndView;
  19. import org.jeecgframework.core.common.controller.BaseController;
  20. import org.jeecgframework.core.common.exception.BusinessException;
  21. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  22. import org.jeecgframework.core.common.model.json.AjaxJson;
  23. import org.jeecgframework.core.common.model.json.DataGrid;
  24. import org.jeecgframework.core.constant.Globals;
  25. import org.jeecgframework.core.util.ExceptionUtil;
  26. import org.jeecgframework.core.util.ResourceUtil;
  27. import org.jeecgframework.core.util.StringUtil;
  28. import org.jeecgframework.tag.core.easyui.TagUtil;
  29. import org.jeecgframework.web.system.service.SystemService;
  30. import org.jeecgframework.core.util.MyBeanUtils;
  31. import org.jeecgframework.poi.excel.ExcelImportUtil;
  32. import org.jeecgframework.poi.excel.entity.ExportParams;
  33. import org.jeecgframework.poi.excel.entity.ImportParams;
  34. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  35. import org.springframework.ui.ModelMap;
  36. import org.springframework.web.multipart.MultipartFile;
  37. import org.springframework.web.multipart.MultipartHttpServletRequest;
  38. import java.io.IOException;
  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 javax.validation.ConstraintViolation;
  47. import javax.validation.Validator;
  48. import java.net.URI;
  49. import org.springframework.http.MediaType;
  50. import org.springframework.web.util.UriComponentsBuilder;
  51. /**
  52. * @Title: Controller
  53. * @Description: 预算控制
  54. * @author onlineGenerator
  55. * @date 2021-02-16 09:02:00
  56. * @version V1.0
  57. *
  58. */
  59. @Controller
  60. @RequestMapping("/budgetControlController")
  61. public class BudgetControlController extends BaseController {
  62. /**
  63. * Logger for this class
  64. */
  65. private static final Logger logger = Logger.getLogger(BudgetControlController.class);
  66. @Autowired
  67. private BudgetControlServiceI budgetControlService;
  68. @Autowired
  69. private SystemService systemService;
  70. @Autowired
  71. private Validator validator;
  72. /**
  73. * 预算控制列表 页面跳转
  74. *
  75. * @return
  76. */
  77. @RequestMapping(params = "list")
  78. public ModelAndView list(HttpServletRequest request) {
  79. return new ModelAndView("cn/com/lzt/budget/control/budgetControlList");
  80. }
  81. /**
  82. * easyui AJAX请求数据
  83. *
  84. * @param request
  85. * @param response
  86. * @param dataGrid
  87. */
  88. @RequestMapping(params = "datagrid")
  89. public void datagrid(BudgetControlEntity budgetControl,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  90. CriteriaQuery cq = new CriteriaQuery(BudgetControlEntity.class, dataGrid);
  91. //查询条件组装器
  92. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, budgetControl);
  93. try{
  94. //自定义追加查询条件
  95. }catch (Exception e) {
  96. throw new BusinessException(e.getMessage());
  97. }
  98. cq.add();
  99. this.budgetControlService.getDataGridReturn(cq, true);
  100. TagUtil.datagrid(response, dataGrid);
  101. }
  102. /**
  103. * 删除预算控制
  104. *
  105. * @return
  106. */
  107. @RequestMapping(params = "doDel")
  108. @ResponseBody
  109. public AjaxJson doDel(BudgetControlEntity budgetControl, HttpServletRequest request) {
  110. AjaxJson j = new AjaxJson();
  111. budgetControl = systemService.getEntity(BudgetControlEntity.class, budgetControl.getId());
  112. String message = "预算控制删除成功";
  113. try{
  114. budgetControlService.delMain(budgetControl);
  115. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  116. }catch(Exception e){
  117. e.printStackTrace();
  118. message = "预算控制删除失败";
  119. throw new BusinessException(e.getMessage());
  120. }
  121. j.setMsg(message);
  122. return j;
  123. }
  124. /**
  125. * 批量删除预算控制
  126. *
  127. * @return
  128. */
  129. @RequestMapping(params = "doBatchDel")
  130. @ResponseBody
  131. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  132. AjaxJson j = new AjaxJson();
  133. String message = "预算控制删除成功";
  134. try{
  135. for(String id:ids.split(",")){
  136. BudgetControlEntity budgetControl = systemService.getEntity(BudgetControlEntity.class,
  137. id
  138. );
  139. budgetControlService.delMain(budgetControl);
  140. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  141. }
  142. }catch(Exception e){
  143. e.printStackTrace();
  144. message = "预算控制删除失败";
  145. throw new BusinessException(e.getMessage());
  146. }
  147. j.setMsg(message);
  148. return j;
  149. }
  150. /**
  151. * 添加预算控制
  152. * @return
  153. */
  154. @RequestMapping(params = "doAdd")
  155. @ResponseBody
  156. public AjaxJson doAdd(BudgetControlEntity budgetControl,BudgetControlPage budgetControlPage, HttpServletRequest request) {
  157. if(StringUtils.isNotBlank(budgetControl.getId())){
  158. return doUpdate(budgetControl, budgetControlPage, request);
  159. }
  160. List<BudgetControlDetailEntity> budgetControlDetailList = budgetControlPage.getBudgetControlDetailList();
  161. AjaxJson j = new AjaxJson();
  162. String message = "添加成功";
  163. try{
  164. budgetControlService.addMain(budgetControl, budgetControlDetailList);
  165. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  166. }catch(Exception e){
  167. e.printStackTrace();
  168. message = "预算控制添加失败";
  169. throw new BusinessException(e.getMessage());
  170. }
  171. j.setMsg(message);
  172. return j;
  173. }
  174. /**
  175. * 更新预算控制
  176. * @returnbh
  177. */
  178. @RequestMapping(params = "doUpdate")
  179. @ResponseBody
  180. public AjaxJson doUpdate(BudgetControlEntity budgetControl,BudgetControlPage budgetControlPage, HttpServletRequest request) {
  181. List<BudgetControlDetailEntity> budgetControlDetailList = budgetControlPage.getBudgetControlDetailList();
  182. AjaxJson j = new AjaxJson();
  183. String message = "更新成功";
  184. try{
  185. budgetControlService.updateMain(budgetControl, budgetControlDetailList);
  186. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  187. }catch(Exception e){
  188. e.printStackTrace();
  189. message = "更新预算控制失败";
  190. throw new BusinessException(e.getMessage());
  191. }
  192. j.setMsg(message);
  193. return j;
  194. }
  195. /**
  196. * 预算控制新增页面跳转
  197. *
  198. * @return
  199. */
  200. @RequestMapping(params = "goAdd")
  201. public ModelAndView goAdd(BudgetControlEntity budgetControl, HttpServletRequest req) {
  202. if (StringUtil.isNotEmpty(budgetControl.getId())) {
  203. budgetControl = budgetControlService.getEntity(BudgetControlEntity.class, budgetControl.getId());
  204. req.setAttribute("entity", budgetControl);
  205. }
  206. return new ModelAndView("cn/com/lzt/budget/control/budgetControl-add");
  207. }
  208. /**
  209. * 预算控制编辑页面跳转
  210. *
  211. * @return
  212. */
  213. @RequestMapping(params = "goUpdate")
  214. public ModelAndView goUpdate(BudgetControlEntity budgetControl, HttpServletRequest req) {
  215. if (StringUtil.isNotEmpty(budgetControl.getId())) {
  216. budgetControl = budgetControlService.getEntity(BudgetControlEntity.class, budgetControl.getId());
  217. req.setAttribute("entity", budgetControl);
  218. }
  219. return new ModelAndView("cn/com/lzt/budget/control/budgetControl-add");
  220. }
  221. /**
  222. * 加载明细列表[预算控制详情]
  223. *
  224. * @return
  225. */
  226. @RequestMapping(params = "budgetControlDetailList")
  227. public ModelAndView budgetControlDetailList(BudgetControlEntity budgetControl, HttpServletRequest req) {
  228. //===================================================================================
  229. //获取参数
  230. Object id0 = budgetControl.getId();
  231. //===================================================================================
  232. //查询-预算控制详情
  233. String hql0 = "from BudgetControlDetailEntity where 1 = 1 AND fK_ID =? ";
  234. try{
  235. String type = budgetControl.getType();
  236. if(StringUtils.equals(type,"0" )) {//成本控制方案
  237. String hql = "from BudgetMeasureEntity where custom_code like 'A01_%' and (length(custom_code)<=9 or custom_code like 'A01A01A05___') order by custom_code";
  238. List<BudgetMeasureEntity> budgetMeasureEntityList = systemService.findHql(hql);
  239. req.setAttribute("budgetMeasureEntityList", budgetMeasureEntityList);
  240. }else {//1 专项控制方案
  241. String hql = "from BudgetMeasureEntity where custom_code like 'B%' order by custom_code";
  242. List<BudgetMeasureEntity> budgetMeasureEntityList = systemService.findHql(hql);
  243. req.setAttribute("budgetMeasureEntityList", budgetMeasureEntityList);
  244. }
  245. List<BudgetControlDetailEntity> budgetControlDetailEntityList = systemService.findHql(hql0,id0);
  246. Map<String,BudgetControlDetailEntity> map = new HashMap<>();
  247. for(BudgetControlDetailEntity detailEntity:budgetControlDetailEntityList){
  248. map.put(detailEntity.getMeasureId(), detailEntity);
  249. }
  250. req.setAttribute("map", map);
  251. // req.setAttribute("budgetControlDetailList", budgetControlDetailEntityList);
  252. }catch(Exception e){
  253. logger.info(e.getMessage());
  254. }
  255. return new ModelAndView("cn/com/lzt/budget/control/budgetControlDetailList");
  256. }
  257. /**
  258. * 导出excel
  259. *
  260. * @param request
  261. * @param response
  262. */
  263. @RequestMapping(params = "exportXls")
  264. public String exportXls(BudgetControlEntity budgetControl,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid,ModelMap map) {
  265. CriteriaQuery cq = new CriteriaQuery(BudgetControlEntity.class, dataGrid);
  266. //查询条件组装器
  267. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, budgetControl);
  268. try{
  269. //自定义追加查询条件
  270. }catch (Exception e) {
  271. throw new BusinessException(e.getMessage());
  272. }
  273. cq.add();
  274. List<BudgetControlEntity> list=this.budgetControlService.getListByCriteriaQuery(cq, false);
  275. List<BudgetControlPage> pageList=new ArrayList<BudgetControlPage>();
  276. if(list!=null&&list.size()>0){
  277. for(BudgetControlEntity entity:list){
  278. try{
  279. BudgetControlPage page=new BudgetControlPage();
  280. MyBeanUtils.copyBeanNotNull2Bean(entity,page);
  281. Object id0 = entity.getId();
  282. String hql0 = "from BudgetControlDetailEntity where 1 = 1 AND fK_ID =? ";
  283. List<BudgetControlDetailEntity> budgetControlDetailEntityList = systemService.findHql(hql0,id0);
  284. page.setBudgetControlDetailList(budgetControlDetailEntityList);
  285. pageList.add(page);
  286. }catch(Exception e){
  287. logger.info(e.getMessage());
  288. }
  289. }
  290. }
  291. map.put(NormalExcelConstants.FILE_NAME,"预算控制");
  292. map.put(NormalExcelConstants.CLASS,BudgetControlPage.class);
  293. map.put(NormalExcelConstants.PARAMS,new ExportParams("预算控制列表", "导出人:Jeecg",
  294. "导出信息"));
  295. map.put(NormalExcelConstants.DATA_LIST,pageList);
  296. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  297. }
  298. /**
  299. * 通过excel导入数据
  300. * @param request
  301. * @param
  302. * @return
  303. */
  304. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  305. @ResponseBody
  306. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  307. AjaxJson j = new AjaxJson();
  308. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  309. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  310. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  311. MultipartFile file = entity.getValue();// 获取上传文件对象
  312. ImportParams params = new ImportParams();
  313. params.setTitleRows(2);
  314. params.setHeadRows(2);
  315. params.setNeedSave(true);
  316. try {
  317. List<BudgetControlPage> list = ExcelImportUtil.importExcel(file.getInputStream(), BudgetControlPage.class, params);
  318. BudgetControlEntity entity1=null;
  319. for (BudgetControlPage page : list) {
  320. entity1=new BudgetControlEntity();
  321. MyBeanUtils.copyBeanNotNull2Bean(page,entity1);
  322. budgetControlService.addMain(entity1, page.getBudgetControlDetailList());
  323. }
  324. j.setMsg("文件导入成功!");
  325. } catch (Exception e) {
  326. j.setMsg("文件导入失败!");
  327. logger.error(ExceptionUtil.getExceptionMessage(e));
  328. }finally{
  329. try {
  330. file.getInputStream().close();
  331. } catch (IOException e) {
  332. e.printStackTrace();
  333. }
  334. }
  335. }
  336. return j;
  337. }
  338. /**
  339. * 导出excel 使模板
  340. */
  341. @RequestMapping(params = "exportXlsByT")
  342. public String exportXlsByT(ModelMap map) {
  343. map.put(NormalExcelConstants.FILE_NAME,"预算控制");
  344. map.put(NormalExcelConstants.CLASS,BudgetControlPage.class);
  345. map.put(NormalExcelConstants.PARAMS,new ExportParams("预算控制列表", "导出人:"+ ResourceUtil.getSessionUser().getRealName(),
  346. "导出信息"));
  347. map.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  348. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  349. }
  350. /**
  351. * 导入功能跳转
  352. *
  353. * @return
  354. */
  355. @RequestMapping(params = "upload")
  356. public ModelAndView upload(HttpServletRequest req) {
  357. req.setAttribute("controller_name", "budgetControlController");
  358. return new ModelAndView("common/upload/pub_excel_upload");
  359. }
  360. @RequestMapping(method = RequestMethod.GET)
  361. @ResponseBody
  362. public List<BudgetControlEntity> list() {
  363. List<BudgetControlEntity> listBudgetControls=budgetControlService.getList(BudgetControlEntity.class);
  364. return listBudgetControls;
  365. }
  366. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  367. @ResponseBody
  368. public ResponseEntity<?> get(@PathVariable("id") String id) {
  369. BudgetControlEntity task = budgetControlService.get(BudgetControlEntity.class, id);
  370. if (task == null) {
  371. return new ResponseEntity(HttpStatus.NOT_FOUND);
  372. }
  373. return new ResponseEntity(task, HttpStatus.OK);
  374. }
  375. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  376. @ResponseBody
  377. public ResponseEntity<?> create(@RequestBody BudgetControlPage budgetControlPage, UriComponentsBuilder uriBuilder) {
  378. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  379. Set<ConstraintViolation<BudgetControlPage>> failures = validator.validate(budgetControlPage);
  380. if (!failures.isEmpty()) {
  381. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  382. }
  383. //保存
  384. List<BudgetControlDetailEntity> budgetControlDetailList = budgetControlPage.getBudgetControlDetailList();
  385. BudgetControlEntity budgetControl = new BudgetControlEntity();
  386. try{
  387. MyBeanUtils.copyBeanNotNull2Bean(budgetControl,budgetControlPage);
  388. }catch(Exception e){
  389. logger.info(e.getMessage());
  390. }
  391. budgetControlService.addMain(budgetControl, budgetControlDetailList);
  392. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  393. String id = budgetControlPage.getId();
  394. URI uri = uriBuilder.path("/rest/budgetControlController/" + id).build().toUri();
  395. HttpHeaders headers = new HttpHeaders();
  396. headers.setLocation(uri);
  397. return new ResponseEntity(headers, HttpStatus.CREATED);
  398. }
  399. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  400. public ResponseEntity<?> update(@RequestBody BudgetControlPage budgetControlPage) {
  401. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  402. Set<ConstraintViolation<BudgetControlPage>> failures = validator.validate(budgetControlPage);
  403. if (!failures.isEmpty()) {
  404. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  405. }
  406. //保存
  407. List<BudgetControlDetailEntity> budgetControlDetailList = budgetControlPage.getBudgetControlDetailList();
  408. BudgetControlEntity budgetControl = new BudgetControlEntity();
  409. try{
  410. MyBeanUtils.copyBeanNotNull2Bean(budgetControl,budgetControlPage);
  411. }catch(Exception e){
  412. logger.info(e.getMessage());
  413. }
  414. budgetControlService.updateMain(budgetControl, budgetControlDetailList);
  415. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  416. return new ResponseEntity(HttpStatus.NO_CONTENT);
  417. }
  418. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  419. @ResponseStatus(HttpStatus.NO_CONTENT)
  420. public void delete(@PathVariable("id") String id) {
  421. BudgetControlEntity budgetControl = budgetControlService.get(BudgetControlEntity.class, id);
  422. budgetControlService.delMain(budgetControl);
  423. }
  424. @RequestMapping(params = "publish")
  425. @ResponseBody
  426. public AjaxJson publish(BudgetControlEntity controlEntity, HttpServletRequest request) {
  427. String message = null;
  428. AjaxJson j = new AjaxJson();
  429. message = "下发成功";
  430. try{
  431. String controlId = controlEntity.getId();
  432. String entityIds=request.getParameter("entityIds");
  433. String type=request.getParameter("type");
  434. String[] entityIdArray = StringUtil.splitString(entityIds, ",");
  435. List<BudgetControlPublishEntity> saveList = new ArrayList<>();
  436. for(String entityId :entityIdArray){
  437. String sql = "select 1 from t_b_budget_control_publish where entity_id=? and control_type=? ";
  438. List<?> controlEntityList = systemService.findListbySql(sql,entityId,type);
  439. if(controlEntityList.size()>0){
  440. j.setSuccess(false);
  441. j.setMsg("下发失败");
  442. return j;
  443. }
  444. BudgetControlPublishEntity contolPublishEntity = new BudgetControlPublishEntity();
  445. contolPublishEntity.setControlId(controlId);
  446. contolPublishEntity.setEntityId(entityId);
  447. contolPublishEntity.setControlType(type);
  448. saveList.add(contolPublishEntity);
  449. }
  450. systemService.batchSave(saveList);
  451. }catch(Exception e){
  452. e.printStackTrace();
  453. message = "下发失败";
  454. j.setSuccess(false);
  455. }
  456. j.setMsg(message);
  457. return j;
  458. }
  459. @RequestMapping(params = "cancel")
  460. @ResponseBody
  461. public AjaxJson cancel(BudgetControlEntity controlEntity, HttpServletRequest request) {
  462. String message = null;
  463. AjaxJson j = new AjaxJson();
  464. message = "撤销成功";
  465. try{
  466. String controlId = controlEntity.getId();
  467. String entityIds=request.getParameter("entityIds");
  468. String sql = "delete from t_b_budget_control_publish where entity_id=? and control_id=? ";
  469. String[] entityIdArray = StringUtil.splitString(entityIds, ",");
  470. for(String entityId :entityIdArray){
  471. systemService.executeSql(sql,entityId,controlId);
  472. }
  473. }catch(Exception e){
  474. e.printStackTrace();
  475. message = "撤销失败";
  476. throw new BusinessException(e.getMessage());
  477. }
  478. j.setMsg(message);
  479. return j;
  480. }
  481. }