package edu.travel.config; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import edu.travel.DateUtil; import edu.travel.cache.util.RedisKey; import org.apache.ibatis.reflection.MetaObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * @author 畅 * 通用字段处理 */ @Component public class FieldMetaObjectHandler implements MetaObjectHandler { @Autowired private StringRedisTemplate stringRedisTemplate; private final static String UPDATE_USER_ID = "updateUserId"; private final static String UPDATE_TIME = "updateTime"; private final static String CREATE_USER_ID = "createUserId"; private final static String CREATE_TIME = "createTime"; private final static String PROJECT = "project"; private final static String DELETE_FLAG = "deleteFlag"; @Override public void insertFill(MetaObject metaObject) { HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Object principal = authentication.getPrincipal(); if (principal == null) { return; } Long userId = null; //时区 String s =null; if (!(principal instanceof String && "anonymousUser".equals(principal))) { //走登录用户 String jsonString = JSON.toJSONString(principal); JSONObject jsonObject = JSON.parseObject(jsonString); userId = jsonObject.getLong("id"); s = stringRedisTemplate.opsForValue().get(RedisKey.USER_COUNTRY + userId); } if (metaObject.hasSetter(CREATE_USER_ID)) { this.strictInsertFill(metaObject, CREATE_USER_ID, String.class, userId != null ? userId.toString() : ""); } if (metaObject.hasSetter(CREATE_TIME)) { this.strictInsertFill(metaObject, CREATE_TIME, Date.class, ObjectUtil.isEmpty(s)? new Date() : DateUtil.convertServerTimeToUserDateTime(s)); } if (metaObject.hasSetter(PROJECT)) { this.strictInsertFill(metaObject, PROJECT, String.class, request.getHeader("project")); } if (metaObject.hasSetter(DELETE_FLAG)) { this.strictInsertFill(metaObject, DELETE_FLAG, Integer.class, 0); } } @Override public void updateFill(MetaObject metaObject) { Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(principal)); String userId = jsonObject.getLong("id").toString(); String s = stringRedisTemplate.opsForValue().get(RedisKey.USER_COUNTRY + userId); if (metaObject.hasSetter(UPDATE_USER_ID)) { this.strictInsertFill(metaObject, UPDATE_USER_ID, String.class, userId); } if (metaObject.hasSetter(UPDATE_TIME)) { this.strictInsertFill(metaObject, UPDATE_TIME, Date.class,ObjectUtil.isEmpty(s)? new Date() : DateUtil.convertServerTimeToUserDateTime(s)); } } }