<?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.tmerclub.cloud.product.mapper.ShopCartItemMapper">
    <resultMap id="shopCartItemMap" type="com.tmerclub.cloud.product.model.ShopCartItem">
        <id column="cart_item_id" property="cartItemId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="spu_id" property="spuId"/>
        <result column="sku_id" property="skuId"/>
        <result column="user_id" property="userId"/>
        <result column="count" property="count"/>
        <result column="discount_id" property="discountId"/>
        <result column="price_fee" property="priceFee"/>
        <result column="distribution_user_id" property="distributionUserId"/>
        <result column="is_checked" property="isChecked"/>
        <result column="is_closed" property="isClosed"/>
    </resultMap>

    <resultMap id="shopCartItemVOMap" type="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        <id column="cart_item_id" property="cartItemId"/>
        <result column="create_time" property="createTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="spu_id" property="spuId"/>
        <result column="sku_id" property="skuId"/>
        <result column="category_id" property="categoryId"/>
        <result column="rate" property="rate"/>
        <result column="user_id" property="userId"/>
        <result column="count" property="count"/>
        <result column="discount_id" property="discountId"/>
<!--        <result column="price_fee" property="priceFee"/>-->
        <result column="distribution_user_id" property="distributionUserId"/>
        <result column="is_checked" property="isChecked"/>
        <result column="delivery_mode" property="deliveryMode"/>
        <result column="party_code" property="partyCode"/>
        <result column="spu_code" property="spuCode"/>
        <result column="delivery_template_id" property="deliveryTemplateId"/>
        <result column="delivery_amount" property="deliveryAmount"/>
        <result column="supplier_delivery_template_id" property="deliveryTemplateId"/>
        <result column="sku_price_fee" property="skuPriceFee"/>
        <result column="score_price" property="scorePrice"/>
        <result column="is_collection" property="isCollection"/>
        <result column="img_url" property="imgUrl"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="supplier_spu_type" property="supplierSpuType"/>
        <result column="spu_type" property="spuType"/>
        <result column="sku_choice_status" property="skuChoiceStatus"/>
        <result column="supplier_delivery_type" property="supplierDeliveryType"/>
        <result column="measure_unit" property="measureUnit"/>
        <result column="supplier_price_fee" property="supplierPriceFee"/>
        <result column="weight" property="weight"/>
        <result column="volume" property="volume"/>
        <result column="combo_id" property="comboId"/>
        <result column="combo_count" property="comboCount"/>
        <result column="parent_cart_item_id" property="parentCartItemId"/>
        <result column="virtual_remark" property="virtualRemark"/>
        <result column="is_refund" property="isRefund"/>
        <result column="spu_mold" property="mold"/>
        <result column="supplier_spu_id" property="supplierSpuId"/>
        <result column="pre_sale_type" property="preSaleType"/>
        <result column="pre_sale_delivery_type" property="preSaleDeliveryType"/>
        <result column="pre_sale_delivery_time" property="preSaleDeliveryTime"/>
        <result column="write_off_num" property="writeOffNum"/>
        <result column="write_off_multiple_count" property="writeOffMultipleCount"/>
        <result column="write_off_time" property="writeOffTime"/>
        <result column="write_off_start" property="writeOffStart"/>
        <result column="write_off_end" property="writeOffEnd"/>
        <collection property="spuLangList" ofType="com.tmerclub.cloud.common.order.vo.OrderSpuLangVO">
            <id column="spu_lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
        </collection>
        <collection property="skuLangList" ofType="com.tmerclub.cloud.common.order.vo.OrderSkuLangVO">
            <id column="sku_lang" property="lang"/>
            <result column="sku_name" property="skuName"/>
        </collection>
    </resultMap>

    <sql id="Vo_Column_List">
        `cart_item_id`,
        `create_time`,
        `update_time`,
        `shop_id`,
        `spu_id`,
        `sku_id`,
        `user_id`,
        `count`,
        `discount_id`,
        `price_fee` as sku_price_fee,
        `distribution_user_id`,
        is_checked,
        `combo_id`,
        `combo_count`,
        `parent_cart_item_id`
    </sql>
    <select id="getByCartId" resultType="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        select
        <include refid="Vo_Column_List"/>
        from shop_cart_item
        where cart_item_id = #{cartItemId}
    </select>
    <insert id="save">
        insert into shop_cart_item (`shop_id`, `spu_id`, `sku_id`, `user_id`, `category_id`, `count`, `discount_id`, `price_fee`,
                                    `distribution_user_id`, is_checked,`combo_id`,`combo_count`,`parent_cart_item_id`)
        values (#{shopCartItem.shopId}, #{shopCartItem.spuId}, #{shopCartItem.skuId}, #{shopCartItem.userId},
                #{shopCartItem.categoryId},#{shopCartItem.count}, #{shopCartItem.discountId}, #{shopCartItem.priceFee},
                #{shopCartItem.distributionUserId}, #{shopCartItem.isChecked},#{shopCartItem.comboId},#{shopCartItem.comboCount},#{shopCartItem.parentCartItemId});
    </insert>
    <update id="update">
        update shop_cart_item
        <set>
            <if test="shopCartItem.shopId != null">
                `shop_id` = #{shopCartItem.shopId},
            </if>
            <if test="shopCartItem.spuId != null">
                `spu_id` = #{shopCartItem.spuId},
            </if>
            <if test="shopCartItem.skuId != null">
                `sku_id` = #{shopCartItem.skuId},
            </if>
            <if test="shopCartItem.userId != null">
                `user_id` = #{shopCartItem.userId},
            </if>
            <if test="shopCartItem.count != null">
                `count` = #{shopCartItem.count},
            </if>
            <if test="shopCartItem.discountId != null">
                `discount_id` = #{shopCartItem.discountId},
            </if>
            <if test="shopCartItem.priceFee != null">
                `price_fee` = #{shopCartItem.priceFee},
            </if>
            <if test="shopCartItem.discountId != null">
                `discount_id` = #{shopCartItem.discountId},
            </if>
            <if test="shopCartItem.distributionUserId != null">
                `distribution_user_id` = #{shopCartItem.distributionUserId},
            </if>
            <if test="shopCartItem.isChecked != null">
                `is_checked` = #{shopCartItem.isChecked},
            </if>
            <if test="shopCartItem.comboId != null">
                `combo_id` = #{shopCartItem.comboId},
            </if>
<!--            <if test="shopCartItem.parentCartItemId != null">-->
<!--                `parent_cart_item_id` = #{shopCartItem.parentCartIitemId},-->
<!--            </if>-->
            <if test="shopCartItem.comboCount != null">
                `combo_count` = #{shopCartItem.comboCount}
            </if>
        </set>
        where cart_item_id = #{shopCartItem.cartItemId} and user_id =#{shopCartItem.userId}
    </update>

    <select id="getShopCartItems" resultMap="shopCartItemVOMap">
        SELECT sci.*,
               spu.delivery_mode,
               sl.lang                  spu_lang,
               sl.spu_name,
               spu.category_id,
               spu.delivery_template_id,
               spu.delivery_amount,
               IFNULL(sku.img_url,spu.main_img_url) AS img_url,
               sku.price_fee             sku_price_fee,
               skul.lang                 sku_lang,
               skul.sku_name,
               sku.score_fee score_price,
               IF(sc.id, TRUE, FALSE) AS is_collection,
               sku.weight,sku.volume,
               spu.supplier_spu_type,
               spu.supplier_spu_id,
               spu.supplier_delivery_type,
               spu.supplier_id,
               spu.measure_unit,
               spu.sku_choice_status,
               spu.spu_code,
               spu.spu_mold,
               spu.is_refund,
               spu.virtual_remark,
               spu.pre_sale_type,
               spu.pre_sale_delivery_type,
               spu.pre_sale_delivery_time,
               spu.write_off_num,
               spu.write_off_multiple_count,
               spu.write_off_time,
               spu.write_off_start,
               spu.write_off_end,
               spu.spu_type,
               sku.party_code
        FROM shop_cart_item sci
        JOIN spu ON spu.spu_id = sci.spu_id
        JOIN spu_lang sl ON spu.spu_id = sl.spu_id
        JOIN sku ON sku.sku_id = sci.sku_id
        LEFT JOIN sku_lang skul ON sku.sku_id = skul.sku_id
        LEFT JOIN spu_collection sc ON sc.spu_id = spu.spu_id AND sci.user_id = sc.user_id
        LEFT JOIN sku ssku ON ssku.sku_id = sku.`supplier_sku_id`
        WHERE sci.user_id = #{userId}

        <if test="!isExpiry">
            AND spu.status = 1
            AND sku.status = 1
            AND sci.is_closed = 1
            and spu.pre_sale_type <![CDATA[<>]]> 1
            AND (ssku.`status` = 1 or sku.`supplier_sku_id` IS NULL)
        </if>
        <if test="isExpiry">
            AND (spu.status != 1 or sku.status != 1 or sci.is_closed = 0 or spu.pre_sale_type = 1 or (sku.`supplier_sku_id` IS NOT NULL AND ssku.status = 0))
        </if>
        <if test="isChecked != null and isChecked">
            AND sci.is_checked = 1
        </if>
        ORDER BY sci.cart_item_id DESC
    </select>


    <select id="getUserShopCartItems" resultMap="shopCartItemVOMap">
        SELECT <include refid="Vo_Column_List" />
        FROM shop_cart_item
        WHERE user_id = #{userId} AND is_checked = 1 AND is_closed = 1
        ORDER BY cart_item_id DESC
    </select>


    <delete id="deleteShopCartItemsByShopCartItemIds">
        delete
        from shop_cart_item
        where user_id = #{userId} and cart_item_id in
        <foreach collection="shopCartItemIds" item="shopCartItemId" open="(" separator="," close=")">
            #{shopCartItemId}
        </foreach>
    </delete>

    <delete id="deleteAllShopCartItems">
        delete
        from shop_cart_item
        where user_id = #{userId}
    </delete>

    <select id="listUserIdBySpuIds" resultType="java.lang.String">
        select user_id
        from shop_cart_item
        where spu_id in
        <foreach collection="spuIds" item="spuId" open="(" separator="," close=")">
          #{spuId}
        </foreach>
    </select>

    <select id="listUserIdBySupplierSpuIds" resultType="java.lang.String">
        SELECT DISTINCT s.user_id
        FROM shop_cart_item s
         LEFT JOIN spu ON spu.`spu_id` = s.`spu_id`
        WHERE spu.`supplier_spu_id` IN
        <foreach collection="supplierSpuIds" item="spuId" open="(" separator="," close=")">
            #{spuId}
        </foreach>
    </select>

    <select id="listUserIdByComboIds" resultType="java.lang.String">
        select user_id
        from shop_cart_item
        where combo_id in
        <foreach collection="comboIds" item="comboId" open="(" separator="," close=")">
            #{comboId}
        </foreach>
    </select>

    <select id="listUserIdBySkuIds" resultType="java.lang.String">
        select user_id
        from shop_cart_item
        where sku_id in
        <foreach collection="skuIds" item="skuId" open="(" separator="," close=")">
            #{skuId}
        </foreach>
    </select>

    <select id="getShopCartItemCount" resultType="java.lang.Integer">
<!--        select sum(`count`)-->
        SELECT
            count(*)
        from shop_cart_item sci
        JOIN spu ON spu.spu_id = sci.spu_id
        JOIN sku ON sku.sku_id = sci.sku_id
        left join sku ssku on sku.supplier_sku_id = ssku.sku_id
        where user_id = #{userId} AND (spu.status = 1 and sku.status = 1 AND sci.is_closed = 1 and spu.pre_sale_type <![CDATA[<>]]> 1
        and (sku.`supplier_sku_id` IS NULL or ssku.status = 1))
    </select>

    <update id="checkShopCartItems">
        <foreach collection="checkShopCartItems" separator=";" item="item">
            update shop_cart_item set is_checked = #{item.isChecked} where cart_item_id = #{item.shopCartItemId} and user_id = #{userId}
        </foreach>
    </update>
    <update id="updateIsCloseByShopIds">
        update shop_cart_item set is_closed = #{isClosed}
        where shop_id in
        <foreach collection="shopIds" item="shopId" open="(" separator="," close=")">
            #{shopId}
        </foreach>
    </update>

    <update id="updateShopCartItemBatch">
        <foreach collection="shopCartItemList" item="shopCartItem" separator=";">
            update shop_cart_item
            <set>
                <if test="shopCartItem.shopId != null">
                    `shop_id` = #{shopCartItem.shopId},
                </if>
                <if test="shopCartItem.spuId != null">
                    `spu_id` = #{shopCartItem.spuId},
                </if>
                <if test="shopCartItem.skuId != null">
                    `sku_id` = #{shopCartItem.skuId},
                </if>
                <if test="shopCartItem.userId != null">
                    `user_id` = #{shopCartItem.userId},
                </if>
                <if test="shopCartItem.count != null">
                    `count` = #{shopCartItem.count},
                </if>
                <if test="shopCartItem.discountId != null">
                    `discount_id` = #{shopCartItem.discountId},
                </if>
                <if test="shopCartItem.priceFee != null">
                    `price_fee` = #{shopCartItem.priceFee},
                </if>
                <if test="shopCartItem.discountId != null">
                    `discount_id` = #{shopCartItem.discountId},
                </if>
                <if test="shopCartItem.distributionUserId != null">
                    `distribution_user_id` = #{shopCartItem.distributionUserId},
                </if>
                <if test="shopCartItem.isChecked != null">
                    `is_checked` = #{shopCartItem.isChecked},
                </if>
            </set>
            where cart_item_id = #{shopCartItem.cartItemId} and user_id =#{shopCartItem.userId}
        </foreach>
    </update>

    <select id="getCartItemBySpuIdAndComboId" resultType="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        select <include refid="Vo_Column_List"/> from shop_cart_item where spu_id = #{spuId} and combo_id = #{comboId} and user_id = #{userId} and sku_id = #{skuId}
    </select>

    <delete id="deleteCartByComboUpdate">
        delete from shop_cart_item
        <where>
            combo_id in
            <foreach collection="comboIdList" item="comboId" open="(" separator="," close=")">
                #{comboId}
            </foreach>
            <if test="spuId != null and spuId != -1">
                AND spu_id = #{spuId}
            </if>
            <if test="skuId != null and skuId != -1">
                AND sku_id = #{spuId}
            </if>
        </where>
    </delete>

    <delete id="deleteCartByComboIdAndSpuIdS">
        delete from shop_cart_item
        <where>
            combo_id = #{comboId}
            and
            spu_id in
            <foreach collection="spuIdList" item="spuId" open="(" separator="," close=")">
                #{spuId}
            </foreach>
        </where>
    </delete>

    <delete id="deleteCartByComboIdAndSkuIdS">
        delete from shop_cart_item
        <where>
            combo_id = #{comboId}
            and
            sku_id in
            <foreach collection="skuIdList" item="skuId" open="(" separator="," close=")">
                #{skuId}
            </foreach>
        </where>
    </delete>

    <update id="updateCartSpuPriceByComboIdAndSkuId">
        update shop_cart_item set price_fee = #{price} where combo_id = #{comboId} and sku_id = #{skuId}
    </update>

    <select id="getMatchSkuIdList" resultType="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        select sc.* from shop_cart_item AS sc
        LEFT JOIN spu AS s ON s.spu_id = sc.spu_id
        where
        s.status = 1
        AND parent_cart_item_id =
    (select cart_item_id from shop_cart_item where combo_id = #{comboId} and sku_id = #{skuId} and user_id = #{userId})
    </select>

    <select id="getComboMainSpu" resultType="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        select <include refid="Vo_Column_List"/> from shop_cart_item where combo_id = #{comboId} and sku_id = #{skuId} and user_id = #{userId}
    </select>

    <select id="getMatchSpuList" resultType="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        select <include refid="Vo_Column_List"/> from shop_cart_item where parent_cart_item_id = #{parentShopCartId}
    </select>

    <select id="skuIdListByComboMainSkuId" resultType="java.lang.Long">
        SELECT sku_id FROM shop_cart_item WHERE parent_cart_item_id
            IN
        (SELECT cart_item_id FROM shop_cart_item WHERE sku_id IN
         <foreach collection="skuIdList" item="skuId" open="(" separator="," close=")">
             #{skuId}
         </foreach>
        )
    </select>

    <select id="listShopCartItemByComboIdAndSpuId" resultType="com.tmerclub.cloud.common.order.vo.ShopCartItemVO">
        select <include refid="Vo_Column_List"/> from shop_cart_item where combo_id = #{comboId} and spu_id = #{spuId}
    </select>
</mapper>
