package cn.newfeifan.mall.module.system.dal.mysql.permission;

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.mybatis.core.dataobject.BaseDO;
import cn.newfeifan.mall.framework.mybatis.core.mapper.BaseMapperX;
import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.module.system.controller.admin.permission.vo.role.RolePageReqVO;
import cn.newfeifan.mall.module.system.dal.dataobject.permission.RoleDO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.lang.Nullable;

import java.util.*;

@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())
                .eqIfPresent(RoleDO::getCategoryId, reqVO.getCategoryId())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RoleDO::getId));
    }

    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(RoleDO::getStatus, statuses);
    }
    default List<String> selectByRoleIds(Set<Long> roleIds){
        if (roleIds.isEmpty()) {
            return Collections.emptyList();
        }
        List<RoleDO> roleDOS = selectList(new LambdaQueryWrapperX<RoleDO>()
                .inIfPresent(RoleDO::getId, roleIds));
        List<String> strings = new ArrayList<>();
        for (RoleDO roleDO : roleDOS) {
            strings.add(roleDO.getName());
        }
        return strings;
    }


}
