<?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.TeamPerformanceMapper">
    <resultMap id="monthMap" type="cn.wisenergy.model.app.TeamPerformance">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="user_level" property="userLevel"/>
        <result column="year_month" property="yearMonth"/>
        <result column="month_team_performance" property="monthTeamPerformance"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        user_id,user_level,`year_month`,month_team_performance,create_time,update_time
    </sql>

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

    <sql id="updateCondition">
        <if test="userId != null">user_id = #{userId},</if>
        <if test="userLevel != null">user_level = #{userLevel},</if>
        <if test="yearMonth != null">`year_month` =#{yearMonth},</if>
        <if test="monthTeamPerformance != null">month_team_performance =#{monthTeamPerformance},</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="userLevel != null">and user_level = #{userLevel}</if>
        <if test="yearMonth != null">and `year_month` =#{yearMonth}</if>
        <if test="monthTeamPerformance != null">and month_team_performance =#{monthTeamPerformance}</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.TeamPerformance" 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.TeamPerformance">
        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="getByUserIdAndTime" resultType="cn.wisenergy.model.app.TeamPerformance">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            <if test="userId != null">
                user_id = #{userId}
            </if>
            <if test="yearMonth != null">
                and `year_month`=#{yearMonth}
            </if>
        </where>
    </select>

    <select id="countTeamMoney" resultType="java.lang.Double">
        select
        sum(month_team_performance)
        from
        <include refid="table"/>
        <where>
            <if test="userLevel != null">
                user_level = #{userLevel}
            </if>
            <if test="yearMonth != null">
                and `year_month` = #{yearMonth}
            </if>
        </where>
    </select>

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

    <select id="countByTime" resultType="java.lang.Double">
        select
        sum(month_team_performance)
        from
        <include refid="table"/>
        <where>
            <if test="yearMonth != null">
                `year_month` = #{yearMonth}
            </if>
        </where>
    </select>

    <select id="countTwenty" resultType="java.lang.Double">
        select
        sum(month_team_performance)
        from
        <include refid="table"/>
        <where>
            <if test="yearMonth != null">
                `year_month` = #{yearMonth}
            </if>
            order by month_team_performance desc
            limit 20
        </where>
    </select>

    <select id="userTwenty" resultType="cn.wisenergy.model.app.TeamPerformance">
        select
        <include refid="cols_all"/>
        from
        <include refid="table"/>
        <where>
            user_level != 0
            <if test="yearMonth != null">
                and `year_month` = #{yearMonth}
            </if>
            order by month_team_performance desc
            limit 20
        </where>
    </select>

    <select id="countUserTeamByUserId" resultType="double">
        select sum(month_team_performance)
        from
        <include refid="table"/>
        <where>
            user_id=#{userId}
        </where>
    </select>

    <select id="monthUserTeamByuserId" resultType="double">
        select month_team_performance
        from
        <include refid="table"/>
        <where>
            user_id=#{userid}
            and `year_month` = #{yearMonth}
        </where>
    </select>
</mapper>