<?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.LiveGoodsDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.LiveGoods" id="LiveGoods">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="image_" property="image" />
		<result column="stock_count_" property="stockCount" />
		<result column="current_price_" property="currentPrice" />
		<result column="original_price_" property="originalPrice" />
		<result column="goods_detail_url_" property="goodsDetailUrl" />
		<result column="desc_" property="desc" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="status_" property="status" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="LiveGoods" >
		SELECT * FROM live_goods WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="LiveGoods">
		SELECT * FROM live_goods ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.LiveGoods" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO live_goods (name_,image_,stock_count_,current_price_,
		                        original_price_,goods_detail_url_,desc_,
		                        create_time_,update_time_,tenant_id_)
		VALUES(#{name},#{image},#{stockCount},#{currentPrice},
		       #{originalPrice},#{goodsDetailUrl},#{desc},NOW(),NOW(),#{tenantId})
	</insert>
	<insert id="addStock">
		UPDATE live_goods SET stock_count_ = stock_count_ + 1 WHERE id_ = #{liveGoodsId}
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.LiveGoods">
		UPDATE live_goods <set>
		<if test="tenantId != null">
			tenant_id_ = #{tenantId},
		</if>
		<if test="originalPrice != null">
			original_price_ = #{originalPrice},
		</if>
		<if test="name != null">
			name_ = #{name},
		</if>
		<if test="stockCount != null">
			stock_count_ = #{stockCount},
		</if>
		<if test="currentPrice != null">
			current_price_ = #{currentPrice},
		</if>
		<if test="image != null">
			image_ = #{image},
		</if>
		<if test="desc != null">
			desc_ = #{desc},
		</if>
		<if test="goodsDetailUrl != null">
			goods_detail_url_ = #{goodsDetailUrl},
		</if>
		update_time_ = NOW()
	</set> WHERE id_ = #{id}
	</update>
	<update id="reduceStock">
		UPDATE live_goods SET stock_count_ = stock_count_ - 1 WHERE id_ = #{liveGoodsId} AND stock_count_ > 0
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM live_goods WHERE id_ = #{id} 
	</delete>
	<sql id="queryPageSql">
		<where>
			<if test="search != null and search != ''">
				AND (name_ like '%${search}%' OR id_ = #{search})
			</if>
			<if test="ignoreGoodsIds != null and ignoreGoodsIds.size > 0">
				AND id_ NOT IN
				<foreach collection="ignoreGoodsIds" item="ignoreGoodsId" open="(" separator="," close=")">
					#{ignoreGoodsId}
				</foreach>
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="LiveGoods" parameterType="map">
		SELECT * FROM live_goods
		<include refid="queryPageSql" />
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM live_goods
		<include refid="queryPageSql" />
	</select>
	<select id="getLock" resultMap="LiveGoods">
		SELECT * FROM live_goods WHERE id_ = #{id} LIMIT 1 FOR UPDATE
	</select>
	<select id="getByName" resultMap="LiveGoods">
		SELECT * FROM live_goods WHERE name_ = #{goodsName} LIMIT 1
	</select>
</mapper>
