<?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.applet.dao.impl.AppletRecordStatisReportDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletRecordStatisReport">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="begin_date" property="beginDate" jdbcType="DATE"/>
        <result column="end_date" property="endDate" jdbcType="DATE"/>
        <result column="score" property="score" jdbcType="DECIMAL"/>
        <result column="compare_score" property="compareScore" jdbcType="DECIMAL"/>
        <result column="compare_user_ratio" property="compareUserRatio" jdbcType="DECIMAL"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, wechat_user_id, begin_date, end_date, score, compare_score, compare_user_ratio, create_time        
    </sql>

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

    <select id="getByWechatUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record_statis_report
        WHERE wechat_user_id = #{wechatUserId} AND begin_date = #{beginDate, jdbcType=DATE}
    </select>

    <select id="getFirstStatisReport" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record_statis_report
        WHERE wechat_user_id = #{wechatUserId}
        ORDER BY id
        LIMIT 1
    </select>

    <select id="getUserHistoryReport" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record_statis_report
        WHERE wechat_user_id = #{wechatUserId} AND begin_date IN
        <foreach collection="allMonday" item="item" separator="," open="(" close=")">
            #{item, jdbcType=DATE}
        </foreach>
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record_statis_report
        WHERE
        wechat_user_id = #{wechatUserId}
        ORDER BY begin_date DESC
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_record_statis_report(
            wechat_user_id, 
            begin_date, 
            end_date, 
            score, 
            compare_score, 
            compare_user_ratio, 
            create_time,
            update_time
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{beginDate, jdbcType=DATE}, 
            #{endDate, jdbcType=DATE}, 
            #{score, jdbcType=DECIMAL}, 
            #{compareScore, jdbcType=DECIMAL}, 
            #{compareUserRatio, jdbcType=DECIMAL}, 
            NOW(),
            NOW()
        )
    </insert>

    <insert id="insertOrUpdate" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_record_statis_report(
            wechat_user_id,
            begin_date,
            end_date,
            score,
            compare_score,
            compare_user_ratio,
            create_time,
            update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.wechatUserId, jdbcType=BIGINT},
                #{item.beginDate, jdbcType=DATE},
                #{item.endDate, jdbcType=DATE},
                #{item.score, jdbcType=DECIMAL},
                #{item.compareScore, jdbcType=DECIMAL},
                #{item.compareUserRatio, jdbcType=DECIMAL},
                NOW(),
                NOW()
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        score = VALUES(score),
        compare_score = VALUES(compare_score),
        compare_user_ratio = VALUES(compare_user_ratio),
        update_time = NOW()
    </insert>
    
    <insert id="batchInsert">
        INSERT INTO applet_record_statis_report (
            wechat_user_id, 
            begin_date, 
            end_date, 
            score, 
            compare_score, 
            compare_user_ratio, 
            create_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.wechatUserId, jdbcType=BIGINT}, 
            #{item.beginDate, jdbcType=DATE}, 
            #{item.endDate, jdbcType=DATE}, 
            #{item.score, jdbcType=DECIMAL}, 
            #{item.compareScore, jdbcType=DECIMAL}, 
            #{item.compareUserRatio, jdbcType=DECIMAL},
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_record_statis_report
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="beginDate != null">
                begin_date = #{beginDate},
            </if>
            <if test="endDate != null">
                end_date = #{endDate},
            </if>
            <if test="score != null">
                score = #{score},
            </if>
            <if test="compareScore != null">
                compare_score = #{compareScore},
            </if>
            <if test="compareUserRatio != null">
                compare_user_ratio = #{compareUserRatio},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

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

</mapper>
