From d3bf3d3917d8201d64e8e2c66e99afb7099487f9 Mon Sep 17 00:00:00 2001
From: zhang <zc857179121@qq.com>
Date: 星期四, 23 十月 2025 17:01:23 +0800
Subject: [PATCH] 13
---
src/main/java/com/zy/common/web/BaseController.java | 128 +++++++++++++++++++++++++++++++++++++-----
1 files changed, 112 insertions(+), 16 deletions(-)
diff --git a/src/main/java/com/zy/common/web/BaseController.java b/src/main/java/com/zy/common/web/BaseController.java
index af55b2b..06a2bf5 100644
--- a/src/main/java/com/zy/common/web/BaseController.java
+++ b/src/main/java/com/zy/common/web/BaseController.java
@@ -2,11 +2,18 @@
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
+import com.baomidou.mybatisplus.plugins.Page;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.controller.AbstractBaseController;
import com.core.exception.CoolException;
+import com.zy.asrs.entity.Node;
+import com.zy.asrs.entity.Tag;
+import com.zy.asrs.service.NodeService;
+import com.zy.asrs.service.TagService;
import com.zy.system.entity.User;
+import com.zy.system.entity.UserLogin;
+import com.zy.system.service.UserLoginService;
import com.zy.system.service.UserService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,25 +21,58 @@
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
/**
* Created by vincent on 2019-09-09
*/
public class BaseController extends AbstractBaseController {
+ protected static final String RANGE_TIME_LINK = " - ";
+
@Autowired
protected HttpServletRequest request;
@Autowired
private UserService userService;
+ @Autowired
+ private TagService tagService;
+ @Autowired
+ private NodeService nodeService;
+ @Autowired
+ private UserLoginService userLoginService;
- protected Long getUserId(){
+ protected Node getOriginNode() {
+ Node node = nodeService.getTop();
+ if (node == null) {
+ throw new CoolException("搴撳尯鏁版嵁閿欒");
+ }
+ return node;
+ }
+
+ protected Long getHostId() {
+ if (getUserId() == 9527) {
+ return null;
+ }
+ User user = getUser();
+ if (user.getRoleId() == 1) {
+ String hostId = String.valueOf(request.getAttribute("hostId"));
+ if (Cools.isEmpty(hostId)) {
+ UserLogin userLogin = userLoginService.selectOne(new EntityWrapper<UserLogin>().eq("user_id", user.getId()));
+ if (userLogin != null) {
+ return userLogin.getHostId();
+ }
+ }
+ return Long.parseLong(hostId);
+ } else {
+ return user.getHostId();
+ }
+ }
+
+ protected Long getUserId() {
return Long.parseLong(String.valueOf(request.getAttribute("userId")));
}
- protected User getUser(){
+ protected User getUser() {
User user = userService.selectById(getUserId());
if (null == user) {
throw new CoolException(BaseRes.DENIED);
@@ -40,31 +80,87 @@
return user;
}
- protected String getComment(Class<?> cls, String fieldName){
+ protected String getComment(Class<?> cls, String fieldName) {
Field[] fields = Cools.getAllFields(cls);
- for (Field field : fields){
- if (fieldName.equals(field.getName())){
+ for (Field field : fields) {
+ if (fieldName.equals(field.getName())) {
return field.getAnnotation(ApiModelProperty.class).value();
}
}
return "";
}
+ protected Tag getOriginTag() {
+ return tagService.getTop();
+ }
+
+ protected <T> void hostEq(EntityWrapper<T> wrapper) {
+ Long hostId = getHostId();
+ if (hostId != null) {
+ wrapper.eq("host_id", hostId);
+ }
+ }
+
+ /**
+ * 鍒嗛〉缁勮
+ *
+ * @param pageNumber
+ * @param pageSize
+ * @param map
+ * @param cls
+ * @param <T>
+ * @return
+ */
+ protected <T> Page<T> toPage(Integer pageNumber, Integer pageSize, Map<String, Object> map, Class<T> cls) {
+ // 鍒嗛〉绱㈠紩鍜屽崟椤垫暟閲忕粍瑁�
+ pageNumber = Optional.ofNullable(pageNumber).orElse(1);
+ pageSize = Optional.ofNullable(pageSize).orElse(10);
+ Page<T> page = new Page<>(pageNumber, pageSize);
+ map.put("pageNumber", pageNumber);
+ map.put("pageSize", pageSize);
+
+ // 鍏ㄥ瓧娈垫ā绯婃悳绱� todo
+ if (!Cools.isEmpty(map.get("condition"))) {
+ Set<String> columns = new HashSet<>();
+ for (Field field : Cools.getAllFields(cls)) {
+ if (Modifier.isFinal(field.getModifiers())
+ || Modifier.isStatic(field.getModifiers())
+ || Modifier.isTransient(field.getModifiers())) {
+ continue;
+ }
+ String column = null;
+ if (field.isAnnotationPresent(TableField.class)) {
+ column = field.getAnnotation(TableField.class).value();
+ }
+ if (Cools.isEmpty(column)) {
+ column = field.getName();
+ }
+ if (!map.keySet().contains(column)) {
+ columns.add(column);
+ }
+ }
+ columns.forEach(col -> map.put(col, map.get("condition")));
+ }
+ page.setCondition(map);
+ return page;
+ }
+
/**
* 鍏ㄥ瓧娈垫ā绯婃悳绱�
- * @param cls 妯″瀷绫�
- * @param set 鎺掗櫎瀛楁闆嗗悎
+ *
+ * @param cls 妯″瀷绫�
+ * @param set 鎺掗櫎瀛楁闆嗗悎
* @param condition 鎼滅储鍐呭
*/
- protected <T> void allLike(Class<T> cls, Set<String> set, EntityWrapper<T> wrapper, String condition){
+ protected <T> void allLike(Class<T> cls, Set<String> set, EntityWrapper<T> wrapper, String condition) {
if (Cools.isEmpty(condition)) {
return;
}
List<String> columns = new ArrayList<>();
- for (Field field :Cools.getAllFields(cls)){
+ for (Field field : Cools.getAllFields(cls)) {
if (Modifier.isFinal(field.getModifiers())
|| Modifier.isStatic(field.getModifiers())
- || Modifier.isTransient(field.getModifiers())){
+ || Modifier.isTransient(field.getModifiers())) {
continue;
}
String column = null;
@@ -81,9 +177,9 @@
if (columns.isEmpty()) {
return;
}
- for (int i=0;i<columns.size();i++){
- if (i==0){
- wrapper.and();
+ for (int i = 0; i < columns.size(); i++) {
+ if (i == 0) {
+ wrapper.andNew();
} else {
wrapper.or();
}
--
Gitblit v1.9.1