<?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.yida.data.user.mapper.EduFaceGroupMapper">
    <resultMap id="pageMap" type="com.yida.data.common.core.entity.user.EduFaceGroup">
        <id property="id" column="id"/>
        <result property="groupName" column="group_name"/>
        <result property="description" column="description"/>
        <result property="issueNum" column="issue_num"/>
        <result property="totalNum" column="total_num"/>
        <result property="type" column="type"/>

        <association property="totalNum" javaType="int"
                     select="getTotalNum"
                     column="{groupId=id}">
        </association>
        <association property="issueNum" javaType="int"
                     select="getIssueNum"
                     column="{groupId=id}">
        </association>

        <collection column="id" property="deviceList" ofType="com.yida.data.common.core.entity.user.EduFaceGroupDevice"
                    select="selectDevice">
        </collection>
    </resultMap>
    <select id="selectDevice" resultType="com.yida.data.common.core.entity.user.EduFaceGroupDevice">
        select *
        from edu_face_group_device
        where face_group_id=#{id}
    </select>

<!--    <select id="listFaceGroupPage" resultMap="pageMap">-->
<!--        select a.id,a.group_name,a.description,a.type,-->
<!--        sum(if(b.status=1,1,0)) issue_num,-->
<!--        count(b.id) total_num-->
<!--        from edu_face_group a-->
<!--        left join edu_face_group_user b on a.id=b.face_group_id-->
<!--        where a.del_flag=0-->
<!--        and a.school_id=#{schoolId}-->
<!--        <if test='groupName != null and groupName != ""'>-->
<!--            and a.group_name like concat('%',#{groupName},'%')-->
<!--        </if>-->
<!--        <if test="groupId">-->
<!--            and a.id = #{groupId}-->
<!--        </if>-->
<!--        group by a.id,a.group_name,a.description,a.create_date-->
<!--        order by a.create_date desc-->
<!--    </select>-->
    <select id="listFaceGroupPage" resultMap="pageMap">
        select a.id,a.group_name,a.description,a.type
        from edu_face_group a
        where a.del_flag=0
        and a.school_id=#{schoolId}
        <if test='groupName != null and groupName != ""'>
            and a.group_name like concat('%',#{groupName},'%')
        </if>
        <if test="groupId">
            and a.id = #{groupId}
        </if>
        order by a.create_date desc
    </select>

    <!-- 查询总数量 -->
    <select id="getTotalNum" resultType="java.lang.Integer">
        SELECT
            SUM( a.num )
        FROM
            (
                SELECT
                    COUNT( s.id ) num
                FROM
                    edu_student s
                LEFT JOIN edu_face_group_user u ON ( s.id = u.user_id AND u.user_type = 0 )
                WHERE
                    s.del_flag = 0
                  AND u.user_type = 0
                  AND u.face_group_id = #{groupId}
                UNION ALL
                SELECT
                    COUNT( s.id ) num
                FROM
                    edu_staff s
                LEFT JOIN edu_face_group_user u ON ( s.id = u.user_id AND u.user_type = 1 )
                WHERE
                    s.del_flag = 0
                  AND u.user_type = 1
                  AND u.face_group_id = #{groupId}
            ) AS a
    </select>

    <!-- 查询已下发数量 -->
    <select id="getIssueNum" resultType="java.lang.Integer">
        SELECT
            SUM( a.num )
        FROM
            (
                SELECT
                    COUNT( s.id ) num
                FROM
                    edu_student s
                LEFT JOIN edu_face_group_user u ON ( s.id = u.user_id AND u.user_type = 0 )
                WHERE
                    s.del_flag = 0
                  AND u.user_type = 0
                  AND u.status = 1
                  AND u.face_group_id = #{groupId}
                UNION ALL
                SELECT
                    COUNT( s.id ) num
                FROM
                    edu_staff s
                LEFT JOIN edu_face_group_user u ON ( s.id = u.user_id AND u.user_type = 1 )
                WHERE
                    s.del_flag = 0
                  AND u.user_type = 1
                  AND u.status = 1
                  AND u.face_group_id = #{groupId}
            ) AS a
    </select>

</mapper>
