MealsDeductController.java 20 KB

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