<?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.zhongzheng.modules.user.mapper.UserMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.User" id="UserResult">
        <result property="userId" column="user_id"/>
        <result property="userAccount" column="user_account"/>
        <result property="nickname" column="nickname"/>
        <result property="realname" column="realname"/>
        <result property="sex" column="sex"/>
        <result property="idCard" column="id_card"/>
        <result property="telphone" column="telphone"/>
        <result property="userLevel" column="user_level"/>
        <result property="userBirth" column="user_birth"/>
        <result property="eduLevel" column="edu_level"/>
        <result property="schoolId" column="school_id"/>
        <result property="major" column="major"/>
        <result property="entranceTime" column="entrance_time"/>
        <result property="openId" column="open_id"/>
        <result property="idCardImg1" column="id_card_img1"/>
        <result property="idCardImg2" column="id_card_img2"/>
        <result property="country" column="country"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="integral" column="integral"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="unionId" column="union_id"/>
        <result property="registerPlat" column="register_plat"/>
        <result property="lastLoginIp" column="last_login_ip"/>
        <result property="lastLoginTime" column="last_login_time"/>
        <result property="certified" column="certified"/>
        <result property="certifiedTime" column="certified_time"/>
        <result property="district" column="district"/>
        <result property="marry" column="marry"/>
        <result property="houseProvince" column="house_province"/>
        <result property="houseCity" column="house_city"/>
        <result property="houseDistrict" column="house_district"/>
        <result property="oneInchPhotos" column="one_inch_photos"/>
        <result property="politic" column="politic"/>
        <result property="email" column="email"/>
        <result property="avatar" column="avatar"/>
        <result property="inviteUserAccount" column="invite_user_account"/>
        <result property="courseNum" column="course_num"/>
        <result property="studyTime" column="study_time"/>
        <result property="password" column="password"/>
    </resultMap>


    <resultMap type="com.zhongzheng.modules.user.vo.RanKingUser" id="RanKingUserResult">
        <result property="hourse" column="sum_study"/>
        <result property="avatar" column="avatar"/>
        <result property="nickname" column="nickname"/>
        <result property="rank" column="pm"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.user.vo.UserStudyRecordVo" id="StudyRecordResult">
        <result property="telphone" column="telphone"/>
        <result property="realname" column="realname"/>
        <result property="endTime" column="end_time"/>
        <result property="startTime" column="start_time"/>
        <result property="title" column="title"/>
        <result property="courseName" column="course_name"/>
        <result property="studyDuration" column="study_duration"/>
    </resultMap>



    <select id="selectByRank" parameterType="com.zhongzheng.modules.user.bo.RanKingUserQuery" resultMap="RanKingUserResult">
        SELECT sum(s.study_duration) AS sum_study,u.nickname,u.avatar   FROM `user` u
            LEFT JOIN user_study_record s ON u.user_id=s.user_id where 1=1
        <if test="userId != null and userId != ''">
            AND u.user_id = #{userId}
        </if>
        <if test="startTime != null and startTime != ''">
            AND s.create_time BETWEEN #{startTime} and #{endTime}
        </if>
        GROUP BY u.user_id
    </select>

    <select id="selectByRanks" parameterType="com.zhongzheng.modules.user.bo.RanKingUserQuery" resultMap="RanKingUserResult">
        SELECT A.*
        FROM
            (
            SELECT sum(s.study_duration) AS sum_study,u.nickname,u.avatar  FROM `user` u
            LEFT JOIN user_study_record s ON u.user_id=s.user_id where 1=1
        <if test="startTime != null and startTime != ''">
            AND s.create_time BETWEEN #{startTime} and #{endTime}
        </if>
            GROUP BY u.user_id ORDER BY sum_study desc LIMIT 20
            ) A where
        A.sum_study >0
    </select>

    <select id="selectByRankOne" parameterType="integer" resultType="int">
        SELECT
            COUNT(s.sum_study)
        FROM
            (SELECT sum( s.study_duration ) AS sum_study FROM user_study_record s  WHERE 1 = 1 GROUP BY s.user_id) s
        WHERE s.sum_study >= #{hourse}
    </select>

    <select id="selectByRankOneByTime" parameterType="map" resultType="int">
        SELECT
            COUNT(s.sum_study)
        FROM
            (SELECT sum( s.study_duration ) AS sum_study FROM user_study_record s  WHERE 1 = 1
        <if test="startTime != null and startTime != ''">
            and s.create_time BETWEEN #{startTime} and #{endTime}
        </if>
            GROUP BY s.user_id) s
        WHERE s.sum_study >= #{hourse}

    </select>


    <select id="selectByOneRank" parameterType="com.zhongzheng.modules.user.bo.RanKingUserQuery" resultMap="RanKingUserResult">
        SELECT u.nickname,u.avatar   FROM `user` u
        where 1=1
        <if test="userId != null and userId != ''">
            AND u.user_id = #{userId}
        </if>
        GROUP BY u.user_id
    </select>
    <select id="selectUserNum" parameterType="integer" resultType="int">
        SELECT
            COUNT(user_id)
        FROM
            `user`
        WHERE 1=1 and user_level = #{grade}
    </select>

    <select id="selectList" parameterType="com.zhongzheng.modules.user.bo.UserQueryBo" resultMap="UserResult">
        SELECT
        u.*
        FROM
        `user` u
        WHERE
        1 = 1
        <if test="status != null and status.size()!=0 ">
            AND u.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="nickname != null and nickname != ''" >
            AND u.nickname like concat('%', #{nickname}, '%')
        </if>
        <if test="telphone != null and telphone != ''" >
            AND u.telphone = #{telphone}
        </if>
        <if test="idCard != null and idCard != ''" >
            AND u.id_card = #{idCard}
        </if>
          <if test="realname != null and realname != ''" >
            AND u.realname like concat('%', #{realname}, '%')
        </if>
        <if test="userId != null and userId != ''" >
            AND u.user_id = #{userId}
        </if>
        <if test="empty != null and empty != ''" >
           and u.realname is NOT NULL
        </if>
        <if test="gradePoint != null and gradePoint == 1" >
            AND (SELECT COUNT(u.user_id) FROM class_grade_user cgu where 1=1 AND u.status in (1) and cgu.user_id= u.user_id) > 0
        </if>
        <if test="gradePoint != null and gradePoint == 2" >
            AND (SELECT COUNT(u.user_id) FROM class_grade_user cgu where 1=1 AND u.status in (1) and cgu.user_id= u.user_id) = 0
        </if>
    </select>


    <select id="selectStudyRecordList" resultMap="StudyRecordResult">
        SELECT
            usr.study_duration,
            c.course_name,
            pv.title,
            usr.start_time,
            usr.end_time,
            u.realname,
            u.telphone
        FROM
            user_study_record usr
                LEFT JOIN course c ON usr.course_id = c.course_id
                LEFT JOIN course_section cs ON usr.section_id = cs.section_id
                LEFT JOIN polyv_video pv ON cs.polyv_id = pv.polyv_id
                LEFT JOIN `user` u ON usr.user_id = u.user_id
        WHERE 1=1
        <if test="userId != null and userId != ''" >
            AND usr.user_id = #{userId}
        </if>
        <if test="courseName != null and courseName != ''" >
            AND c.course_name like concat('%', #{courseName}, '%')
        </if>
        <if test="title != null and title != ''" >
            AND pv.title like concat('%', #{title}, '%')
        </if>
        <if test="startTime1 != null and startTime1 != ''" >
            AND usr.start_time > #{startTime1}
        </if>
        <if test="startTime2 != null and startTime2 != ''" >
            AND #{startTime2} > usr.start_time
        </if>
        <if test="endTime1 != null and endTime1 != ''" >
            AND usr.end_time > #{endTime1}
        </if>
        <if test="endTime2 != null and endTime2 != ''" >
            AND #{endTime2} > usr.end_time
        </if>
        ORDER BY
            usr.record_id DESC
    </select>


    <select id="selectLookNum" parameterType="Long" resultType="integer">
        SELECT
            COUNT( user_id )
        FROM
            recruit_interview_message
        WHERE
            1 = 1
          AND user_id = #{userId}
          AND look_status = 0
    </select>

</mapper>
