<?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.ChannelProdMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.ChannelProd">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="channel_id" jdbcType="BIGINT" property="channelId"/>
        <result column="sku_id" jdbcType="BIGINT" property="skuId"/>
        <result column="purchase_price" jdbcType="DECIMAL" property="purchasePrice"/>
        <result column="delivery_price" jdbcType="DECIMAL" property="deliveryPrice"/>
        <result column="channel_prod_price" jdbcType="DECIMAL" property="channelProdPrice"/>
        <result column="is_delete" jdbcType="TINYINT" property="isDelete"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="rec_time" jdbcType="TIMESTAMP" property="recTime"/>
    </resultMap>

    <select id="getChannelProdsBychannelId" resultMap="BaseResultMap">
        SELECT *
        FROM tz_channel_prod
        WHERE channel_id = #{channelId}
          AND is_delete = 0
        ORDER BY rec_time DESC
    </select>

    <select id="getChannelProdsBySkuId" resultMap="BaseResultMap">
        SELECT *
        FROM tz_channel_prod
        WHERE sku_id = #{skuId}
          AND is_delete = 0
        ORDER BY rec_time DESC
    </select>

    <select id="getChannelProdBychannelIdAndSkuId" resultMap="BaseResultMap">
        SELECT *
        FROM tz_channel_prod
        WHERE channel_id = #{channelId}
          AND sku_id = #{skuId}
          AND is_delete = 0
    </select>

    <select id="getChannelProdPage" resultMap="BaseResultMap">
        # 关联查询查询条件
        SELECT * FROM tz_channel_prod
        <where>
            is_delete = 0
            <if test="channelProd.channelId != null">
                AND channel_id = #{channelProd.channelId}
            </if>
            <if test="channelProd.skuId != null">
                AND sku_id = #{channelProd.skuId}
            </if>
            <if test="channelProd.purchasePrice != null">
                AND purchase_price = #{channelProd.purchasePrice}
            </if>
            <if test="channelProd.deliveryPrice != null">
                AND delivery_price = #{channelProd.deliveryPrice}
            </if>
            <if test="channelProd.channelProdPrice != null">
                AND channel_prod_price = #{channelProd.channelProdPrice}
            </if>
        </where>
        ORDER BY rec_time DESC
    </select>

    <update id="deleteBychannelId">
        UPDATE tz_channel_prod
        SET is_delete   = 1,
            update_time = NOW()
        WHERE channel_id = #{channelId}
    </update>

    <update id="deleteBySkuId">
        UPDATE tz_channel_prod
        SET is_delete   = 1,
            update_time = NOW()
        WHERE sku_id = #{skuId}
    </update>

    <update id="deleteChannelProd">
        UPDATE tz_channel_prod
        SET is_delete = 1,
            update_time = NOW()
        WHERE sku_id = #{skuId} AND shop_id = #{shopId}
    </update>


    <update id="updateBatchPrices">
        <foreach collection="channelProds" item="channelProd" separator=";">
            UPDATE tz_channel_prod
            SET purchase_price = #{channelProd.purchasePrice},
            delivery_price = #{channelProd.deliveryPrice},
            channel_prod_price = #{channelProd.channelProdPrice},
            update_time = NOW()
            WHERE id = #{channelProd.id}
        </foreach>
    </update>

    <select id="countBychannelId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM tz_channel_prod
        WHERE channel_id = #{channelId}
          AND is_delete = 0
    </select>

    <select id="countBySkuId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM tz_channel_prod
        WHERE sku_id = #{skuId}
          AND is_delete = 0
    </select>

    <select id="selectListAll" resultType="com.yami.shop.bean.vo.ProdChannelVO">
        SELECT c.id,c.channel_id channelId, c.channel_prod_price price,c.update_time updateTime, channel.channel_name channelName,s.shop_name shopName
        FROM tz_channel_prod c
                 left join tz_shop_detail s on c.shop_id = s.shop_id
                 left join tz_channel channel on c.channel_id = channel.id
        WHERE c.sku_id = #{skuId}
          AND c.shop_id = #{shopId}
          AND c.is_delete = 0
    </select>


</mapper>
