package com.fs.framework.aspectj;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fs.hospital580.config.Hospital580Config;
import com.fs.hospital580.util.ObjectApiSignUtil;
import lombok.extern.slf4j.Slf4j;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class Hospital580Aspect {


    @Autowired
    private Hospital580Config hospital580Config;


    @Pointcut("execution(* com.fs.app.controller.Hospital580CallbackController.*(..))")
    public void myControllerMethods() {
    }

    /**
     * 验签 580医院
     *
     * @param joinPoint
     */
    @Before("myControllerMethods()")
    public void logBeforeMethodExecution(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Object parm = args[0];
        JSONObject entries = JSONUtil.parseObj(parm);
        log.info("580医院请求:{} {}", methodName,entries);
        String sign1 = entries.getStr("sign");
        String sign = ObjectApiSignUtil.getSign(parm, hospital580Config.getSecretKey());
        if (!sign1.equals(sign)) {
            log.info("验签失败");
            throw new RuntimeException("验签失败");
        }
    }
}