<?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.qinxx.hslink.dao.HSLinkMapper">

    <!--登录验证-->
    <select id="login" resultType="map" parameterType="map">
        select * from hs_user where (real_name = #{username}  and pass_word = #{password} and user_type = #{role})
        or (mobile = #{username} and pass_word = #{password}  and user_type = #{role})
    </select>

    <!--注册-->
    <insert id="register" parameterType="map">
       INSERT INTO hs_user ( user_id, pass_word,real_name, mobile, user_type, create_time, frozen_state, error_times)
        VALUES
        (
        uuid(),
        #{password},
        #{realname},
        #{mobile},
        #{type},
        now(),
        '00',
        '0'
        )
    </insert>

    <!--获取校园通知-->
    <select id="getSchoolNotice" parameterType="map" resultType="map">
        SELECT
        s.id,
        label,
        title,
        content,
        user_id,
        real_name,
        release_time
        FROM
        hs_notice s left join hs_user u on s.release_id = u.user_id
        where type = '校园通知'
        and status = "1"
	    <if test="id != null and id != ''">
            and s.id = #{id}
        </if>
        <if test="type != null and type != ''">
            and label = #{label}
        </if>
        ORDER BY release_time desc
    </select>

    <!--获取家长建议-->
    <select id="getParentAdvice" parameterType="map" resultType="map">
        SELECT
        p.id,
        label,
        title,
        content,
        user_id,
        real_name,
        release_time
        FROM
        hs_notice p left join hs_user u on p.release_id = u.user_id
        where type = '家长建议'
        and status = "1"
        <if test="id != null and id != ''">
            and p.id = #{id}
        </if>
        <if test="type != null and type != ''">
            and label = #{label}
        </if>
        ORDER BY release_time desc
    </select>

    <!--获取学生想法-->
    <select id="getStudentThinking" parameterType="map" resultType="map">
        SELECT
        s.id,
        label,
        title,
        content,
        user_id,
        real_name,
        release_time
        FROM
        hs_notice s left join hs_user u on s.release_id = u.user_id
        where type = '学生想法'
        and status = "1"
        <if test="id != null and id != ''">
            and s.id = #{id}
        </if>
        <if test="type != null and type != ''">
            and label = #{label}
        </if>
        ORDER BY release_time desc
    </select>

    <!--获取一条数据的具体内容-->
    <select id="getOneContent" resultType="map" parameterType="map">
        select
        id,
        release_id author_id,
        real_name author_name,
        create_time,
        label,
        title,
        content,
        integral,
        '' header_photo
        from hs_notice n left join hs_user u on n.release_id = u.user_id
        where id = #{id}
    </select>

    <!--获取一条数据的留言-->
    <select id="getOneLeaveMessage" parameterType="map" resultType="map">
        select id,
		messager_id,
		content,
		hs_leave_message.create_time,
		real_name,
		'' header_photot from hs_leave_message left join hs_user  on messager_id = user_id where notice_id = #{id} ORDER BY create_time desc
    </select>

    <!--获取某些条件的数据-->
    <select id="getListByAttribute" resultType="map" parameterType="map">
        SELECT
        s.id,
        label,
        title,
        content,
        user_id,
        real_name,
        release_time,
        u.real_name,
        u.integral,
        '' header_photo,
        signature,
        type
        FROM
        hs_notice s left join hs_user u on s.release_id = u.user_id
        where 1 = 1
        and status = "1"
        <if test="releaseId != null and releaseId != ''">
            and release_id = #{releaseId}
        </if>
        <if test="release_time != null and release_time != ''">
            and release_time like CONCAT('%',#{release_time},'%')
        </if>
        <if test="type != null and type != ''">
            and type = #{type}
        </if>
        and (label like CONCAT('%',#{text},'%')
        or title like CONCAT('%',#{text},'%')
        or content like CONCAT('%',#{text},'%')
        or real_name like CONCAT('%',#{text},'%'))
        ORDER BY release_time desc
    </select>

    <!--新增留言-->
    <insert id="addMessage" parameterType="map">
       insert into hs_leave_message(id,notice_id,messager_id,content,create_time) VALUES (uuid(),#{noticeId},#{userId},#{content},now())
    </insert>

    <!--新增一篇文章-->
    <insert id="addArticle" parameterType="map">
        insert into hs_notice (id,label,title,content,release_id,release_time,type)
        values
        (uuid(),#{label},#{title},#{content},#{release_id},now(),#{type})
    </insert>

    <!--新增一条班级通知-->
    <insert id="addClassNotice" parameterType="map">
        insert into hs_notice (id,class_id,class_name,label,title,content,release_id,release_time,type)
        values
        (uuid(),#{classId},#{className},#{label},#{title},#{content},#{releaseId},#{release_time},"班级通知")
    </insert>

    <!--获取班级公告-->
    <select id="getClassBulletin" parameterType="map" resultType="map">
        SELECT
        DISTINCT
        id,
        c.class_id,
        c.class_name,
        title,
        content,
        release_time
        FROM
        hs_class_bulletin c
        left JOIN hs_user u ON c.class_name = u.class_name
        WHERE
        c.class_name = ( SELECT class_name FROM hs_user WHERE user_id = #{id} )
    </select>

    <!--获取班级通知-->
    <select id="getClassNotice" parameterType="map" resultType="map">
        select * from hs_notice left join hs_user on release_id = user_id
        where hs_notice.class_name = (select class_name from hs_user
        where user_id = #{id})
        <if test="text != null and text != ''">
            and (label like CONCAT('%',#{text},'%')
            or title like CONCAT('%',#{text},'%')
            or content like CONCAT('%',#{text},'%')
            or real_name like CONCAT('%',#{text},'%'))
        </if>
        and type = "班级通知"
        and status = "1"
        ORDER BY release_time desc
    </select>

    <!--获取班级成员-->
    <select id="getStudents" parameterType="map" resultType="map">
        select * from hs_user where class_name = (select class_name from hs_user where user_id = #{id})
    </select>

    <!--新增班级公告-->
    <insert id="addBulletin" parameterType="map">
        insert into hs_class_bulletin (id,class_id,class_name,title,content,release_time) VALUES (uuid(),#{classId},#{className},#{title},#{content},now())
    </insert>

    <!--修改班级公告-->
    <update id="updateBulletin" parameterType="map">
        update hs_class_bulletin set class_id = #{classId},class_name=#{className},title=#{title},content=#{content},release_time=now() where id = #{id}
    </update>

    <!--修改密码-->
    <update id="updatePassword" parameterType="map">
        update hs_user set pass_word = #{password} where user_id = #{id}
    </update>

    <!--修改一条数据具体内容-->
    <update id="updateOneContent" parameterType="map">
        update hs_notice set label = #{label},title = #{title},content = #{content}, release_time = now() where id =
        #{id}
    </update>

    <!--删除一条数据-->
    <update id="deleteOne" parameterType="map">
        update hs_notice set status = "0" where id = #{id}
    </update>

    <!--删除一条留言-->
    <delete id="deleteOneMessage" parameterType="map">
        delete from hs_leave_message where id = #{id}
    </delete>

    <!--获取个人信息-->
    <select id="getPersonalInfo" parameterType="map" resultType="map">
        select * , (select count(*) from hs_private_letter where receive_id = #{releaseId} and already_read = "0" and receive_status = "1") letter_number
        from hs_user where user_id = #{releaseId}
    </select>

    <!--获取个人私信-->
    <select id="getPersonalPrivateLetter" resultType="map" parameterType="map">
        select * from hs_private_letter left join hs_user on receive_id = user_id where receive_id = #{userId} and receive_status = "1"
        <if test="id != '' and id != null">
            and id = #{id}
        </if>
        ORDER BY letter_create_time desc
    </select>

    <!--获取已读私信内容-->
    <select id="getSentPrivateLetterContent" resultType="map" parameterType="map">
        select * from hs_private_letter left join hs_user on send_id = user_id where send_id = #{userId} and send_status = "1"
        <if test="id != '' and id != null">
            and id = #{id}
        </if>
        ORDER BY letter_create_time desc
    </select>

    <!--将未读私信置位已读-->
    <update id="updateUnread" parameterType="map">
        update hs_private_letter set already_read = "1" where id = #{id}
    </update>

    <!--发送私信-->
    <insert id="sendLetter" parameterType="map">
        insert into hs_private_letter (id,send_id,receive_id,content,letter_create_time) values
        (uuid(),#{sendId},#{receiveId},#{content},now())
    </insert>

    <!--删除一条私信-->
    <update id="deleteLetter" parameterType="map">
        update hs_private_letter set receive_status = "0" where id = #{id}
    </update>

    <!--私信改为已读-->
    <update id="updateReadApp" parameterType="map">
        UPDATE hs_private_letter
        SET already_read = '1'
        WHERE
            send_id = #{otherId} and receive_id = #{userId}
    </update>

    <!--删除一条已读私信-->
    <delete id="deleteSentLetter" parameterType="map">
        update hs_private_letter set send_status = "0" where id = #{id}
    </delete>

    <!--更新个人积分-->
    <delete id="updateIntegral" parameterType="map">
        UPDATE hs_user
        SET integral = (
        ( ( SELECT count( * ) FROM hs_notice WHERE release_id = #{userId} ) * 100 ) +
        ( ( SELECT count( * ) FROM hs_leave_message WHERE messager_id = #{userId} ) * 10 )
        )
        WHERE
	    user_id = #{userId}
    </delete>

    <!--获取已发送私信-->
    <select id="getSentPrivateLetter" resultType="map" parameterType="map">
        select * from hs_private_letter left join hs_user on send_id = user_id where send_id = #{userId} and send_status = "1"
        <if test="id != '' and id != null">
            and id = #{id}
        </if>
        ORDER BY letter_create_time desc
    </select>

    <!--保存文件路径-->
    <insert id="insertFilePath" parameterType="map">
        insert into hs_file_info
        (id,file_name,file_encryption,file_path,create_time)
        values
        (#{id},#{fileName},#{fileEncryption},#{filePath},now())
    </insert>

    <!--获取文件信息-->
    <select id="getFileList" resultType="java.util.Map">
       select
        id,
        file_name fileName,
        file_encryption fileEncryption,
        file_path filePath,
        create_time createTime
        from
        hs_file_info
        order by
        create_time desc
    </select>

    <!--查询轮播图列表-->
    <select id="getRotationPhotoList" resultType="java.util.Map">
        SELECT
        t.id,
        file_id fileId,
        file_name fileName,
        file_encryption fileEncryption,
        file_path filePath,
        type_code typeCode,
        type_name typeName,
        i.create_time createTime
        FROM hs_file_type t
        left join hs_file_info i on t.file_id = i.id
        where type_code = 'rotationChart'
        ORDER BY i.create_time
    </select>

    <!--获取私信其他人信息-->
    <select id="getLetterOtherInfo" resultType="map" parameterType="map">
        select * from 	hs_user where user_id in (SELECT
        DISTINCT
            receive_id id
        FROM
            hs_private_letter
        WHERE
            send_id = #{userId}
            OR receive_id = #{userId} )
            or user_id in
            (SELECT
        DISTINCT
            send_id id
        FROM
            hs_private_letter
        WHERE
            send_id = #{userId}
            OR receive_id = #{userId} )
    </select>

    <!--获取两个人交流的私信-->
    <select id="getCommunicationLetter" resultType="map" parameterType="map">
        SELECT
            *
        FROM
            hs_private_letter
        WHERE
            ( send_id = #{userId} AND receive_id = #{otherId} )
            OR ( receive_id = #{userId} AND send_id = #{otherId} )
        ORDER BY
            letter_create_time
    </select>

</mapper>