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

    <resultMap type="ActivityDiscountInfo" id="ActivityDiscountInfoResult">
        <result property="id" column="id"/>
        <result property="activityId" column="activity_id"/>
        <result property="discount" column="discount"/>
        <result property="userScope" column="user_scope"/>
        <result property="payScope" column="pay_scope"/>
        <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="selectActivityDiscountListVo">
        select a.*
        from activity_info as a
    </sql>

    <sql id="selectActivityDiscountInfoVo">
        select * from activity_discount_info
    </sql>

    <select id="selectActivityDiscountInfoList" parameterType="com.yiqi.activity.domain.ActivityInfo" resultType="com.yiqi.activity.domain.vo.ActivityDiscountInfoQueryVO">
        <include refid="selectActivityDiscountListVo"/>
        <where>
            <if test="title != null  and title != ''">
                and a.title like concat('%', #{title}, '%')
            </if>
            <if test="startDate != null ">
                and a.start_date &gt;= #{startDate}
            </if>
            <if test="endDate != null ">
                and a.end_date &lt;= #{endDate}
            </if>
            and a.del_flag='0' and a.activity_type='2'
        </where>
    </select>

    <select id="selectActivityDiscountInfoById" parameterType="Long"
            resultMap="ActivityDiscountInfoResult">
        <include refid="selectActivityDiscountInfoVo"/>
        where id = #{id}
        and del_flag='0'
    </select>
    <select id="getDiscountListByStoreId" resultType="com.yiqi.activity.domain.vo.ActivityDiscountInfoQueryVO"
            parameterType="java.lang.Long">
        select a.*, d.discount
        from activity_info as a
            left join activity_discount_info  as d on a.id=d.activity_id
            INNER JOIN activity_apply_org aao on aao.activity_id = a.id
        where aao.org_id = #{storeId} and a.del_flag = 0 and a.status = 0 and a.activity_type = 2 and <![CDATA[ a.start_date <= now() and a.end_date >= now()]]>
    </select>

    <insert id="insertActivityDiscountInfo" parameterType="ActivityDiscountInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity_discount_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,
            </if>
            <if test="discount != null">discount,
            </if>
            <if test="userScope != null">user_scope,
            </if>
            <if test="payScope != null">pay_scope,
            </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="discount != null">#{discount},
            </if>
            <if test="userScope != null">#{userScope},
            </if>
            <if test="payScope != null">#{payScope},
            </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="updateActivityDiscountInfo" parameterType="ActivityDiscountInfo">
        update activity_discount_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="discount != null">discount =
                #{discount},
            </if>
            <if test="userScope != null">user_scope =
                #{userScope},
            </if>
            <if test="payScope != null">pay_scope =
                #{payScope},
            </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 activity_id = #{activityId}
    </update>

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

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