
package com.lideeyunji.core.framework.config.aspect.nologin;

import com.lideeyunji.core.framework.service.IFormService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.constants.FrameErrorCodeConstants;
import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import com.lideeyunji.tool.framework.yunji.enums.AuthTypeEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;

/**
 * 不登录是否可以查看dbform配置
 */
@Aspect
@Component
public class LideeYunJiNoLoginViewDbFormAspect {

    private ExpressionParser parser = new SpelExpressionParser();

    @Autowired
    private IFormService formService;

    @Around("@annotation(com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiNoLoginViewDbForm)")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取方法的参数
        Object[] args = joinPoint.getArgs();
        // 假设dbformId是第一个参数，你可以根据实际情况调整索引
        Long dbformId = (Long) args[0];
        if (Func.isEmpty(dbformId)) {
            throw new lideeYunJiException(FrameErrorCodeConstants.FRAME_PARAM_NULL_ERROR.getMsg());
        }
        //判断该接口是否勾选 不登录可查看
        AuthTypeEnum authType = formService.getAuthType(dbformId);

        if (!Func.equals(authType, AuthTypeEnum.authOpen)) {//配置没有勾选,判断是否已经登录了
            throw new lideeYunJiException(FrameErrorCodeConstants.FRAME_LOGIN_VIEW_ERROR.getMsg());
        }

        return joinPoint.proceed();
    }

}

