package com.fs.hospital.mapper;

import java.util.List;
import com.fs.hospital.domain.HospitalDept;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 部门Mapper接口
 *
 * @author fs
 * @date 2024-02-26
 */
public interface HospitalDeptMapper
{
    /**
     * 查询部门
     *
     * @param deptId 部门主键
     * @return 部门
     */
    public HospitalDept selectHospitalDeptByDeptId(Long deptId);

    /**
     * 查询部门列表
     *
     * @param hospitalDept 部门
     * @return 部门集合
     */
    public List<HospitalDept> selectHospitalDeptList(HospitalDept hospitalDept);

    /**
     * 新增部门
     *
     * @param hospitalDept 部门
     * @return 结果
     */
    public int insertHospitalDept(HospitalDept hospitalDept);

    /**
     * 修改部门
     *
     * @param hospitalDept 部门
     * @return 结果
     */
    public int updateHospitalDept(HospitalDept hospitalDept);

    /**
     * 删除部门
     *
     * @param deptId 部门主键
     * @return 结果
     */
    public int deleteHospitalDeptByDeptId(Long deptId);

    /**
     * 批量删除部门
     *
     * @param deptIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteHospitalDeptByDeptIds(Long[] deptIds);
    @Select({"<script> " +
            " select d.dept_id\n" +
            "        from hospital_dept d\n" +
            "        left join hospital_role_dept rd on d.dept_id = rd.dept_id\n" +
            "        where rd.role_id = #{roleId}\n" +
            "        <if test=\"deptCheckStrictly\">\n" +
            "            and d.dept_id not in (select d.parent_id from hospital_role_dept d inner join hospital_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})\n" +
            "        </if>\n" +
            "        order by d.parent_id, d.order_num"+
            "</script>"})
    List<Integer> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);

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

    @Select("select count(*) from hospital_dept where status = 0 and is_del = '0' and find_in_set(#{deptId}, ancestors)")
    int selectNormalChildrenDeptById(Long deptId);

    @Select("select count(1) from hospital_user where dept_id = #{deptId} and is_del = '0'")
    int checkDeptExistUser(Long deptId);

    @Select("select count(1) from hospital_dept\n" +
            "\t\twhere is_del = '0' and parent_id = #{deptId} limit 1")
    int hasChildByDeptId(Long deptId);
}
