<?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="cn.iocoder.mall.product.dao.ProductSkuMapper">

    <sql id="FIELDS">
        id, spu_id, status, pic_url, attrs,
        price, quantity, create_time
    </sql>

    <select id="selectById" parameterType="Integer" resultType="ProductSkuDO">
        SELECT
            <include refid="FIELDS" />
        FROM product_sku
        WHERE id = #{id}
        AND deleted = 0
    </select>

    <select id="selectByIds" resultType="ProductSkuDO">
        SELECT
            <include refid="FIELDS" />
        FROM product_sku
            WHERE id IN
            <foreach item="id" collection="ids" separator="," open="(" close=")" index="">
                #{id}
            </foreach>
            AND deleted = 0
    </select>

    <insert id="insertList" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO product_sku (
            spu_id, status, pic_url, attrs, price,
            quantity, deleted, create_time
        ) VALUES
        <foreach collection="productSkuDOs" item="productSkuDO" separator=",">
            (#{productSkuDO.spuId}, #{productSkuDO.status}, #{productSkuDO.picUrl}, #{productSkuDO.attrs}, #{productSkuDO.price},
             #{productSkuDO.quantity}, #{productSkuDO.deleted}, #{productSkuDO.createTime}
            )
        </foreach>
    </insert>

    <select id="selectListBySpuIdAndStatus" resultType="ProductSkuDO">
      SELECT
          <include refid="FIELDS" />
      FROM product_sku
      WHERE spu_id = #{spuId}
      AND status = #{status}
      AND deleted = 0
    </select>

    <update id="update" parameterType="ProductSpuDO">
        UPDATE product_sku
        <set>
            <if test="spuId != null">
                spu_id = #{spuId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="picUrl != null">
                pic_url = #{picUrl},
            </if>
            <if test="attrs != null">
                attrs = #{attrs},
            </if>
            <if test="price != null">
                price = #{price},
            </if>
            <if test="quantity != null">
                quantity = #{quantity},
            </if>
            <if test="deleted != null">
                deleted = #{deleted}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateToDeleted" parameterType="Integer">
        UPDATE product_sku
        SET deleted = 1
        WHERE id IN
            <foreach item="id" collection="ids" separator="," open="(" close=")" index="">
                #{id}
            </foreach>
    </update>

</mapper>
