package com.njcn.msgpush.module.system.dal.mysql.permission;

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

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

@Mapper
public interface RoleMapper extends BaseMapperX<RoleDO> {

    default PageResult<RoleDO> selectPage(RolePageReqVO reqVO) {
        LambdaQueryWrapperX<RoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        boolean hasName = StringUtils.hasText(reqVO.getName());
        boolean hasCode = StringUtils.hasText(reqVO.getCode());
        if (hasName && hasCode) {
            queryWrapper.and(wrapper -> wrapper.like(RoleDO::getName, reqVO.getName())
                    .or()
                    .like(RoleDO::getCode, reqVO.getCode()));
        } else {
            queryWrapper.likeIfPresent(RoleDO::getName, reqVO.getName())
                    .likeIfPresent(RoleDO::getCode, reqVO.getCode());
        }
        queryWrapper.eqIfPresent(RoleDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(RoleDO::getSort);
        return selectPage(reqVO, queryWrapper);
    }

    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);
    }

}
