<?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.admin.dao.ProductStoreRelaDao">

    <resultMap type="com.kmall.admin.entity.ProductStoreRelaEntity" id="productStoreRelaMap">
        <result property="id" column="id"/>
        <result property="storeId" column="store_id"/>
        <result property="productId" column="product_id"/>
        <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="storeName" column="storeName"/>
        <result property="goodsName" column="goodsName"/>
        <result property="goodsSn" column="goodsSn"/>
        <result property="productSn" column="productSn"/>
        <result property="categoryName" column="categoryName"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.ProductStoreRelaEntity">
		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`,
			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.id = #{id}
	</select>

    <select id="queryByStoreIdProductId" resultType="com.kmall.admin.entity.ProductStoreRelaEntity">
        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`,
        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="queryList" resultType="com.kmall.admin.entity.ProductStoreRelaEntity">
        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`,
        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 1=1 and b.is_delete = 0
        <if test="goodsId != null">
            AND a.goods_id = #{goodsId}
        </if>
        <if test="goodsName != null and goodsName != '' ">
            AND b.name LIKE concat('%',#{goodsName},'%')
        </if>
        <if test="goodsSn != null and goodsSn != '' ">
            AND b.goods_sn = #{goodsSn}
        </if>
        <if test="productSn != null and productSn != '' ">
            AND c.goods_sn = #{productSn}
        </if>
        <if test="productId != null">
            AND a.product_id = #{productId}
        </if>
        <if test="storeId != null and storeId.trim() != ''">
            AND s.id = #{storeId}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(a.id)
        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 1=1 and b.is_delete = 0
        <if test="goodsId != null">
            AND a.goods_id = #{goodsId}
        </if>
        <if test="goodsName != null and goodsName != '' ">
            AND b.name LIKE concat('%',#{goodsName},'%')
        </if>
        <if test="goodsSn != null and goodsSn != '' ">
            AND b.goods_sn = #{goodsSn}
        </if>
        <if test="productSn != null and productSn != '' ">
            AND c.goods_sn = #{productSn}
        </if>
        <if test="productId != null">
            AND a.product_id = #{productId}
        </if>
        <if test="storeId != null and storeId.trim() != ''">
            AND s.id = #{storeId}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.ProductStoreRelaEntity" useGeneratedKeys="true"
            keyProperty="id">
		insert into mall_product_store_rela(
			`store_id`,
			`product_id`,
			`stock_num`,
			`goods_id`,
			`retail_price`,
			`market_price`,
            `stock_price`,
            `sell_volume`)
		values(
			#{storeId},
			#{productId},
			#{stockNum},
			#{goodsId},
			#{retailPrice},
			#{marketPrice},
			#{stockPrice},
			#{sellVolume})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.ProductStoreRelaEntity">
        update mall_product_store_rela
        <set>
            <if test="storeId != null">`store_id` = #{storeId},</if>
            <if test="productId != null">`product_id` = #{productId},</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>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_product_store_rela where id = #{value}
	</delete>

    <delete id="deleteByGoodsId">
		delete from mall_product_store_rela where goods_id = #{goodsId}
	</delete>

    <delete id="deleteBatch">
        delete from mall_product_store_rela where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="updateStockNum" parameterType="com.kmall.admin.entity.ProductStoreRelaEntity">
        update mall_product_store_rela a
        <set>
            <if test="stock_num != null">a.`stock_num` = #{stock_num},</if>
            <if test="sell_volume != null">a.`sell_volume` = #{sell_volume},</if>
        </set>
        where a.id = #{id}
    </update>

</mapper>