package com.ruoyi.logistics.config;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.logistics.annotation.LargeDataScope;
import com.ruoyi.logistics.domain.LargeRequest;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class LargeDataScopeAspect {

    // 数据权限常量（保持你项目原有一致即可）
    public static final String DATA_SCOPE_CUSTOM = "2";
    public static final String DATA_SCOPE_DEPT = "3";
    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
    /**
     * 仅本人数据权限
     */
    public static final String DATA_SCOPE_SELF = "5";
    /**
     * 大屏专用数据权限切面
     */
    @Before("@annotation(com.ruoyi.logistics.annotation.LargeDataScope)")
    public void dataScopeBefore(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        LargeDataScope dataScope = method.getAnnotation(LargeDataScope.class);

        if (!dataScope.enabled()) {
            return;
        }

        // 获取入参 LargeRequest
        Object[] args = point.getArgs();
        LargeRequest largeRequest = null;
        for (Object arg : args) {
            if (arg instanceof LargeRequest) {
                largeRequest = (LargeRequest) arg;
                break;
            }
        }
        if (largeRequest == null) {
            return;
        }

        largeRequest.getParams().put("dataScope", "");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getSysUser();

        if (user.isAdmin()) {
            return;
        }

        StringBuilder sql = new StringBuilder();
        String deptAlias = dataScope.deptAlias();

        for (SysRole role : user.getRoles()) {
            String ds = role.getDataScope();

            if (DATA_SCOPE_CUSTOM.equals(ds)) {
                sql.append(" OR ").append(deptAlias).append(".dept_id IN (SELECT dept_id FROM sys_role_dept WHERE   role_id = ").append(role.getRoleId()).append(")");
            } else if (DATA_SCOPE_DEPT.equals(ds)) {
                sql.append(" OR ").append(deptAlias).append(".dept_id = ").append(user.getDeptId());
            } else if (DATA_SCOPE_DEPT_AND_CHILD.equals(ds)) {
                sql.append(" OR ").append(deptAlias).append(".dept_id IN (SELECT dept_id FROM sys_dept WHERE dept_id = ").append(user.getDeptId()).append(" OR find_in_set(").append(user.getDeptId()).append(", ancestors))");
            }
            else if (DATA_SCOPE_SELF.equals(ds)) {
                sql.append(" OR ").append(deptAlias).append(".dept_id = ").append(user.getDeptId());
            }
        }

        if (sql.length() > 0) {
            largeRequest.getParams().put("dataScope", " AND (" + sql.substring(4) + ")");
        }
    }
}

