|
|
@@ -0,0 +1,123 @@
|
|
|
+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.constant.HttpStatus;
|
|
|
+import com.mokamrp.privates.entity.AddHandle;
|
|
|
+import com.mokamrp.privates.entity.DelHandle;
|
|
|
+import com.mokamrp.privates.entity.EditHandle;
|
|
|
+import com.mokamrp.privates.entity.IndexHandle;
|
|
|
+import com.mokamrp.privates.help.AjaxResult;
|
|
|
+import com.mokamrp.privates.mapper.pojo.*;
|
|
|
+import com.mokamrp.privates.utils.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.validation.Valid;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 一个粗糙的鸡肋
|
|
|
+ *
|
|
|
+ * @author gy @since 2021-04-17
|
|
|
+ * @reviser leon @since 2021-04-20
|
|
|
+ */
|
|
|
+public class BaseController<T> {
|
|
|
+ @Autowired
|
|
|
+ public IService<T> service;
|
|
|
+
|
|
|
+ @PostMapping("/del")
|
|
|
+ public Object delete(@Valid @RequestBody DelHandle<T> delHandle, BindingResult bindingResult){
|
|
|
+ if(bindingResult.hasErrors()){
|
|
|
+ return AjaxResult.error(HttpStatus.ERROR,bindingResult.getFieldError().getDefaultMessage());
|
|
|
+ }
|
|
|
+ EntityWrapper<T> entityWrapper = new EntityWrapper<>();
|
|
|
+ entityWrapper.in("id",delHandle.getIds());
|
|
|
+ boolean res = service.delete(entityWrapper);
|
|
|
+ if(!res){
|
|
|
+ return AjaxResult.error("删除失败");
|
|
|
+ }
|
|
|
+ return AjaxResult.success("success",res);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/add")
|
|
|
+ public Object add(@Valid @RequestBody AddHandle<T> addHandle, BindingResult bindingResult){
|
|
|
+ if(bindingResult.hasErrors()){
|
|
|
+ return AjaxResult.error(HttpStatus.ERROR,bindingResult.getFieldError().getDefaultMessage());
|
|
|
+ }
|
|
|
+ Date date = new Date();
|
|
|
+ Boolean res = service.insert(addHandle.getRaw());
|
|
|
+ if (res){
|
|
|
+ return AjaxResult.success("success",res);
|
|
|
+ }else{
|
|
|
+ return AjaxResult.error("新增失败",res);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/edit")
|
|
|
+ public Object edit(@Valid @RequestBody EditHandle<T> editHandle, BindingResult bindingResult){
|
|
|
+ if(bindingResult.hasErrors()){
|
|
|
+ return AjaxResult.error(HttpStatus.ERROR,bindingResult.getFieldError().getDefaultMessage());
|
|
|
+ }
|
|
|
+ service.update(editHandle.getRaw(),new EntityWrapper<T>().in("id",editHandle.getIds()));
|
|
|
+ return AjaxResult.success("success",true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/index")
|
|
|
+ public Object index(@Valid @RequestBody IndexHandle indexHandle, BindingResult bindingResult){
|
|
|
+ if(bindingResult.hasErrors()){
|
|
|
+ return AjaxResult.error(HttpStatus.ERROR,bindingResult.getFieldError().getDefaultMessage());
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+ //.这里sql字段需要驼峰转下划线
|
|
|
+ if(op.get(key) == null || op.get(key).isEmpty() || op.get(key).toString().equals("=")){
|
|
|
+ warpper = warpper.eq(StringUtils.humpToLine2(key.toString()),value.toString());
|
|
|
+ }else if (op.get(key).toString().equals("like")){
|
|
|
+ warpper = warpper.like(StringUtils.humpToLine2(key.toString()),"%"+value.toString()+"%");
|
|
|
+ }else if (op.get(key).toString().equals("<>")){
|
|
|
+ warpper = warpper.ne(StringUtils.humpToLine2(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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|