package com.hzya.frame.web.aop;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.uuid.UUIDLong;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.HashMap;

/**
 * @Description 绑定用户id
 * @Author xiangerlin
 * @Date 2022/12/21 15:23
 **/
@Aspect
@Component
public class BindUserIdAop {
    Logger logger = LoggerFactory.getLogger(BindUserIdAop.class);

    //切点, 拦截除了sys下 其他controller层所有的以query开头的方法
    @Pointcut("(execution(* com.hzya.frame..*.save*(..)) || execution(* com.hzya.frame..*.update*(..)) )")
    public void bindPoint() {
    }

    @Before(value = "bindPoint()")
    public void bindUserId(JoinPoint point) {
        try {
            Object[] objs = point.getArgs();
            if (objs.length > 0) {
                if (StpUtil.isLogin()) {
                    Object loginId = StpUtil.getLoginId();
                    Object obj = objs[0];
                    // 获取方法签名
                    MethodSignature signature = (MethodSignature) point.getSignature();
                    String methodName = signature.getName();
                    if (obj instanceof HashMap) {
                        ((HashMap<String,Object>) obj).put("create_user_id",loginId);
                        ((HashMap<String,Object>) obj).put("modify_user_id",loginId);
                        ((HashMap<String,Object>) obj).put("create_time", DateUtil.now());
                        ((HashMap<String,Object>) obj).put("modify_time", DateUtil.now());
                    }
                    if (obj instanceof JSONObject){
                        JSONObject    jsonStrObj = null;
                        String jsonStr = (String) ((JSONObject) obj).get("jsonStr");
                        if(!StrUtil.isEmpty(jsonStr)){
                            jsonStrObj = JSON.parseObject(jsonStr);
                        }
                        if(null != jsonStrObj){
                            //如果是update的方法，则不处理create_user_id 和 create_time
                            if (jsonStrObj.getString("modify_user_id") == null) {
                                jsonStrObj.put("modify_user_id",loginId);
                            }
                            if (jsonStrObj.getString("modify_time") == null) {
                                jsonStrObj.put("modify_time",DateUtil.now());
                            }
                            //save方法才要赋值create_user_id 和 creat_time  id
                            if (methodName.startsWith("save")){
                                if (jsonStrObj.getString("create_user_id") == null) {
                                    jsonStrObj.put("create_user_id",loginId);
                                }
                                if (jsonStrObj.getString("create_time") == null) {
                                    jsonStrObj.put("create_time",DateUtil.now());
                                }
                                if (jsonStrObj.getString("id") == null) {
                                    jsonStrObj.put("id",UUIDLong.longUUID());
                                }
                            }
                        }else{
                            jsonStrObj = new JSONObject();
                            jsonStrObj.put("create_user_id",loginId);
                            jsonStrObj.put("modify_user_id",loginId);
                            jsonStrObj.put("create_time",DateUtil.now());
                            jsonStrObj.put("modify_time",DateUtil.now());
                            jsonStrObj.put("id", UUIDLong.longUUID());
                        }
                        ((JSONObject) obj).put("jsonStr",jsonStrObj.toJSONString());
                    }
                }
            }
        }catch (Exception e){
            //logger.error(e.getMessage());
        }
    }
}
