<?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.cool.store.mapper.order.StoreWishlistMapper">

    <insert id="insertBatch">
        INSERT INTO zxjp_store_wishlist
        (line_id, store_type_id, total_amount, version_no, wishlist_status, point_id, deleted, create_time, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.lineId}, #{item.storeTypeId}, #{item.totalAmount}, #{item.versionNo}, #{item.wishlistStatus}, #{item.pointId}, 0, NOW(), NOW())
        </foreach>
    </insert>

    <update id="softDeleteByLineIdAndStoreTypeAndVersion">
        UPDATE zxjp_store_wishlist
        SET deleted = 1,
            update_time = NOW()
        WHERE line_id = #{lineId}
          AND store_type_id = #{storeTypeId}
          AND version_no = #{versionNo}
          AND deleted = 0
    </update>

    <select id="getById" resultType="com.cool.store.entity.order.StoreWishlistDO">
        SELECT id, line_id, store_type_id, total_amount, version_no, wishlist_status, point_id, deleted, create_time, update_time
        FROM zxjp_store_wishlist
        WHERE id = #{id}
          AND deleted = 0
        LIMIT 1
    </select>

    <select id="getByPointId" resultType="com.cool.store.entity.order.StoreWishlistDO">
        SELECT id, line_id, store_type_id, total_amount, version_no, wishlist_status, point_id, deleted, create_time, update_time
        FROM zxjp_store_wishlist
        where deleted = 0
        and wishlist_status !=15
        <if test="lineId!=null">
            and line_id = #{lineId}
        </if>
        <if test="pointId!=null">
            and point_id = #{pointId}
        </if>
        limit 1
    </select>

    <select id="getPointBindWishlistCount" resultType="java.lang.Integer">
        select count(1) from zxjp_store_wishlist
        where deleted = 0
        <if test="pointId != null">
            AND point_id = #{pointId}
        </if>
        <if test="wishlistStatus != null">
            AND wishlist_status != #{wishlistStatus}
        </if>

    </select>

    <select id="listByLineIdAndStoreTypeAndVersion" resultType="com.cool.store.entity.order.StoreWishlistDO">
        SELECT id, line_id, store_type_id, total_amount, version_no, wishlist_status, point_id, deleted, create_time, update_time
        FROM zxjp_store_wishlist
        WHERE line_id = #{lineId}
          AND store_type_id = #{storeTypeId}
          AND version_no = #{versionNo}
          AND deleted = 0
        ORDER BY id ASC
    </select>

    <select id="listByLineId" resultType="com.cool.store.vo.order.MiniStoreWishlistListVO">
        SELECT
            w.id as wishlistId,
            w.store_type_id AS storeTypeId,
            st.brand AS brand,
            st.store_type_name AS storeTypeName,
            st.store_type AS storeType,
            st.image_url AS imageUrl,
            w.create_time AS createTime,
            w.total_amount AS totalAmount,
            w.version_no AS versionNo,
            w.point_id as pointId,
            w.wishlist_status as wishlistStatus
        FROM zxjp_store_wishlist w
        LEFT JOIN zxjp_store_type st ON st.id = w.store_type_id
        WHERE w.line_id = #{lineId}
          AND w.deleted = 0
        <if test="storeTypeId != null">
            AND w.store_type_id = #{storeTypeId}
        </if>
        <if test="wishlistStatus != null">
            AND w.wishlist_status = #{wishlistStatus}
        </if>
        ORDER BY w.create_time DESC
    </select>

    <select id="listByVersionList" resultType="com.cool.store.vo.order.MiniStoreWishlistListVO">
        SELECT
        w.id as wishlistId,
        w.store_type_id AS storeTypeId,
        w.line_id as lineId,
        st.brand AS brand,
        st.store_type_name AS storeTypeName,
        st.store_type AS storeType,
        st.image_url AS imageUrl,
        w.create_time AS createTime,
        w.total_amount AS totalAmount,
        w.version_no AS versionNo,
        w.point_id as pointId,
        w.wishlist_status as wishlistStatus
        FROM zxjp_store_wishlist w
        LEFT JOIN zxjp_store_type st ON st.id = w.store_type_id
        <if test="keyword!=null and keyword !=''">
            left join xfsg_line_info b on w.line_id = b.id
        </if>
        WHERE  w.deleted = 0
        <if test="storeTypeId != null">
            AND w.store_type_id = #{storeTypeId}
        </if>
        <if test="wishlistStatus != null">
            AND w.wishlist_status = #{wishlistStatus}
        </if>
        <if test="keyword!=null and keyword !=''">
            and (b.username like concat("%", #{keyword}, "%") or b.mobile like concat("%", #{keyword}, "%"))
        </if>
        ORDER BY w.create_time DESC
    </select>

    <update id="updateByLineId">
        UPDATE zxjp_store_wishlist
        SET wishlist_status = 5,
            update_time = NOW()
        WHERE wishlist_status = 0
          and line_id = #{lineId}
          AND deleted = 0
    </update>

    <update id="unbindPoint">
        UPDATE zxjp_store_wishlist
        SET wishlist_status = 5,
            point_id = null,
            update_time = NOW()
        WHERE id = #{wishlistId}
    </update>



</mapper>
