<?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.api.dao.ApiUserCampMinusMapper">

    <resultMap type="com.kmall.api.entity.UserCampMinusVo" id="userCampMinusMap">
        <result property="userCampId" column="user_camp_id"/>
        <result property="storeTopicId" column="store_topic_id"/>
        <result property="campName" column="camp_name"/>
        <result property="userId" column="user_id"/>
        <result property="usedTime" column="used_time"/>
        <result property="orderId" column="order_id"/>
        <result property="endTime" column="end_time"/>
        <result property="money" column="money"/>
        <result property="isUsed" column="is_used"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.api.entity.UserCampMinusVo">
		select
			`user_camp_id`,
			`store_topic_id`,
			`camp_name`,
			`user_id`,
			`used_time`,
			`order_id`,
			`end_time`,
			`money`,
			`is_used`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mall_user_camp_minus
		where user_camp_id = #{id}
	</select>

	<select id="queryUserCampByOrderId" resultType="com.kmall.api.entity.UserCampMinusVo">
		select user_camp_id from mall_user_camp_minus where order_id = #{orderId}
	</select>

	<select id="queryCampByStoreTopicId" resultType="com.kmall.api.entity.UserCampMinusVo">
		select * from mall_user_camp_minus where store_topic_id = #{storeTopicId}
	</select>

	<update id="cancelUserCampOrder" parameterType="com.kmall.api.entity.UserCampMinusVo">
		update mall_user_camp_minus
		<set>
			`used_time` = null,
			`order_id` = 0,
			is_used = 0
		</set>
		where user_camp_id = #{userCampId}
	</update>

	<select id="queryList" resultType="com.kmall.api.entity.UserCampMinusVo">
		select
    		`user_camp_id`,
    		`store_topic_id`,
    		`camp_name`,
    		`user_id`,
    		`used_time`,
    		`order_id`,
    		`end_time`,
    		`money`,
    		`is_used`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`
		from mall_user_camp_minus
		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 user_camp_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_user_camp_minus
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.api.entity.UserCampMinusVo" useGeneratedKeys="true" keyProperty="userCampId">
		insert into mall_user_camp_minus(
			`store_topic_id`,
			`camp_name`,
			`user_id`,
			`used_time`,
			`order_id`,
			`end_time`,
			`money`,
			`is_used`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{storeTopicId},
			#{campName},
			#{userId},
			#{usedTime},
			#{orderId},
			#{endTime},
			#{money},
			#{isUsed},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.api.entity.UserCampMinusVo">
		update mall_user_camp_minus 
		<set>
			<if test="storeTopicId != null">`store_topic_id` = #{storeTopicId}, </if>
			<if test="campName != null">`camp_name` = #{campName}, </if>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="usedTime != null">`used_time` = #{usedTime}, </if>
			<if test="orderId != null">`order_id` = #{orderId}, </if>
			<if test="endTime != null">`end_time` = #{endTime}, </if>
			<if test="money != null">`money` = #{money}, </if>
			<if test="isUsed != null">`is_used` = #{isUsed}, </if>
			<if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
			<if test="modTime != null">`mod_time` = #{modTime}, </if>
			<if test="tstm != null">`tstm` = #{tstm}</if>
		</set>
		where user_camp_id = #{userCampId}
	</update>
	
	<delete id="delete">
		delete from mall_user_camp_minus where user_camp_id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_user_camp_minus where user_camp_id in 
		<foreach item="userCampId" collection="array" open="(" separator="," close=")">
			#{userCampId}
		</foreach>
	</delete>

</mapper>