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

    <select id="findFirstBySignDateBtAndType"
            resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRuleTable">
        select rule.*
        from activity_race_rule rule
        where rule.start_date <![CDATA[<=]]> #{signDate}
        and rule.end_date <![CDATA[>]]> #{signDate}
        and rule.race_type = #{raceType}
        limit 1
    </select>

    <select id="findAllLimitedByRaceType"
            parameterType="Integer"
            resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRuleTable">
        select rule.*
        from activity_race_rule rule
        where rule.race_type = #{raceType}
        order by rule.end_date desc
        limit #{limit}
    </select>

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

    <select id="findByCustomIdAndRaceTypeAndSerial"
            resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRuleTable">
        select *
        from activity_race_rule
        where race_type = #{raceType} and serial = #{serial}
    </select>

    <select id="getMaxSerialByRaceType" parameterType="Integer" resultType="Integer">
        select ifnull(max(serial),0)
        from activity_race_rule
        where race_type = #{raceType}
    </select>

    <select id="findAllByDateBt" parameterType="java.util.Date"
            resultType="com.crm.rely.backend.model.pojo.table.ActivityRaceRuleTable">
        select rule.*
        from activity_race_rule rule
        where rule.start_date <![CDATA[<]]> #{date}
        and rule.end_date <![CDATA[>=]]> #{date}
    </select>

    <select id="countByStartDateAndEndDateAndRaceType" resultType="Integer">
        select count(rule.id)
        from activity_race_rule rule
        where rule.start_date = #{startDate}
        and rule.end_date = #{endDate}
        and rule.race_type = #{raceType}
    </select>
</mapper>