package com.fs.company.mapper;

import com.fs.company.domain.CompanyDept;
import com.fs.course.vo.DeptFullPathVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 部门Mapper接口
 *
 * @author fs
 * @date 2021-05-25
 */
public interface CompanyDeptMapper
{
    /**
     * 查询部门
     *
     * @param deptId 部门ID
     * @return 部门
     */
    public CompanyDept selectCompanyDeptById(Long deptId);

    /**
     * 查询部门列表
     *
     * @param companyDept 部门
     * @return 部门集合
     */
    public List<CompanyDept> selectCompanyDeptList(CompanyDept companyDept);

    @Select("select * from company_dept where dept_id = #{parentId} or parent_id = #{parentId} and company_id=#{companyId} and status=#{status}")
    public List<CompanyDept> selectCompanyDeptListByDeptAndParent(CompanyDept companyDept);
    /**
     * 获取公司默认部门
     * @param companyId 公司ID
     * @return  公司部门
     * 获取所有部门的数据
     * @param companyId 公司id
     * @return 部门数据
     */
    @Select("select cd.* from company_dept cd where cd.company_id = #{companyId} and cd.dept_name = '默认' and cd.parent_id = 0 limit 1")
    CompanyDept getTopCompanyDeptByCompanyId(@Param("companyId") Long companyId);
    /**
     * 新增部门
     *
     * @param companyDept 部门
     * @return 结果
     */
    public int insertCompanyDept(CompanyDept companyDept);

    /**
     * 修改部门
     *
     * @param companyDept 部门
     * @return 结果
     */
    public int updateCompanyDept(CompanyDept companyDept);

    /**
     * 删除部门
     *
     * @param deptId 部门ID
     * @return 结果
     */
    public int deleteCompanyDeptById(Long deptId);


    /**
     * 批量删除部门
     *
     * @param deptIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCompanyDeptByIds(Long[] deptIds);

    List<Integer> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);

    @Select("select count(1) from company_dept where company_id=#{companyId} and dept_name =#{deptName} ")
    int checkDeptNameUnique(@Param("companyId")Long companyId, @Param("deptName")String deptName);

    int checkDeptExistUser(Long deptId);

    int hasChildByDeptId(Long deptId);

    int selectNormalChildrenDeptById(Long deptId);

    List<CompanyDept> selectChildrenDeptById(Long deptId);

    int updateDeptChildren(@Param("depts")List<CompanyDept> children);

    int updateDeptStatus(CompanyDept dept);
    @Select("select dept_name from company_dept where FIND_IN_SET(dept_id,#{ids}) ")
    List<String> selectCompanyDeptNamesByIds(String ids);

    @Select("select company_id from company_dept where dept_id =#{deptId} ")
    Long selectCompanyDeptByIdCompany(Long deptId);
    /**
     * 获取所有部门的数据
     * @param companyId 公司id
     * @return 部门数据
     */
    @Select("select * from company_dept where company_id = ${companyId}")
    List<CompanyDept> selectDeptDataByCompanyId(@Param("companyId") Long companyId);

    @Select("select dept_id from company_dept where parent_id=#{parentId}")
    List<Long> selectCompanyDeptByParentId(@Param("parentId") Long parentId);

    @Select("select parent_id from company_dept where dept_id=#{deptId}")
    List<Long> selectCompanyDeptByDeptId(@Param("deptId") Long deptId);


    @Select("select * from company_dept where status='0' and del_flag='0'")
    List<CompanyDept> queryDeptDataAll();

    @Select("select dept_name from company_dept where dept_id=${deptId} limit 1")
    String selectDeptNameById(@Param("deptId") Long deptId);

    @Select("select dept_id,dept_name from company_dept where dept_name=#{deptName} limit 1")
    CompanyDept selectDeptNameBydeptName(@Param("deptName") String deptName);

    void deleteCompanyDeptByCompanyIds(Long[] companyIds);

    List<CompanyDept> selectCompanyDeptByIds(@Param("depts")List<Long> deptIds);

    List<Long> getCurrentDeptIdDownTreeIds(@Param("deptId") Long deptId);

    /**
     * 根据公司id -> 部门 & 全路径查询
     * @return
     */
    List<DeptFullPathVO> getDeptFullPathList(@Param("companyId") Long companyId);
}
