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 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 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 getUserAvatar(DataTableEntity fileDt) throws BaseException { Map 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 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(); } }