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

import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.mybatis.core.dataobject.BaseDO;
import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.system.controller.admin.permission.vo.role.RolePageReqVO;
import com.njcn.rdms.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) {
        return selectPageByScope(reqVO, null, null);
    }

    default PageResult<RoleDO> selectPageByScope(RolePageReqVO reqVO, @Nullable String scopeType, @Nullable String objectType) {
        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())
                .eq(scopeType != null, RoleDO::getScopeType, scopeType)
                .eq(objectType != null, RoleDO::getObjectType, objectType)
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(RoleDO::getSort);
        return BaseMapperX.super.selectPage(reqVO, queryWrapper);
    }

    default RoleDO selectByName(String name) {
        return selectByName(name, null, null);
    }

    default RoleDO selectByName(String name, @Nullable String scopeType, @Nullable String objectType) {
        return selectOne(new LambdaQueryWrapperX<RoleDO>()
                .eq(RoleDO::getName, name)
                .eq(scopeType != null, RoleDO::getScopeType, scopeType)
                .eq(objectType != null, RoleDO::getObjectType, objectType));
    }

    default RoleDO selectByCode(String code) {
        return selectByCode(code, null, null);
    }

    default RoleDO selectByCode(String code, @Nullable String scopeType, @Nullable String objectType) {
        return selectOne(new LambdaQueryWrapperX<RoleDO>()
                .eq(RoleDO::getCode, code)
                .eq(scopeType != null, RoleDO::getScopeType, scopeType)
                .eq(objectType != null, RoleDO::getObjectType, objectType));
    }

    default List<RoleDO> selectListByStatus(@Nullable Collection<Integer> statuses) {
        return selectListByStatus(statuses, null, null);
    }

    default List<RoleDO> selectListByStatus(@Nullable Collection<Integer> statuses,
                                            @Nullable String scopeType,
                                            @Nullable String objectType) {
        return selectList(new LambdaQueryWrapperX<RoleDO>()
                .inIfPresent(RoleDO::getStatus, statuses)
                .eq(scopeType != null, RoleDO::getScopeType, scopeType)
                .eq(objectType != null, RoleDO::getObjectType, objectType)
                .orderByAsc(RoleDO::getSort));
    }

}
