<?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.yiqi.activity.mapper.ActivityRechargeItemMapper">

    <resultMap type="ActivityRechargeItem" id="ActivityRechargeItemResult">
        <result property="id" column="id"/>
        <result property="activityId" column="activity_id"/>
        <result property="rechargeTypeId" column="recharge_type_id"/>
        <result property="giveAmount" column="give_amount"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectActivityRechargeItemVo">
        select * from activity_recharge_item
    </sql>

    <select id="selectActivityRechargeItemList" parameterType="ActivityRechargeItem"
            resultType="com.yiqi.activity.domain.vo.ActivityRechargeItemVO">
        select ar.*,r.title as rechargeTypeTitle, r.recharge_amount as rechargeAmount, r.give_amount as giveAmount
        from activity_recharge_item as ar left join recharge_type as r on ar.recharge_type_id=r.id
        <where>
            and ar.del_flag='0'
        </where>
    </select>

    <select id="selectActivityRechargeItemById" parameterType="Long"
            resultMap="ActivityRechargeItemResult">
        <include refid="selectActivityRechargeItemVo"/>
        where id = #{id}
        and del_flag='0'
    </select>
    <select id="getGiveAmountByActivity" resultType="com.yiqi.activity.domain.ActivityRechargeItem">
        SELECT
        aci.*
        FROM
            activity_recharge_item aci
                LEFT JOIN activity_info ai ON aci.activity_id = ai.id and ai.activity_type = '0'
                LEFT JOIN activity_apply_org aao ON aci.activity_id = aao.activity_id
        WHERE
            aci.del_flag = '0'
          AND ai.`status` = '0'
          AND ai.del_flag = '0'
          AND aao.org_id = #{storeId}
          and aci.recharge_type_id =  #{rechargeTypeId}
            <if test="nowTime != null and nowTime !=''">
                <![CDATA[and ai.start_date <= #{nowTime} and  ai.end_date >= #{nowTime}]]>
            </if>
            order by aci.activity_give_amount desc limit 1
    </select>
    <select id="getRechargeActivityItemInfo" resultType="com.yiqi.activity.domain.vo.ActivityRechargeItemVO">
        select ar.*,r.title as rechargeTypeTitle, r.recharge_amount as rechargeAmount
        from activity_recharge_item as ar
            left join recharge_type as r on ar.recharge_type_id=r.id
        where ar.activity_id = #{activityId}
            and ar.del_flag='0'
    </select>

    <insert id="insertActivityRechargeItem" parameterType="ActivityRechargeItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity_recharge_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,
            </if>
            <if test="rechargeTypeId != null">recharge_type_id,
            </if>
            <if test="giveAmount != null">give_amount,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null">#{activityId},
            </if>
            <if test="rechargeTypeId != null">#{rechargeTypeId},
            </if>
            <if test="giveAmount != null">#{giveAmount},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
        </trim>
    </insert>

    <update id="updateActivityRechargeItem" parameterType="ActivityRechargeItem">
        update activity_recharge_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityId != null">activity_id =
                #{activityId},
            </if>
            <if test="rechargeTypeId != null">recharge_type_id =
                #{rechargeTypeId},
            </if>
            <if test="giveAmount != null">give_amount =
                #{giveAmount},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteActivityRechargeItemById" parameterType="Long">
            update activity_recharge_item set del_flag='2' where id = #{id}
        </delete>

    <delete id="deleteActivityRechargeItemByIds" parameterType="String">
        update activity_recharge_item set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>