|
@@ -0,0 +1,76 @@
|
|
|
+package edu.travel.web;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
+import edu.travel.interfaces.InsertGroups;
|
|
|
+import edu.travel.interfaces.UpdateGroups;
|
|
|
+import edu.travel.resp.BaseResponse;
|
|
|
+import edu.travel.resp.PageResponse;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.Errors;
|
|
|
+import org.springframework.validation.ObjectError;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class BaseController<T> {
|
|
|
+ @Autowired
|
|
|
+ private IService<T> service;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用根据updateById 更新
|
|
|
+ * @param entity
|
|
|
+ * @param errors
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("/updateById")
|
|
|
+ public BaseResponse<T> updateById(@Validated(UpdateGroups.class) @RequestBody T entity, Errors errors) {
|
|
|
+ if (errors.hasErrors()) {
|
|
|
+ List<ObjectError> allErrors = errors.getAllErrors();
|
|
|
+ for (ObjectError allError : allErrors) {
|
|
|
+ return PageResponse.out(500,allError.getDefaultMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean update = service.updateById(entity);
|
|
|
+ if (update) {
|
|
|
+ return PageResponse.out(200,"success",entity);
|
|
|
+ }
|
|
|
+ return PageResponse.out(500,"error",entity);
|
|
|
+ }
|
|
|
+ @PostMapping("/save")
|
|
|
+ public BaseResponse<T> save(@Validated(InsertGroups.class) @RequestBody T entity, Errors errors) {
|
|
|
+ if (errors.hasErrors()) {
|
|
|
+ List<ObjectError> allErrors = errors.getAllErrors();
|
|
|
+ for (ObjectError allError : allErrors) {
|
|
|
+ return PageResponse.out(500,allError.getDefaultMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ boolean save = service.save(entity);
|
|
|
+ if (save) {
|
|
|
+ return PageResponse.out(200,"success",entity);
|
|
|
+ }
|
|
|
+ return PageResponse.out(500,"error",entity);
|
|
|
+ }
|
|
|
+ @PostMapping("/deleteById")
|
|
|
+ public BaseResponse<T> deleteById(@RequestBody List<String> ids) {
|
|
|
+ if (ids == null || ids.isEmpty()) {
|
|
|
+ return PageResponse.out(404,"error,not found delete data",null);
|
|
|
+ }
|
|
|
+ boolean byIds = service.removeByIds(ids);
|
|
|
+ if (byIds) {
|
|
|
+ return PageResponse.out(200,"success",null);
|
|
|
+ }
|
|
|
+ return PageResponse.out(500,"error, delete data error",null);
|
|
|
+ }
|
|
|
+ @GetMapping("/list")
|
|
|
+ public BaseResponse<List<T>> list() {
|
|
|
+ List<T> list = service.list();
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return PageResponse.out(200,"success",null);
|
|
|
+ }
|
|
|
+ return PageResponse.out(404,"error,not found delete data",list);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|