<?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.ym.mec.biz.dal.dao.SysCouponDao">

	<resultMap type="com.ym.mec.biz.dal.entity.SysCoupon" id="SysCoupon">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="description_" property="description" />
		<result column="status_" property="status" />
        <result column="issuance_type_" jdbcType="INTEGER" property="issuanceType"/>
		<result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="type_detail_" jdbcType="VARCHAR" property="typeDetail"/>
		<result column="face_value_" property="faceValue" />
		<result column="full_amount_" property="fullAmount" />
		<result column="limit_exchange_num_" property="limitExchangeNum" />
		<result column="effective_type_" property="effectiveType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="deadline_" property="deadline" />
		<result column="effective_start_time_" property="effectiveStartTime" />
		<result column="effective_expire_time_" property="effectiveExpireTime" />
		<result column="end_date_" property="endDate" />
		<result column="start_date_" property="startDate" />
		<result column="stock_count_" property="stockCount" />
		<result column="consume_num_" property="consumeNum" />
		<result column="warning_stock_num_" property="warningStockNum" />
		<result column="warning_status_" property="warningStatus" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
        <result column="tenant_id_" property="tenantId"/>
        <result column="use_condition_" property="useCondition"/>
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SysCoupon" >
		SELECT * FROM sys_coupon WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="SysCoupon">
		SELECT * FROM sys_coupon where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysCoupon" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sys_coupon (id_,name_,description_,status_,issuance_type_,type_,type_detail_,face_value_,full_amount_,limit_exchange_num_,effective_type_,deadline_,
		                        effective_start_time_, effective_expire_time_,end_date_,start_date_,stock_count_,consume_num_,warning_stock_num_,
		                        create_time_,update_time_,tenant_id_,use_condition_)
		                        VALUES(#{id},#{name},#{description},#{status},#{issuanceType},
		                               #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		                               #{typeDetail},#{faceValue},#{fullAmount},#{limitExchangeNum},
		                               #{effectiveType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{deadline},
		                               #{effectiveStartTime},#{effectiveExpireTime},#{endDate},#{startDate},#{stockCount},#{consumeNum},#{warningStockNum},
		                               NOW(), NOW(),#{tenantId},#{useCondition})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SysCoupon">
		UPDATE sys_coupon
		<set>
			<if test="status != null">
				status_ = #{status},
			</if>
			<if test="startDate != null">
				start_date_ = #{startDate},
			</if>
			<if test="faceValue != null">
				face_value_ = #{faceValue},
			</if>
			<if test="name != null">
				name_ = #{name},
			</if>
				full_amount_ = #{fullAmount},
			<if test="effectiveType != null">
				effective_type_ = #{effectiveType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
				effective_start_time_ = #{effectiveStartTime},
				effective_expire_time_ = #{effectiveExpireTime},
				deadline_ = #{deadline},
			<if test="stockCount != null">
				stock_count_ = #{stockCount},
			</if>
			<if test="endDate != null">
				end_date_ = #{endDate},
			</if>
			<if test="description != null">
				description_ = #{description},
			</if>
			<if test="consumeNum != null">
				consume_num_ = #{consumeNum},
			</if>
			<if test="type != null">
				type_ = #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="limitExchangeNum != null">
				limit_exchange_num_ = #{limitExchangeNum},
			</if>
			<if test="warningStockNum != null">
				warning_stock_num_ = #{warningStockNum},
			</if>
			<if test="warningStatus != null">
				warning_status_ = #{warningStatus},
			</if>
			<if test="useCondition != null">
				use_condition_ = #{useCondition},
			</if>
			<if test="issuanceType != null">
				issuance_type_ = #{issuanceType},
			</if>
				update_time_ = NOW()
		</set>
		WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>

    <!-- 修改库存 -->
    <update id="updateExchange" parameterType="map">
        UPDATE sys_coupon
        <set>
            consume_num_ = consume_num_ + #{param.exchangeNum},
            <if test="param.warningStatus != null">
                warning_status_ = #{param.warningStatus},
            </if>
            update_time_ = NOW()
        </set>
        WHERE id_ = #{param.id}
        and tenant_id_ = #{param.tenantId}
        <if test="param.checkStock != null">
           and stock_count_ >= (consume_num_ + #{param.exchangeNum})
        </if>
    </update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM sys_coupon WHERE id_ = #{id}
	</delete>

	<sql id="queryCondition">
		<where>
			tenant_id_ = #{tenantId}
			<if test="status != null">
				AND status_ = #{status}
			</if>
			<if test="type != null">
				AND type_ = #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="search != null and search != ''">
				AND (id_=#{search} OR name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SysCoupon" parameterType="map">
		SELECT * FROM sys_coupon
		<include refid="queryCondition"></include>
		ORDER BY id_ DESC
		<include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM sys_coupon
		<include refid="queryCondition"></include>
	</select>

	<select id="lockCoupon" resultMap="SysCoupon">
		SELECT * FROM sys_coupon WHERE id_ = #{id} FOR UPDATE
	</select>

</mapper>
