<?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.sqx.modules.goods.dao.GoodsDao">

    <!--<select id="selectGoodsList" resultType="com.sqx.modules.goods.entity.Goods">
        select * from goods where 1 = 1
        <if test="classify!=null and classify!=''">
            and classify = #{classify}
        </if>
        <if test="goodsName!=null and goodsName!=''">
            and goods_name = #{goodsName}
        </if>
        <if test="putawayFlag!=null and putawayFlag!=''">
            and putaway_flag = #{putawayFlag}
        </if>
        order by create_time desc
    </select>-->

    <select id="selectGoodsClassifyList" resultType="com.sqx.modules.goods.entity.Goods">
        select DISTINCT g.classify_id
        from goods g
                 left join goods_shop_relevancy gsr on gsr.goods_id = g.goods_id
        where 1 = 1
          and gsr.shop_id = #{shopId}
        order by g.classify_id asc
    </select>

    <select id="selectClassify" resultType="com.sqx.modules.goods.entity.GoodsClassify">
        select *
        from goods_classify
        where classify_id = #{classifyId}
    </select>

    <select id="selectShortShop" resultType="com.sqx.modules.goods.entity.GoodsShop">
        select shop_id, shop_name, shop_lat, shop_lng, open_time, close_time, detailed_address, shop_banner,
        (st_distance (point (shop_lat, shop_lng), point(#{lat},#{lng}) ) *111195) AS distancess
        from goods_shop where 1 = 1
        and status=1 and putaway_flag=0 and banned_flag=0
        <if test="shopName!=null and shopName!=''">
            and shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="shopId!=null">
            and shop_id = #{shopId}
        </if>
        order by is_recommend desc,distancess asc
    </select>

    <select id="selectAllClassify" resultType="com.sqx.modules.goods.entity.GoodsClassify">
        select gc.*, gs.shop_name as shopName from goods_classify gc left join goods_shop gs on gc.shop_id = gs.shop_id
        where 1 = 1
        <if test="shopName!=null and shopName!=''">
            and gs.shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="shopId!=null">
            and gc.shop_id = #{shopId}
        </if>
        order by gc.sort asc
    </select>

    <select id="selectAllClassifyList" resultType="com.sqx.modules.goods.entity.GoodsClassify">
        select gc.*, gs.shop_name as shopName from goods_classify gc left join goods_shop gs on gc.shop_id = gs.shop_id
        where 1 = 1
        <if test="shopName!=null and shopName!=''">
            and gs.shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="shopId!=null">
            and gc.shop_id = #{shopId}
        </if>
        order by gc.sort asc
    </select>

    <insert id="shopAddGoods">
        insert into goods_shop_relevancy (shop_id, goods_id) values
        <foreach collection="goodsIdList" item="item" open="(" close=")" separator=",">
            (#{shopId}, #{item})
        </foreach>
    </insert>

    <select id="selectGoodsByClassify" resultType="com.sqx.modules.goods.entity.Goods">
        select g.*, gsr.sales as sales, gsr.inventory as inventory, ga.activityList
        from goods_shop_relevancy gsr
                 left join goods g on gsr.goods_id = g.goods_id
                 left join (
                    select
                           tmp.goods_id, group_concat(a.title) as activityList
                    from
                        (
                            select t.activity_shop_id, substring_index(substring_index(t.goods_ids, ',', n.n), ',', -1) goods_id
                            from
                                    (select 1 as n union select 2 union select 3 union select 4) n
                                        inner join activity_goods t on char_length(t.goods_ids)-char_length(replace(t.goods_ids, ',', ''))>=n.n-1
                            order by n.n
                        ) tmp
                            left join activity_shop ach on ach.id = tmp.activity_shop_id
                            left join activity a on ach.activity_id = a.id
                    where a.del_flag = '0'
                    group by tmp.goods_id
                ) ga on ga.goods_id = g.goods_id
        where gsr.shop_id = #{shopId}
          and g.classify_id = #{classifyId}
          and g.status = 0
        order by LPAD(g.sort, 10, '0') ASC
    </select>

    <select id="selectGoodsBySales" resultType="com.sqx.modules.goods.entity.Goods">
        select *
        from goods
        where shop_id = #{shopId}
          and status = 0
        order by sort asc, sales_volume desc limit 3
    </select>

    <select id="selectGoodsBySalesAndGoodsName" resultType="com.sqx.modules.goods.entity.Goods">
        select * from goods where shop_id = #{shopId}
        <if test="goodsName!=null and goodsName!=''">
            and goods_name like concat('%',#{goodsName},'%')
        </if>
        and status=0 order by sort asc,sales_volume desc limit 3
    </select>



    <select id="selectGoodsBySalesAndGoodsNameAndShopIds" resultType="com.sqx.modules.goods.entity.Goods">
        SELECT
            x.*
        FROM (
            SELECT
                g.*,
                ROW_NUMBER() OVER (PARTITION BY g.shop_id ORDER BY g.sort asc, g.sales_volume desc) AS `rank`
            FROM
                goods g
            WHERE g.status=0
                <if test="goodsName!=null and goodsName!=''">
                    and g.goods_name like concat('%',#{goodsName},'%')
                </if>
                <if test="shopIdList.size()>0">
                    and g.shop_id in
                    <foreach collection="shopIdList" item="id" index="index" open="(" close=")" separator=",">
                        #{id}
                    </foreach>
                </if>
        ) x
        WHERE x.rank &lt;= 3
    </select>

    <select id="selectGoodsById" resultType="com.sqx.modules.goods.entity.Goods">
        select g.*, gsr.inventory as inventory, gsr.sales
        from goods g
                 left join goods_shop_relevancy gsr on g.goods_id = gsr.goods_id
        where g.goods_id = #{goodsId}
    </select>

    <select id="getByAllGoodsIdByShopId" resultType="java.lang.Long">
        select goods_id from goods where shop_id = #{shopId}
    </select>
    <select id="goodsCoverList" resultType="com.sqx.modules.goods.entity.Goods">
        SELECT
            ( @i := @i + 1 ) AS id,
            goods_name,
            goods_cover
        FROM
            goods,
            ( SELECT @i := #{total} ) AS itable
        WHERE
            goods_name LIKE concat('%',#{name},'%')
        GROUP BY goods_cover
    </select>
    <select id="goodsPictureList" resultType="com.sqx.modules.goods.entity.Goods">
        SELECT a.* FROM (
        SELECT
            ( @i := @i + 1 ) AS id,
            goods_name,
            SUBSTRING_INDEX( SUBSTRING_INDEX( goods_picture, ',', n ), ',', - 1 ) AS goods_picture
        FROM
            goods,
            ( SELECT @rownum := @rownum + 1 AS n FROM ( SELECT @rownum := 0 ) r, goods ) x,
            ( SELECT @i := #{total} ) AS itable
        WHERE
            goods_name LIKE concat('%',#{name},'%')
            AND ( LENGTH( goods_picture ) - LENGTH( REPLACE ( goods_picture, ',', '' ) ) + 1 ) >= n
                        ) a
        GROUP BY a.goods_picture
    </select>
    <select id="goodsParticularsPictureList" resultType="com.sqx.modules.goods.entity.Goods">
        SELECT a.* FROM (
        SELECT
            ( @i := @i + 1 ) AS id,
            goods_name,
            SUBSTRING_INDEX( SUBSTRING_INDEX( goods_particulars_picture, ',', n ), ',', - 1 ) AS goods_particulars_picture
        FROM
            goods,
            ( SELECT @rownum := @rownum + 1 AS n FROM ( SELECT @rownum := 0 ) r, goods ) x,
            ( SELECT @i := #{total} ) AS itable
        WHERE
            goods_name LIKE concat('%',#{name},'%')
            AND ( LENGTH( goods_particulars_picture ) - LENGTH( REPLACE ( goods_particulars_picture, ',', '' ) ) + 1 ) >= n
                        ) a
        GROUP BY a.goods_particulars_picture
    </select>

    <select id="selectGoodsVoByClassify" resultType="com.sqx.modules.goods.vo.GoodsListVo">
        select g.goods_id as goodsId,
               g.goods_cover as goodsCover,
               g.goods_name as goodsName,
               g.goods_describe as goodsDescribe,
               g.goods_money as goodsMoney,
               gsr.sales as sales
        from goods_shop_relevancy gsr
                 left join goods g on gsr.goods_id = g.goods_id
        where gsr.shop_id = #{shopId}
          and g.classify_id = #{classifyId}
          and g.status = 0
        order by LPAD(g.sort, 10, '0') ASC
    </select>
    <select id="selectSupermarketGoodsList" resultType="com.sqx.modules.goods.entity.Goods">
        select g.*, gsr.sales as sales, gsr.inventory as inventory, ga.activityList
        from goods_shop_relevancy gsr
                 left join goods g on gsr.goods_id = g.goods_id
                 left join (
            select
                tmp.goods_id, group_concat(a.title) as activityList
            from
                (
                    select t.activity_shop_id, substring_index(substring_index(t.goods_ids, ',', n.n), ',', -1) goods_id
                    from
                            (select 1 as n union select 2 union select 3 union select 4) n
                                inner join activity_goods t on char_length(t.goods_ids)-char_length(replace(t.goods_ids, ',', ''))>=n.n-1
                    order by n.n
                ) tmp
                    left join activity_shop ach on ach.id = tmp.activity_shop_id
                    left join activity a on ach.activity_id = a.id
            where a.del_flag = '0'
            group by tmp.goods_id
        ) ga on ga.goods_id = g.goods_id
        where gsr.shop_id = #{shopId}
          and g.classify_id = #{classifyId}
          and g.status = 0
        order by LPAD(g.sort, 10, '0') ASC
    </select>

</mapper>