<?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.zcloud.domain.persistence.mapper.DomainDepartmentMapper">

    <select id="getInfoById" resultType="com.zcloud.domain.persistence.dataobject.DomainDepartmentDO">
        select dd.*, d.name as masterDepartmentName, u.name as masterUserName
        from domain_department dd
                 left join department d on dd.master_department_id = d.id
                 left join user u on dd.master_user_id = u.id
        where dd.delete_enum = 'FALSE'
          and dd.id = #{id}

    </select>
    <select id="listPage" resultType="com.zcloud.domain.persistence.dataobject.DomainDepartmentDO">
        select dd.*,
               d.name        as masterDepartmentName,
               u.name        as masterUserName,
               count(distinct pt.id)  as projectTaskCount,
               count(distinct ddu.id) as domainDepartmentUserCount
        from domain_department dd
            left join department d on dd.master_department_id = d.id and d.delete_enum = 'FALSE'
            left join user u on dd.master_user_id = u.id and u.delete_enum = 'FALSE'
            left join project_task pt on pt.domain_department_id = dd.domain_department_id and pt.delete_enum = 'FALSE'
            left join domain_department_user ddu on ddu.domain_department_id = dd.domain_department_id and ddu.delete_enum = 'FALSE'
         where dd.delete_enum = 'FALSE'
            <if test="params.departmentName != null and params.departmentName !='' ">
                and d.name like  CONCAT('%', #{params.departmentName}, '%')
            </if>
        group by dd.id
        order by dd.create_time desc
    </select>
    <select id="listAll" resultType="com.zcloud.domain.persistence.dataobject.DomainDepartmentDO">
        select dd.*, d.name as masterDepartmentName, u.name as masterUserName
        from domain_department dd
                 left join department d on dd.master_department_id = d.id
                 left join user u on dd.master_user_id = u.id
                 left join domain_group dg on dd.domain_type = dg.domain_type and dg.delete_enum='FALSE'
        where dd.delete_enum = 'FALSE'
            <if test="corpinfoId != null ">
                and dg.corpinfo_id =#{corpinfoId}
            </if>
        group by dd.id
        order by dd.create_time desc
    </select>

    <select id="getInfoByUUID" resultType="com.zcloud.domain.persistence.dataobject.DomainDepartmentDO">
        select dd.*, d.name as masterDepartmentName, u.name as masterUserName
        from domain_department dd
                 left join department d on dd.master_department_id = d.id
                 left join user u on dd.master_user_id = u.id
        where dd.delete_enum = 'FALSE'
          and dd.domain_department_id = #{domainDepartmentId}
    </select>
</mapper>

