package com.zhangmeng.admin.manager.dao;


import com.zhangmeng.model.base.baseDao.AbstractBaseMapper;
import com.zhangmeng.model.entity.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author zhengmeng
 * @version 1.0
 * @date 2021年1月7日17:44:40
 */
@Mapper
public interface PermissionDao extends AbstractBaseMapper<Permission> {

    @Results(value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "addTime",property = "addTime"),
            @Result(column = "updateTime",property = "updateTime"),
            @Result(column = "name",property = "name"),
            @Result(column = "type",property = "type"),
            @Result(column = "permissionCode",property = "permissionCode"),
            @Result(column = "url",property = "url"),
            @Result(column = "openType",property = "openType"),
            @Result(column = "parent_id",property = "parent_id"),
            @Result(column = "long_text_id",property = "long_text_id"),
            @Result(column = "parent_long_text_id",property = "parent_long_text_id"),
            @Result(column = "icon",property = "icon"),
            @Result(column = "sort",property = "sort"),
    })
    @Select("select DISTINCT p.* from permission p left join role_permission rp on p.id = rp.permission_id left join user_role ur on ur.role_id = rp.role_id where ur.user_id = #{userId}")
    List<Permission> findByUserId(@Param("userId") Long userId);

    @Select("select * from permission p where p.parentId is null")
    List<Permission> findByParentIdIsNull();

    @Select("select * from permission p where p.parentId = #{parentId}")
    List<Permission> findByParentId(Long parentId);
}
