package com.mosty.common.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mosty.common.base.util.StringUtils;
import com.mosty.common.token.DeptInfo;
import com.mosty.common.token.UserInfo;

import java.util.Arrays;

/**
 * 数据权限获取工具
 */
public class PermissionsUtil {

    // 根据组织机构ID获取查询权限
    public static void queryWrapperUtilByDeptInfo(QueryWrapper queryWrapper, DeptInfo deptInfo) {
        if (deptInfo != null && deptInfo.getPermissionsLevel() != null) {
            String level = deptInfo.getPermissionsLevel();
            if ("1".equals(level) || "2".equals(level)) {
                // do nothing
            } else if ("3".equals(level)) {
                queryWrapper.eq("sssgajid", deptInfo.getSssgajid());
            } else if ("4".equals(level)) {
                queryWrapper.eq("ssxgajid", deptInfo.getSsxgajid());
            } else if ("5".equals(level)) {
                if (StringUtils.isNotEmpty(deptInfo.getIds())) {
                    String[] ids = deptInfo.getIds().split(",");
                    queryWrapper.in("ssbmid", Arrays.asList(ids));
                }
            } else if ("6".equals(level)) {
                queryWrapper.eq("ssbmid", deptInfo.getSsbmid());
            }
        }
    }

    // 根据组织机构ID获取查询权限
    public static void queryWrapperUtil(QueryWrapper queryWrapper, UserInfo userInfo) {
        if (userInfo != null && userInfo.getUserPermissionsInfo() != null) {
            String level = userInfo.getUserPermissionsInfo().getPermissionsLevel();
            if ("1".equals(level) || "2".equals(level)) {
                // do nothing
            } else if ("3".equals(level)) {
                queryWrapper.eq("sssgajid", userInfo.getDszDeptId());
            } else if ("4".equals(level)) {
                queryWrapper.eq("ssxgajid", userInfo.getFxjDeptId());
            } else if ("5".equals(level)) {
                if (userInfo.getUserPermissionsInfo() != null
                        && StringUtils.isNotEmpty(userInfo.getUserPermissionsInfo().getIds())) {
                    String[] ids = userInfo.getUserPermissionsInfo().getIds().split(",");
                    queryWrapper.in("ssbmid", Arrays.asList(ids));
                }
            } else if ("6".equals(level)) {
                queryWrapper.eq("ssbmid", userInfo.getDeptId());
            }
        }
    }

    // 根据组织机构编码获取查询权限
    public static void queryWrapperUtilByCode(QueryWrapper queryWrapper, UserInfo userInfo) {
        if (userInfo != null && userInfo.getUserPermissionsInfo() != null) {
            String level = userInfo.getUserPermissionsInfo().getPermissionsLevel();
            if ("1".equals(level) || "2".equals(level)) {
                // do nothing
            } else if ("3".equals(level)) {
                queryWrapper.eq("sssgajdm", userInfo.getDszDeptCode());
            } else if ("4".equals(level)) {
                queryWrapper.eq("ssxgajdm", userInfo.getFxjDeptCode());
            } else if ("5".equals(level)) {
                String[] orgcodes = userInfo.getUserPermissionsInfo().getOrgcodes().split(",");
                queryWrapper.in("ssbmdm", Arrays.asList(orgcodes));
            } else if ("6".equals(level)) {
                queryWrapper.eq("ssbmdm", userInfo.getDeptId());
            }
        }
    }

    // 表别名(使用ssbmid)
    public static String createSql(String tableAlias, UserInfo userInfo) {
        String dataPermissionSql = "";
        if (userInfo != null && userInfo.userPermissionsInfo != null && !StringUtils.isEmpty(userInfo.userPermissionsInfo.getPermissionsSql())) {
            String sql = userInfo.userPermissionsInfo.getPermissionsSql().replaceAll("&apos;", "'");
            if (StringUtils.isEmpty(tableAlias)) {
                dataPermissionSql = sql.replaceAll("alias.", "");
            } else {
                dataPermissionSql = sql.replaceAll("alias", tableAlias);
            }
        }
        return dataPermissionSql;
    }

    // 表别名(使用ssbmdm)
    public static String createSqlOfCode(String tableAlias, UserInfo userInfo) {
        String dataPermissionSql = "";
        if (userInfo != null && userInfo.userPermissionsInfo != null &&
                !StringUtils.isEmpty(userInfo.userPermissionsInfo.getPermissionsSqlCode())) {
            String sql = userInfo.userPermissionsInfo.getPermissionsSqlCode().replaceAll("&apos;", "'");
            if (StringUtils.isEmpty(tableAlias)) {
                dataPermissionSql = sql.replaceAll("alias.", "");
            } else {
                dataPermissionSql = sql.replaceAll("alias", tableAlias);
            }
        }
        return dataPermissionSql;
    }
}
