MealsSubsidyController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package cn.com.lzt.mealssubsidy.controller;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  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.log4j.Logger;
  14. import org.jeecgframework.core.beanvalidator.BeanValidators;
  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.ExceptionUtil;
  22. import org.jeecgframework.core.util.MyBeanUtils;
  23. import org.jeecgframework.core.util.ResourceUtil;
  24. import org.jeecgframework.core.util.StringUtil;
  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.tag.core.easyui.TagUtil;
  30. import org.jeecgframework.web.system.pojo.base.TSDepart;
  31. import org.jeecgframework.web.system.service.SystemService;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.http.HttpHeaders;
  34. import org.springframework.http.HttpStatus;
  35. import org.springframework.http.MediaType;
  36. import org.springframework.http.ResponseEntity;
  37. import org.springframework.stereotype.Controller;
  38. import org.springframework.ui.ModelMap;
  39. import org.springframework.web.bind.annotation.PathVariable;
  40. import org.springframework.web.bind.annotation.RequestBody;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestMethod;
  43. import org.springframework.web.bind.annotation.ResponseBody;
  44. import org.springframework.web.bind.annotation.ResponseStatus;
  45. import org.springframework.web.multipart.MultipartFile;
  46. import org.springframework.web.multipart.MultipartHttpServletRequest;
  47. import org.springframework.web.servlet.ModelAndView;
  48. import org.springframework.web.util.UriComponentsBuilder;
  49. import cn.com.lzt.mealssubsidy.entity.MealsSubsidyEntity;
  50. import cn.com.lzt.mealssubsidy.service.MealsSubsidyServiceI;
  51. import cn.com.lzt.megathermalsubsidy.entity.MegathermalSubsidyEntity;
  52. /**
  53. * @Title: Controller
  54. * @Description: 餐费补贴
  55. * @author onlineGenerator
  56. * @date 2017-10-18 18:02:39
  57. * @version V1.0
  58. *
  59. */
  60. @Controller
  61. @RequestMapping("/mealsSubsidyController")
  62. public class MealsSubsidyController extends BaseController {
  63. /**
  64. * Logger for this class
  65. */
  66. private static final Logger logger = Logger.getLogger(MealsSubsidyController.class);
  67. @Autowired
  68. private MealsSubsidyServiceI mealsSubsidyService;
  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/mealssubsidy/mealsSubsidyList");
  81. }
  82. /**
  83. * easyui AJAX请求数据
  84. *
  85. * @param request
  86. * @param response
  87. * @param dataGrid
  88. * @param user
  89. */
  90. @RequestMapping(params = "datagrid")
  91. public void datagrid(MealsSubsidyEntity mealsSubsidy, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  92. CriteriaQuery cq = new CriteriaQuery(MealsSubsidyEntity.class, dataGrid);
  93. //查询条件组装器
  94. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mealsSubsidy, request.getParameterMap());
  95. try{
  96. //自定义追加查询条件
  97. String[] status = new String[]{Globals.Enabled_Status.toString(), Globals.Disabled_Status.toString()};
  98. cq.in("status", status);
  99. cq.eq("deleteFlag", Globals.Delete_Normal.toString());
  100. Map map = new HashMap();
  101. map.put("createDate", "desc");
  102. cq.setOrder(map);
  103. }catch (Exception e) {
  104. throw new BusinessException(e.getMessage());
  105. }
  106. cq.add();
  107. this.mealsSubsidyService.getDataGridReturn(cq, true);
  108. TagUtil.datagrid(response, dataGrid);
  109. }
  110. /**
  111. * 异步获取项目名称
  112. * @author zbw
  113. * 2017-10-13
  114. * @param request
  115. * @param response
  116. * @return
  117. */
  118. @RequestMapping(params = "getPName")
  119. @ResponseBody
  120. public AjaxJson getPName(HttpServletRequest request, HttpServletResponse response) {
  121. AjaxJson j = new AjaxJson();
  122. String id = request.getParameter("id");
  123. String message = null;
  124. TSDepart projectentity = systemService.getEntity(TSDepart.class, id);
  125. try {
  126. j.setMsg(projectentity.getDepartname());
  127. //systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. throw new BusinessException(e.getMessage());
  131. }
  132. return j;
  133. }
  134. /**
  135. * 保存新增/更新的行数据
  136. * @param page
  137. * @return
  138. */
  139. @RequestMapping(params = "saveRows")
  140. @ResponseBody
  141. public AjaxJson saveRows(MealsSubsidyEntity mealsSubsidy, HttpServletRequest request){
  142. String message = null;
  143. AjaxJson j = new AjaxJson();
  144. message = "";
  145. if (StringUtil.isNotEmpty(mealsSubsidy.getId())) {
  146. MealsSubsidyEntity t = mealsSubsidyService.get(MealsSubsidyEntity.class, mealsSubsidy.getId());
  147. try {
  148. if(Globals.SUBSIDY_TYPE_BCCB.equals(mealsSubsidy.getSubsidyType())){//加班餐补
  149. mealsSubsidy.setCalcuUnit(Globals.CALCU_UNIT_dun);
  150. mealsSubsidy.setIffixed(Globals.FIXED_NO);//0不固定
  151. }else if(Globals.SUBSIDY_TYPE_AYGD.equals(mealsSubsidy.getSubsidyType())){//按月固定
  152. mealsSubsidy.setCalcuUnit(Globals.CALCU_UNIT_yue);
  153. mealsSubsidy.setIffixed(Globals.FIXED_YES);//1固定
  154. }else if(Globals.SUBSIDY_TYPE_ACGD.equals(mealsSubsidy.getSubsidyType())){//按次固定
  155. mealsSubsidy.setCalcuUnit(Globals.CALCU_UNIT_ci);
  156. mealsSubsidy.setIffixed(Globals.FIXED_YES);//1固定
  157. }
  158. MyBeanUtils.copyBeanNotNull2Bean(mealsSubsidy, t);
  159. mealsSubsidyService.updateEntitie(t);
  160. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  161. message = "餐费补贴更新成功";
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. message = "餐费补贴更新失败";
  165. throw new BusinessException(e.getMessage());
  166. }
  167. } else {
  168. try{
  169. if(Globals.SUBSIDY_TYPE_BCCB.equals(mealsSubsidy.getSubsidyType())){//加班餐补
  170. mealsSubsidy.setCalcuUnit(Globals.CALCU_UNIT_dun);
  171. mealsSubsidy.setIffixed(Globals.FIXED_NO);//0不固定
  172. }else if(Globals.SUBSIDY_TYPE_AYGD.equals(mealsSubsidy.getSubsidyType())){//按月固定
  173. mealsSubsidy.setCalcuUnit(Globals.CALCU_UNIT_yue);
  174. mealsSubsidy.setIffixed(Globals.FIXED_YES);//1固定
  175. }else if(Globals.SUBSIDY_TYPE_ACGD.equals(mealsSubsidy.getSubsidyType())){//按次固定
  176. mealsSubsidy.setCalcuUnit(Globals.CALCU_UNIT_ci);
  177. mealsSubsidy.setIffixed(Globals.FIXED_YES);//1固定
  178. }
  179. mealsSubsidy.setDeleteFlag(Globals.Delete_Normal.toString());
  180. mealsSubsidy.setStatus(Globals.Enabled_Status.toString());
  181. mealsSubsidyService.save(mealsSubsidy);
  182. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  183. message = "餐费补贴添加成功";
  184. }catch(Exception e){
  185. e.printStackTrace();
  186. message = "餐费补贴添加失败";
  187. throw new BusinessException(e.getMessage());
  188. }
  189. }
  190. j.setMsg(message);
  191. return j;
  192. }
  193. /**
  194. * 删除餐费补贴
  195. *
  196. * @return
  197. */
  198. @RequestMapping(params = "doDel")
  199. @ResponseBody
  200. public AjaxJson doDel(MealsSubsidyEntity mealsSubsidy, HttpServletRequest request) {
  201. String message = null;
  202. AjaxJson j = new AjaxJson();
  203. mealsSubsidy = systemService.getEntity(MealsSubsidyEntity.class, mealsSubsidy.getId());
  204. message = "餐费补贴删除成功";
  205. try{
  206. mealsSubsidyService.delete(mealsSubsidy);
  207. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  208. }catch(Exception e){
  209. e.printStackTrace();
  210. message = "餐费补贴删除失败";
  211. throw new BusinessException(e.getMessage());
  212. }
  213. j.setMsg(message);
  214. return j;
  215. }
  216. /**
  217. * 批量删除餐费补贴
  218. *
  219. * @return
  220. */
  221. @RequestMapping(params = "doBatchDel")
  222. @ResponseBody
  223. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  224. String message = null;
  225. AjaxJson j = new AjaxJson();
  226. message = "餐费补贴删除成功";
  227. try{
  228. for(String id:ids.split(",")){
  229. MealsSubsidyEntity mealsSubsidy = systemService.getEntity(MealsSubsidyEntity.class, id);
  230. mealsSubsidyService.delete(mealsSubsidy);
  231. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  232. }
  233. }catch(Exception e){
  234. e.printStackTrace();
  235. message = "餐费补贴删除失败";
  236. throw new BusinessException(e.getMessage());
  237. }
  238. j.setMsg(message);
  239. return j;
  240. }
  241. /**
  242. * 批量逻辑删除餐费补贴
  243. *
  244. * @return
  245. */
  246. @RequestMapping(params = "doBatchLogicDel")
  247. @ResponseBody
  248. public AjaxJson doBatchLogicDel(String ids,HttpServletRequest request){
  249. String message = null;
  250. AjaxJson j = new AjaxJson();
  251. message = "餐费补贴删除成功";
  252. List<MealsSubsidyEntity> pojoList = new ArrayList<MealsSubsidyEntity>();
  253. try{
  254. for(String id:ids.split(",")){
  255. MealsSubsidyEntity mealsSubsidy = systemService.getEntity(MealsSubsidyEntity.class, id);
  256. mealsSubsidy.setDeleteFlag(Globals.Delete_Forbidden.toString());// 设置为1已删除
  257. pojoList.add(mealsSubsidy);
  258. }
  259. mealsSubsidyService.batchUpdate(pojoList);// 执行批量更新
  260. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  261. }catch(Exception e){
  262. e.printStackTrace();
  263. message = "餐费补贴删除失败";
  264. throw new BusinessException(e.getMessage());
  265. }
  266. j.setMsg(message);
  267. return j;
  268. }
  269. /**
  270. * 启用餐费补贴
  271. *
  272. * @return
  273. */
  274. @RequestMapping(params = "doEnableObj")
  275. @ResponseBody
  276. public AjaxJson doEnableObj(String ids,HttpServletRequest request){
  277. String message = null;
  278. AjaxJson j = new AjaxJson();
  279. message = "餐费补贴启用成功";
  280. List<MealsSubsidyEntity> pojoList = new ArrayList<MealsSubsidyEntity>();
  281. try{
  282. for(String id:ids.split(",")){
  283. MealsSubsidyEntity mealsSubsidy = systemService.getEntity(MealsSubsidyEntity.class, id);
  284. mealsSubsidy.setStatus(Globals.Enabled_Status.toString());// 设置为0启用
  285. pojoList.add(mealsSubsidy);
  286. }
  287. mealsSubsidyService.batchUpdate(pojoList);// 执行批量更新
  288. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  289. }catch(Exception e){
  290. e.printStackTrace();
  291. message = "餐费补贴启用失败";
  292. throw new BusinessException(e.getMessage());
  293. }
  294. j.setMsg(message);
  295. return j;
  296. }
  297. /**
  298. * 停用餐费补贴
  299. *
  300. * @return
  301. */
  302. @RequestMapping(params = "doDisableObj")
  303. @ResponseBody
  304. public AjaxJson doDisableObj(String ids,HttpServletRequest request){
  305. String message = null;
  306. AjaxJson j = new AjaxJson();
  307. message = "餐费补贴停用成功";
  308. List<MealsSubsidyEntity> pojoList = new ArrayList<MealsSubsidyEntity>();
  309. try{
  310. for(String id:ids.split(",")){
  311. MealsSubsidyEntity mealsSubsidy = systemService.getEntity(MealsSubsidyEntity.class, id);
  312. mealsSubsidy.setStatus(Globals.Disabled_Status.toString());// 设置为1停用
  313. pojoList.add(mealsSubsidy);
  314. }
  315. mealsSubsidyService.batchUpdate(pojoList);// 执行批量更新
  316. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  317. }catch(Exception e){
  318. e.printStackTrace();
  319. message = "餐费补贴停用失败";
  320. throw new BusinessException(e.getMessage());
  321. }
  322. j.setMsg(message);
  323. return j;
  324. }
  325. /**
  326. * 补贴名称重复校验
  327. *
  328. * @param ids
  329. * @return
  330. */
  331. @RequestMapping(params = "checkName")
  332. @ResponseBody
  333. public Map<String,Object> checkName(MealsSubsidyEntity mealsSubsidy, HttpServletRequest request) {
  334. Boolean flag = mealsSubsidyService.findByName(mealsSubsidy);
  335. Map<String,Object> res = new HashMap<String, Object>();
  336. res.put("flag",flag);
  337. return res;
  338. }
  339. /**
  340. * 添加餐费补贴
  341. *
  342. * @param ids
  343. * @return
  344. */
  345. @RequestMapping(params = "doAdd")
  346. @ResponseBody
  347. public AjaxJson doAdd(MealsSubsidyEntity mealsSubsidy, HttpServletRequest request) {
  348. String message = null;
  349. AjaxJson j = new AjaxJson();
  350. message = "餐费补贴添加成功";
  351. try{
  352. mealsSubsidy.setDeleteFlag(Globals.Delete_Normal.toString());
  353. mealsSubsidy.setStatus(Globals.Enabled_Status.toString());
  354. mealsSubsidyService.save(mealsSubsidy);
  355. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  356. }catch(Exception e){
  357. e.printStackTrace();
  358. message = "餐费补贴添加失败";
  359. throw new BusinessException(e.getMessage());
  360. }
  361. j.setMsg(message);
  362. return j;
  363. }
  364. /**
  365. * 更新餐费补贴
  366. *
  367. * @param ids
  368. * @return
  369. */
  370. @RequestMapping(params = "doUpdate")
  371. @ResponseBody
  372. public AjaxJson doUpdate(MealsSubsidyEntity mealsSubsidy, HttpServletRequest request) {
  373. String message = null;
  374. AjaxJson j = new AjaxJson();
  375. message = "餐费补贴更新成功";
  376. MealsSubsidyEntity t = mealsSubsidyService.get(MealsSubsidyEntity.class, mealsSubsidy.getId());
  377. try {
  378. MyBeanUtils.copyBeanNotNull2Bean(mealsSubsidy, t);
  379. mealsSubsidyService.saveOrUpdate(t);
  380. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  381. } catch (Exception e) {
  382. e.printStackTrace();
  383. message = "餐费补贴更新失败";
  384. throw new BusinessException(e.getMessage());
  385. }
  386. j.setMsg(message);
  387. return j;
  388. }
  389. /**
  390. * 餐费补贴新增页面跳转
  391. *
  392. * @return
  393. */
  394. @RequestMapping(params = "goAdd")
  395. public ModelAndView goAdd(MealsSubsidyEntity mealsSubsidy, HttpServletRequest req) {
  396. if (StringUtil.isNotEmpty(mealsSubsidy.getId())) {
  397. mealsSubsidy = mealsSubsidyService.getEntity(MealsSubsidyEntity.class, mealsSubsidy.getId());
  398. req.setAttribute("mealsSubsidyPage", mealsSubsidy);
  399. }
  400. return new ModelAndView("cn/com/lzt/mealssubsidy/mealsSubsidy-add");
  401. }
  402. /**
  403. * 餐费补贴编辑页面跳转
  404. *
  405. * @return
  406. */
  407. @RequestMapping(params = "goUpdate")
  408. public ModelAndView goUpdate(MealsSubsidyEntity mealsSubsidy, HttpServletRequest req) {
  409. if (StringUtil.isNotEmpty(mealsSubsidy.getId())) {
  410. mealsSubsidy = mealsSubsidyService.getEntity(MealsSubsidyEntity.class, mealsSubsidy.getId());
  411. req.setAttribute("mealsSubsidyPage", mealsSubsidy);
  412. }
  413. return new ModelAndView("cn/com/lzt/mealssubsidy/mealsSubsidy-update");
  414. }
  415. /**
  416. * 导入功能跳转
  417. *
  418. * @return
  419. */
  420. @RequestMapping(params = "upload")
  421. public ModelAndView upload(HttpServletRequest req) {
  422. req.setAttribute("controller_name","mealsSubsidyController");
  423. return new ModelAndView("common/upload/pub_excel_upload");
  424. }
  425. /**
  426. * 导出excel
  427. *
  428. * @param request
  429. * @param response
  430. */
  431. @RequestMapping(params = "exportXls")
  432. public String exportXls(MealsSubsidyEntity mealsSubsidy,HttpServletRequest request,HttpServletResponse response
  433. , DataGrid dataGrid,ModelMap modelMap) {
  434. CriteriaQuery cq = new CriteriaQuery(MealsSubsidyEntity.class, dataGrid);
  435. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mealsSubsidy, request.getParameterMap());
  436. List<MealsSubsidyEntity> mealsSubsidys = this.mealsSubsidyService.getListByCriteriaQuery(cq,false);
  437. modelMap.put(NormalExcelConstants.FILE_NAME,"餐费补贴");
  438. modelMap.put(NormalExcelConstants.CLASS,MealsSubsidyEntity.class);
  439. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("餐费补贴列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  440. "导出信息"));
  441. modelMap.put(NormalExcelConstants.DATA_LIST,mealsSubsidys);
  442. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  443. }
  444. /**
  445. * 导出excel 使模板
  446. *
  447. * @param request
  448. * @param response
  449. */
  450. @RequestMapping(params = "exportXlsByT")
  451. public String exportXlsByT(MealsSubsidyEntity mealsSubsidy,HttpServletRequest request,HttpServletResponse response
  452. , DataGrid dataGrid,ModelMap modelMap) {
  453. modelMap.put(NormalExcelConstants.FILE_NAME,"餐费补贴");
  454. modelMap.put(NormalExcelConstants.CLASS,MealsSubsidyEntity.class);
  455. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("餐费补贴列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  456. "导出信息"));
  457. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  458. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  459. }
  460. @SuppressWarnings("unchecked")
  461. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  462. @ResponseBody
  463. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  464. AjaxJson j = new AjaxJson();
  465. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  466. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  467. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  468. MultipartFile file = entity.getValue();// 获取上传文件对象
  469. ImportParams params = new ImportParams();
  470. params.setTitleRows(2);
  471. params.setHeadRows(1);
  472. params.setNeedSave(true);
  473. try {
  474. List<MealsSubsidyEntity> listMealsSubsidyEntitys = ExcelImportUtil.importExcel(file.getInputStream(),MealsSubsidyEntity.class,params);
  475. for (MealsSubsidyEntity mealsSubsidy : listMealsSubsidyEntitys) {
  476. mealsSubsidyService.save(mealsSubsidy);
  477. }
  478. j.setMsg("文件导入成功!");
  479. } catch (Exception e) {
  480. j.setMsg("文件导入失败!");
  481. logger.error(ExceptionUtil.getExceptionMessage(e));
  482. }finally{
  483. try {
  484. file.getInputStream().close();
  485. } catch (IOException e) {
  486. e.printStackTrace();
  487. }
  488. }
  489. }
  490. return j;
  491. }
  492. @RequestMapping(method = RequestMethod.GET)
  493. @ResponseBody
  494. public List<MealsSubsidyEntity> list() {
  495. List<MealsSubsidyEntity> listMealsSubsidys=mealsSubsidyService.getList(MealsSubsidyEntity.class);
  496. return listMealsSubsidys;
  497. }
  498. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  499. @ResponseBody
  500. public ResponseEntity<?> get(@PathVariable("id") String id) {
  501. MealsSubsidyEntity task = mealsSubsidyService.get(MealsSubsidyEntity.class, id);
  502. if (task == null) {
  503. return new ResponseEntity(HttpStatus.NOT_FOUND);
  504. }
  505. return new ResponseEntity(task, HttpStatus.OK);
  506. }
  507. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  508. @ResponseBody
  509. public ResponseEntity<?> create(@RequestBody MealsSubsidyEntity mealsSubsidy, UriComponentsBuilder uriBuilder) {
  510. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  511. Set<ConstraintViolation<MealsSubsidyEntity>> failures = validator.validate(mealsSubsidy);
  512. if (!failures.isEmpty()) {
  513. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  514. }
  515. //保存
  516. try{
  517. mealsSubsidyService.save(mealsSubsidy);
  518. } catch (Exception e) {
  519. e.printStackTrace();
  520. return new ResponseEntity(HttpStatus.NO_CONTENT);
  521. }
  522. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  523. String id = mealsSubsidy.getId();
  524. URI uri = uriBuilder.path("/rest/mealsSubsidyController/" + id).build().toUri();
  525. HttpHeaders headers = new HttpHeaders();
  526. headers.setLocation(uri);
  527. return new ResponseEntity(headers, HttpStatus.CREATED);
  528. }
  529. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  530. public ResponseEntity<?> update(@RequestBody MealsSubsidyEntity mealsSubsidy) {
  531. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  532. Set<ConstraintViolation<MealsSubsidyEntity>> failures = validator.validate(mealsSubsidy);
  533. if (!failures.isEmpty()) {
  534. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  535. }
  536. //保存
  537. try{
  538. mealsSubsidyService.saveOrUpdate(mealsSubsidy);
  539. } catch (Exception e) {
  540. e.printStackTrace();
  541. return new ResponseEntity(HttpStatus.NO_CONTENT);
  542. }
  543. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  544. return new ResponseEntity(HttpStatus.NO_CONTENT);
  545. }
  546. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  547. @ResponseStatus(HttpStatus.NO_CONTENT)
  548. public void delete(@PathVariable("id") String id) {
  549. mealsSubsidyService.deleteEntityById(MealsSubsidyEntity.class, id);
  550. }
  551. }