BaseController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.mokamrp.privates.controller;
  2. import com.baomidou.mybatisplus.mapper.EntityWrapper;
  3. import com.baomidou.mybatisplus.mapper.Wrapper;
  4. import com.baomidou.mybatisplus.plugins.Page;
  5. import com.baomidou.mybatisplus.service.IService;
  6. import com.mokamrp.privates.entity.IndexHandle;
  7. import com.mokamrp.privates.help.AjaxResult;
  8. import com.mokamrp.privates.mapper.pojo.*;
  9. import com.mokamrp.privates.service.TagService;
  10. import com.mokamrp.privates.service.WorkRoomService;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.*;
  13. import javax.validation.Valid;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. /**
  18. * 一个粗糙的鸡肋
  19. *
  20. * @author gy
  21. * @since 2021-04-17
  22. */
  23. public class BaseController<T> {
  24. @Autowired
  25. public IService<T> service;
  26. @RequestMapping("/del")
  27. @ResponseBody
  28. public Object delete(String id){
  29. EntityWrapper<T> entityWrapper = new EntityWrapper<>();
  30. System.out.println(id);
  31. entityWrapper.eq("id",id);
  32. boolean obj = service.delete(entityWrapper);
  33. if(!obj){
  34. return AjaxResult.error("删除失败");
  35. }
  36. return AjaxResult.success("success",obj);
  37. }
  38. @PostMapping("/add")
  39. public Object add(@RequestBody T body){
  40. service.insert(body);
  41. return AjaxResult.success("success","ok");
  42. }
  43. @PostMapping("/edit")
  44. public Object edit(@RequestBody T body){
  45. service.updateById(body);
  46. return AjaxResult.success("success","ok");
  47. }
  48. @PostMapping("/index")
  49. public Object index(@Valid @RequestBody IndexHandle indexHandle){
  50. Iterator fileterIt = indexHandle.getFilter().entrySet().iterator();
  51. Wrapper<T> warpper = new EntityWrapper<T>();
  52. //.@leon where like,=,<>
  53. while (fileterIt.hasNext()){
  54. Map.Entry entray = (Map.Entry) fileterIt.next();
  55. Object key = entray.getKey();
  56. Object value = entray.getValue();
  57. Map<String,String> op = indexHandle.getOp();
  58. if(op.get(key) == null || op.get(key).isEmpty() || op.get(key).toString().equals("=")){
  59. warpper = warpper.eq(key.toString(),value.toString());
  60. }else if (op.get(key).toString().equals("like")){
  61. warpper = warpper.like(key.toString(),"%"+value.toString()+"%");
  62. }else if (op.get(key).toString().equals("<>")){
  63. warpper = warpper.ne(key.toString(),value.toString());
  64. }
  65. }
  66. //使用相同条件
  67. Integer total = service.selectCount(warpper);
  68. //.@leon order
  69. if (indexHandle.getOrder() != null && indexHandle.getSort() != null){
  70. String order = indexHandle.getOrder();
  71. String sort = indexHandle.getSort();
  72. if (!order.isEmpty() && !sort.isEmpty()){
  73. if (order.equals("asc")){
  74. warpper = warpper.orderBy(sort,true);
  75. }else if(order.equals("desc")){
  76. warpper = warpper.orderBy(sort,false);
  77. }
  78. }
  79. }
  80. //.@leon offset limit
  81. Page<T> page = service.selectPage(
  82. new Page<T>(indexHandle.getOffset(),indexHandle.getLimit()),warpper
  83. );
  84. Map<String, Object> resMap = new HashMap<String,Object>();
  85. resMap.put("list",page.getRecords());
  86. resMap.put("total",total);
  87. return AjaxResult.success(resMap);
  88. }
  89. }