<?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.fs.store.mapper.FsCouponScheduleMapper">

    <resultMap type="FsCouponSchedule" id="FsCouponScheduleResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="setmealId"    column="setmeal_id"    />
        <result property="month"    column="month"    />
        <result property="count"    column="count"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="orderTime"    column="order_time"    />
        <result property="couponIds"    column="coupon_ids"    />
        <result property="sendTime"    column="send_time"    />
        <result property="actualSendTime"    column="actual_send_time"    />
        <result property="errorMessage"    column="error_message"    />
        <result property="retryCount"    column="retry_count"    />
        <result property="maxRetries"    column="max_retries"    />
        <result property="remark"    column="remark"    />
        <result property="page" column="page"/>
    </resultMap>

    <sql id="selectFsCouponScheduleVo">
        select id, user_id, order_id, setmeal_id, month, count, status, create_time, create_by, update_time, update_by, order_time, coupon_id as coupon_ids, send_time, actual_send_time, error_message, retry_count, max_retries,  remark,page from fs_coupon_schedule
    </sql>

    <select id="selectFsCouponScheduleList" parameterType="FsCouponSchedule" resultMap="FsCouponScheduleResult">
        <include refid="selectFsCouponScheduleVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="setmealId != null "> and setmeal_id = #{setmealId}</if>
            <if test="month != null "> and month = #{month}</if>
            <if test="count != null "> and count = #{count}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="orderTime != null "> and order_time = #{orderTime}</if>
            <if test="couponIds != null "> and coupon_id = #{couponIds}</if>
            <if test="sendTime != null "> and send_time = #{sendTime}</if>
            <if test="actualSendTime != null "> and actual_send_time = #{actualSendTime}</if>
            <if test="errorMessage != null  and errorMessage != ''"> and error_message = #{errorMessage}</if>
            <if test="retryCount != null "> and retry_count = #{retryCount}</if>
            <if test="maxRetries != null "> and max_retries = #{maxRetries}</if>
            <if test="remark != null "> and remark = #{remark}</if>
            <if test="page != null "> and page = #{page}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
    </select>

    <select id="selectFsCouponScheduleById" parameterType="Long" resultMap="FsCouponScheduleResult">
        <include refid="selectFsCouponScheduleVo"/>
        where id = #{id}
    </select>

    <select id="selectPendingCouponList" resultType="com.fs.store.domain.FsCouponSchedule">
        <include refid="selectFsCouponScheduleVo"/>
        where status IN (0,-1,2) AND retry_count &lt; 3 AND count &lt; month
        AND now()>=send_time
        limit 500
    </select>
    <select id="selectFsCouponScheduleByOrderId" resultType="com.fs.store.domain.FsCouponSchedule">
        <include refid="selectFsCouponScheduleVo"/>
        where order_id = #{orderId} limit 1
    </select>

    <insert id="insertFsCouponSchedule" parameterType="FsCouponSchedule" useGeneratedKeys="true" keyProperty="id">
        insert into fs_coupon_schedule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="orderId != null and orderId != ''">order_id,</if>
            <if test="setmealId != null">setmeal_id,</if>
            <if test="month != null">month,</if>
            <if test="count != null">count,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="orderTime != null">order_time,</if>
            <if test="couponIds != null">coupon_id,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="actualSendTime != null">actual_send_time,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="retryCount != null">retry_count,</if>
            <if test="maxRetries != null">max_retries,</if>
            <if test="remark != null">remark,</if>
            <if test="page != null">page,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="orderId != null and orderId != ''">#{orderId},</if>
            <if test="setmealId != null">#{setmealId},</if>
            <if test="month != null">#{month},</if>
            <if test="count != null">#{count},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="orderTime != null">#{orderTime},</if>
            <if test="couponIds != null">#{couponIds},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="actualSendTime != null">#{actualSendTime},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="retryCount != null">#{retryCount},</if>
            <if test="maxRetries != null">#{maxRetries},</if>
            <if test="remark != null">#{remark},</if>
            <if test="page != null">#{page},</if>
         </trim>
    </insert>

    <update id="updateFsCouponSchedule" parameterType="FsCouponSchedule">
        update fs_coupon_schedule
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="orderId != null and orderId != ''">order_id = #{orderId},</if>
            <if test="setmealId != null">setmeal_id = #{setmealId},</if>
            <if test="month != null">month = #{month},</if>
            <if test="count != null">count = #{count},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="orderTime != null">order_time = #{orderTime},</if>
            <if test="couponIds != null">coupon_id = #{couponIds},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
            <if test="actualSendTime != null">actual_send_time = #{actualSendTime},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="retryCount != null">retry_count = #{retryCount},</if>
            <if test="maxRetries != null">max_retries = #{maxRetries},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="page != null">page = #{page},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updateBatchFsCouponSchedule">
        <foreach collection="list" separator=";" item="item">
            update fs_coupon_schedule
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.userId != null">user_id = #{item.userId},</if>
                <if test="item.orderId != null and item.orderId != ''">order_id = #{item.orderId},</if>
                <if test="item.setmealId != null">setmeal_id = #{item.setmealId},</if>
                <if test="item.month != null">month = #{item.month},</if>
                <if test="item.count != null">count = #{item.count},</if>
                <if test="item.status != null">status = #{item.status},</if>
                <if test="item.createTime != null">create_time = #{item.createTime},</if>
                <if test="item.createBy != null">create_by = #{item.createBy},</if>
                <if test="item.updateTime != null">update_time = #{item.updateTime},</if>
                <if test="item.updateBy != null">update_by = #{item.updateBy},</if>
                <if test="item.orderTime != null">order_time = #{item.orderTime},</if>
                <if test="item.couponIds != null">coupon_id = #{item.couponIds},</if>
                <if test="item.sendTime != null">send_time = #{item.sendTime},</if>
                <if test="item.actualSendTime != null">actual_send_time = #{item.actualSendTime},</if>
                <if test="item.errorMessage != null">error_message = #{item.errorMessage},</if>
                <if test="item.retryCount != null">retry_count = #{item.retryCount},</if>
                <if test="item.maxRetries != null">max_retries = #{item.maxRetries},</if>
                <if test="item.remark != null">remark = #{item.remark},</if>
                <if test="item.page != null">page = #{item.page},</if>
            </trim>
            where id = #{item.id}
        </foreach>
    </update>

    <delete id="deleteFsCouponScheduleById" parameterType="Long">
        delete from fs_coupon_schedule where id = #{id}
    </delete>

    <delete id="deleteFsCouponScheduleByIds" parameterType="String">
        delete from fs_coupon_schedule where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
