<?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.template.mapper.SmartUserMapper">
    <resultMap type="com.template.model.vo.AffiliateUserVo" id="affiliateUserMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="cardNo" column="card_no"/>
        <result property="departmentId" column="department_id"/>
        <result property="headImage" column="head_image"/>
    </resultMap>

    <select id="queryAffiliateUserById" resultType="com.template.model.vo.AffiliateUserVo" resultMap="affiliateUserMap">
        select id,name,card_no,department_id,head_image from smart_user
        where find_in_set(id,(select affiliate from smart_user where
        deleted = 0
        <if test="id != null and id != ''">
            and id = #{id}
        </if>
        )) and deleted = 0
    </select>

    <resultMap type="com.template.model.vo.UserVo" id="smartUserPageMap">
        <result property="id" column="id"/>
        <result property="cardNo" column="card_no"/>
        <result property="name" column="name"/>
        <result property="identityId" column="identity_id"/>
        <result property="idCard" column="id_card"/>
        <result property="sexId" column="sex_id"/>
        <result property="departmentId" column="department_id"/>
        <result property="headImage" column="head_image"/>
        <result property="grade" column="grade"/>
        <result property="college" column="college"/>
        <result property="speciality" column="speciality"/>
        <result property="schoolClass" column="school_class"/>
        <result property="campus" column="campus"/>
        <result property="dormitoryNumber" column="dormitory_number"/>
        <result property="phone" column="phone"/>
        <result property="affiliate" column="affiliate"/>
        <result property="title" column="title"/>
        <result property="address" column="address"/>
        <result property="nation" column="nation"/>
        <result property="ofStudent" column="of_student"/>
        <result property="graduate" column="graduate"/>
        <result property="duties" column="duties"/>
        <result property="timeGroupId" column="time_group_id"/>
    </resultMap>
    <select id="querySmartUserPages" resultType="com.template.model.vo.UserVo" resultMap="smartUserPageMap">
        select
        id,card_no,name,identity_id,id_card,sex_id,department_id,head_image,grade,college,speciality,school_class,campus,dormitory_number,
        phone,affiliate,title,address,nation,of_student,graduate,duties,time_group_id
        from smart_user
        where deleted = 0 and is_cancel = 0
        <if test="departmentIds != null and departmentIds.size() > 0">
            and department_id in
            <foreach collection="departmentIds" item="departmentId" index="index" open="(" close=")" separator=",">
                ${departmentId}
            </foreach>
        </if>
        <if test="name != null and name != ''">
            and (name like '%' #{name} '%' or card_no like '%' #{name} '%')
        </if>
    </select>


    <resultMap type="com.template.model.pojo.SmartUser" id="smartUsersMap">
        <result property="cardNo" column="card_no"/>
        <result property="name" column="name"/>
        <result property="identityId" column="identity_id"/>
        <result property="idCard" column="id_card"/>
        <result property="sexId" column="sex_id"/>
        <result property="departmentId" column="department_id"/>
        <result property="headImage" column="head_image"/>
        <result property="grade" column="grade"/>
        <result property="college" column="college"/>
        <result property="speciality" column="speciality"/>
        <result property="schoolClass" column="school_class"/>
        <result property="campus" column="campus"/>
        <result property="dormitoryNumber" column="dormitory_number"/>
        <result property="phone" column="phone"/>
        <result property="affiliate" column="affiliate"/>
        <result property="title" column="title"/>
        <result property="address" column="address"/>
        <result property="nation" column="nation"/>
        <result property="ofStudent" column="of_student"/>
        <result property="graduate" column="graduate"/>
        <result property="duties" column="duties"/>
    </resultMap>
    <select id="querySmartUsers" resultType="com.template.model.pojo.SmartUser" resultMap="smartUsersMap">
        select
        card_no,name,identity_id,id_card,sex_id,department_id,head_image,grade,college,speciality,school_class,campus,dormitory_number,
        phone,affiliate,title,address,nation,of_student,graduate,duties from smart_user
        where deleted = 0 and is_cancel = 0
        <if test="departmentIds != null and departmentIds.size() > 0">
            and department_id in
            <foreach collection="departmentIds" item="departmentId" index="index" open="(" close=")" separator=",">
                ${departmentId}
            </foreach>
        </if>
        <if test="name != null and name != ''">
            and name like '%' #{name} '%'
        </if>
    </select>


    <resultMap type="com.template.model.vo.GradeVo" id="smartSecordUserPageMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="grade" column="grade"/>
        <result property="schoolClass" column="school_class"/>
        <result property="cardNo" column="card_no"/>
    </resultMap>
    <select id="querySmartSecordPage" resultType="com.template.model.vo.GradeVo" resultMap="smartSecordUserPageMap">
        select id,name,grade,school_class,card_no from smart_user
        where deleted = 0 and is_cancel = 0 and identity_id = 2
        <if test="name != null and name != ''">
            and name like '%' #{name} '%'
        </if>
        <if test="userId != null and userId != '' and userId != 0">
            and id != #{userId}
        </if>
    </select>

    <select id="warningUserList" resultType="com.template.model.dto.WarningUserDto">
        SELECT
        su.id AS id,
        su.`name` AS `name`,
        su.card_no AS cardNo,
        sd.`name` AS departmentName
        FROM
        `smart_user` su
        LEFT JOIN smart_department sd ON sd.id = su.department_id and sd.deleted=0
        where su.deleted = 0 and su.is_cancel = 0
        <if test="name != null and name != ''">
            and su.`name` like '%' #{name} '%'
        </if>
    </select>

    <select id="warningPushList" resultType="com.template.model.dto.WarningUserDto">
        SELECT su.id      AS id,
               su.`name`  AS `name`,
               su.card_no AS cardNo,
               sd.`name`  AS departmentName
        FROM `smart_user` su
                 LEFT JOIN smart_department sd ON sd.id = su.department_id and sd.deleted = 0
        WHERE su.is_push = 1
          and su.is_cancel = 0
          and su.deleted = 0
    </select>


    <resultMap type="com.template.model.vo.AffiliateParentVo" id="affiliateParentMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
    </resultMap>

    <select id="queryAffiliateParents" resultType="com.template.model.vo.AffiliateParentVo"
            resultMap="affiliateParentMap">
        SELECT id,name FROM `smart_user`
        where deleted = 0 and is_cancel=0
        <if test="userId != null and userId != ''">
            and FIND_IN_SET(#{userId},affiliate)
        </if>
    </select>

    <select id="getAffiliateList" resultType="com.template.model.pojo.SmartUser">
        SELECT * FROM `smart_user`
        where deleted = 0 and is_cancel=0
        <if test="id != null and id != ''">
            and FIND_IN_SET(#{id},affiliate)
        </if>
    </select>


    <resultMap type="com.template.model.vo.SmartUserVo" id="smartUserMap">
        <result property="id" column="id"/>
        <result property="grade" column="grade"/>
        <result property="gradeId" column="grade_id"/>
        <result property="schoolClass" column="school_class"/>
        <result property="schoolClassId" column="school_class_id"/>
        <result property="name" column="name"/>
        <result property="cardNo" column="card_no"/>
    </resultMap>
    <select id="querySmartUserById" resultType="com.template.model.vo.SmartUserVo" resultMap="smartUserMap">
        select su.id,sg.name as grade,sg.id as grade_id,sc.name as school_class,sc.id as
        school_class_id,su.name,su.card_no
        from smart_user su
        left join smart_grade sg on sg.id = su.grade and sg.deleted = 0
        left join smart_class sc on sc.id = su.school_class and sc.deleted = 0
        where su.deleted = 0
        <if test="userId != null and userId != '' and userId != 0">
            and su.id = #{userId}
        </if>
    </select>


    <update id="deleteUserBatch" parameterType="com.template.model.vo.deleteUserVo">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update smart_user
            <set>
                delete_reason=#{item.reason}
            </set>
            where id = ${item.id}
        </foreach>
    </update>


    <resultMap type="com.template.model.vo.UserDeleteVo" id="smartUserDeletePageMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="identityId" column="identity_id"/>
        <result property="departmentId" column="department_id"/>
        <result property="cardNo" column="card_no"/>
        <result property="headImage" column="head_image"/>
        <result property="deleteReason" column="delete_reason"/>
    </resultMap>
    <select id="queryUserDeletePage" resultType="com.template.model.vo.UserDeleteVo" resultMap="smartUserDeletePageMap">
        select id, name, identity_id, department_id, card_no, head_image, delete_reason
        from smart_user
        where delete_reason != '' or delete_reason != null
        order by update_time desc
    </select>


    <resultMap type="com.template.model.vo.SmartStudentVo" id="smartStudentMap">
        <result property="id" column="id"/>
        <result property="identityId" column="identity_id"/>
        <result property="bsStudentNo" column="bs_student_no"/>
        <result property="bsGradeNo" column="bs_grade_no"/>
        <result property="bsClassNo" column="bs_class_no"/>
    </resultMap>
    <select id="querySmartStudentById" resultType="com.template.model.vo.SmartStudentVo" resultMap="smartStudentMap">
        SELECT su.id,su.identity_id,su.bs_student_no,sg.bs_grade_no,sc.bs_class_no FROM smart_user su
        left join smart_grade sg on sg.deleted = 0 and sg.id = su.grade
        left join smart_class sc on sc.deleted = 0 and sc.id = su.school_class
        where su.deleted = 0
        <if test="userId != null and userId != '' and userId != 0">
            and su.id = #{userId}
        </if>
    </select>

    <select id="getAffiliateUser" resultType="com.template.model.vo.SmartUserAffiliateVo">
        SELECT su.*,sc.name as className FROM `smart_user` su
        left join smart_class sc on su.school_class=sc.id
        where su.deleted = 0 and su.is_cancel=0
        <if test="affiliate != null and affiliate != ''">
            and FIND_IN_SET(su.id,#{affiliate})
        </if>
    </select>
    <select id="studentSelect" resultType="com.template.model.vo.StudentSelectVo">
        SELECT su.`name`     as name,
               su.head_image as headImage,
               su.card_no    as cardNo,
               sc.`name`     as className
        FROM `smart_user` su
                 LEFT JOIN smart_class sc on su.school_class = sc.id
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND (su.`name` LIKE '%' #{keyWord} '%' or su.card_no= #{keyWord})
    </select>


    <select id="querySmartParentByIds" resultType="com.template.model.pojo.SmartUser">
        select * from smart_user
        where deleted = 0 and identity_id = 1 and is_cancel = 0
        <if test="studentIds != null and studentIds.size() > 0">
            and
            <foreach collection="studentIds" item="studentId" index="index" open="(" close=")" separator="or">
                FIND_IN_SET(${studentId},affiliate)
            </foreach>
        </if>
    </select>
    <select id="getAccessStatisticsCampus" resultType="com.template.model.vo.StatisticsCampusVo">
        SELECT su.id      as userId,
               su.`name`  as name,
               su.card_no as cardNo
        FROM `smart_user` su
                 LEFT JOIN smart_access sa on sa.user_id = su.id
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND su.school_class = #{classId}
          AND sa.date_time >= #{startTime}
          AND #{endTime} >= sa.date_time
        GROUP BY su.id
    </select>
    <select id="getAttendanceStatisticsCampus" resultType="com.template.model.vo.StatisticsCampusVo">
        SELECT su.id      as userId,
               su.`name`  as name,
               su.card_no as cardNo
        FROM `smart_user` su
                 LEFT JOIN smart_attendance sat on sat.user_id = su.id
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND su.school_class = #{classId}
          AND sat.`status`!=3
          AND sat.attend_time >= #{startTime}
          AND #{endTime} >= sat.attend_time
        GROUP BY su.id
    </select>
    <select id="getFaceDiscernStatisticsCampus" resultType="com.template.model.vo.StatisticsCampusVo">
        SELECT su.id      as userId,
               su.`name`  as name,
               su.card_no as cardNo
        FROM `smart_user` su
                 LEFT JOIN smart_face_discern sfd on sfd.user_id = su.id
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND su.school_class = #{classId}
          AND sfd.date_time >= #{startTime}
          AND #{endTime} >= sfd.date_time
        GROUP BY su.id
    </select>
    <select id="getAckStatisticsCampus" resultType="com.template.model.vo.StatisticsCampusVo">
        SELECT su.id      as userId,
               su.`name`  as name,
               su.card_no as cardNo
        FROM `smart_user` su
                 LEFT JOIN smart_attendance sat on sat.user_id = su.id
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND su.school_class = #{classId}
          AND sat.`status` = 6
          AND sat.initiate_time >= #{startTime}
          AND #{endTime} >= sat.initiate_time
        GROUP BY su.id
    </select>
    <select id="getClassStudent" resultType="com.template.model.vo.StatisticsCampusVo">
        SELECT su.id      as userId,
               su.`name`  as name,
               su.card_no as cardNo
        FROM `smart_user` su
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND su.school_class = #{classId}
    </select>
    <select id="getAccessStatisticsCampusManage" resultType="com.template.model.vo.StatisticsCampusManageVo">
        SELECT su.id as userId,
        su.`name` as name,
        su.card_no as cardNo,
        sc.`name` as className,
        sg.`name` as gradeName
        FROM `smart_user` su
        LEFT JOIN smart_access sa on sa.user_id = su.id
        LEFT JOIN smart_class sc on sc.id = su.school_class
        LEFT JOIN smart_grade sg on sg.id = sc.grade_id
        WHERE su.deleted = 0
        AND su.is_cancel = 0
        AND su.identity_id = 2
        AND sc.grade_id = #{gradeId}
        AND su.school_class = #{classId}
        AND sa.date_time >= #{startTime}
        AND #{endTime} >= sa.date_time
        <if test="keyWord != null and keyWord != '' ">
            and (su.name like '%' #{keyWord} '%' or su.card_no like '%' #{keyWord} '%')
        </if>
        GROUP BY su.id
    </select>

    <select id="getAttendanceStatisticsCampusManage"
            resultType="com.template.model.vo.StatisticsCampusManageVo">
        SELECT su.id as userId,
        su.`name` as name,
        su.card_no as cardNo,
        sc.`name` as className,
        sg.`name` as gradeName
        FROM `smart_user` su
        LEFT JOIN smart_attendance sat on sat.user_id = su.id
        LEFT JOIN smart_class sc on sc.id = su.school_class
        LEFT JOIN smart_grade sg on sg.id = sc.grade_id
        WHERE su.deleted = 0
        AND su.is_cancel = 0
        AND su.identity_id = 2
        AND sc.grade_id = #{gradeId}
        AND su.school_class = #{classId}
        AND sat.`status`!=3
        AND sat.attend_time >= #{startTime}
        AND #{endTime} >= sat.attend_time
        <if test="keyWord != null and keyWord != '' ">
            and (su.name like '%' #{keyWord} '%' or su.card_no like '%' #{keyWord} '%')
        </if>
        GROUP BY su.id
    </select>

    <select id="getFaceDiscernStatisticsCampusManage"
            resultType="com.template.model.vo.StatisticsCampusManageVo">

        SELECT su.id as userId,
        su.`name` as name,
        su.card_no as cardNo,
        sc.`name` as className,
        sg.`name` as gradeName
        FROM
        `smart_user` su
        LEFT JOIN smart_face_discern sfd on sfd.user_id=su.id
        LEFT JOIN smart_class sc on sc.id=su.school_class
        LEFT JOIN smart_grade sg on sg.id=sc.grade_id
        WHERE
        su.deleted = 0
        AND su.is_cancel = 0
        AND su.identity_id = 2
        AND sc.grade_id = #{gradeId}
        AND su.school_class=#{classId}
        AND sfd.date_time >= #{startTime}
        AND #{endTime} >= sfd.date_time
        <if test="keyWord != null and keyWord != '' ">
            and (su.name like '%' #{keyWord} '%' or su.card_no like '%' #{keyWord} '%')
        </if>
        GROUP BY su.id


    </select>
    <select id="getAckStatisticsCampusManage" resultType="com.template.model.vo.StatisticsCampusManageVo">
        SELECT su.id as userId,
        su.`name` as name,
        su.card_no as cardNo,
        sc.`name` as className,
        sg.`name` as gradeName
        FROM
        `smart_user` su
        LEFT JOIN smart_attendance sat on sat.user_id=su.id
        LEFT JOIN smart_class sc on sc.id=su.school_class
        LEFT JOIN smart_grade sg on sg.id=sc.grade_id
        WHERE
        su.deleted = 0
        AND su.is_cancel = 0
        AND su.identity_id = 2
        AND sc.grade_id = #{gradeId}
        AND su.school_class=#{classId}
        AND sat.`status`=6
        AND sat.initiate_time >= #{startTime}
        AND #{endTime} >= sat.initiate_time
        <if test="keyWord != null and keyWord != '' ">
            and (su.name like '%' #{keyWord} '%' or su.card_no like '%' #{keyWord} '%')
        </if>
        GROUP BY su.id
    </select>
    <select id="getClassStudentManage" resultType="com.template.model.vo.StatisticsCampusManageVo">
        SELECT su.id      as userId,
               su.`name`  as name,
               su.card_no as cardNo,
               sc.`name`  as className,
               sg.`name`  as gradeName
        FROM `smart_user` su
                 LEFT JOIN smart_class sc on sc.id = su.school_class
                 LEFT JOIN smart_grade sg on sg.id = sc.grade_id
        WHERE su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND sc.grade_id = #{gradeId}
          AND su.school_class = #{classId}
        <if test="keyWord != null and keyWord != '' ">
            and (su.name like '%' #{keyWord} '%' or su.card_no like '%' #{keyWord} '%')
        </if>
    </select>
    <select id="getPage" resultType="com.template.model.vo.SmartUserPageVo">
        SELECT su.id as id,
        su.`name`  as name,
        su.card_no as cardNo,
        sc.`name`  as className
        FROM `smart_user` su
        LEFT JOIN smart_class sc on sc.id = su.school_class
        WHERE su.deleted = 0
        AND su.is_cancel = 0
        AND su.identity_id = 2
        <if test="schoolClass != null and schoolClass != '' ">
            AND su.school_class = #{schoolClass}
        </if>
        <if test="keyWord != null and keyWord != '' ">
            and su.name like '%' #{keyWord} '%'
        </if>
    </select>
    <select id="getUserClassTotal" resultType="com.template.model.vo.StatisticsStatement">
        SELECT
            sc.`name` AS className,
            sg.`name` AS gradeName,
            count( su.id ) as count,
            sc.id as classId,
            sg.id as gradeId
        FROM
            smart_user su
                LEFT JOIN smart_class sc ON sc.id = su.school_class
                LEFT JOIN smart_grade sg ON sg.id = sc.grade_id
        WHERE
            su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
        GROUP BY
            sc.id
    </select>
    <select id="getUserAckTotal" resultType="com.template.model.vo.StatisticsStatement">
        SELECT
            sc.`name` AS className,
            sg.`name` AS gradeName,
            count( su.id ) as count,
            sc.id as classId,
            sg.id as gradeId
        FROM
            `smart_user` su
                LEFT JOIN smart_class sc ON sc.id = su.school_class
                LEFT JOIN smart_grade sg ON sg.id = sc.grade_id
                LEFT JOIN smart_attendance sat ON sat.user_id = su.id
        WHERE
            su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND sat.initiate_time >= #{startTime}
          AND #{endTime} >= sat.initiate_time
          AND sat.`status` = 6
        GROUP BY
            sc.id
    </select>
    <select id="getUserNormalTotal" resultType="com.template.model.vo.StatisticsStatement">
        SELECT
            sc.`name` AS className,
            sg.`name` AS gradeName,
            count( su.id ) as count,
            sc.id as classId,
            sg.id as gradeId
        FROM
            `smart_user` su
                LEFT JOIN smart_class sc ON sc.id = su.school_class
                LEFT JOIN smart_grade sg ON sg.id = sc.grade_id
                LEFT JOIN ( SELECT DISTINCT ( user_id ) FROM smart_access sa WHERE sa.date_time >= #{startTime} AND #{endTime} >= sa.date_time ) a ON su.id = a.user_id
                LEFT JOIN ( SELECT DISTINCT ( user_id ) FROM smart_face_discern sfd WHERE sfd.date_time >= #{startTime} AND #{endTime} >= sfd.date_time ) b ON su.id = b.user_id
                LEFT JOIN (
                SELECT DISTINCT
                    ( user_id )
                FROM
                    smart_attendance sat
                WHERE
                    ( sat.attend_time >= #{startTime} AND #{endTime} >= sat.attend_time AND sat.`status` != 3 )
                   OR ( sat.initiate_time >= #{startTime} AND #{endTime} >= sat.initiate_time AND sat.`status` = 6 )
            ) c ON su.id = c.user_id
        WHERE
            su.deleted = 0
          AND su.is_cancel = 0
          AND su.identity_id = 2
          AND ( a.user_id IS NOT NULL OR b.user_id IS NOT NULL OR c.user_id IS NOT NULL )
        GROUP BY
            sc.id
    </select>

</mapper>
