<?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.yami.shop.dao.ShopSkuMapper">


    <resultMap id="shopProdMap" type="com.yami.shop.bean.model.ShopSku">
        <id column="id" property="id"/>
        <result column="shop_id" property="shopId"/>
        <result column="sku_id" property="skuId"/>
        <result column="shop_sku_price" property="shopSkuPrice"/>
        <result column="shop_sku_stocks" property="shopSkuStocks"/>
        <result column="is_delete" property="isDelete"/>
        <result column="update_time" property="updateTime"/>
        <result column="rec_time" property="recTime"/>
    </resultMap>

    <update id="deleteByProdId">
        UPDATE tz_shop_sku
        SET is_delete = 1
        WHERE spu_id = #{prodId}
    </update>

    <select id="selectByShopAndSku" resultType="com.yami.shop.bean.model.ShopSku">
        SELECT s.*, sku.sku_name AS skuName
        FROM tz_shop_sku s join tz_sku sku on sku.sku_id = s.sku_id and sku.is_delete = 0
        WHERE s.shop_id = #{shopId}
          AND s.sku_id = #{skuId}
          AND s.is_delete = 0
    </select>


    <select id="selectShopSkuPage" resultMap="shopProdMap">
        SELECT sk.* FROM tz_shop_sku sk join tz_sku s on sk.sku_id = s.sku_id
        WHERE s.is_delete = 0 and sk.is_delete = 0
        <if test="shopSku.shopId != null">
            AND sk.shop_id = #{shopSku.shopId}
        </if>
        <if test="shopSku.skuId != null">
            AND sk.sku_id = #{shopSku.skuId}
        </if>
        <if test="shopSku.skuName != null">
            AND s.sku_name LIKE CONCAT('%',#{shopSku.skuName},'%')
        </if>
        order by sk.rec_time desc
    </select>


    <select id="exportList" resultType="com.yami.shop.bean.vo.ChannelProdExportVO">
        SELECT
        sha.hb_sku_id AS skuId,
        sha.sku_name AS skuName,
        p.channel_id AS channelId,
        c.channel_name AS channelName,
        d.shop_name AS shopName,
        p.purchase_price AS purchasePrice,
        p.delivery_price AS deliveryPrice,
        p.channel_prod_price AS channelProdPrice
        FROM tz_shop_sku sh
        LEFT JOIN  tz_channel_prod p ON p.shop_id = sh.shop_id and sh.sku_id = p.sku_id and p.is_delete = 0
        LEFT JOIN tz_channel c ON p.channel_id = c.id
        LEFT JOIN tz_sku sha ON sh.sku_id = sha.sku_id
        LEFT JOIN tz_shop_detail d ON sh.shop_id = d.shop_id
        WHERE sh.is_delete = 0 and sha.is_delete = 0 and p.is_delete = 0
        <if test="shopSku.shopId != null">
            AND sh.shop_id = #{shopSku.shopId}
        </if>
        <if test="shopSku.skuName != null">
            AND sha.sku_name LIKE CONCAT('%',#{shopSku.skuName},'%')
        </if>
        order by sh.rec_time desc
    </select>
</mapper>
