<?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.ApiProductStoreRelaMapper">

    <resultMap type="com.kmall.api.entity.ProductStoreRelaVo" id="productStoreRelaMap">
        <result property="id" column="id"/>
        <result property="storeId" column="store_id"/>
        <result property="productId" column="product_id"/>
        <result property="sku" column="sku"/>
        <result property="stockNum" column="stock_num"/>
        <result property="goodsId" column="goods_id"/>
        <result property="retailPrice" column="retail_price"/>
        <result property="marketPrice" column="market_price"/>
        <result property="stockPrice" column="stock_price"/>
        <result property="sellVolume" column="sell_volume"/>
        <result property="goodsBizType" column="goods_biz_type"/>
        <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.ProductStoreRelaVo">
		select
			`id`,
			`store_id`,
			`product_id`,
			`sku`,
			`stock_num`,
			`goods_id`,
			`retail_price`,
			`market_price`,
			`stock_price`,
			`sell_volume`,
			`goods_biz_type`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from mall_product_store_rela
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.kmall.api.entity.ProductStoreRelaVo">
		select
    		`id`,
    		`store_id`,
    		`product_id`,
    		`sku`,
    		`stock_num`,
    		`goods_id`,
    		`retail_price`,
    		`market_price`,
    		`stock_price`,
    		`sell_volume`,
    		`goods_biz_type`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`
		from mall_product_store_rela
		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="queryByStoreIdProductId" resultType="com.kmall.api.entity.ProductStoreRelaVo">
		select
		a.`id`,
		a.`store_id`,
		a.`product_id`,
		a.`stock_num`,
		a.`goods_id`,
		a.`retail_price`,
		a.`market_price`,
		a.`stock_price`,
		a.`sell_volume`,
		a.sku,a.goods_biz_type, a.creater_sn, a.create_time, a.moder_sn, a.mod_time, a.tstm,
		b.goods_sn goodsSn,
		c.goods_sn productSn,
		b.name goodsName,
		cg.name categoryName,
		s.store_name storeName
		from mall_product_store_rela a
		left join mall_goods b on a.goods_id = b.id
		left join mall_product c on a.product_id = c.id
		LEFT JOIN mall_category cg ON b.category_id = cg.id
		left join mall_store s on a.store_id = s.id
		where a.store_id = #{storeId} and a.product_id = #{productId}
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mall_product_store_rela
		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.ProductStoreRelaVo" useGeneratedKeys="true" keyProperty="id">
		insert into mall_product_store_rela(
			`store_id`,
			`product_id`,
			`sku`,
			`stock_num`,
			`goods_id`,
			`retail_price`,
			`market_price`,
			`stock_price`,
			`sell_volume`,
			`goods_biz_type`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{storeId},
			#{productId},
			#{sku},
			#{stockNum},
			#{goodsId},
			#{retailPrice},
			#{marketPrice},
			#{stockPrice},
			#{sellVolume},
			#{goodsBizType},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.api.entity.ProductStoreRelaVo">
		update mall_product_store_rela 
		<set>
			<if test="storeId != null">`store_id` = #{storeId}, </if>
			<if test="productId != null">`product_id` = #{productId}, </if>
			<if test="sku != null">`sku` = #{sku}, </if>
			<if test="stockNum != null">`stock_num` = #{stockNum}, </if>
			<if test="goodsId != null">`goods_id` = #{goodsId}, </if>
			<if test="retailPrice != null">`retail_price` = #{retailPrice}, </if>
			<if test="marketPrice != null">`market_price` = #{marketPrice}, </if>
			<if test="stockPrice != null">`stock_price` = #{stockPrice}, </if>
			<if test="sellVolume != null">`sell_volume` = #{sellVolume}, </if>
			<if test="goodsBizType != null">`goods_biz_type` = #{goodsBizType}, </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 id = #{id}
	</update>
	
	<delete id="delete">
		delete from mall_product_store_rela where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from mall_product_store_rela where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>