许鹏程
2023-05-25 213cc37cbf0b2515a4de56cc1e01813211bad183
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.product.admin.service;
 
import com.alibaba.fastjson.JSONObject;
import com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.common.lang.StringUtils;
import com.product.core.cache.util.RedisUtil;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldMetaEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.entity.RequestParameterEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.file.service.FileManagerService;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.util.HashMap;
import java.util.Map;
 
@Service("userOperService")
public class UserService extends AbstractBaseService {
 
    @Autowired
    FileManagerService fileManagerService;
 
    @Autowired
    UpdateLoginUserInfoService updateLoginUserInfoService;
 
    public static final String USER_SIGNATURE_KEY = "user:signature:";
 
    public static final String USER_AVATAR_KEY = "user:avatar:";
 
    /**
     * 刷新所有缓存
     */
    public void cacheUserAvatar() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT a.user_id,a.user_name,b.* FROM product_sys_users a ")
                .append(" join product_sys_attachments b on ")
                .append(" length(").append(CmnConst.THUMBNAIL_IMG)
                .append(")>0  ")
                .append(" and a.thumbnail_img = b.uuid ");
        DataTableEntity dt = getBaseDao().listTable(sql.toString(), new Object[]{});
        cacheUserAvatar(dt);
    }
 
    public void cacheUserAvatar(DataTableEntity dt) {
        Map<String, String> map = new HashMap<>();
        if (!DataTableEntity.isEmpty(dt)) {
            for (int i = 0; i < dt.getRows(); i++) {
                String user_id = dt.getString(i, CmnConst.USER_ID);
                String img_uuid = dt.getString(i, CmnConst.UUID);
                if (!StringUtils.isEmpty(user_id) && !StringUtils.isEmpty(img_uuid)) {
                    if (isExistAvatar(user_id, img_uuid)) {
                        dt.removeFieldSetEntity(i);
                        i--;
                    }
                    map.put(user_id, img_uuid);
                }
            }
            if (!DataTableEntity.isEmpty(dt)) {
                Map<String, String> userAvatar = getUserAvatar(dt);
                if (userAvatar != null && !userAvatar.isEmpty()) {
                    userAvatar.forEach((k, v) -> {
                        String img_uuid = map.get(k);
                        if (img_uuid != null) {
                            setUserAvatar(k, v, img_uuid);
                        }
                    });
                }
            }
        }
    }
 
    /**
     * 刷新缓存
     *
     * @param uuid user表uuid
     */
    public void cacheUserAvatar(String uuid) {
        if (!StringUtils.isEmpty(uuid)) {
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT a.user_id,b.* FROM product_sys_users a ")
                    .append(" join product_sys_attachments b on ")
                    .append(" a.uuid=? ")
                    .append(" and a.thumbnail_img = b.uuid ");
            DataTableEntity dt = getBaseDao().listTable(sql.toString(), new Object[]{uuid});
            cacheUserAvatar(dt);
        }
    }
 
    /**
     * 将用户头衔存到redis 中
     *
     * @param user_id 用户id
     * @param img     图片base64字符串
     * @param imgUuid 头像附件表uuid
     */
    private void setUserAvatar(String user_id, String img, String imgUuid) {
        RedisUtil.set(this.USER_AVATAR_KEY + user_id, new String[]{img, imgUuid});
    }
 
    /**
     * 在缓存中获取用户头像
     *
     * @param user_id 用户id
     * @return
     */
    public String getUserAvatar(String user_id) {
        Object o = RedisUtil.get(this.USER_AVATAR_KEY + user_id);
        if (o != null) {
            String[] o1 = (String[]) o;
            if (o1.length == 2) {
                return o1[0];
            }
        }
        return null;
    }
 
    /**
     * 判断用户头像是否存在
     *
     * @param user_id 用户id
     * @param imgUuid 用户头像附件uuid
     * @return
     */
    public boolean isExistAvatar(String user_id, String imgUuid) {
        if (!StringUtils.isEmpty(user_id) && !StringUtils.isEmpty(imgUuid)) {
            Object o = RedisUtil.get(this.USER_AVATAR_KEY + user_id);
            if (o != null) {
                String[] o1 = (String[]) o;
                if (o1.length == 2) {
                    return o1[1].equals(imgUuid);
                }
            }
        }
        return false;
    }
 
    public void initUserSignature() {
        DataTableEntity dataTableEntity = getBaseDao().listTable(CmnConst.PRODUCT_SYS_USERS, "length(user_signature)>0", null, new Object[]{CmnConst.UUID, CmnConst.USER_ID, CmnConst.USER_SIGNATURE});
        if (!BaseUtil.dataTableIsEmpty(dataTableEntity)) {
            for (int i = 0; i < dataTableEntity.getRows(); i++) {
                String user_id = dataTableEntity.getString(i, CmnConst.USER_ID);
                String user_signature = dataTableEntity.getString(i, CmnConst.USER_SIGNATURE);
                RedisUtil.set(USER_SIGNATURE_KEY + user_id, user_signature);
            }
        }
    }
 
 
