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 { @Autowired public IService service; @RequestMapping("/del") @ResponseBody public Object delete(String id){ EntityWrapper 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 warpper = new EntityWrapper(); //.@leon where like,=,<> while (fileterIt.hasNext()){ Map.Entry entray = (Map.Entry) fileterIt.next(); Object key = entray.getKey(); Object value = entray.getValue(); Map 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 page = service.selectPage( new Page(indexHandle.getOffset(),indexHandle.getLimit()),warpper ); Map resMap = new HashMap(); resMap.put("list",page.getRecords()); resMap.put("total",total); return AjaxResult.success(resMap); } }