package com.xjrsoft.common.handler;

import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
import com.xjrsoft.common.annotation.DataPermission;
import net.sf.jsqlparser.expression.Expression;

/**
 * @Description 自定义数据权限处理
 * @Author: tzx
 * @Date: 2023/2/6 16:54
 */
public class MyDataPermissionHandler implements DataPermissionHandler {



    @Override
    public Expression getSqlSegment(Expression where, String mappedStatementId) {
        try {
            Class<?> clazz = Class.forName(mappedStatementId.substring(0, mappedStatementId.lastIndexOf(".")));
            DataPermission mapperAnnotation = clazz.getAnnotation(DataPermission.class);

            //如果mapper 没有数据权限注解 不需要设置
            if(mapperAnnotation == null){
                return where;
            }




//            String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(".") + 1);
//            Method[] methods = clazz.getDeclaredMethods();
//            for (Method method : methods) {
//                if (!methodName.equals(method.getName())) {
//                    continue;
//                }
//                // 获取自定义注解，无此注解则不控制数据权限
//                DataPermission annotation = method.getAnnotation(DataPermission.class);
//                if (annotation == null) {
//                    continue;
//                }
//
//            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return where;
    }
}