package org.agt.module.system.dal.mysql.permission;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.mybatis.core.dataobject.BaseDO;
import org.agt.framework.mybatis.core.mapper.BaseMapperX;
import org.agt.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.agt.module.system.controller.admin.permission.vo.role.RolePageReqVO;
import org.agt.module.system.dal.dataobject.permission.RoleDO;
import org.agt.module.system.enums.permission.RoleCodeEnum;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapperX<RoleDO> {

    default PageResult<RoleDO> selectPage(RolePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RoleDO>()
                .likeIfPresent(RoleDO::getName, reqVO.getName())
                .likeIfPresent(RoleDO::getCode, reqVO.getCode())
                .eqIfPresent(RoleDO::getStatus, reqVO.getStatus())
                .neIfPresent(RoleDO::getCode, RoleCodeEnum.SUPER_ADMIN.getCode())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(RoleDO::getSort));
    }

    default RoleDO selectByName(String name) {
        return selectOne(RoleDO::getName, name);
    }

    default RoleDO selectByCode(String code) {
        return selectOne(RoleDO::getCode, code);
    }

    default List<RoleDO> selectListByStatus(@Nullable Collection<Integer> statuses) {
        return selectList(Wrappers.<RoleDO>lambdaQuery().eq(RoleDO::getStatus, statuses)
                .ne(RoleDO::getCode, RoleCodeEnum.SUPER_ADMIN.getCode()));
    }

}
