<?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="cn.wisenergy.mapper.ProgressPrizeMapper">
    <resultMap id="monthMap" type="cn.wisenergy.model.app.ProgressPrize">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="year_month" property="yearMonth"/>
        <result column="growth_rate" property="growthRate"/>
        <result column="award_money" property="awardMoney"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="table">
        progress_prize
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        user_id,`year_month`,`growth_rate`,award_money,create_time,update_time
    </sql>

    <sql id="vals">
        #{userId},#{yearMonth},#{growthRate},#{awardMoney},now(),now()
    </sql>

    <sql id="updateCondition">
        <if test="userId != null">user_id = #{userId},</if>
        <if test="yearMonth != null">`year_month` = #{yearMonth},</if>
        <if test="growthRate != null">`growth_rate` =#{growthRate},</if>
        <if test="awardMoney != null">award_money =#{awardMoney},</if>
        update_time =now()
    </sql>

    <sql id="criteria">
        <if test="id != null">id = #{id}</if>
        <if test="userId != null">and user_id = #{userId}</if>
        <if test="yearMonth != null">and `year_month` = #{yearMonth}</if>
        <if test="growthRate != null">and `growth_rate` =#{growthRate}</if>
        <if test="awardMoney != null">and award_money =#{awardMoney}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="updateTime != null">and #{updateTime} &gt;= update_time</if>
    </sql>

    <insert id="add" parameterType="cn.wisenergy.model.app.ProgressPrize" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value(
        <include refid="vals"/>
        )
    </insert>

    <update id="edit" parameterType="cn.wisenergy.model.app.ProgressPrize">
        UPDATE
        <include refid="table"/>
        <set>
            <include refid="updateCondition"/>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <delete id="delById" parameterType="java.lang.Integer">
        delete from
        <include refid="table"/>
        where id = #{id}
    </delete>

    <select id="getByYearMonth" resultType="cn.wisenergy.model.app.ProgressPrize">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="yearMonth != null">
                `year_month` = #{yearMonth}
            </if>
        </where>
    </select>

    <select id="getByTime" resultType="cn.wisenergy.model.vo.ProgressPrizeVo">
        select p.user_id as userId,u.head_image as headImage,p.`growth_rate` as growthRate, p.award_money as awardMoney
        from
        progress_prize p ,user_info u
        where
        p.user_id=u.user_id
        and p.`year_month` = #{yearMonth}
        order by p.`growth_rate` desc,p.award_money desc,u.create_time desc
    </select>

    <select id="getByUserIdYearMonth" resultType="cn.wisenergy.model.app.ProgressPrize">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="userId != null and userId != ''">
                user_id=#{userId}
            </if>
            <if test="yearMonth != null">
               and `year_month`= #{yearMonth}
            </if>
        </where>
    </select>

    <select id="count" resultType="java.lang.Integer">
        select count(1)
        from
        <include refid="table"/>
        <where>
            <if test="queryTime != null and queryTime != ''">
                `year_month` = #{queryTime}
            </if>
        </where>
    </select>

    <select id="getList" resultType="cn.wisenergy.model.app.ProgressPrize">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="queryTime != null and queryTime != ''">
                `year_month` = #{queryTime}
            </if>
        </where>
        order by create_time desc
        limit #{startNum},#{endNum}
    </select>

</mapper>