| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package com.mokamrp.privates.controller;
- import com.baomidou.mybatisplus.mapper.EntityWrapper;
- import com.baomidou.mybatisplus.mapper.Wrapper;
- import com.baomidou.mybatisplus.plugins.Page;
- import com.baomidou.mybatisplus.service.IService;
- import com.mokamrp.privates.entity.IndexHandle;
- import com.mokamrp.privates.help.AjaxResult;
- import com.mokamrp.privates.mapper.pojo.*;
- import com.mokamrp.privates.service.TagService;
- import com.mokamrp.privates.service.WorkRoomService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import javax.validation.Valid;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- /**
- * 一个粗糙的鸡肋
- *
- * @author gy
- * @since 2021-04-17
- */
- public class BaseController<T> {
- @Autowired
- public IService<T> service;
- @RequestMapping("/del")
- @ResponseBody
- public Object delete(String id){
- EntityWrapper<T> entityWrapper = new EntityWrapper<>();
- System.out.println(id);
- entityWrapper.eq("id",id);
- boolean obj = service.delete(entityWrapper);
- if(!obj){
- return AjaxResult.error("删除失败");
- }
- return AjaxResult.success("success",obj);
- }
- @PostMapping("/add")
- public Object add(@RequestBody T body){
- service.insert(body);
- return AjaxResult.success("success","ok");
- }
- @PostMapping("/edit")
- public Object edit(@RequestBody T body){
- service.updateById(body);
- return AjaxResult.success("success","ok");
- }
- @PostMapping("/index")
- public Object index(@Valid @RequestBody IndexHandle indexHandle){
- Iterator fileterIt = indexHandle.getFilter().entrySet().iterator();
- Wrapper<T> warpper = new EntityWrapper<T>();
- //.@leon where like,=,<>
- while (fileterIt.hasNext()){
- Map.Entry entray = (Map.Entry) fileterIt.next();
- Object key = entray.getKey();
- Object value = entray.getValue();
- Map<String,String> op = indexHandle.getOp();
- if(op.get(key) == null || op.get(key).isEmpty() || op.get(key).toString().equals("=")){
- warpper = warpper.eq(key.toString(),value.toString());
- }else if (op.get(key).toString().equals("like")){
- warpper = warpper.like(key.toString(),"%"+value.toString()+"%");
- }else if (op.get(key).toString().equals("<>")){
- warpper = warpper.ne(key.toString(),value.toString());
- }
- }
- //使用相同条件
- Integer total = service.selectCount(warpper);
- //.@leon order
- if (indexHandle.getOrder() != null && indexHandle.getSort() != null){
- String order = indexHandle.getOrder();
- String sort = indexHandle.getSort();
- if (!order.isEmpty() && !sort.isEmpty()){
- if (order.equals("asc")){
- warpper = warpper.orderBy(sort,true);
- }else if(order.equals("desc")){
- warpper = warpper.orderBy(sort,false);
- }
- }
- }
- //.@leon offset limit
- Page<T> page = service.selectPage(
- new Page<T>(indexHandle.getOffset(),indexHandle.getLimit()),warpper
- );
- Map<String, Object> resMap = new HashMap<String,Object>();
- resMap.put("list",page.getRecords());
- resMap.put("total",total);
- return AjaxResult.success(resMap);
- }
- }
|