<?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.pcloud.book.group.dao.impl.WeworkTeacherFriendRecordDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.WeworkTeacherFriendRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="wework_teacher_id" property="weworkTeacherId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="create_date" property="createDate" jdbcType="DATE"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, wechat_user_id, wework_teacher_id, create_time, create_date
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM wework_teacher_friend_record
        WHERE id = #{id}
    </select>
    
    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM wework_teacher_friend_record
    </select>

    <select id="getAggrStatis" resultType="com.pcloud.book.applet.dto.AppletRecordDTO">
        SELECT #{recordType} recordType, wework_teacher_id fromId, COUNT(id) clickCount, COUNT(DISTINCT wechat_user_id) userCount
        FROM wework_teacher_friend_record
        <where>
            <if test="beginDate != null">
                create_date BETWEEN #{beginDate} AND #{endDate}
            </if>
        </where>
        GROUP BY wework_teacher_id
        ORDER BY clickCount DESC
        LIMIT #{offset},#{limit}
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO wework_teacher_friend_record(
            wechat_user_id, 
            wework_teacher_id,
            create_time, 
            create_date
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{weworkTeacherId, jdbcType=BIGINT},
            NOW(),
            NOW()
        )
    </insert>
    
    <insert id="batchInsert">
        INSERT INTO wework_teacher_friend_record (
            wechat_user_id, 
            wework_teacher_id,
            create_time, 
            create_date
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.wechatUserId, jdbcType=BIGINT}, 
            #{item.weworkTeacherId, jdbcType=BIGINT},
            NOW(),
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE wework_teacher_friend_record
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="weworkTeacherId != null">
                wework_teacher_id = #{weworkTeacherId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM wework_teacher_friend_record where id = #{id}
    </delete>

</mapper>
