<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ningdatech.carapi.company.mapper.CompanyEmployeesMapper">

    <select id="countEmployeesByCompanyIds" resultType="com.ningdatech.carapi.company.entity.dto.ResCompanyEmployeesCountDTO">
        select
            c.id,
            c.name,
            sum(case when e.id is not null then 1 else 0 end) employeesNum,
            sum(case when e.employees_status = 0 then 1 else 0 end) driverCount,
            sum(case when e.employees_status = 1 then 1 else 0 end) adminCount,
            sum(case when e.employees_status = 2 then 1 else 0 end) captainCount
        from nd_company_employees e
        right join nd_company_info c on e.company_id = c.id
        where c.id in
        <foreach collection="companyIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by c.id
    </select>
</mapper>
