<?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.ActivityRaceSignMapper">

    <select id="findFirstByCustomIdAndRaceType" resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceSignView">
        select sign.*,ci.first_name,ci.middle,ci.last_name,ci.country
        from activity_race_sign sign
        inner join custom_info ci on sign.custom_id = ci.id
        <where>
            sign.custom_id = #{customId}
            and sign.race_type = #{raceType}
        </where>
        order by sign.sign_date desc
        limit 1
    </select>

    <select id="countList"
            parameterType="com.crm.rely.backend.model.entity.activity.ActivityRaceSignListEntity"
            resultType="Integer">
        select count(sign.id)
        from activity_race_sign sign
        <where>
            <if test="customId != null and customId > 0">
                and sign.custom_id = #{customId}
            </if>
            <if test="cId != null and cId > 0">
                and sign.c_id = #{cId}
            </if>
            <if test="login != null and login > 0">
                and sign.login = #{login}
            </if>
            <if test="ruleId != null and ruleId > 0">
                and sign.rule_id = #{ruleId}
            </if>
            <if test="raceType != null and raceType > 0">
                and sign.race_type = #{raceType}
            </if>
            <if test="startDate != null">
                and sign.race_end_date <![CDATA[>=]]> #{startDate}
            </if>
            <if test="endDate != null">
                and sign.race_end_date <![CDATA[<=]]> #{endDate}
            </if>
        </where>
    </select>

    <select id="getList"
            parameterType="com.crm.rely.backend.model.entity.activity.ActivityRaceSignListEntity"
            resultType="com.crm.rely.backend.model.pojo.view.ActivityRaceSignView">
        select sign.*,ci.first_name,ci.middle,ci.last_name,ci.country
        from activity_race_sign sign
        inner join custom_info ci on sign.custom_id = ci.id
        <where>
            <if test="customId != null and customId > 0">
                and sign.custom_id = #{customId}
            </if>
            <if test="cId != null and cId > 0">
                and sign.c_id = #{cId}
            </if>
            <if test="login != null and login > 0">
                and sign.login = #{login}
            </if>
            <if test="ruleId != null and ruleId > 0">
                and sign.rule_id = #{ruleId}
            </if>
            <if test="raceType != null and raceType > 0">
                and sign.race_type = #{raceType}
            </if>
            <if test="startDate != null">
                and sign.race_end_date <![CDATA[>=]]> #{startDate}
            </if>
            <if test="endDate != null">
                and sign.race_end_date <![CDATA[<=]]> #{endDate}
            </if>
        </where>
        order by sign.race_end_date desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

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

    <select id="findValidSignByCustomIdAndDate" resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceSignTable">
        select sign.*
        from activity_race_sign sign
        where sign.custom_id = #{customId}
        and sign.race_start_date <![CDATA[<=]]> #{date}
        and sign.race_end_date <![CDATA[>]]> #{date}
    </select>
</mapper>