BudgetTempSheetEntityController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package cn.com.lzt.budget.tempsheetentity.controller;
  2. import cn.com.lzt.budget.tempsheetentity.entity.BudgetTempSheetEntityMap;
  3. import cn.com.lzt.budget.tempsheetentity.service.BudgetTempsheetEntityServiceI;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.log4j.Logger;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.ModelMap;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import org.springframework.web.servlet.ModelAndView;
  15. import org.jeecgframework.core.common.controller.BaseController;
  16. import org.jeecgframework.core.common.exception.BusinessException;
  17. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  18. import org.jeecgframework.core.common.model.json.AjaxJson;
  19. import org.jeecgframework.core.common.model.json.DataGrid;
  20. import org.jeecgframework.core.constant.Globals;
  21. import org.jeecgframework.core.util.StringUtil;
  22. import org.jeecgframework.tag.core.easyui.TagUtil;
  23. import org.jeecgframework.web.system.service.SystemService;
  24. import org.jeecgframework.core.util.MyBeanUtils;
  25. import org.jeecgframework.poi.excel.ExcelImportUtil;
  26. import org.jeecgframework.poi.excel.entity.ExportParams;
  27. import org.jeecgframework.poi.excel.entity.ImportParams;
  28. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  29. import org.jeecgframework.core.util.ResourceUtil;
  30. import java.io.IOException;
  31. import org.springframework.web.bind.annotation.RequestMethod;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import org.springframework.web.multipart.MultipartHttpServletRequest;
  34. import java.util.Map;
  35. import org.jeecgframework.core.util.ExceptionUtil;
  36. import org.springframework.http.ResponseEntity;
  37. import org.springframework.web.bind.annotation.PathVariable;
  38. import org.springframework.web.bind.annotation.RequestBody;
  39. import org.springframework.web.bind.annotation.ResponseStatus;
  40. import org.springframework.http.HttpHeaders;
  41. import org.springframework.http.HttpStatus;
  42. import org.jeecgframework.core.beanvalidator.BeanValidators;
  43. import java.util.Set;
  44. import javax.validation.ConstraintViolation;
  45. import javax.validation.Validator;
  46. import java.net.URI;
  47. import org.springframework.http.MediaType;
  48. import org.springframework.web.util.UriComponentsBuilder;
  49. /**
  50. * @Title: Controller
  51. * @Description: 预算样表主体关联
  52. * @author onlineGenerator
  53. * @date 2020-07-04 13:51:19
  54. * @version V1.0
  55. *
  56. */
  57. @Controller
  58. @RequestMapping("/budgetTempSheetEntityController")
  59. public class BudgetTempSheetEntityController extends BaseController {
  60. /**
  61. * Logger for this class
  62. */
  63. private static final Logger logger = Logger.getLogger(BudgetTempSheetEntityController.class);
  64. @Autowired
  65. private BudgetTempsheetEntityServiceI budgetTempsheetEntityService;
  66. @Autowired
  67. private SystemService systemService;
  68. @Autowired
  69. private Validator validator;
  70. /**
  71. * 预算样表主体关联列表 页面跳转
  72. *
  73. * @return
  74. */
  75. @RequestMapping(params = "list")
  76. public ModelAndView list(HttpServletRequest request) {
  77. return new ModelAndView("cn/com/lzt/budget/tempsheetentity/tempSheetEntityList");
  78. }
  79. /**
  80. * easyui AJAX请求数据
  81. *
  82. * @param request
  83. * @param response
  84. * @param dataGrid
  85. * @param user
  86. */
  87. @RequestMapping(params = "datagrid")
  88. public void datagrid(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  89. CriteriaQuery cq = new CriteriaQuery(BudgetTempSheetEntityMap.class, dataGrid);
  90. //查询条件组装器
  91. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, budgetDemoDept, request.getParameterMap());
  92. try{
  93. //自定义追加查询条件
  94. }catch (Exception e) {
  95. throw new BusinessException(e.getMessage());
  96. }
  97. cq.add();
  98. this.budgetTempsheetEntityService.getDataGridReturn(cq, true);
  99. TagUtil.datagrid(response, dataGrid);
  100. }
  101. /**
  102. * 删除预算样表主体关联
  103. *
  104. * @return
  105. */
  106. @RequestMapping(params = "doDel")
  107. @ResponseBody
  108. public AjaxJson doDel(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest request) {
  109. String message = null;
  110. AjaxJson j = new AjaxJson();
  111. budgetDemoDept = systemService.getEntity(BudgetTempSheetEntityMap.class, budgetDemoDept.getId());
  112. message = "预算样表主体关联删除成功";
  113. try{
  114. budgetTempsheetEntityService.delete(budgetDemoDept);
  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. String message = null;
  133. AjaxJson j = new AjaxJson();
  134. message = "预算样表主体关联删除成功";
  135. try{
  136. for(String id:ids.split(",")){
  137. BudgetTempSheetEntityMap budgetDemoDept = systemService.getEntity(BudgetTempSheetEntityMap.class,
  138. id
  139. );
  140. budgetTempsheetEntityService.delete(budgetDemoDept);
  141. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  142. }
  143. }catch(Exception e){
  144. e.printStackTrace();
  145. message = "预算样表主体关联删除失败";
  146. throw new BusinessException(e.getMessage());
  147. }
  148. j.setMsg(message);
  149. return j;
  150. }
  151. /**
  152. * 添加预算样表主体关联
  153. *
  154. * @param ids
  155. * @return
  156. */
  157. @RequestMapping(params = "doAdd")
  158. @ResponseBody
  159. public AjaxJson doAdd(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest request) {
  160. String message = null;
  161. AjaxJson j = new AjaxJson();
  162. message = "预算样表主体关联添加成功";
  163. try{
  164. budgetTempsheetEntityService.save(budgetDemoDept);
  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. *
  177. * @param ids
  178. * @return
  179. */
  180. @RequestMapping(params = "doUpdate")
  181. @ResponseBody
  182. public AjaxJson doUpdate(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest request) {
  183. String message = null;
  184. AjaxJson j = new AjaxJson();
  185. message = "预算样表主体关联更新成功";
  186. BudgetTempSheetEntityMap t = budgetTempsheetEntityService.get(BudgetTempSheetEntityMap.class, budgetDemoDept.getId());
  187. try {
  188. MyBeanUtils.copyBeanNotNull2Bean(budgetDemoDept, t);
  189. budgetTempsheetEntityService.saveOrUpdate(t);
  190. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  191. } catch (Exception e) {
  192. e.printStackTrace();
  193. message = "预算样表主体关联更新失败";
  194. throw new BusinessException(e.getMessage());
  195. }
  196. j.setMsg(message);
  197. return j;
  198. }
  199. /**
  200. * 预算样表主体关联新增页面跳转
  201. *
  202. * @return
  203. */
  204. @RequestMapping(params = "goAdd")
  205. public ModelAndView goAdd(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest req) {
  206. if (StringUtil.isNotEmpty(budgetDemoDept.getId())) {
  207. budgetDemoDept = budgetTempsheetEntityService.getEntity(BudgetTempSheetEntityMap.class, budgetDemoDept.getId());
  208. req.setAttribute("budgetDemoDeptPage", budgetDemoDept);
  209. }
  210. return new ModelAndView("cn/com/lzt/budget/tempsheetentity/tempSheetEntity-add");
  211. }
  212. /**
  213. * 预算样表主体关联编辑页面跳转
  214. *
  215. * @return
  216. */
  217. @RequestMapping(params = "goUpdate")
  218. public ModelAndView goUpdate(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest req) {
  219. if (StringUtil.isNotEmpty(budgetDemoDept.getId())) {
  220. budgetDemoDept = budgetTempsheetEntityService.getEntity(BudgetTempSheetEntityMap.class, budgetDemoDept.getId());
  221. req.setAttribute("budgetDemoDeptPage", budgetDemoDept);
  222. }
  223. return new ModelAndView("cn/com/lzt/budget/tempsheetentity/tempSheetEntity-update");
  224. }
  225. /**
  226. * 导入功能跳转
  227. *
  228. * @return
  229. */
  230. @RequestMapping(params = "upload")
  231. public ModelAndView upload(HttpServletRequest req) {
  232. req.setAttribute("controller_name","budgetTempSheetEntityController");
  233. return new ModelAndView("common/upload/pub_excel_upload");
  234. }
  235. /**
  236. * 导出excel
  237. *
  238. * @param request
  239. * @param response
  240. */
  241. @RequestMapping(params = "exportXls")
  242. public String exportXls(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest request, HttpServletResponse response
  243. , DataGrid dataGrid, ModelMap modelMap) {
  244. CriteriaQuery cq = new CriteriaQuery(BudgetTempSheetEntityMap.class, dataGrid);
  245. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, budgetDemoDept, request.getParameterMap());
  246. List<BudgetTempSheetEntityMap> budgetDemoDepts = this.budgetTempsheetEntityService.getListByCriteriaQuery(cq,false);
  247. modelMap.put(NormalExcelConstants.FILE_NAME,"预算样表主体关联");
  248. modelMap.put(NormalExcelConstants.CLASS,BudgetTempSheetEntityMap.class);
  249. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("预算样表主体关联列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  250. "导出信息"));
  251. modelMap.put(NormalExcelConstants.DATA_LIST,budgetDemoDepts);
  252. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  253. }
  254. /**
  255. * 导出excel 使模板
  256. *
  257. * @param request
  258. * @param response
  259. */
  260. @RequestMapping(params = "exportXlsByT")
  261. public String exportXlsByT(BudgetTempSheetEntityMap budgetDemoDept, HttpServletRequest request, HttpServletResponse response
  262. , DataGrid dataGrid, ModelMap modelMap) {
  263. modelMap.put(NormalExcelConstants.FILE_NAME,"预算样表主体关联");
  264. modelMap.put(NormalExcelConstants.CLASS,BudgetTempSheetEntityMap.class);
  265. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("预算样表主体关联列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  266. "导出信息"));
  267. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  268. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  269. }
  270. @SuppressWarnings("unchecked")
  271. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  272. @ResponseBody
  273. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  274. AjaxJson j = new AjaxJson();
  275. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  276. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  277. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  278. MultipartFile file = entity.getValue();// 获取上传文件对象
  279. ImportParams params = new ImportParams();
  280. params.setTitleRows(2);
  281. params.setHeadRows(1);
  282. params.setNeedSave(true);
  283. try {
  284. List<BudgetTempSheetEntityMap> listBudgetTempSheetEntities = ExcelImportUtil.importExcel(file.getInputStream(),BudgetTempSheetEntityMap.class,params);
  285. for (BudgetTempSheetEntityMap budgetDemoDept : listBudgetTempSheetEntities) {
  286. budgetTempsheetEntityService.save(budgetDemoDept);
  287. }
  288. j.setMsg("文件导入成功!");
  289. } catch (Exception e) {
  290. j.setMsg("文件导入失败!");
  291. logger.error(ExceptionUtil.getExceptionMessage(e));
  292. }finally{
  293. try {
  294. file.getInputStream().close();
  295. } catch (IOException e) {
  296. e.printStackTrace();
  297. }
  298. }
  299. }
  300. return j;
  301. }
  302. @RequestMapping(method = RequestMethod.GET)
  303. @ResponseBody
  304. public List<BudgetTempSheetEntityMap> list() {
  305. List<BudgetTempSheetEntityMap> listBudgetDemoDepts=budgetTempsheetEntityService.getList(BudgetTempSheetEntityMap.class);
  306. return listBudgetDemoDepts;
  307. }
  308. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  309. @ResponseBody
  310. public ResponseEntity<?> get(@PathVariable("id") String id) {
  311. BudgetTempSheetEntityMap task = budgetTempsheetEntityService.get(BudgetTempSheetEntityMap.class, id);
  312. if (task == null) {
  313. return new ResponseEntity(HttpStatus.NOT_FOUND);
  314. }
  315. return new ResponseEntity(task, HttpStatus.OK);
  316. }
  317. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  318. @ResponseBody
  319. public ResponseEntity<?> create(@RequestBody BudgetTempSheetEntityMap budgetDemoDept, UriComponentsBuilder uriBuilder) {
  320. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  321. Set<ConstraintViolation<BudgetTempSheetEntityMap>> failures = validator.validate(budgetDemoDept);
  322. if (!failures.isEmpty()) {
  323. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  324. }
  325. //保存
  326. try{
  327. budgetTempsheetEntityService.save(budgetDemoDept);
  328. } catch (Exception e) {
  329. e.printStackTrace();
  330. return new ResponseEntity(HttpStatus.NO_CONTENT);
  331. }
  332. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  333. String id = budgetDemoDept.getId();
  334. URI uri = uriBuilder.path("/rest/budgetTempSheetEntityController/" + id).build().toUri();
  335. HttpHeaders headers = new HttpHeaders();
  336. headers.setLocation(uri);
  337. return new ResponseEntity(headers, HttpStatus.CREATED);
  338. }
  339. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  340. public ResponseEntity<?> update(@RequestBody BudgetTempSheetEntityMap budgetDemoDept) {
  341. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  342. Set<ConstraintViolation<BudgetTempSheetEntityMap>> failures = validator.validate(budgetDemoDept);
  343. if (!failures.isEmpty()) {
  344. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  345. }
  346. //保存
  347. try{
  348. budgetTempsheetEntityService.saveOrUpdate(budgetDemoDept);
  349. } catch (Exception e) {
  350. e.printStackTrace();
  351. return new ResponseEntity(HttpStatus.NO_CONTENT);
  352. }
  353. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  354. return new ResponseEntity(HttpStatus.NO_CONTENT);
  355. }
  356. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  357. @ResponseStatus(HttpStatus.NO_CONTENT)
  358. public void delete(@PathVariable("id") String id) {
  359. budgetTempsheetEntityService.deleteEntityById(BudgetTempSheetEntityMap.class, id);
  360. }
  361. }