//    /**
//     * 获取用户头像
//     *
//     * @param user_id
//     * @return
//     * @throws BaseException
//     */
//    public String getUserAvatar(String user_id) throws BaseException {
//        try {
//            if (user_id.contains(",")) {
//                return this.getUserAvatar(user_id.split(","));
//            }
//            FieldSetEntity fse = getBaseDao().getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS, new String[]{CmnConst.THUMBNSIL_IMG, "user_id,uuid"}, "length(thumbnail_img)>0 and user_id=? ", new Object[]{user_id}, false, null);
//            if (fse == null) {
//                return null;
//            }
//            String bytes = Base64.encodeBase64String(fileManagerService.getFileContent(fse.getString(CmnConst.THUMBNSIL_IMG)));
//            return "data:image/*;base64," + bytes;
//        } catch (Exception e) {
//            return null;
//        }
//    }
 
    /**
     * 通过用户id数组获取用户头像
     *
     * @return
     * @throws BaseException
     */
    public Map<String, String> getUserAvatar(DataTableEntity fileDt) throws BaseException {
        Map<String, String> jsonObject = new HashMap<>();
        if (DataTableEntity.isEmpty(fileDt)) {
            return null;
        }
        DataTableEntity dataTableEntity = fileDt;
        FieldMetaEntity f = new FieldMetaEntity();
        f.setTableName(new String[]{CmnConst.PRODUCT_SYS_ATTACHMENTS});
        for (int i = 0; i < dataTableEntity.getRows(); i++) {
            try {
                dataTableEntity.setMeta(f);
                FieldSetEntity fse = dataTableEntity.getFieldSetEntity(i);
                fse.setTableName(CmnConst.PRODUCT_SYS_ATTACHMENTS);
                String userId = fse.getString(CmnConst.USER_ID);
                String bytes = Base64.encodeBase64String(fileManagerService.getFileContent(fse));
                jsonObject.put(userId, "data:image/*;base64," + bytes);
            } catch (Exception e) {
 
            }
        }
        return jsonObject;
    }
 
    /**
     * 通过用户id数组获取用户头像
     *
     * @param user_ids
     * @return
     * @throws BaseException
     */
    public String getUserAvatar(String[] user_ids) throws BaseException {
        JSONObject jsonObject = new JSONObject();
        if (user_ids == null || user_ids.length == 0) {
            return jsonObject.toJSONString();
        }
        for (String user_id : user_ids) {
            jsonObject.put(user_id, getUserAvatar(user_id));
        }
        return jsonObject.toJSONString();
    }
 
    /**
     * 获取当前用户签名
     *
     * @param uuid 签名附件uuid
     * @return
     * @throws BaseException
     */
    public String getUserSignature(String uuid) throws BaseException {
        try {
            String bytes = Base64.encodeBase64String(fileManagerService.getFileContent(uuid));
            return "data:image/*;base64," + bytes;
        } catch (Exception e) {
            return null;
        }
    }
 
    /**
     * 获取当前用户签名
     *
     * @return
     * @throws BaseException
     */
    public Object getUserSignature() throws BaseException {
//        FieldSetEntity userInfo = getBaseDao().getFieldSetEntity(CmnConst.PRODUCT_SYS_USERS, new String[]{CmnConst.UUID, CmnConst.USER_SIGNATURE}, getUUID(), false);
//        String user_signature = userInfo.getString(CmnConst.USER_SIGNATURE);
        //用户没有签名
        if (!RedisUtil.exists(USER_SIGNATURE_KEY + getUserId())) {
            throw new BaseException(SystemCode.USER_SIGNATURE_NOT_EXIST.getValue(), SystemCode.USER_SIGNATURE_NOT_EXIST.getText());
        }
        String bytes = Base64.encodeBase64String(fileManagerService.getFileContent((String) RedisUtil.get(USER_SIGNATURE_KEY + getUserId())));
        if (bytes == null) {
            return null;
        }
        JSONObject object = new JSONObject();
        object.put("img", "data:image/*;base64," + bytes);
        object.put(CmnConst.USER_SIGNATURE, RedisUtil.get(USER_SIGNATURE_KEY + getUserId()));
        return object;
 
    }
 
 
    public void clearUserSignature() throws BaseException {
        FieldSetEntity f = new FieldSetEntity();
        f.setTableName(CmnConst.PRODUCT_SYS_USERS);
        f.setValue(CmnConst.UUID, getUUID());
        f.setValue(CmnConst.USER_SIGNATURE, null);
        getBaseDao().update(f);
        RedisUtil.del(USER_SIGNATURE_KEY + getUserId());
    }
 
    /**
     * 上传用户签名
     *
     * @param rpe
     * @throws BaseException
     */
    public String uploadUserSignature(RequestParameterEntity rpe) throws BaseException {
        Map<String, File> files = rpe.getFiles();
        if (files == null || files.size() <= 0 || files.get(rpe.getFormData().getString(CmnConst.USER_SIGNATURE)) == null) {
            throw new BaseException(SystemCode.GET_UPLOAD_SIGNATURE_FILE_FAIL.getValue(), SystemCode.GET_UPLOAD_SIGNATURE_FILE_FAIL.getText());
        }
        FieldSetEntity fieldSetEntity = fileManagerService.uploadFile(rpe);
        fieldSetEntity.setValue(CmnConst.UUID, getUUID());
        BaseUtil.createCreatorAndCreationTime(fieldSetEntity);
        getBaseDao().update(fieldSetEntity);
        RedisUtil.set(USER_SIGNATURE_KEY + getUserId(), fieldSetEntity.getString(CmnConst.USER_SIGNATURE));
        return fieldSetEntity.getString(CmnConst.USER_SIGNATURE);
    }
 
    public String getUUID() {
        return getCurrentUser().getUuid();
    }
 
    public int getUserId() {
        return getCurrentUser().getUser_id();
    }
 
    public SystemUser getCurrentUser() {
        return SpringMVCContextHolder.getCurrentUser();
    }
 
 
}