许鹏程
2023-05-24 5839384228e2f10d880d23c7a1c99ba86430634c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.product.admin.service;
 
import com.google.common.collect.Maps;
import com.product.admin.config.CmnConst;
import com.product.admin.entity.FaceListEntity;
import com.product.admin.service.idel.ISystemFaceService;
import com.product.common.lang.StringUtils;
import com.product.core.cache.util.RedisUtil;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.service.support.QueryFilterService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
 
/**
 * 表单配置业务层
 *
 * @author cheng
 */
@Service
public class SystemFaceService extends AbstractBaseService implements ISystemFaceService {
 
    /**
     * 缓存起始Key
     */
    public static final String FACE_LIST_KEY = "face:fieldList:";
 
 
    @Autowired
    QueryFilterService queryFilterService;
 
    @Autowired
    RolesService rolesService;
 
    /**
     * 获取模块功能树
     *
     * @return
     * @throws BaseException
     */
    @Override
    public DataTableEntity getModuleFunctionTree() throws BaseException {
 
        return rolesService.getModuleFunctions();
    }
 
    /**
     * 表单列表
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Override
    public DataTableEntity getFaceList(FieldSetEntity fse) throws BaseException {
        String queryFilter = queryFilterService.getQueryFilter(fse);
        String filter = fse.getString("filter");
        if (StringUtils.isEmpty(filter)) {
            filter = queryFilter;
        } else if (!StringUtils.isEmpty(queryFilter)) {
            filter += " and (" + queryFilter + ") ";
        }
        DataTableEntity dataTableEntity = getBaseDao().listTable(CmnConst.PRODUCT_SYS_FACE, filter, new Object[]{}, null, null, fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE));
        getBaseDao().loadPromptData(dataTableEntity);
        return dataTableEntity;
    }
 
    /**
     * 获取表单详情根据uuid
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Override
    public FieldSetEntity getFaceInfoByUuid(FieldSetEntity fse) throws BaseException {
        return getBaseDao().getFieldSetEntity(CmnConst.PRODUCT_SYS_FACE, fse.getUUID(), true);
    }
 
    /**
     * 保存表单详情根据uuid
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    @Override
    @Transactional
    public String saveFaceInfo(FieldSetEntity fse) throws BaseException {
        BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), fse);
        Map<String, DataTableEntity> subData = fse.getSubData();
        subData.forEach((k, v) -> {
            BaseUtil.createCreatorAndCreationTime(SpringMVCContextHolder.getCurrentUser(), v);
        });
        getBaseDao().saveFieldSetEntity(fse);
        this.initialFieldCache(fse.getUUID());
        return fse.getUUID();
    }
 
    /**
     * 删除表单详情根据uuid
     *
     * @param uuids
     * @return
     * @throws BaseException
     */
    @Override
    @Transactional
    public boolean deleteFaceInfoByUuids(String[] uuids) throws BaseException {
        boolean delete = getBaseDao().delete(CmnConst.PRODUCT_SYS_FACE, uuids);
        deleteFieldCache(uuids);
        return delete;
    }
 
    @Override
    public FaceListEntity getFaceFieldList(String face_uuid) throws BaseException {
        if (StringUtils.isEmpty(face_uuid)) {
            return null;
        }
        if (!RedisUtil.exists(this.FACE_LIST_KEY + face_uuid)) {
            this.initialFieldCache(face_uuid);
        }
        FaceListEntity faceListEntity = (FaceListEntity) RedisUtil.get(this.FACE_LIST_KEY + face_uuid);
        if (faceListEntity == null) {
            initialFieldCache(face_uuid);
            faceListEntity = (FaceListEntity) RedisUtil.get(this.FACE_LIST_KEY + face_uuid);
        }
        return faceListEntity;
    }
 
