<?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.LiveGoodsMapperDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.LiveGoodsMapper" id="LiveGoodsMapper">
		<result column="id_" property="id" />
		<result column="goods_id_" property="goodsId" />
		<result column="live_id_" property="liveId" />
		<result column="sell_count_" property="sellCount" />
		<result column="status_" property="status" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="LiveGoodsMapper" >
		SELECT * FROM live_goods_mapper WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="LiveGoodsMapper">
		SELECT * FROM live_goods_mapper ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.LiveGoodsMapper" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO live_goods_mapper (goods_id_,live_id_,sell_count_,create_time_,update_time_)
		VALUES(#{goodsId},#{liveId},#{sellCount},NOW(),NOW())
	</insert>
	<insert id="batchInsert">
		INSERT INTO live_goods_mapper (goods_id_,live_id_,create_time_,update_time_)
		VALUES
		<foreach collection="liveGoodsIds" item="goodsId" index="index" separator=",">
			(#{goodsId},#{liveId},NOW(),NOW())
		</foreach>
		ON DUPLICATE KEY UPDATE
		goods_id_ = VALUES(goods_id_),
		live_id_ = VALUES(live_id_)
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.LiveGoodsMapper">
		UPDATE live_goods_mapper <set>
		<if test="status != null">
		status_ = #{status},
		</if>
		<if test="liveId != null">
		live_id_ = #{liveId},
		</if>
		<if test="sellCount != null">
		sell_count_ = #{sellCount},
		</if>
		<if test="goodsId != null">
		goods_id_ = #{goodsId},
		</if>
		update_time_ = NOW()
	</set> WHERE id_ = #{id}
	</update>
	<update id="updateStatus">
		UPDATE live_goods_mapper SET status_ = #{status},update_time_ = NOW()
		WHERE 1=1
		<if test="liveId != null and liveId != ''">
		AND live_id_ = #{liveId}
		</if>
		<if test="liveGoodsId != null">
		AND goods_id_ = #{liveGoodsId}
		</if>
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM live_goods_mapper WHERE id_ = #{id} 
	</delete>
	<sql id="queryPageSql">
		<where>
			<if test="liveId != null and liveId != ''">
				AND lgm.live_id_ = #{liveId}
			</if>
			<if test="search != null and search != ''">
				AND (lg.name_ LIKE CONCAT('%',#{search},'%') OR lg.id_ = #{search})
			</if>
			<if test="status != null">
				AND lgm.status_ = #{status}
			</if>
			<if test="liveGoodsId != null">
				AND lg.id_ = #{liveGoodsId}
			</if>
		</where>
	</sql>
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="LiveGoodsMapper" parameterType="map">
		SELECT lg.*,lgm.status_ FROM live_goods_mapper lgm
		LEFT JOIN live_goods lg ON lgm.goods_id_ = lg.id_
		<include refid="queryPageSql" />
		ORDER BY lgm.id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(lgm.id_) FROM live_goods_mapper lgm
		LEFT JOIN live_goods lg ON lgm.goods_id_ = lg.id_
		<include refid="queryPageSql" />
	</select>

	<select id="findByLiveGoodsIdAndStatus" resultMap="LiveGoodsMapper">
		SELECT * FROM live_goods_mapper WHERE goods_id_ = #{liveGoodsId}
		<if test="status != null">
			AND status_ = #{status}
		</if>
	</select>
	<select id="findByLiveIdAndGoodsId" resultMap="LiveGoodsMapper">
		SELECT * FROM live_goods_mapper WHERE live_id_ = #{liveId} AND goods_id_ = #{liveGoodsId}
	</select>

	<sql id="queryGoodsPageSql">
		<where>
			<if test="liveId != null and liveId != ''">
				AND lgm.live_id_ = #{liveId}
			</if>
			<if test="status != null">
				AND lgm.status_ = #{status}
			</if>
			<if test="liveGoodsId != null">
				AND lg.id_ = #{liveGoodsId}
			</if>
		</where>
	</sql>
	<resultMap id="LiveGoodsMapperDto" type="com.ym.mec.biz.dal.dto.LiveGoodsMapperDto">
		<result column="id_" property="id" />
		<result column="image_" property="image" />
		<result column="status_" property="status" />
		<result column="name_" property="name" />
		<result column="original_price_" property="originalPrice" />
		<result column="current_price_" property="currentPrice" />
		<result column="goods_detail_url_" property="goodsDetailUrl" />
		<result column="desc_" property="desc" />
		<result column="stock_count_" property="stockCount" />
	</resultMap>
	<select id="getLiveGoodsList" resultMap="LiveGoodsMapperDto">
		SELECT lg.*,lgm.status_
		FROM live_goods_mapper lgm
		LEFT JOIN live_goods lg ON lgm.goods_id_ = lg.id_
		<include refid="queryGoodsPageSql"/>
		ORDER BY lgm.update_time_ DESC
	</select>
	<select id="countLiveGoods" resultType="java.lang.Integer">
		SELECT COUNT(lgm.id_) FROM live_goods_mapper lgm
		LEFT JOIN live_goods lg ON lgm.goods_id_ = lg.id_
		<include refid="queryPageSql" />
	</select>
	<select id="queryGoodsPage" resultMap="LiveGoodsMapperDto">
		SELECT lg.*,lgm.status_
		FROM live_goods_mapper lgm
		LEFT JOIN live_goods lg ON lgm.goods_id_ = lg.id_
		<include refid="queryPageSql" />
		ORDER BY lgm.update_time_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="queryGoodsIdsByLiveId" resultType="java.lang.Integer">
		SELECT DISTINCT lgm.goods_id_ FROM live_goods_mapper lgm
		WHERE lgm.live_id_ = #{liveId}
	</select>
</mapper>
