<?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.xinyilu.system.mapper.applogkindergarteninfo.AppLogKindergartenInfoMapper">

    <resultMap type="AppLogKindergartenInfo" id="AppLogKindergartenInfoResult">
        <result property="id" column="id"/>
        <result property="appletLogInfoId" column="applet_log_info_id"/>
        <result property="areaId" column="area_id"/>
        <result property="areaCode" column="area_code"/>
        <result property="organizeId" column="organize_id"/>
        <result property="kindergartenId" column="kindergarten_id"/>
        <result property="classId" column="class_id"/>
        <result property="studentId" column="student_id"/>
        <result property="logTime" column="log_time"/>
        <result property="logDate" column="log_date"/>
        <result property="logType" column="log_type"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectAppLogKindergartenInfoVo">
        select id,
               applet_log_info_id,
               area_id,
               area_code,
               organize_id,
               kindergarten_id,
               class_id,
               student_id,
               log_time,
               log_date,
               log_type,
               create_by,
               create_time,
               update_by,
               update_time
        from app_log_kindergarten_info
    </sql>

    <select id="selectAppLogKindergartenInfoList" parameterType="AppLogKindergartenInfo"
            resultMap="AppLogKindergartenInfoResult">
        <include refid="selectAppLogKindergartenInfoVo"/>
        <where>
            <if test="appletLogInfoId != null ">
                and applet_log_info_id = #{appletLogInfoId}
            </if>
            <if test="areaId != null ">
                and area_id = #{areaId}
            </if>
            <if test="areaCode != null  and areaCode != ''">
                and area_code = #{areaCode}
            </if>
            <if test="organizeId != null ">
                and organize_id = #{organizeId}
            </if>
            <if test="kindergartenId != null ">
                and kindergarten_id = #{kindergartenId}
            </if>
            <if test="classId != null ">
                and class_id = #{classId}
            </if>
            <if test="studentId != null ">
                and student_id = #{studentId}
            </if>
            <if test="logType != null  and logType != ''">
                and log_type = #{logType}
            </if>
            <if test="logTime != null ">
                and log_time = #{logTime}
            </if>
            <if test="logDate != null ">
                and log_date = #{logDate}
            </if>
        </where>
    </select>

    <select id="selectAppLogKindergartenInfoById" parameterType="Long"
            resultMap="AppLogKindergartenInfoResult">
        <include refid="selectAppLogKindergartenInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertAppLogKindergartenInfo" parameterType="AppLogKindergartenInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_log_kindergarten_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="appletLogInfoId != null">applet_log_info_id,
            </if>
            <if test="areaId != null">area_id,
            </if>
            <if test="areaCode != null">area_code,
            </if>
            <if test="organizeId != null">organize_id,
            </if>
            <if test="kindergartenId != null">kindergarten_id,
            </if>
            <if test="classId != null">class_id,
            </if>
            <if test="studentId != null">student_id,
            </if>
            <if test="logTime != null">log_time,
            </if>
            <if test="logDate != null">log_date,
            </if>
            <if test="logType != null">log_type,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="appletLogInfoId != null">#{appletLogInfoId},
            </if>
            <if test="areaId != null">#{areaId},
            </if>
            <if test="areaCode != null">#{areaCode},
            </if>
            <if test="organizeId != null">#{organizeId},
            </if>
            <if test="kindergartenId != null">#{kindergartenId},
            </if>
            <if test="classId != null">#{classId},
            </if>
            <if test="studentId != null">#{studentId},
            </if>
            <if test="logTime != null">#{logTime},
            </if>
            <if test="logDate != null">#{logDate},
            </if>
            <if test="logType != null">#{logType},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateAppLogKindergartenInfo" parameterType="AppLogKindergartenInfo">
        update app_log_kindergarten_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="appletLogInfoId != null">applet_log_info_id =
                #{appletLogInfoId},
            </if>
            <if test="areaId != null">area_id =
                #{areaId},
            </if>
            <if test="areaCode != null">area_code =
                #{areaCode},
            </if>
            <if test="organizeId != null">organize_id =
                #{organizeId},
            </if>
            <if test="kindergartenId != null">kindergarten_id =
                #{kindergartenId},
            </if>
            <if test="classId != null">class_id =
                #{classId},
            </if>
            <if test="studentId != null">student_id =
                #{studentId},
            </if>
            <if test="logTime != null">log_time =
                #{logTime},
            </if>
            <if test="logDate != null">log_date =
                #{logDate},
            </if>
            <if test="logType != null">log_type =
                #{logType},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppLogKindergartenInfoById" parameterType="Long">
        delete
        from app_log_kindergarten_info
        where id = #{id}
    </delete>

    <delete id="deleteAppLogKindergartenInfoByIds" parameterType="String">
        delete from app_log_kindergarten_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertAppLogKindergartenInfoList">
        insert into app_log_kindergarten_info(
        applet_log_info_id,
        area_id,
        area_code,
        organize_id,
        kindergarten_id,
        class_id,
        student_id,
        log_time,
        log_date,
        log_type,
        create_time
        ) values
        <foreach item="appLogKindergartenInfoList" index="index" collection="list" separator=",">
            (
            #{appLogKindergartenInfoList.appletLogInfoId},
            #{appLogKindergartenInfoList.areaId},
            #{appLogKindergartenInfoList.areaCode},
            #{appLogKindergartenInfoList.organizeId},
            #{appLogKindergartenInfoList.kindergartenId},
            #{appLogKindergartenInfoList.classId},
            #{appLogKindergartenInfoList.studentId},
            #{appLogKindergartenInfoList.logTime},
            #{appLogKindergartenInfoList.logDate},
            #{appLogKindergartenInfoList.logType},
            #{appLogKindergartenInfoList.createTime}
            )
        </foreach>
    </insert>

    <select id="selectAppLogKindergartenCount"
            resultType="com.xinyilu.system.domain.vo.applogmanagestatistics.AppLogManageStatisticsVO">
        SELECT sa.NAME                                          areaName,
               (SELECT COUNT(1)
                FROM app_log_kindergarten_info
                WHERE area_id = #{areaId}
                  AND log_type = 'LOGIN')                       loginCount,
               (SELECT COUNT(1)
                FROM app_log_kindergarten_info
                WHERE area_id = #{areaId}
                  AND log_type = 'EXERCISE_PRESCRIPTION')       prescriptionCount,
               (SELECT COUNT(1)
                FROM app_log_kindergarten_info
                WHERE area_id = #{areaId}
                  AND log_type = 'EXERCISE_PRESCRIPTION_VIDEO') prescriptionVideoCount
        FROM app_log_kindergarten_info alki
                 LEFT JOIN sys_area sa ON sa.id = alki.area_id
        WHERE alki.area_id = #{areaId}
          and log_date &gt;= #{startDate}
          and log_date &lt;= #{endDate}
        GROUP BY alki.area_id
    </select>

    <select id="selectAppLogKindergartenCounts"
            resultType="com.xinyilu.system.domain.vo.applogmanagestatistics.AppLogManageStatisticsVO">
        SELECT
        sa.id areaId,
        sa.name areaName,
        ( SELECT COUNT( 1 ) FROM app_log_info
        <where>
            area_id = alki.area_id
            AND log_type = 'LOGIN'
            <if test="startDate != null">and log_date &gt;= #{startDate}</if>
            <if test="endDate != null">and log_date &lt;= #{endDate}</if>
        </where>
        ) loginCount,
        ( SELECT COUNT( 1 ) FROM app_log_info
        <where>
            area_id = alki.area_id
            AND log_type ='EXERCISE_PRESCRIPTION'
            <if test="startDate != null">and log_date &gt;= #{startDate}</if>
            <if test="endDate != null">and log_date &lt;= #{endDate}</if>
        </where>
        ) prescriptionCount,
        ( SELECT COUNT( 1 ) FROM app_log_info
        <where>
            area_id = alki.area_id
            AND log_type ='EXERCISE_PRESCRIPTION_VIDEO'
            <if test="startDate != null">and log_date &gt;= #{startDate}</if>
            <if test="endDate != null">and log_date &lt;= #{endDate}</if>
        </where>
        ) prescriptionVideoCount
        FROM
        sys_area sa
        LEFT JOIN app_log_kindergarten_info alki ON sa.id = alki.area_id
        LEFT JOIN app_log_info ali on ali.id = alki.applet_log_info_id
        <where>
            <if test="list != null">
                sa.id in
                <foreach item="list" collection="list" open="(" separator="," close=")">
                    #{list}
                </foreach>
            </if>
        </where>
        GROUP BY sa.id
    </select>
</mapper>
