<?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.ruoyi.web.modules.home.mapper.ShopAppMapper">

    <select id="selectProfileByShopId" resultType="com.ruoyi.web.modules.home.dto.ShopProfileRowDTO">
        select s.shop_id as shopId,
               s.shop_name as shopName,
               s.shop_avatar as shopAvatar,
               s.shop_desc as shopDesc,
               s.shop_status as shopStatus,
               s.shop_phone as shopPhone,
               st.rating as rating,
               ifnull(st.fans_count, 0) as fansCount
        from biz_shop s
        left join biz_shop_stats st on s.shop_id = st.shop_id
        where s.shop_id = #{shopId}
          and s.del_flag = '0'
    </select>

    <select id="selectShopLevel2Tabs" resultType="com.ruoyi.web.modules.category.vo.CategoryVisibleChildVO">
        select category_id as categoryId,
               category_name as categoryName,
               category_pic as categoryPic,
               hot_flag as hotFlag,
               sort_no as sortNo
        from biz_goods_category
        where shop_id = #{shopId}
          and category_level = '2'
          and parent_id = #{level1Id}
          and show_flag = '1'
          and del_flag = '0'
        order by sort_no asc, create_time asc
    </select>

    <select id="selectShopGoods" resultType="com.ruoyi.web.modules.home.vo.HomeHotGoodsVO">
        select g.goods_id as goodsId,
               g.goods_sn as goodsSn,
               g.goods_name as goodsName,
               g.main_pic as mainPic,
               g.sale_price as salePrice,
               s.shop_id as shopId,
               s.shop_name as shopName
        from biz_goods g
        inner join biz_shop s on g.shop_id = s.shop_id and s.del_flag = '0'
        where g.shop_id = #{shopId}
          and g.goods_status = '2'
          and g.del_flag = '0'
        <if test="shopCategoryId != null">
          and g.shop_category_id = #{shopCategoryId}
        </if>
        <if test="keyword != null and keyword != ''">
          and g.goods_name like concat('%', #{keyword}, '%')
        </if>
        order by g.sales_count desc, g.goods_sn asc
    </select>

    <select id="countFollow" resultType="int">
        select count(1)
        from biz_member_shop_follow
        where member_id = #{memberId}
          and shop_id = #{shopId}
    </select>

    <insert id="insertFollow">
        insert into biz_member_shop_follow (member_id, shop_id, create_time)
        values (#{memberId}, #{shopId}, now())
    </insert>

    <delete id="deleteFollow">
        delete from biz_member_shop_follow
        where member_id = #{memberId}
          and shop_id = #{shopId}
    </delete>

    <insert id="insertShopStats">
        insert into biz_shop_stats (shop_id, rating, fans_count, update_time)
        values (#{shopId}, null, 1, now())
    </insert>

    <update id="incrementFansCount">
        update biz_shop_stats
        set fans_count = fans_count + 1,
            update_time = now()
        where shop_id = #{shopId}
    </update>

    <update id="decrementFansCount">
        update biz_shop_stats
        set fans_count = case when fans_count > 0 then fans_count - 1 else 0 end,
            update_time = now()
        where shop_id = #{shopId}
    </update>

</mapper>
