<?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.crm.activity.dao.mapper.ActivityRaceRankingMapper">

    <select id="countList"
            parameterType="com.crm.rely.backend.model.entity.activity.ActivityRaceRankingListEntity"
            resultType="Integer">
        select count(ranking.id)
        from activity_race_ranking ranking
        inner join activity_race_rule rule on ranking.rule_id = rule.id
        where rule.race_type = #{raceType}
        and rule.serial = #{serial}
--         and ranking.active > 0
        <if test="login !=null and login > 0">
            and ranking.login = #{login}
        </if>
    </select>

    <select id="getList"
            parameterType="com.crm.rely.backend.model.entity.activity.ActivityRaceRankingListEntity"
            resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceRankingView">
        select ranking.*,ci.first_name,ci.middle,ci.last_name,ci.country
        from activity_race_ranking ranking
        inner join custom_info ci on ranking.custom_id = ci.id
        inner join activity_race_rule rule on ranking.rule_id = rule.id
        where rule.race_type = #{raceType}
        and rule.serial = #{serial}
--         and ranking.active > 0
        <if test="login !=null and login > 0">
            and ranking.login = #{login}
        </if>
        order by ranking.active desc,ranking.`rank` asc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="getViewById"
            parameterType="Long"
            resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceRankingView">
        select ranking.*,ci.first_name,ci.middle,ci.last_name,ci.country
        from activity_race_ranking ranking
        inner join custom_info ci on ranking.custom_id = ci.id
        where ranking.id = #{id}
    </select>

    <select id="getById" parameterType="Long" resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRankingTable">
        select *
        from activity_race_ranking where id=#{id}
    </select>

    <select id="getAllByRaceTypeAndSerial" resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRankingTable">
        select ranking.*
        from activity_race_ranking ranking
        inner join activity_race_rule rule on ranking.rule_id = rule.id
        where rule.race_type=#{raceType} and rule.serial = #{serial}
    </select>

    <update id="batchUpdateRank" parameterType="com.crm.rely.backend.model.pojo.table.ActivityRaceRankingTable">
        update activity_race_ranking
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="rank = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.rank}
                </foreach>
            </trim>
            <trim prefix="modify_time = case" suffix="end,">
                <foreach collection="list" index="idx" item="item">
                    when id=#{item.id} then #{item.modifyTime}
                </foreach>
            </trim>
        </trim>
        <where>
            and id in
            <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
                #{item.id}
            </foreach>
        </where>
    </update>

    <update id="updateById" parameterType="com.crm.rely.backend.model.pojo.table.ActivityRaceRankingTable">
        update activity_race_ranking
        set profit = #{profit},
        manual = #{manual}
        where id = #{id}
    </update>

    <select id="getViewByCustomIdAndRuleId" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceRankingView">
        select ranking.*,ci.first_name,ci.middle,ci.last_name,ci.country
        from activity_race_ranking ranking
        inner join custom_info ci on ranking.custom_id = ci.id
        where ranking.rule_id = #{ruleId} and ranking.custom_id = #{customId}
    </select>

    <update id="batchUpdateProfit" parameterType="com.crm.rely.backend.model.pojo.table.ActivityRaceRankingTable">
        update activity_race_ranking
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="rank = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.rank}
                </foreach>
            </trim>
            <trim prefix="profit = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.profit}
                </foreach>
            </trim>
            <trim prefix="dw = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.dw}
                </foreach>
            </trim>
            <trim prefix="active = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.active}
                </foreach>
            </trim>
            <trim prefix="end_equity = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.endEquity}
                </foreach>
            </trim>
            <trim prefix="modify_time = case" suffix="end,">
                <foreach collection="list" index="idx" item="item">
                    when id=#{item.id} then #{item.modifyTime}
                </foreach>
            </trim>
        </trim>
        <where>
            and id in
            <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
                #{item.id}
            </foreach>
        </where>
    </update>

    <select id="getAllByRuleId" parameterType="Long" resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRankingTable">
        select ranking.*
        from activity_race_ranking ranking
        where ranking.rule_id = #{ruleId}
    </select>
</mapper>