<?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'),total_num,0)) 'total',
        sum(if(order_status not in ('NOPAY','CLOSE','OVER'),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'),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'),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>
        </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')

        <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>
    </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>

    <select id="countTotalSettleExpense" resultType="com.gree.mall.manager.bean.settle.DataBoardBean">
        SELECT
        SUM(a.settle_order_count) AS waitGatherCount,
        SUM(a.settle_amount) AS waitGatherAmount
        FROM
        settle_expense a
        WHERE
        a.company_wechat_id = #{companyWechatId}
        AND a.status IN ('NO', 'NOT')
        <if test="websitId != null and websitId != ''">
            AND a.websit_id = #{websitId}
        </if>
    </select>

    <select id="countTotalSettleSend" resultType="com.gree.mall.manager.bean.settle.DataBoardBean">
        SELECT
            SUM(a.wait_by_count) AS waitSendByCount,
            SUM(a.wait_amount) AS waitSendAmount
        FROM
            settle_month_wages a
        WHERE
            a.company_wechat_id = #{companyWechatId}
          AND a.status IN ('NOT', 'PART')
    </select>

    <select id="countTotalSettleExpenseItem" resultType="com.gree.mall.manager.bean.settle.DataBoardBean">
        SELECT
        SUM(IF(b.category_type = 'INSTALL_HOME', 1, 0)) AS waitGatherInstallHomeCount,
        SUM(IF(b.category_type = 'INSTALL_HOME', b.worker_settle_amount1 + b.worker_settle_amount2, 0)) AS waitGatherInstallHomeAmount,
        SUM(IF(b.category_type = 'INSTALL_OTHER', 1, 0)) AS waitGatherInstallCount,
        SUM(IF(b.category_type = 'INSTALL_OTHER', b.worker_settle_amount1 + IFNULL(b.worker_settle_amount2, 0), 0)) AS waitGatherInstallAmount,
        SUM(IF(b.category_type = 'REPAIR', 1, 0)) AS waitGatherRepairCount,
        SUM(IF(b.category_type = 'REPAIR', b.worker_settle_amount1 + IFNULL(b.worker_settle_amount2, 0), 0)) AS waitGatherRepairAmount,
        SUM(IF(b.category_type = 'OTHER', 1, 0)) AS waitGatherOtherCount,
        SUM(IF(b.category_type = 'OTHER', b.worker_settle_amount1 + IFNULL(b.worker_settle_amount2, 0), 0)) AS waitGatherOtherAmount
        FROM
        settle_expense a JOIN settle_expense_item b ON a.id = b.settle_expense_id
        WHERE
        a.company_wechat_id = #{companyWechatId}
        AND a.status IN ('NO', 'NOT')
        <if test="websitId != null and websitId != ''">
            AND a.websit_id = #{websitId}
        </if>
    </select>

    <select id="countTotalSettleSendItemAmount" resultType="com.gree.mall.manager.bean.settle.DataBoardBean">
        SELECT
            SUM(a.install_home_amount) AS waitSendInstallHomeAmount,
            SUM(a.install_other_amount) AS waitSendInstallAmount,
            SUM(a.repair_amount) AS waitSendRepairAmount,
            SUM(a.other_amount) AS waitSendOtherAmount
        FROM
            settle_month_wages_worker a
        WHERE
            a.company_wechat_id = #{companyWechatId}
          AND a.status = 'NOT'
    </select>

    <select id="countTotalSettleSendItemCount" resultType="com.gree.mall.manager.bean.settle.DataBoardBean">
        SELECT
        <if test="categoryName != null and categoryName == 'installHome'">
            COUNT(DISTINCT a.worker_id) AS waitSendByInstallHomeCount
        </if>
        <if test="categoryName != null and categoryName == 'installOther'">
            COUNT(DISTINCT a.worker_id) AS waitSendByInstallCount
        </if>
        <if test="categoryName != null and categoryName == 'repair'">
            COUNT(DISTINCT a.worker_id) AS waitSendByRepairCount
        </if>
        <if test="categoryName != null and categoryName == 'other'">
            COUNT(DISTINCT a.worker_id) AS waitSendByOtherCount
        </if>
        FROM
        settle_month_wages_worker a
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.status = 'NOT'
            <if test="categoryName != null and categoryName == 'installHome'">
                AND a.install_home_amount <![CDATA[ <> ]]> 0
            </if>
            <if test="categoryName != null and categoryName == 'installOther'">
                AND a.install_other_amount <![CDATA[ <> ]]> 0
            </if>
            <if test="categoryName != null and categoryName == 'repair'">
                AND a.repair_amount <![CDATA[ <> ]]> 0
            </if>
            <if test="categoryName != null and categoryName == 'other'">
                AND a.other_amount <![CDATA[ <> ]]> 0
            </if>
    </select>
    <select id="monthSendData" resultType="com.gree.mall.manager.bean.settle.MonthSendWorkerBean">
        SELECT
            a.worker_id,
            a.settle_amount,
            b.send_date
        FROM
            settle_month_wages_worker a
                JOIN settle_month_wages_detail b ON a.settle_month_wages_detail_id = b.id
        WHERE
            a.company_wechat_id = #{companyWechatId}
          AND a.`status` = 'END'
          AND b.send_date BETWEEN #{startMonth} AND #{endMonth}
    </select>








    <select id="countEsInfo" resultType="com.gree.mall.manager.bean.count.EsCountBean">
        select
            sum(if(`status` not in ('WAIT','CANCEL','REFUND','TIMEOUT'),num,0)) 'total',
            sum(if(`status` not in ('WAIT','CANCEL','COMPLETE','TIMEOUT'),num,0)) 'sold',
            sum(if(`status` ='WAIT_SEND',1,0)) 'dfh',
            sum(if(`status` in ('REFUND','TIMEOUT'),1,0)) 'refund',
            sum(if(create_time between date(now()) and now() and `status` not in ('WAIT','CANCEL','TIMEOUT','REFUND'),1,0)) 'todayOrder',
            sum(if(create_time between date_sub(date(now()), interval 1 day) and date(now())
            and `status` not in ('WAIT','CANCEL','REFUND','TIMEOUT'),1,0)) 'yestodayOrder',
            sum(if(create_time between date(now()) and now() and `status` not in ('WAIT','CANCEL','REFUND','TIMEOUT'),total_amount,0)) 'todayProfit',
            sum(if(create_time between date_sub(date(now()), interval 1 day) and date(now())
            and `status` not in ('WAIT','CANCEL','REFUND','TIMEOUT'),total_amount,0)) 'yestodayProfit',
            if(count(if(create_time between date(now()) and now(),1,null))=0,0,
            round(
            sum(if(`status` not in ('NOPAY','CANCEL','TIMEOUT') 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 es_order_info
        where 1=1
        <if test="companyWechatId != null and companyWechatId != ''">
            and company_wechat_id = #{companyWechatId}
        </if>
    </select>

    <select id="countEsGoodsRank" resultType="com.gree.mall.manager.bean.count.EsGoodsRankBean">
        select
            es_order_id,
            es_goods_name,
            num,
            status
        from es_order_info
        where status not in ('WAIT','CANCEL','REFUND','TIMEOUT')
        and create_time between #{startTime} and #{endTime}
        <if test="companyWechatId != null and companyWechatId != ''">
            and company_wechat_id = #{companyWechatId}
        </if>

        order by 3 desc
    </select>


    <select id="countEsSaleOrder" resultType="com.gree.mall.manager.bean.count.CountOrderBean">
        select
            count(DISTINCT a.es_order_id) orderNum,
            sum(a.total_amount) amount,
            round(sum(a.total_amount)/if(count(DISTINCT a.wechat_user_id)=0,1,count(DISTINCT a.wechat_user_id)),2) avgAmount
        from es_order_info a
        where a.status not in ('WAIT','CANCEL','REFUND','TIMEOUT')
         and a.create_time between #{startTime} and #{endTime}
    </select>

</mapper>