ny
23 小时以前 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package jnpf.base.mapper;
 
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.entity.FilterEntity;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.model.filter.RuleInfo;
import jnpf.base.util.OnlineFilterUtil;
import jnpf.database.util.DbTypeUtil;
import jnpf.database.util.DynamicDataSourceUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.context.RequestContext;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
import org.mybatis.dynamic.sql.DerivedColumn;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.where.WhereModel;
import org.springframework.stereotype.Repository;
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Mapper
@Repository
public interface FilterMapper extends SuperMapper<FilterEntity> {
 
    default void saveRuleList(String moduleId, VisualdevEntity visualdevEntity, Integer app, Integer pc, Map<String, String> tableMap) {
        if (MapUtils.isEmpty(tableMap)) return;
        String columnData = visualdevEntity.getColumnData();
        String appColumnData = visualdevEntity.getAppColumnData();
 
        if (columnData == null || columnData.length() == 0) {
            columnData = "{}";
        }
        if (appColumnData == null || appColumnData.length() == 0) {
            appColumnData = "{}";
        }
        Map config = JsonUtil.getJsonToBean(columnData, Map.class);
        String ruleList = JSONUtil.toJsonStr(config.get("ruleList"));
        Map configApp = JsonUtil.getJsonToBean(appColumnData, Map.class);
        String ruleListApp = JSONUtil.toJsonStr(configApp.get("ruleListApp"));
        FilterEntity entity = new FilterEntity();
        entity.setId(RandomUtil.uuId());
        entity.setModuleId(moduleId);
        replaceRealValue(app, pc, tableMap, ruleList, ruleListApp, entity);
        entity.setCreatorTime(new Date());
        entity.setLastModifyTime(new Date());
        this.insert(entity);
    }
 
    default void updateRuleList(String moduleId, VisualdevEntity visualdevEntity, Integer app, Integer pc, Map<String, String> tableMap) {
        if (MapUtils.isEmpty(tableMap)) return;
        String columnData = visualdevEntity.getColumnData();
        String appColumnData = visualdevEntity.getAppColumnData();
        if (columnData == null || columnData.length() == 0) {
            columnData = "{}";
        }
        if (appColumnData == null || appColumnData.length() == 0) {
            appColumnData = "{}";
        }
 
        Map config = JsonUtil.getJsonToBean(columnData, Map.class);
        String ruleList = JSONUtil.toJsonStr(config.get("ruleList"));
        Map configApp = JsonUtil.getJsonToBean(appColumnData, Map.class);
        String ruleListApp = JSONUtil.toJsonStr(configApp.get("ruleListApp"));
 
        List<FilterEntity> list = this.selectList(new QueryWrapper<FilterEntity>().lambda().eq(FilterEntity::getModuleId, moduleId));
        if (list == null || list.size() == 0) {
            this.saveRuleList(moduleId, visualdevEntity, app, pc, tableMap);
        } else {
            FilterEntity entity = list.get(0);
            replaceRealValue(app, pc, tableMap, ruleList, ruleListApp, entity);
            entity.setLastModifyTime(new Date());
            this.updateById(entity);
        }
    }
 
    /**
     * 把子表的表名换成实际数据库表名
     *
     * @param app         是否更新app配置
     * @param pc          是否更新pc配置
     * @param tableMap    虚拟表名和实际表名映射
     * @param ruleList    pc配置
     * @param ruleListApp app配置
     * @param entity      更新的数据
     */
    default void replaceRealValue(Integer app, Integer pc, Map<String, String> tableMap, String ruleList, String ruleListApp, FilterEntity entity) {
        if (app == 1 && StringUtils.isNotBlank(ruleListApp)) {
            for (String key : tableMap.keySet()) {
                ruleListApp = ruleListApp.replaceAll(key, tableMap.get(key));
            }
 
            entity.setConfigApp(ruleListApp);
        }
        if (pc == 1 && StringUtils.isNotBlank(ruleList)) {
            for (String key : tableMap.keySet()) {
                ruleList = ruleList.replaceAll(key, tableMap.get(key));
            }
            entity.setConfig(ruleList);
        }
    }
 
 
    default void handleWhereCondition(SqlTable sqlTable, QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder where, String id, Map<String, SqlTable> subSqlTableMap, String databaseProductName, Map<String, Object> params) {
        try {
            DynamicDataSourceUtil.switchToDataSource(null);
            List<RuleInfo> ruleInfos = this.getCondition(id);
 
            QueryExpressionDSL<SelectModel>.QueryExpressionWhereBuilder whereFilter = SqlBuilder.select(sqlTable.allColumns()).from(sqlTable).where();
 
            for (int i = 0; i < ruleInfos.size(); i++) {
                RuleInfo info = ruleInfos.get(i);
                OnlineFilterUtil genUtil = JsonUtil.getJsonToBean(info, OnlineFilterUtil.class);
                genUtil.setDbType(DbTypeUtil.getDbEncodeByProductName(databaseProductName));
                genUtil.setSubSqlTableMap(subSqlTableMap);
                genUtil.setParams(params);
                genUtil.solveValue(whereFilter, sqlTable);
            }
 
            Method method = whereFilter.getClass().getDeclaredMethod("buildWhereModel");
            method.setAccessible(true);
            WhereModel invoke = (WhereModel) method.invoke(whereFilter);
            List<AndOrCriteriaGroup> groupList = invoke.subCriteria();
            where.and(DerivedColumn.of("1"), SqlBuilder.isEqualTo(1), groupList);
 
 
        } catch (Exception ignored) {
        } finally {
            DynamicDataSourceUtil.clearSwitchDataSource();
        }
 
    }
 
    /**
     * 获取过滤配置
     *
     * @param id
     * @return
     */
    default List<RuleInfo> getCondition(String id) {
        if (StringUtils.isEmpty(id)) {
            return new ArrayList<>();
        }
        QueryWrapper<FilterEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FilterEntity::getModuleId, id);
        FilterEntity entity = this.selectOne(wrapper);
        // 获取app端还是web端
        String config;
        boolean isApp = !RequestContext.isOrignPc();
        if (isApp) {
            config = entity.getConfigApp();
        } else {
            config = entity.getConfig();
        }
 
        List<RuleInfo> ruleInfos = new ArrayList<>();
        if (StringUtils.isNoneBlank(config)) {
            ruleInfos = JsonUtil.getJsonToList(config, RuleInfo.class);
        }
 
        return ruleInfos;
    }
}