<?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.tashow.cloud.product.mapper.SkuMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="getSkuRecycleBinPageList" resultType="com.tashow.cloud.productapi.api.product.vo.sku.SkuRecycleBinVO" >
        select * from tz_sku where prod_id = #{prodId} and deleted = 1
        <if test="properties != null and properties != ''">
            and properties like concat('%', #{properties}, '%')
        </if>
    </select>


    <select id="getSkuPageList" resultType="com.tashow.cloud.productapi.api.product.dto.SkuDO">
        Select * from tz_sku where deleted = 0
        <if test="prodId != null and prodId != ''">
           and  prod_id = #{prodId}
        </if>
        <if test="skuId != null and skuId != ''">
            and sku_id = #{skuId}
        </if>
        <if test="properties != null and properties != ''">
            and properties like concat('%', #{properties}, '%')
        </if>
    </select>


    <select id="getSkuListByName" resultType="com.tashow.cloud.productapi.api.product.dto.SkuDO" >
        select sku_id, properties, delete_time,deleted from tz_sku where deleted = 0 and prod_id = #{prodId}
        <if test="propertiesName != null and propertiesName != ''">
            and properties like concat('%', #{propertiesName}, '%')
        </if>
    </select>

    <!-- SkuMapper.xml -->
    <select id="selectPropertiesByProdIdAndNotDeleted" resultType="string">
        SELECT properties
        FROM tz_sku
        WHERE prod_id = #{prodId}
          AND deleted = 0
    </select>

    <!-- SkuMapper.xml -->
    <select id="selectPropertiesByProdIdShelf" resultType="string">
        SELECT properties
        FROM tz_sku
        WHERE prod_id = #{prodId}
          AND deleted = 0
          and is_shelf =0
    </select>

    <update id="deleteBySkuId">
        UPDATE tz_sku
        SET
            deleted = 1,
            delete_time = now()
        WHERE sku_id = #{skuId}
    </update>


    <update id="batchSkuDeleted">
        UPDATE tz_sku
        SET  deleted = 1,
        delete_time = now()
        WHERE sku_id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="getskuListBySkuIds" resultType="com.tashow.cloud.productapi.api.product.dto.SkuDO">
        SELECT *
        FROM tz_sku
        WHERE sku_id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getskuListByDeleted" resultType="com.tashow.cloud.productapi.api.product.dto.SkuDO">
        SELECT *
        FROM tz_sku
        WHERE prod_id = #{prodId} AND deleted = 1 and sku_id  not IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>



    <update id="updateSkuDeleted">
        UPDATE tz_sku
        SET  deleted = 0
             WHERE sku_id = #{skuId}
    </update>


    <update id="batchSkuRecover">
        UPDATE tz_sku
        SET  deleted = 0
        WHERE sku_id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectShelfStatusByProdId" resultType="int">
        SELECT is_shelf
        FROM tz_sku
        WHERE prod_id = #{prodId}
          AND deleted = 0
    </select>

</mapper>