package com.cico.nest.lcdp.boot.core.system.dal.mysql.permission;

import com.cico.nest.lcdp.boot.core.system.controller.admin.permission.vo.role.RolePageReqVO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.permission.vo.role.RoleRespVO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.dept.DeptDO;
import com.cico.nest.lcdp.boot.core.system.dal.dataobject.permission.RoleDO;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import com.cico.nest.lcdp.framework.mybatis.core.dataobject.BaseDO;
import com.cico.nest.lcdp.framework.mybatis.core.mapper.BaseMapperX;
import com.cico.nest.lcdp.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RoleDO::getId));
    }

    default PageResult<RoleRespVO> selectPage2(RolePageReqVO reqVO) {
        MPJLambdaWrapper<RoleDO> wrapper = new MPJLambdaWrapper<RoleDO>()
                .selectAll(RoleDO.class)
                .selectAs(DeptDO::getName,RoleRespVO::getDeptName)
                .leftJoin(DeptDO.class,DeptDO::getId,RoleDO::getDeptId)
                .likeIfExists(RoleDO::getName, reqVO.getName())
                .likeIfExists(RoleDO::getCode, reqVO.getCode())
                .likeIfExists(DeptDO::getName, reqVO.getDeptName())
                .eq(RoleDO::getDeleted, 0)
                .orderByDesc(RoleDO::getId);
        PageResult<RoleRespVO> result = selectJoinPage(reqVO,RoleRespVO.class,wrapper);
        return result ;
    }

    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<RoleDO> selectListByCode(String code){
        return selectList(RoleDO::getCode, code);
    }

}
