<?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.DomainGroupMapper">

    <select id="listPage" resultType="com.zcloud.domain.persistence.dataobject.DomainGroupDO">
        select dg.*,
               c.corp_name                               as corpinfoname,
               d.name                                    as teamdepartmentname,
               u.name                                    as teamusername,
               count(distinct ptg.id) as projectTaskCount,
              SUM(CASE WHEN ptg.score_status = 2 THEN 1 ELSE 0 END) AS projectTaskFinishedCount
        from domain_group dg
                 left join corp_info c on dg.corpinfo_id = c.id and c.delete_enum = 'false'
                 left join department d on dg.team_department_id = d.id and d.delete_enum = 'false'
                 left join user u on dg.team_user_id = u.id and u.delete_enum = 'false'
                 left join project_task ptg on ptg.domain_group_id = dg.domain_group_id and ptg.delete_enum = 'false'

        where dg.delete_enum = 'false'
            <if test="params.domainType != null and params.domainType != ''">
                and dg.domain_type = #{params.domainType}
            </if>
            <if test="params.domainGroupName != null and params.domainGroupName != ''">
                and dg.domain_group_name like  CONCAT('%', #{params.domainGroupName}, '%')
            </if>
        group by dg.id
        order by dg.create_time desc
    </select>
    <select id="getInfoById" resultType="com.zcloud.domain.persistence.dataobject.DomainGroupDO">
        select dg.*,
               c.corp_name as corpinfoname,
               d.name      as teamdepartmentname,
               u.name      as teamusername
        from domain_group dg
                 left join corp_info c on dg.corpinfo_id = c.id and c.delete_enum = 'false'
                 left join department d on dg.team_department_id = d.id and d.delete_enum = 'false'
                 left join user u on dg.team_user_id = u.id and u.delete_enum = 'false'
        where dg.delete_enum = 'false' and dg.id=#{id}
    </select>
    <select id="listAll" resultType="com.zcloud.domain.persistence.dataobject.DomainGroupDO">
        select dg.*,
        c.corp_name                               as corpinfoName,
        d.name                                    as teamDepartmentName,
        u.name                                    as teamUserName
        from domain_group dg
            left join corp_info c on dg.corpinfo_id = c.id and c.delete_enum = 'false'
            left join department d on dg.team_department_id = d.id and d.delete_enum = 'false'
            left join user u on dg.team_user_id = u.id and u.delete_enum = 'false'
        where dg.delete_enum = 'false'
            <if test="domainType != null and domainType != ''">
                and dg.domain_type = #{domainType}
            </if>
            <if test="domainGroupName != null and domainGroupName != ''">
                and dg.domain_group_name like  CONCAT('%', #{domainGroupName}, '%')
            </if>
            <if test="corpinfoId != null ">
                and dg.corpinfo_id = #{corpinfoId}
            </if>
            order by dg.create_time desc
    </select>
    <select id="getInfoByGroupId" resultType="com.zcloud.domain.persistence.dataobject.DomainGroupDO">
        select dg.*,
               c.corp_name as corpinfoname,
               d.name      as teamdepartmentname,
               u.name      as teamusername
        from domain_group dg
                 left join corp_info c on dg.corpinfo_id = c.id and c.delete_enum = 'false'
                 left join department d on dg.team_department_id = d.id and d.delete_enum = 'false'
                 left join user u on dg.team_user_id = u.id and u.delete_enum = 'false'
        where dg.delete_enum = 'false' and dg.domain_group_id=#{domainGroupId}
    </select>
</mapper>

