package com.ruoyi.web.modules.industryservice.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;

@DisplayName("ReportDeptDataScopeSupport")
class ReportDeptDataScopeSupportTest
{
    @Test
    @DisplayName("管理员不过滤")
    void adminNoFilter()
    {
        BaseEntity query = new BaseEntity();
        ReportDeptDataScopeSupport.apply(query, "h", true, null);
        assertEquals("", query.getParams().get(ReportDeptDataScopeSupport.PARAM_KEY));
    }

    @Test
    @DisplayName("非管理员即使角色为全部数据权限也按本部门过滤")
    void allDataScopeStillFilters()
    {
        BaseEntity query = new BaseEntity();
        SysRole role = new SysRole();
        role.setDataScope(Constants.Dept.DATA_SCOPE_ALL);
        SysUser user = new SysUser();
        user.setDeptId(117L);
        user.setRoles(Collections.singletonList(role));
        ReportDeptDataScopeSupport.apply(query, "h", false, user);
        String scope = String.valueOf(query.getParams().get(ReportDeptDataScopeSupport.PARAM_KEY));
        assertTrue(scope.contains("117"));
        assertTrue(scope.contains("town_dept_id"));
    }

    @Test
    @DisplayName("乡镇/村用户拼接本部门及以下条件")
    void deptAndChildFilter()
    {
        BaseEntity query = new BaseEntity();
        SysRole role = new SysRole();
        role.setDataScope(Constants.Dept.DATA_SCOPE_DEPT_AND_CHILD);
        SysUser user = new SysUser();
        user.setDeptId(117L);
        user.setRoles(Collections.singletonList(role));
        ReportDeptDataScopeSupport.apply(query, "h", false, user);
        String scope = String.valueOf(query.getParams().get(ReportDeptDataScopeSupport.PARAM_KEY));
        assertTrue(scope.contains("village_dept_id"));
        assertTrue(scope.contains("town_dept_id"));
        assertTrue(scope.contains("117"));
        assertTrue(scope.contains("find_in_set"));
    }

    @Test
    @DisplayName("无部门时查不到数据")
    void noDeptBlocks()
    {
        BaseEntity query = new BaseEntity();
        SysUser user = new SysUser();
        user.setDeptId(null);
        ReportDeptDataScopeSupport.apply(query, "r", false, user);
        assertEquals(" AND 1=0 ", query.getParams().get(ReportDeptDataScopeSupport.PARAM_KEY));
    }
}
