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

    <resultMap type="FsStoreCouponIssue" id="FsStoreCouponIssueResult">
        <result property="id"    column="id"    />
        <result property="couponName"    column="coupon_name"    />
        <result property="couponId"    column="coupon_id"    />
        <result property="couponType"    column="coupon_type"    />
        <result property="startTime"    column="start_time"    />
        <result property="limitTime"    column="limit_time"    />
        <result property="totalCount"    column="total_count"    />
        <result property="remainCount"    column="remain_count"    />
        <result property="isPermanent"    column="is_permanent"    />
        <result property="status"    column="status"    />
        <result property="isDel"    column="is_del"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectFsStoreCouponIssueVo">
        select id, coupon_name, coupon_id, coupon_type, start_time, limit_time, total_count, remain_count, is_permanent, status, is_del, create_time, update_time from fs_store_coupon_issue
    </sql>

    <select id="selectFsStoreCouponIssueList" parameterType="FsStoreCouponIssue" resultMap="FsStoreCouponIssueResult">
        <include refid="selectFsStoreCouponIssueVo"/>
        <where>
            <if test="couponName != null  and couponName != ''"> and coupon_name like concat('%', #{couponName}, '%')</if>
            <if test="couponId != null "> and coupon_id = #{couponId}</if>
            <if test="couponType != null "> and coupon_type = #{couponType}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="limitTime != null "> and limit_time = #{limitTime}</if>
            <if test="totalCount != null "> and total_count = #{totalCount}</if>
            <if test="remainCount != null "> and remain_count = #{remainCount}</if>
            <if test="isPermanent != null "> and is_permanent = #{isPermanent}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
        order by id desc
    </select>

    <select id="selectFsStoreCouponIssueById" parameterType="Long" resultMap="FsStoreCouponIssueResult">
        <include refid="selectFsStoreCouponIssueVo"/>
        where id = #{id}
    </select>
    <select id="listAllAvailable" resultMap="FsStoreCouponIssueResult">
        SELECT
            id,
            coupon_name,
            coupon_id,
            coupon_type,
            start_time,
            limit_time,
            total_count,
            remain_count,
            is_permanent,
            STATUS,
            is_del,
            create_time,
            update_time
        FROM
            fs_store_coupon_issue
        WHERE
            coupon_type = 1
          AND ifnull(is_del,0)= 0
          AND `status`=1
          AND now() BETWEEN start_time and limit_time
        UNION

        SELECT
            id,
            coupon_name,
            coupon_id,
            coupon_type,
            start_time,
            limit_time,
            total_count,
            remain_count,
            is_permanent,
            STATUS,
            is_del,
            create_time,
            update_time
        FROM
            fs_store_coupon_issue
        WHERE
            coupon_type = 1
          AND is_permanent= 1
          AND ifnull(is_del,0)= 0
          AND `status`=1
    </select>
    <insert id="insertFsStoreCouponIssue" parameterType="FsStoreCouponIssue" useGeneratedKeys="true" keyProperty="id">
        insert into fs_store_coupon_issue
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="couponName != null">coupon_name,</if>
            <if test="couponId != null">coupon_id,</if>
            <if test="couponType != null">coupon_type,</if>
            <if test="startTime != null">start_time,</if>
            <if test="limitTime != null">limit_time,</if>
            <if test="totalCount != null">total_count,</if>
            <if test="remainCount != null">remain_count,</if>
            <if test="isPermanent != null">is_permanent,</if>
            <if test="status != null">status,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="couponName != null">#{couponName},</if>
            <if test="couponId != null">#{couponId},</if>
            <if test="couponType != null">#{couponType},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="limitTime != null">#{limitTime},</if>
            <if test="totalCount != null">#{totalCount},</if>
            <if test="remainCount != null">#{remainCount},</if>
            <if test="isPermanent != null">#{isPermanent},</if>
            <if test="status != null">#{status},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateFsStoreCouponIssue" parameterType="FsStoreCouponIssue">
        update fs_store_coupon_issue
        <trim prefix="SET" suffixOverrides=",">
            <if test="couponName != null">coupon_name = #{couponName},</if>
            <if test="couponId != null">coupon_id = #{couponId},</if>
            <if test="couponType != null">coupon_type = #{couponType},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="limitTime != null">limit_time = #{limitTime},</if>
            <if test="totalCount != null">total_count = #{totalCount},</if>
            <if test="remainCount != null">remain_count = #{remainCount},</if>
            <if test="isPermanent != null">is_permanent = #{isPermanent},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFsStoreCouponIssueById" parameterType="Long">
        delete from fs_store_coupon_issue where id = #{id}
    </delete>

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

</mapper>
