<?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.CustomSalesReportMapper">
    <!--网点统计-->
    <select id="queryData" resultType="com.gree.mall.manager.plus.entity.SaleReport">
          	select
              a.*,
              a.saleOnlineAmount + ifnull(sum(c.sale_amount),0) 'saleTotalAmount',
              round(ifnull(sum(sale_amount),0),2) 'saleOfflineTotalAmount',
              round(sum(if(sale_type='RETAIL',sale_amount,0)),2) 'saleOfflineSaleAmount',
              round(sum(if(sale_type='PROJECT',sale_amount,0)),2) 'saleOfflineProAmount',
             if(a.saleOnlineAmount + ifnull(sum(c.sale_amount),0) = 0,0,
              round(a.saleOnlineAmount / (a.saleOnlineAmount + ifnull(sum(c.sale_amount),0)) * 100,2)
             ) 'saleOnlinePercent',
             if(a.saleOnlineAmount + ifnull(sum(c.sale_amount),0) = 0,0,
              round((sum(if(sale_type='RETAIL',sale_amount,0))) / (a.saleOnlineAmount + ifnull(sum(c.sale_amount),0)) * 100,2)
             ) 'saleOfflineSalePercent',
             if(a.saleOnlineAmount + ifnull(sum(c.sale_amount),0) = 0,0,
              round((sum(if(sale_type='PROJECT',sale_amount,0))) / (a.saleOnlineAmount + ifnull(sum(c.sale_amount),0)) * 100,2)
             ) 'saleOfflineProPercent'
             from (
                        select
                             x.corp_id,
                             x.zone_id,
                             x.zone_name ,
                             x.jind_sale_name,
                             x.websit_number ,
                             x.name 'websit_name',
                             count(DISTINCT a.worker_id) 'bill_num',
                             ifnull(sum(b.pay_amount)-sum(b.refund_amount)+ sum(b.freight),0) 'saleOnlineAmount'
                         from admin_websit x
                         left join (select websit_number,worker_id,order_id from order_info
                                    where order_status in ('DFH','YFH','OVER','REFUND')
                                    and pay_time between #{startTime} and  DATE_ADD(#{endTime} , INTERVAL 1 DAY)
                                    ) a
                         on x.websit_number = a.websit_number
                         left join order_detail b on a.order_id = b.order_id and b.work_order_type='INSTALL'
                         where x.websit_number!='' and x.zone_id != '' and x.zone_name is not null
                         <if test="websitNumbers != null and websitNumbers.size > 0">
                             and x.websit_number in
                             <foreach item="item" index="index" collection="websitNumbers" open="(" separator="," close=")">
                                 #{item}
                             </foreach>
                         </if>
                        group by x.websit_number
            ) a left join
            (select * from offline_sale_record where status=1
            and create_time between #{startTime} and  DATE_ADD(#{endTime} , INTERVAL 1 DAY) ) c on c.websit_number = a.websit_number
        <where>
            <if test="corpIds != null and corpIds.size > 0">
                and a.corp_id in
                <foreach item="item" index="index" collection="corpIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
            group by a.websit_number
            order by 1,3,5 desc;
    </select>

</mapper>