package cn.iocoder.yudao.module.system.dal.mysql.dept;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

@Mapper
public interface DeptMapper extends BaseMapperX<DeptDO> {

    default List<DeptDO> selectList(DeptListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DeptDO>()
                .likeIfPresent(DeptDO::getName, reqVO.getName())
                .eqIfPresent(DeptDO::getStatus, reqVO.getStatus()));
    }

    default DeptDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name);
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(DeptDO::getParentId, parentId);
    }

    default List<DeptDO> selectListByParentId(Collection<Long> parentIds) {
        return selectList(DeptDO::getParentId, parentIds);
    }

    default List<DeptDO> selectByLeaderId(Long leaderUserId) {

        return selectList(new LambdaQueryWrapperX<DeptDO>()
                .eq(DeptDO::getLeaderUserId, leaderUserId)
                .orderByAsc(DeptDO::getLevel));
    }

    default List<DeptDO> selectLikeFlag(String flag) {

        return selectList(new LambdaQueryWrapperX<DeptDO>().like(DeptDO::getFlag, flag));
    }

    default List<DeptDO> selectTree() {

        return selectList(new LambdaQueryWrapperX<DeptDO>().orderByAsc(DeptDO::getFlag));
    }

    default DeptDO selectDeptByUserId(Long userId) {

        MPJLambdaWrapperX<DeptDO> query = new MPJLambdaWrapperX<DeptDO>();
        query.selectAll(DeptDO.class);
        query.innerJoin(AdminUserDO.class, on -> on
                .eq(AdminUserDO::getDeptId, DeptDO::getId)
                .eq(AdminUserDO::getId, userId));

        return selectJoinOne(DeptDO.class, query);
    }

    List<DeptDO> selectCompany(@Param("flag") String flag,
                               @Param("type") List<String> type);
}