    /**
     * 获取列表过滤条件
     *
     * @param aliasTable 表别名集合
     * @param face_uuid  表单uuid
     * @param val        模糊搜索值
     * @return sql过滤条件
     * @throws BaseException
     */
    @Override
    public String getListFilter(Map<String, List<String>> aliasTable, Map<String, List<String>> aliasField, String face_uuid, String val) throws BaseException {
        if (StringUtils.isEmpty(val)) {
            return "";
        }
        FaceListEntity face = (FaceListEntity) RedisUtil.get(this.FACE_LIST_KEY + face_uuid);
        if (face != null) {
            String tableAlias = null;
            String tableName = face.getTableName();
            List<String> t = aliasTable.get(tableName);
            if (t != null && t.size() > 0) {
                tableAlias = t.get(0);
            }
            Map<String, List<String>> params = face.getFilters(tableAlias, aliasField);
            List<String> fieldNames = params.get("fields");
            List<String> filters = params.get("filter");
            String filter = "";
            for (int i = 0; i < fieldNames.size(); i++) {
                if (i > 0) {
                    filter += " or ";
                }
                filter += " " + fieldNames.get(i) + " LIKE BINARY concat('%','" + val + "','%')";
            }
            for (int i = 0; i < filters.size(); i++) {
                if (!"".equals(filter)) {
                    filter += " or ";
                }
                filter += filters.get(i).replace("~val~", val);
            }
            return "".equals(filter) ? "" : "( " + filter + " )";
        }
        return "";
    }
 
    /**
     * 删除表单字段缓存根据表单uuid
     *
     * @param uuid
     */
    private void deleteFieldCache(String[] uuid) {
        if (uuid != null && uuid.length > 0) {
            String[] keys = new String[uuid.length];
            for (int i = 0; i < uuid.length; i++) {
                keys[i] = this.FACE_LIST_KEY + uuid[i];
            }
            if (keys.length > 0) {
                RedisUtil.del(keys);
            }
        }
    }
 
    /**
     * 初始化表单字段缓存
     *
     * @param faceUuids 表单uuid 多个用逗号分割 传入null 则初始化所有
     */
    private void initialFieldCache(String faceUuids) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ");
        sql.append(" ifnull(d.field_name,a.field_name) field_name,a.field_alias, ");
        sql.append(" d.field_reference, ");
        sql.append(" d.uuid field_uuid, ");
        sql.append(" b.face_name, ");
        sql.append(" b.table_uuid, ");
        sql.append(" a.face_uuid, ");
        sql.append(" column_name, ");
        sql.append(" column_width, ");
        sql.append(" sequence ");
        sql.append(" FROM ");
        sql.append(" product_sys_face_list a ");
        sql.append(" RIGHT JOIN product_sys_face b ON a.face_uuid = b.uuid ");
        sql.append(" LEFT JOIN product_sys_datamodel_field d ON a.field_name = d.field_name and b.table_uuid=d.table_uuid");
        sql.append(" WHERE ");
        sql.append(" a.is_used = 1 ");
        Object[] objects = {};
        if (!StringUtils.isEmpty(faceUuids)) {
            sql.append(" AND ( ");
            objects = faceUuids.split(",");
            sql.append(BaseUtil.buildQuestionMarkFilter("b.uuid", objects.length, true));
            sql.append(" ) ");
        }
        sql.append(" ORDER BY face_uuid,sequence ");
        DataTableEntity dt = getBaseDao().listTable(sql.toString(), objects);
        Map<String, FaceListEntity> faceListEntityMap = Maps.newHashMap();
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            for (int i = 0; i < dt.getRows(); i++) {
                String face_uuid = dt.getString(i, "face_uuid");
                FaceListEntity faceListEntity = faceListEntityMap.get(face_uuid);
                if (faceListEntity == null) {
                    faceListEntity = new FaceListEntity();
                    faceListEntity.setFaceName(face_uuid);
                    faceListEntity.setTableUuid(dt.getString(i, CmnConst.TABLE_UUID));
                    faceListEntity.setUuid(dt.getString(i, "face_uuid"));
                    faceListEntityMap.put(face_uuid, faceListEntity);
                }
                faceListEntity.addField(dt.getString(i, CmnConst.FIELD_NAME), dt.getString(i, "field_uuid"),
                        dt.getString(i, "column_name"),
                        dt.getString(i, "column_width"),
                        dt.getInt(i, CmnConst.SEQUENCE), dt.getString(i, "field_reference"));
 
            }
            if (faceListEntityMap.size() > 0) {
                faceListEntityMap.forEach((f, m) -> {
                    RedisUtil.set(FACE_LIST_KEY + m.getUuid(), m);
                });
            }
        }
    }
 
 
}