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

import com.cico.nest.lcdp.boot.core.system.controller.admin.dept.vo.dept.DeptListReqVO;
import com.cico.nest.lcdp.boot.core.system.controller.admin.dept.vo.dept.DeptRespVO;
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.dict.DictDataDO;
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.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 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(String parentId, String name) {
        return selectOne(DeptDO::getParentId, parentId, DeptDO::getName, name);
    }

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

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

    default List<DeptRespVO> selectListV2(DeptListReqVO reqVO){
        MPJLambdaWrapper<DeptDO> wrapper = new MPJLambdaWrapper<DeptDO>()
                .selectAll(DeptDO.class)
                .selectAs(DictDataDO::getLabel, DeptRespVO::getType)
                .leftJoin(DictDataDO.class,DictDataDO::getValue,DeptDO::getType)
                .likeIfExists(DeptDO::getName, reqVO.getName())
                .likeIfExists(DeptDO::getCode, reqVO.getCode())
                .likeIfExists(DeptDO::getAlias, reqVO.getAlias())
                .eq(DeptDO::getDeleted, 0)
                .orderByDesc(RoleDO::getId);
        List<DeptRespVO> result = selectJoinList(DeptRespVO.class,wrapper);
        return result;
    };
}
