ProjectRatingController.java 15 KB

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