<?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.CountMapper">

    <select id="countInfo" resultType="com.gree.mall.manager.bean.count.CountBean">
        select
            sum(if(order_status not in ('NOPAY','CLOSE','TIMEOUT'),total_num,0)) 'total',
            sum(if(order_status not in ('NOPAY','CLOSE','OVER','TIMEOUT'),total_num,0)) 'sold',
            sum(if(order_status ='DFH',1,0)) 'dfh',
            (select count(1) from order_refund where order_status not in ('OVER','CLOSE')
                <if test="companyWechatIds != null and companyWechatIds.size > 0">
                    and company_wechat_id in
                    <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            ) 'refund',
            sum(if(create_time between date(now()) and now() and order_status !='CLOSE',1,0)) 'todayOrder',
            sum(if(create_time between date_sub(date(now()), interval 1 day) and date(now())  and order_status !='CLOSE',1,0)) 'yestodayOrder',
            sum(if(create_time between date(now()) and now() and order_status not in ('NOPAY','CLOSE','TIMEOUT'),pay_amount,0)) 'todayProfit',
            sum(if(create_time between date_sub(date(now()), interval 1 day) and date(now()) and order_status not in ('NOPAY','CLOSE','TIMEOUT'),pay_amount,0)) 'yestodayProfit',
						if(count(if(create_time between date(now()) and now(),1,null))=0,0,
							round(
									sum(if(order_status not in ('NOPAY','CLOSE') and create_time between date(now()) and now(),1,0))
										/count(if(create_time between date(now()) and now(),1,null))*100
								,2)
						)	'orderTax'
        from order_info
        <where>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and company_wechat_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
                and websit_id in
                <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="countSaleOrder" resultType="com.gree.mall.manager.bean.count.CountOrderBean">
        select
            count(DISTINCT a.order_id) orderNum,
            sum(b.pay_amount) amount,
            round(sum(b.pay_amount)/if(count(DISTINCT a.user_id)=0,1,count(DISTINCT a.user_id)),2) avgAmount
        from order_info a
        join order_detail b on a.order_id=b.order_id
        where a.order_status not in ('NOPAY','CLOSE')
        and a.pay_time between #{startTime} and #{endTime}
        <if test="workOrderType != null and workOrderType !=''">
          and b.work_order_type=#{workOrderType}
        </if>
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and a.company_wechat_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="websitIds != null and websitIds.size > 0">
            and a.websit_id in
            <foreach item="item" index="index" collection="websitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>


    <select id="countGoodsRank" resultType="com.gree.mall.manager.bean.count.GoodsRankBean">
      select * from (
        select count(1) num,b.goods_id,sum(b.pay_amount) pay_amount,b.goods_name from order_info a join order_detail b on a.order_id = b.order_id
        where a.create_time BETWEEN #{startTime} and #{endTime} and a.order_status not in ('NOPAY','CLOSE','TIMEOUT')

        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and a.company_wechat_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="websitIds != null and websitIds.size > 0">
            and a.websit_id in
            <foreach item="item" index="index" collection="websitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        group by b.goods_id
        order by count(1) desc
        ) t limit 7
    </select>

    <select id="countVisit" resultType="java.lang.Integer">
        select count(DISTINCT user_id) total from user_visit where visit_time BETWEEN #{startTime} and #{endTime}
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and company_wechat_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="adminWebsitIds != null and adminWebsitIds.size > 0">
            and websit_id in
            <foreach item="item" index="index" collection="adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="countWorkAmount" resultType="java.util.Map">
        select
            a.worker_phone,
            sum(a.pay_amount) - sum(b.refund_amount) 'total_amount'
        from order_info a
        join order_detail b on a.order_id = b.order_id
        join goods c on c.goods_id = b.goods_id
        where a.order_status in ('DFH','YFH','OVER')
        and c.category_id not in (
            select category_id from goods_category where name like '%清洗%'
        )
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and a.company_wechat_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="websitIds != null and websitIds.size > 0">
            and a.websit_id in
            <foreach item="item" index="index" collection="websitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and pay_time between #{startTime} and #{endTime}
        group by a.worker_phone
    </select>

    <select id="countWebsitAmount" resultType="java.util.Map">
        select
            sum(pay_amount) 'amount',
            sum(total_num) 'num'
        from order_info where order_status in ('DFH','YFH','OVER')
        and pay_time between #{startTime} and #{endTime}
        <if test="websitNumbers != null and websitNumbers.size > 0">
            and websit_number in
            <foreach item="item" index="index" collection="websitNumbers" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="queryExchangeCodeCountList" resultType="com.gree.mall.manager.bean.exchange.GiftExchangeCodeBean">
        select
            count(1) 'num',
            min(bind_time) 'first_time',
            sum(amount) 'total_amount',
            sum(if(status=1,1,0)) 'use_num' ,
            a.*
        from gift_exchange_code a
        where type='SUB' and user_id !=''
        <if test="keyword != null and keyword !=''">
            and (a.nick_name like concat('%',#{keyword},'%') or a.user_phone like concat('%',#{keyword},'%'))
        </if>
        <if test="adminWebsitId != null and adminWebsitId !=''">
            and a.admin_websit_id = #{adminWebsitId}
        </if>
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and a.company_wechat_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by user_id
    </select>

</mapper>