<?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.kmall.admin.fromcomm.dao.FormIdsDao">

    <resultMap type="com.kmall.api.fromcomm.entity.FormIdsEntity" id="formIdsMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="merchOrderSn" column="merch_order_sn"/>
        <result property="formId" column="form_id"/>
        <result property="validNum" column="valid_num"/>
        <result property="expireTime" column="expire_time"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.api.fromcomm.entity.FormIdsEntity">
		select
			`id`,
            `merch_order_sn`,
			`user_id`,
			`form_id`,
			`valid_num`,
			`expire_time`
		from mall_form_ids
		where id = #{id}
	</select>

    <select id="getFormIdsByMerchOrderSn" resultType="com.kmall.api.fromcomm.entity.FormIdsEntity">
        select
        `id`,
        `merch_order_sn`,
        `user_id`,
        `form_id`,
        `valid_num`,
        `expire_time`
        from mall_form_ids
        where merch_order_sn = #{merchOrderSn}
    </select>

    <select id="queryByUserId" resultType="com.kmall.api.fromcomm.entity.FormIdsEntity">
        select
        `id`,
        `merch_order_sn`,
        `user_id`,
        `form_id`,
        `valid_num`,
        `expire_time`
        from mall_form_ids
        where user_id = #{userId} and valid_num>0 and expire_time> now() LIMIT 1
    </select>

    <select id="queryList" resultType="com.kmall.api.fromcomm.entity.FormIdsEntity">
        select
        `id`,
        `merch_order_sn`,
        `user_id`,
        `form_id`,
        `valid_num`,
        `expire_time`
        from mall_form_ids
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_form_ids
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.api.fromcomm.entity.FormIdsEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_form_ids(
			`user_id`,
            `merch_order_sn`,
			`form_id`,
			`valid_num`,
			`expire_time`)
		values(
			#{userId},
            #{merchOrderSn},
			#{formId},
			#{validNum},
			#{expireTime})
	</insert>

    <update id="update" parameterType="com.kmall.api.fromcomm.entity.FormIdsEntity">
        update mall_form_ids
        <set>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="merchOrderSn != null">`merch_order_sn` = #{merchOrderSn},</if>
            <if test="formId != null">`form_id` = #{formId},</if>
            <if test="validNum != null">`valid_num` = #{validNum},</if>
            <if test="expireTime != null">`expire_time` = #{expireTime}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_form_ids where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from mall_form_ids where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>