BudgetDimFunctionController.java 17 KB

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