<?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.gree.mall.manager.commonmapper.CustomGoodsMapper">

    <select id="pageList" resultType="com.gree.mall.manager.bean.goods.GoodsSpecBean">
        SELECT
        t1.*,
        t1.goods_name AS goodsName,
        ifnull(t2.share_amount,0) AS shareAmount,
        ifnull(t2.share_percent,0) AS sharePercent,
        ifnull(t2.inner_share_amount,0) AS innerShareAmount,
        ifnull(t2.inner_share_percent,0) AS innerSharePercent,
        <!--sum(if(t2.del=0 and t2.sold_num is NOT NULL,t2.sold_num,0)) AS soldNum,-->
        sum(if(t2.del=0 and t2.stock_num is NOT NULL,t2.stock_num,0)) AS stockNum,
        t3.`name` AS categoryName,
        t2.bar_code
        FROM
        goods t1
        left join goods_spec t2 on t1.goods_id = t2.goods_id         and t2.del = 0
        left join goods_category t3 on t3.category_id = t1.category_id
        WHERE
        t1.del = 0
        <if test=' companyWechatId != null and companyWechatId.size > 0' >
            AND  t1.goods_id in (select b.goods_id from goods_company b where b.company_id in
            <foreach collection="companyWechatId" open="(" close=")" item="companyWechatId" separator=",">
                #{companyWechatId}
            </foreach>
            )
        </if>
        <if test='goodsTypes != null and goodsTypes.size > 0' >
            AND t1.goods_type  in
            <foreach collection="goodsTypes" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="flag != null">
            <choose>
                <when test="flag == 'S'.toString()">AND t1.status = 1</when>
                <when test="flag == 'W'.toString()">AND t1.status = 0</when>
                <when test="flag == 'A'.toString()">AND t2.flag = 1</when>
                <otherwise/>
            </choose>
        </if>
        <if test="keyword != null and keyword != ''">
            AND
            (t1.goods_id like CONCAT('%', #{keyword},'%') OR t1.goods_name like CONCAT('%',#{keyword},'%') OR t3.`name`
            like CONCAT('%',#{keyword},'%') OR CONCAT(t1.goods_name, '(', t2.`name`,')') like
            CONCAT('%',#{keyword},'%'))
        </if>
        <if test="startPrice != null and endPrice != null">
            AND
            t2.price BETWEEN #{startPrice} AND #{endPrice}
        </if>
        <if test="startShare != null and endShare != null">
            AND
            t2.share_amount BETWEEN #{startShare} AND #{endShare}
        </if>
        <if test="categoryId != null and categoryId.size() != 0">
            AND
            t3.category_id in
            <foreach item="item" index="index" collection="categoryId" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="status != null">
            AND
            t1.status = #{status}
        </if>
        group by t1.goods_id
        <if test="flag == 'O'.toString()">
            having sum(t2.stock_num) = 0
        </if>
        <choose>
            <when test="sortStr == null">
                ORDER BY t1.sort_num desc,t1.create_time desc
            </when>
            <otherwise>
                ORDER BY t1.sort_num desc,${sortStr},t1.create_time desc
            </otherwise>
        </choose>
    </select>

    <select id="list" resultType="com.gree.mall.manager.bean.goods.GoodsSpecBean">
        SELECT t1.*,
               t1.goods_name                                                     AS goodsName,
               ifnull(t2.share_amount, 0)                                        AS shareAmount,
               ifnull(t2.share_percent, 0)                                       AS sharePercent,
               sum(IF(t2.del = 0 AND t2.sold_num IS NOT NULL, t2.sold_num, 0))   AS soldNum,
               sum(IF(t2.del = 0 AND t2.stock_num IS NOT NULL, t2.stock_num, 0)) AS stockNum
        FROM goods_template gt
                 LEFT JOIN goods t1 ON gt.goods_id = t1.goods_id
                 LEFT JOIN goods_spec t2 ON t1.goods_id = t2.goods_id
        WHERE gt.template_id = #{commonTemplateId}
          and t1.del = 0
        GROUP BY t1.goods_id
        ORDER BY t1.sort_num DESC
    </select>


    <select id="countByType" resultType="com.gree.mall.manager.bean.goods.GoodsTypeCount">
        select count(1) 'total', sum(if(status = true, 1, 0)) 'csz', sum(if(status = false, 1, 0)) 'ckz'
        from goods a
        where del = false
        <if test=' companyWechatIds != null and companyWechatIds.size > 0' >
            AND  a.goods_id in (select b.goods_id from goods_company b where b.company_id in
            <foreach collection="companyWechatIds" open="(" close=")" item="companyWechatId" separator=",">
                #{companyWechatId}
            </foreach>
            )
        </if>

    </select>

    <select id="ysq" resultType="java.lang.Integer">
        SELECT
        count(*) total
        FROM
        (
        SELECT
        sum( c.stock_num ),
        c.goods_id
        FROM
        goods_spec c
        JOIN goods t1
        JOIN goods_company b ON t1.goods_id = b.goods_id
        WHERE
        c.del = FALSE
        <if test=' companyWechatIds != null and companyWechatIds.size > 0' >
            AND b.company_id in
            <foreach collection="companyWechatIds" open="(" close=")" item="companyWechatId" separator=",">
                #{companyWechatId}
            </foreach>
        </if>
        GROUP BY
        goods_id
        HAVING
        sum( stock_num ) = 0
        ) t
    </select>

    <select id="countByFlag" resultType="java.lang.Integer">
        select count(DISTINCT t1.goods_id)
        FROM goods t1 join goods_company b on t1.goods_id = b.goods_id
        left join goods_spec t2 on t1.goods_id = t2.goods_id
        WHERE t1.del = 0
          AND t2.flag = 1
          and t2.del = 0
        <if test=' companyWechatIds != null and companyWechatIds.size > 0' >
            AND b.company_id in
            <foreach collection="companyWechatIds" open="(" close=")" item="companyWechatId" separator=",">
                #{companyWechatId}
            </foreach>
        </if>
    </select>


    <select id="queryGoodsCategoryByGoodsId" resultType="com.gree.mall.manager.plus.entity.GoodsCategory">
        select a.* from goods_category a
            join goods b on a.category_id = b.category_id
            where b.goods_id=#{goodsId}
    </select>

    <update id="syncGoodsSpecInnerShareMsg" parameterType="java.lang.String">
        update goods_spec
        set inner_share_amount = share_amount , inner_share_percent = share_percent
        where inner_share_amount is null and inner_share_amount is null and company_id = #{companyWechatId}
    </update>



    <select id="queryGoodsPackageList" resultType="com.gree.mall.manager.bean.goods.GoodsPackageList">
          select
            a.goods_id,
            a.img_url,
            a.goods_name,
            a.status,
            (select count(DISTINCT goods_id) from goods_package_pop where goods_package_id= a.goods_id) 'goodsNums',
            a.package_min_amount,
            a.package_min_share_amount,
            use_coupon,
            count(DISTINCT c.order_id) 'orderNums',
            sum(c.pay_amount) 'orderPayAmount',
            a.package_user_type,
            a.company_name

            from goods a
            left join order_info c on c.promotion_package_goods_id=a.goods_id and c.order_status in ('DFH','YFH','OVER')
            where a.goods_type='PACKAGE'
        and a.del = 0
        <if test='companyWechatIds != null and companyWechatIds.size > 0' >
            AND a.company_id  in
            <foreach collection="companyWechatIds" open="(" close=")" item="companyWechatId" separator=",">
                #{companyWechatId}
            </foreach>
        </if>
        <if test="keyword != null and keyword != ''">
            AND(a.goods_id like CONCAT('%', #{keyword},'%') OR a.goods_name like CONCAT('%',#{keyword},'%'))
        </if>
        <if test="startPrice != null and endPrice != null">
            AND
            a.package_min_amount BETWEEN #{startPrice} AND #{endPrice}
        </if>
        <if test="startShare != null and endShare != null">
            AND
            a.package_min_share_amount BETWEEN #{startShare} AND #{endShare}
        </if>
        <if test="categoryId != null and categoryId.size() != 0">
            AND
            a.category_id in
            <foreach item="item" index="index" collection="categoryId" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="status != null">
            AND
            a.status = #{status}
        </if>
        group by a.goods_id
        <choose>
            <when test="sortStr == null">
                ORDER BY a.sort_num desc,a.create_time desc
            </when>
            <otherwise>
                ORDER BY a.sort_num desc,${sortStr},a.create_time desc
            </otherwise>
        </choose>

    </select>
    <select id="goodsList" resultType="com.gree.mall.manager.bean.goods.GoodsVO">
        SELECT
        ${ex.selected}
        FROM goods a
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>


</mapper>