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

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <insert id="insertPropValues">
        insert into tz_prod_prop_value (prop_id,prop_value) values
        <foreach collection="prodPropValues" item="prodPropValue" separator=",">
            (#{propId},#{prodPropValue.propValue})
        </foreach>
    </insert>



    <!-- ProdPropValueMapper.xml -->
    <select id="selectSalesValuesByProdId" resultType="com.tashow.cloud.productapi.api.product.dto.ProdPropValueDO">
        SELECT
            pp.id AS propId,
            pp.prop_name,
            ppv.id AS id,
            ppv.prop_value
        FROM tz_prod_prop pp
                 JOIN tz_prod_prop_value ppv ON pp.id = ppv.prop_id
        WHERE pp.prod_id = #{prodId}
          AND pp.rule = 1
          AND ppv.is_expire = 0
    </select>

    <select id="selectSalesValuesByState" resultType="com.tashow.cloud.productapi.api.product.dto.ProdPropValueDO">
        SELECT
            pp.id AS propId,
            pp.prop_name,
            ppv.id AS id,
            ppv.prop_value
        FROM tz_prod_prop pp
                 JOIN tz_prod_prop_value ppv ON pp.id = ppv.prop_id
        WHERE pp.prod_id = #{prodId}
          AND pp.rule = 1
          AND ppv.state = 1
          AND ppv.is_expire = 0

    </select>


    <select id="getMaxPropValue" resultType="int">
        SELECT max(sort) FROM `tz_prod_prop_value`  WHERE prop_id =  #{propId} AND deleted = 0
    </select>

    <update id="updateStateByProdId">
        UPDATE tz_prod_prop_value
        SET state = #{state}
        WHERE prop_id IN (
            SELECT id FROM tz_prod_prop
            WHERE prod_id = #{prodId}
              AND deleted = 0
        )
          AND deleted = 0
    </update>


    <update id="batchMarkDeleted">
        UPDATE tz_prod_prop_value
        SET is_expire = 1, delete_time = NOW()
        WHERE id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <update id="deleteProdPropValueById">
        UPDATE tz_prod_prop_value
        SET
            deleted = 1, delete_time = NOW()
        WHERE id = #{id}
    </update>

    <select id="selectRestoreProp" resultType="com.tashow.cloud.productapi.api.product.dto.ProdPropValueDO">
        SELECT
            ppv.id AS id,
            ppv.prop_value
        FROM tz_prod_prop pp
                 JOIN tz_prod_prop_value ppv ON pp.id = ppv.prop_id
        WHERE pp.prod_id = #{prodId}
          AND pp.rule = 1
          AND ppv.is_expire = 1

    </select>

    <select id="getSKuPropRecycleBinList" resultType="com.tashow.cloud.productapi.api.product.vo.prodpropvalue.ProPropRecycleBinVO" >
        SELECT
        ppv.id,
        ppv.prop_value,
        ppv.prop_id,
        ppv.delete_time
        FROM tz_prod_prop pp
        JOIN tz_prod_prop_value ppv ON pp.id = ppv.prop_id
        WHERE pp.prod_id = #{prodId}
        AND (ppv.is_expire = 1 or ppv.deleted = 1)
        <if test="propValue != null and propValue != ''">
            and prop_value like concat('%', #{propValue}, '%')
        </if>
    </select>


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


    <update id="restorePropValue">
        UPDATE tz_prod_prop_value
        SET is_expire = 1, deleted = 0
        WHERE id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>