<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop;

use app\admin\controller\bbfxshop\Base;
use think\Db;
use think\Cache;

class Tongji extends Base
{
    protected $initConfigFlag = false;

    public function _initialize(){
        parent::_initialize();
    }

    public function getGoodsAll(){
        $list = Db::name("bbfx_goods")->where(['uniacid'=>UNIACID])->field("id,title,thumb,mid")->select();

        __success("操作成功",['list'=>$list]);
    }

    public function goods(){
        
        $params = $this->param(['sdate/s','edate/s','goods_id/d']);
        $stime = strtotime(date("Y-m-d 00:00:00")." -29 day");
        $etime = strtotime(date("Y-m-d 23:59:59"));
        if(!empty($params['sdate'])){ 
            $stime = strtotime($params['sdate']." 00:00:00");
        }
        if(!empty($params['edate'])){
            $etime = strtotime($params['edate']." 23:59:59");
        }
         
        $goods_where = ['>',0];
        if($params['goods_id'] > 0){
            $goods_where = $params['goods_id'];
        }
        // print_r(date('Y-m-d H:i:s',$stime)."----".date('Y-m-d H:i:s',$etime));die;
        // 浏览数
        $where = ['a.uniacid'=>UNIACID,'a.type'=>0,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where];
        $browse_list = Db::name("bbfx_goods_log")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->order("a.createtime asc")->field("a.id,a.createtime")->select();
        $browse_num = count($browse_list);
        // 访问数
        $where = ['a.uniacid'=>UNIACID,'a.type'=>1,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where];
        $visitor_list = Db::name("bbfx_goods_log")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->group("a.user_id,a.utk")->order("a.createtime asc")->field("a.id,a.createtime")->select();
        $visitor_num = count($visitor_list);

        // 浏览数
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where];
        $collection_list = Db::name("bbfx_goods_collection")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->order("a.createtime asc")->field("a.id,a.createtime")->select();
        $collection_num = count($collection_list);
        
        // 下单数
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where];
        $order_goods_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->sum("a.total");
        // 支付数
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        $pay_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.total");
        // 订单总金额（包含未支付，已退款）
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where];
        $order_list = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->field("a.id,a.price,a.createtime")->order("a.createtime asc")->select();
        $order_price = 0;
        foreach ($order_list as $val){
            $order_price += $val['price'];
        }
        // 订单成交金额（已支付金额-退款金额）
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        $pay_list = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>['in',[-1,0,2]]])->field("a.id,a.price,a.createtime")->order("a.createtime asc")->select();
        $pay_price = 0;
        foreach ($pay_list as $val){
            $pay_price += $val['price'];
        }
        // 成本金额
        $cost_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.cost_price");
        // 退款金额
        $refund_list = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->field("a.id,a.price,a.createtime")->order("a.createtime asc")->select();
        $refund_price = 0;
        foreach ($refund_list as $val){
            $refund_price += $val['price'];
        }
        // 退款数量
        $refund_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->sum("a.total");
        // 支付转化率
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        $pay_user_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->group("c.user_id")->count();
        $pay_rate = 0;
        $visitor_num1 = $visitor_num;
        if($visitor_num <= $pay_user_num){
            $visitor_num1 = $pay_user_num;
        }
        if($visitor_num1 > 0){
            $pay_rate = round($pay_user_num / $visitor_num1,2) * 100;
        }
        

        $result = [
            'browse_num'=>intval($browse_num),
            'visitor_num'=>intval($visitor_num),
            'order_goods_num'=>intval($order_goods_num),
            'collection_num'=>intval($collection_num),
            'pay_num'=>intval($pay_num),
            'refund_num'=>intval($refund_num),
            'cost_price'=>round($cost_price,2),
            'order_price'=>round($order_price,2),
            'pay_price'=>round($pay_price,2),
            'pay_rate'=>round($pay_rate,2),
            'refund_price'=>round($refund_price,2),
        ];

        $list = [];
        $time = $stime;
        while(true){
            $st = $time;
            $et = strtotime(date('Y-m-d 23:59:59',$time));

            // $where = ['a.uniacid'=>UNIACID,'a.type'=>0,'a.createtime'=>['between',[$st,$et]],'b.id'=>$goods_where];
            // $browse_num = Db::name("bbfx_goods_log")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->count();
            $browse_num = 0;
            $datas = [];
            foreach ($browse_list as $key => $val){
                if($st < $val['createtime'] && $et >= $val['createtime']){
                    $browse_num++;
                }
                if($val['createtime'] > $et){
                    $datas[] = $val;
                }
            }
            $browse_list = $datas;

            // $where = ['a.uniacid'=>UNIACID,'a.type'=>1,'a.createtime'=>['between',[$st,$et]],'b.id'=>$goods_where];
            // $visitor_num = Db::name("bbfx_goods_log")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->group("a.user_id,a.utk")->count();
            $visitor_num = 0;
            $datas = [];
            foreach ($visitor_list as $key => $val){
                if($st < $val['createtime'] && $et >= $val['createtime']){
                    $visitor_num++;
                }
                if($val['createtime'] > $et){
                    $datas[] = $val;
                }
            }
            $visitor_list = $datas;
            
            // GMV、订单总金额（包含未支付，已退款）
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$st,$et]],'b.id'=>$goods_where];
            // $order_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.price");
            $order_price = 0;
            $datas = [];
            foreach ($order_list as $key => $val){
                if($st < $val['createtime'] && $et >= $val['createtime']){
                    $order_price += $val['price'];
                }
                if($val['createtime'] > $et){
                    $datas[] = $val;
                }
            }
            $order_list = $datas;
            
            // GMV（净）、支付金额（已支付金额-退款金额）
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$st,$et]],'b.id'=>$goods_where,'c.is_pay'=>1];
            // $pay_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>['in',[-1,0,2]]])->sum("a.price");
            $pay_price = 0;
            $datas = [];
            foreach ($pay_list as $key => $val){
                if($st < $val['createtime'] && $et >= $val['createtime']){
                    $pay_price += $val['price'];
                }
                if($val['createtime'] > $et){
                    $datas[] = $val;
                }
            }
            $pay_list = $datas;
            
            // 退款金额
            // $refund_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->sum("a.price");
            $refund_price = 0;
            $datas = [];
            foreach ($refund_list as $key => $val){
                if($st < $val['createtime'] && $et >= $val['createtime']){
                    $refund_price += $val['price'];
                }
                if($val['createtime'] > $et){
                    $datas[] = $val;
                }
            }
            $refund_list = $datas;
            
            $list[] = [
                'date'=>date('m-d',$time),
                'browse_num'=>intval($browse_num),
                'visitor_num'=>intval($visitor_num),
                'pay_price'=>round($pay_price,2),
                'order_price'=>round($order_price,2),
                'refund_price'=>round($refund_price,2)
            ];

            $time = strtotime(date('Y-m-d 00:00:00',$time)." +1 day");
            
            if($time > $etime){
                break;
            }
            
        }

        $result['list'] = $list;

        __success("操作成功",$result);
    }

    public function goodsrank(){
        
        $params = $this->param(['sdate/s','edate/s','type/d','page/d','limit/d','export/d']);
        $stime = strtotime(date("Y-m-d 00:00:00")." -29 day");
        $etime = strtotime(date("Y-m-d 23:59:59"));
        if(!empty($params['sdate'])){ 
            $stime = strtotime($params['sdate']." 00:00:00");
        }
        if(!empty($params['edate'])){
            $etime = strtotime($params['edate']." 23:59:59");
        }
        
        $page = max(1,$params['page']);
        $limit = max(10,$params['limit']);

        $export = intval($params['export']);
        if($export == 1){
            $page = 1;
            $limit = 999999;
        }
         
        $list = Db::name("bbfx_goods")->where(['uniacid'=>UNIACID])->field("id,mid,title,thumb")->select();

        $where = ['uniacid'=>UNIACID,'type'=>0,'createtime'=>['between',[$stime,$etime]]];
        $goods_log_list = Db::name("bbfx_goods_log")->where($where)
        ->group("goods_id")
        ->field("count(*) as total,goods_id")
        ->select();
        $goods_logs0 = array_skey($goods_log_list,"goods_id");
        $where = ['uniacid'=>UNIACID,'type'=>1,'createtime'=>['between',[$stime,$etime]]];
        $goods_log_list = Db::name("bbfx_goods_log")->where($where)->group("goods_id,user_id,utk")
        ->field("goods_id,user_id,utk")
        ->select();
        $goods_logs1 = [];
        foreach ($goods_log_list as $val){
            if(empty($goods_logs1[$val['goods_id']])){
                $goods_logs1[$val['goods_id']] = 0;
            }
            $goods_logs1[$val['goods_id']]++;
        }
        
        $where = ['o.uniacid'=>UNIACID,'o.createtime'=>['between',[$stime,$etime]]];
        $order_goods_list = Db::name("bbfx_order_goods")->alias("og")->join("bbfx_order o","og.order_id=o.id","LEFT")->where($where)->group("og.goods_id")
            ->field("
                COUNT(*) as order_goods_num,og.goods_id,og.total,o.user_id,
                SUM(og.price) as order_price,
                SUM(CASE WHEN o.is_pay = 1 AND o.refund_status IN (-1,0,2) THEN og.price ELSE 0 END) AS pay_price,
                SUM(CASE WHEN o.is_pay = 1 THEN og.upgrade_score ELSE 0 END) AS order_goods_unit
            ")
            ->select();
        $order_goods = [];
        $order_goods_users = [];
        foreach ($order_goods_list as $val){
            $order_goods[$val['goods_id']] = $val;
            if(empty($order_goods_users[$val['goods_id']])){
                $order_goods_users[$val['goods_id']] = [];
            }
        }
        $where['o.is_pay'] = 1;
        $order_goods_list = Db::name("bbfx_order_goods")->alias("og")->join("bbfx_order o","og.order_id=o.id","LEFT")->where($where)->group("og.goods_id,user_id")
            ->field("og.goods_id,o.user_id")
            ->select();
        foreach ($order_goods_list as $val){
            if(empty($order_goods_users[$val['goods_id']])){
                $order_goods_users[$val['goods_id']] = 0;
            }
            $order_goods_users[$val['goods_id']]++;
        }
        
        $where = ['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]]];
        $collection_list = Db::name("bbfx_goods_collection")->where($where)->group("goods_id")->field("count(*) as count,goods_id")->select();
        $collection_list = array_skey($collection_list,"goods_id");
        
        $where = ['o.uniacid'=>UNIACID,'o.createtime'=>['between',[$stime,$etime]],'o.is_pay'=>1];
        $order_goods_list = Db::name("bbfx_order_goods")->alias("og")->join("bbfx_order o","og.order_id=o.id","LEFT")->where($where)->group("og.goods_id,delivery_province")
            ->field("
                goods_id,delivery_province as name,
                sum(og.upgrade_score) as goods_total
            ")
            ->select();
        $goods_provinces = [];
        foreach ($order_goods_list as $val){
            if(empty($goods_provinces[$val['goods_id']])){
                $goods_provinces[$val['goods_id']] = [];
            }
            
            $goods_provinces[$val['goods_id']][] = $val;
        }
        
        foreach($list as $key => &$val){
            $val['thumb'] = cdnurl($val['thumb']);
            // 浏览数
            // $where = ['a.uniacid'=>UNIACID,'a.type'=>0,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id']];
            // $browse_num = Db::name("bbfx_goods_log")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->count();
            $browse_num = intval($goods_logs0[$val['id']]['total']);
            // 访问数
            // $where = ['a.uniacid'=>UNIACID,'a.type'=>1,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id']];
            // $visitor_num = Db::name("bbfx_goods_log")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->group("a.user_id,a.utk")->count();
            $visitor_num = intval($goods_logs1[$val['id']]);
            // 下单数
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id']];
            // $order_goods_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->sum("a.total");
            $order_goods_num = intval($order_goods[$val['id']]['order_goods_num']);
            // GMV、订单总金额（包含未支付，已退款）
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id']];
            // $order_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.price");
            $order_price = round($order_goods[$val['id']]['order_price'],2);
            // GMV（净）、支付金额（已支付金额-退款金额）
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id'],'c.is_pay'=>1];
            // $pay_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>['in',[-1,0,2]]])->sum("a.price");
            $pay_price = round($order_goods[$val['id']]['pay_price'],2);
            // 收藏数
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id']];
            // $collection_num = Db::name("bbfx_goods_collection")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->count();
            $collection_num = intval($collection_list[$val['id']]['count']);
            // 支付转化率
            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$val['id'],'c.is_pay'=>1];
            // $pay_user_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->group("c.user_id")->count();
            $pay_user_num = intval($order_goods_users[$val['id']]);
            $pay_rate = 0;
            $visitor_num1 = $visitor_num;
            if($pay_user_num > $visitor_num){
                $visitor_num1 = $pay_user_num;
            }
            if($visitor_num > 0){
                $pay_rate = round($pay_user_num / $visitor_num1,2) * 100;
            }

            // 商品单位
            /*
            $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'a.goods_id'=>$val['id'],'c.is_pay'=>1];
            $field = "a.id,a.order_id,a.upgrade_score,a.total,c.delivery_method,c.delivery_province,c.delivery_city,c.delivery_area,c.order_type";
            $order_goods_unit = 0;
            $order_goods_list = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->field($field)->select();
            $goods_province = [];
            if(!empty($order_goods_list)){
                $delivery_provinces = [];
                foreach($order_goods_list as $order_goods){
                    $order_goods_unit += $order_goods['upgrade_score'];

                    if(!empty($order_goods['delivery_province']) && $order_goods['upgrade_score'] > 0){
                        if(empty($delivery_provinces[$order_goods['delivery_province']])){
                            $delivery_provinces[$order_goods['delivery_province']] = ['goods_total'=>0];
                        }
                        $delivery_provinces[$order_goods['delivery_province']]['goods_total'] += $order_goods['upgrade_score'];
                    }
                }
                if(!empty($delivery_provinces)){
                    foreach($delivery_provinces as $k => $v){
                        $goods_province[] = ['name'=>$k,'goods_total'=>$v['goods_total']];
                    }

                    $goods_totals = array_column($goods_province,'goods_total');
                    array_multisort($goods_totals,SORT_DESC,$goods_province);
                }
            }
            */

            $goods_province = $goods_provinces[$val['id']];
            if(empty($goods_province)){
                $goods_province = [];
            }
            else{
                $goods_totals = array_column($goods_province,'goods_total');
                array_multisort($goods_totals,SORT_DESC,$goods_province);
            }
            $order_goods_unit = intval($order_goods[$val['id']]['order_goods_unit']);
            
            $val['browse_num'] = intval($browse_num);
            $val['visitor_num'] = intval($visitor_num);
            $val['order_goods_num'] = intval($order_goods_num);
            $val['order_price'] = round($order_price,2);
            $val['pay_price'] = round($pay_price,2);
            $val['pay_rate'] = round($pay_rate,2);
            $val['collection_num'] = intval($collection_num);
            $val['order_goods_unit'] = intval($order_goods_unit);
            $val['goods_province'] = $goods_province;
        }
        unset($val);
        
        if($params['type'] == 2){
            $sortName = "visitor_num";
        }
        else if($params['type'] == 3){
            $sortName = "order_goods_num";
        }
        else if($params['type'] == 4){
            $sortName = "order_price";
        }
        else if($params['type'] == 5){
            $sortName = "pay_price";
        }
        else if($params['type'] == 6){
            $sortName = "collection_num";
        }
        else if($params['type'] == 7){
            $sortName = "pay_rate";
        }
        else if($params['type'] == 8){
            $sortName = "order_goods_unit";
        }
        else{
            $sortName = "browse_num";
        }
        if(empty($list)) $list = [];
        $total = count($list);
        $sortNames = array_column($list,$sortName);
        array_multisort($sortNames,SORT_DESC,$list);

        if($export == 1){
            $export_list = [];
            foreach($list as $key => $val){
                $export_list[] = [
                    ($key+1),
                    $val['title'],
                    $val['browse_num'],
                    $val['visitor_num'],
                    $val['order_goods_num'],
                    $val['order_price'],
                    $val['pay_price'],
                    $val['collection_num'],
                    $val['pay_rate'],
                    $val['order_goods_unit'],
                ];
            }
    	    $headArr = ["排名","名称","商品浏览量","UV","下单件数","GMV","销售额","收藏数","支付转化率","商品单位"];
    	    \app\common\model\PhpOffice::excelExport("goodsrank-".date("Y-m-d"),$headArr,$export_list);
    	}

        $list = array_slice($list, ($page-1)*$limit, $limit);

        __success("操作成功",['list'=>$list,'total'=>$total]); 
    }

    public function reward(){
        global $_W;

        if(empty($_W['base'])){
            $_W['base'] = \app\common\model\bbfxshop\Common::getConfig(true);
        }

        $reward_list = \app\common\model\bbfxshop\FenxiaoTongji::getRewardList(0,true);
        $list = [];
        foreach($reward_list as $key => $val){
            if($val['type'] > 0){
                $list[] = $val;
            }
        }

        $where = ['a.uniacid'=>UNIACID,'a.status'=>1,'b.id'=>['>',0],'a.money_type'=>['in',[1,3]]];
        $commission_total = Db::name("bbfx_fenxiao_order")->alias("a")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->sum("a.money");
        $commission_ok_history = Db::name("bbfx_fenxiao_order")->alias("a")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->where(['a.is_daozhang'=>1])->sum('a.money');
        $commission_ok = Db::name("bbfx_user")->where(['uniacid'=>UNIACID])->sum('commission_ok');

        $result = [
            'list'=>$list,
            'commission_total'=>round($commission_total,2),
            'commission_ok_history'=>round($commission_ok_history,2),
            'commission_ok'=>round($commission_ok,2),
        ];

        __success("操作成功",$result);
    }

    public function order(){
        $params = $this->param(['sdate/s','edate/s','goods_id/d','type/d']);
        $stime = strtotime(date("Y-m-d 00:00:00")." -29 day");
        $etime = strtotime(date("Y-m-d 23:59:59"));
        if(!empty($params['sdate'])){ 
            $stime = strtotime($params['sdate']." 00:00:00");
        }
        if(!empty($params['edate'])){
            $etime = strtotime($params['edate']." 23:59:59");
        }
        $type = $params['type'];
        if($type == 1){
            $stime = strtotime(date("Y-m-d 00:00:00"));
            $etime = strtotime(date("Y-m-d 23:59:59"));
        }
        else if($type == 2){
            $stime = strtotime(date("Y-m-d 00:00:00")." -1 day");
            $etime = strtotime(date("Y-m-d 23:59:59",$stime));
        }
        else if($type == 3){
            $stime = strtotime(date("Y-m-d 00:00:00")." -6 day");
            $etime = strtotime(date("Y-m-d 23:59:59"));
        }
        else if($type == 4){
            $stime = strtotime(date("Y-m-d 00:00:00")." -29 day");
            $etime = strtotime(date("Y-m-d 23:59:59"));
        }
        else if($type == 5){
            $stime = strtotime(date("Y-m-01 00:00:00"));
            $etime = strtotime(date("Y-m-d 23:59:59"));
        }
        else if($type == 6){
            $stime = strtotime(date("Y-01-01 00:00:00"));
            $etime = strtotime(date("Y-m-d 23:59:59"));
        }
         
        $goods_where = ['>',0];
        if($params['goods_id'] > 0){
            $goods_where = $params['goods_id'];
        }

        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        $order_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->group("a.order_id")->count();
        $order_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.price");
        $refund_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->group("a.order_id")->count();
        $refund_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->sum("a.price");
        

        $order_platform = [
            ['title'=>'小程序','value'=>['wxapp','WXAPP']],
            ['title'=>'H5','value'=>['H5']],
            ['title'=>'公众号','value'=>['GZH']],
            ['title'=>'APP','value'=>['APP']],
            ['title'=>'PC','value'=>['PC']],
        ];
        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        foreach($order_platform  as $key => &$val){
            $where['c.platform'] = ['in',$val['value']];
            $price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.price");
            $val['price'] = round($price,2);
            $val['rate'] = 0;
            if($order_price > 0){
                $val['rate'] = round($price / $order_price,2) * 100;
            }
            
        }
        unset($val);

        $pay_plugin = \app\common\model\bbfxshop\Plugin::getPayPlugin();
        
        $order_source = [
            ['title'=>'普通订单','value'=>0],
            ['title'=>'后台录单','value'=>7],
        ];
        if(in_array('seckill',$pay_plugin)){
            $order_source[] = ['title'=>'秒杀订单','value'=>1];
        }
        if(in_array('cloud_warehouse',$pay_plugin)){
            $order_source[] = ['title'=>'云仓提货订单','value'=>2];
            $order_source[] = ['title'=>'云仓进货订单','value'=>8];
        }
        if(in_array('pintuan',$pay_plugin)){
            $order_source[] = ['title'=>'拼团订单','value'=>3];
        }
        if(in_array('crazy',$pay_plugin)){
            $order_source[] = ['title'=>'疯狂卖货模式复购单','value'=>4];
        }
        if(in_array('super_maihuo',$pay_plugin)){
            $order_source[] = ['title'=>'超级卖货模式复购单','value'=>5];
        }
        if(in_array('gift',$pay_plugin)){
            $order_source[] = ['title'=>'礼品订单','value'=>6];
        }

        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        foreach($order_source  as $key => &$val){
            $where['c.order_source'] = $val['value'];
            $price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.price");
            $val['price'] = round($price,2);
            $val['rate'] = 0;
            if($order_price > 0){
                $val['rate'] = round($price / $order_price,2) * 100;
            }
        }
        unset($val);
        
        // 成交用户数
        $order_user_num = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'is_pay'=>1])->group("user_id")->count();
        // 累计用户数
        $user_total = Db::name("bbfx_user")->where(['uniacid'=>UNIACID])->count();
        $pay_rate = 0;
        if($user_total > 0){
            $pay_rate = round($order_user_num / $user_total,2) * 100;
        }
        
        
        $result = [
            'order_num'=>intval($order_num),
            'refund_num'=>intval($refund_num),
            'order_price'=>round($order_price,2),
            'refund_price'=>round($refund_price,2),
            'order_platform'=>$order_platform,
            'order_source'=>$order_source,
            'pay_rate'=>round($pay_rate,2),
        ];

        $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$stime,$etime]],'b.id'=>$goods_where,'c.is_pay'=>1];
        $order_list = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->group("a.order_id")->field("a.id,sum(a.price) as price,a.createtime")->order("a.createtime asc")->select();
        $refund_list = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->group("a.order_id")->field("a.id,sum(a.price) as price,a.createtime")->order("a.createtime asc")->select();
    
        $list = [];
        $time = $stime;
        while(true){
            $st = $time;
            $et = strtotime(date('Y-m-d 23:59:59',$time));

            // $where = ['a.uniacid'=>UNIACID,'a.createtime'=>['between',[$st,$et]],'b.id'=>$goods_where,'c.is_pay'=>1];
            // $order_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->group("a.order_id")->count();
            // $order_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->sum("a.price");
            // $refund_num = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->group("a.order_id")->count();
            // $refund_price = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->join("bbfx_order c","a.order_id=c.id","LEFT")->where($where)->where(['c.refund_status'=>1])->sum("a.price");
            
            $order_num = 0;
            $order_price = 0;
            foreach ($order_list as $val){
                if($val['createtime'] >= $st && $val['createtime'] < $et){
                    $order_num++;
                    $order_price += $val['price'];
                }
                if($val['createtime'] > $et){
                    break;
                }
            }
            $refund_num = 0;
            $refund_price = 0;
            foreach ($refund_list as $val){
                if($val['createtime'] >= $st && $val['createtime'] < $et){
                    $refund_num++;
                    $refund_price += $val['price'];
                }
                if($val['createtime'] > $et){
                    break;
                }
            }

            $list[] = [
                'date'=>date('m-d',$time),
                'order_num'=>intval($order_num),
                'refund_num'=>intval($refund_num),
                'order_price'=>round($order_price,2),
                'refund_price'=>round($refund_price,2)
            ];

            $time = strtotime(date('Y-m-d 00:00:00',$time)." +1 day");
            
            if($time > $etime){
                break;
            }
            
        }

        $result['list'] = $list;

        __success('操作成功',$result);
    }

    public function user(){
        $params = $this->param(['sdate/s','edate/s','platform/s']);
        $stime = strtotime(date("Y-m-d 00:00:00")." -29 day");
        $etime = strtotime(date("Y-m-d 23:59:59"));
        if(!empty($params['sdate'])){ 
            $stime = strtotime($params['sdate']." 00:00:00");
        }
        if(!empty($params['edate'])){
            $etime = strtotime($params['edate']." 23:59:59");
        }

        $platform = ['in',['wxapp','WXAPP','H5','GZH','APP','PC']];
        if(!empty($params['platform'])){
            $platform = $params['platform'];
        }

        // 访客数
        $visitor_num = Db::name("bbfx_user_log")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'platform'=>$platform])->group("user_id,utk")->count();
        // 用户数
        $user_num = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'platform'=>$platform])->count();
        // 成交用户数
        $order_user_num = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'is_pay'=>1,'platform'=>$platform])->group("user_id")->count();
        // 支付转化率
        $pay_rate = 0;
        $visitor_num1 = $visitor_num;
        if($order_user_num > $visitor_num){
            $visitor_num1 = $order_user_num;
        }
        if($visitor_num1 > 0){
            $pay_rate = round($order_user_num / $visitor_num1,2) * 100;
        }
        // 分销商用户数
        $fenxiao_user_num = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'platform'=>$platform,'become_distributor_time'=>['between',[$stime,$etime]],'is_distributor'=>1,'distributor_status'=>1])->count();
        // 充值用户数
        $recharge_user_num = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'platform'=>$platform,'createtime'=>['between',[$stime,$etime]],'is_pay'=>1])->group("user_id")->count();
        // 累计用户数
        $user_total = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'platform'=>$platform])->count();
        // 累计分销商用户数
        $fenxiao_user_total = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'platform'=>$platform,'is_distributor'=>1,'distributor_status'=>1])->count();
        // 累计充值用户数
        $recharge_user_total = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'platform'=>$platform,'is_pay'=>1])->group("user_id")->count();
        // 累计成交用户数
        $order_user_total = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'platform'=>$platform])->group("user_id")->count();
        // 访客总数
        // $visitor_total = Db::name("bbfx_user_log")->where(['uniacid'=>UNIACID,'platform'=>$platform])->group("user_id,utk")->count();
        $visitor_total_sql = Db::name('bbfx_user_log')->where(['uniacid'=>UNIACID,'platform'=>$platform])->field('DISTINCT user_id,utk')->buildSql();
        // 嵌套子查询统计数量
        $visitor_total = Db::table($visitor_total_sql.' a')->count();

        $result = [
            'visitor_num'=>intval($visitor_num),
            'user_num'=>intval($user_num),
            'order_user_num'=>intval($order_user_num),
            'pay_rate'=>round($pay_rate,2),
            'fenxiao_user_num'=>intval($fenxiao_user_num),
            'recharge_user_num'=>intval($recharge_user_num),
            'user_total'=>intval($user_total),
            'fenxiao_user_total'=>intval($fenxiao_user_total),
            'recharge_user_total'=>intval($recharge_user_total),
            'order_user_total'=>intval($order_user_total),
            'visitor_total'=>intval($visitor_total)
        ];

        $visitor_num_list = Db::name("bbfx_user_log")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'platform'=>$platform])->group("user_id,utk")->field("createtime")->select();
        $user_num_list = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'platform'=>$platform])->field("createtime")->select();
        $order_user_num_list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]],'is_pay'=>1,'platform'=>$platform])->group("user_id")->field("createtime")->select();
        $fenxiao_user_num_list = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'become_distributor_time'=>['between',[$stime,$etime]],'platform'=>$platform,'is_distributor'=>1,'distributor_status'=>1])->field("createtime")->select();
        $recharge_user_nums_list = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'platform'=>$platform,'createtime'=>['between',[$stime,$etime]],'is_pay'=>1])->group("user_id")->field("createtime")->select();
        $visitor_nums = [];
        $user_nums = [];
        $order_user_nums = [];
        $fenxiao_user_nums = [];
        $recharge_user_nums = [];
        foreach($visitor_num_list as $val){
            $date = date('Y-m-d',$val['createtime']);
            if(empty($visitor_nums[$date])) $visitor_nums[$date] = 0;
            $visitor_nums[$date]++;
        }
        foreach($user_num_list as $val){
            $date = date('Y-m-d',$val['createtime']);
            if(empty($user_nums[$date])) $user_nums[$date] = 0;
            $user_nums[$date]++;
        }
        foreach($order_user_num_list as $val){
            $date = date('Y-m-d',$val['createtime']);
            if(empty($order_user_nums[$date])) $order_user_nums[$date] = 0;
            $order_user_nums[$date]++;
        }
        foreach($fenxiao_user_num_list as $val){
            $date = date('Y-m-d',$val['createtime']);
            if(empty($fenxiao_user_nums[$date])) $fenxiao_user_nums[$date] = 0;
            $fenxiao_user_nums[$date]++;
        }
        foreach($recharge_user_nums_list as $val){
            $date = date('Y-m-d',$val['createtime']);
            if(empty($recharge_user_nums[$date])) $recharge_user_nums[$date] = 0;
            $recharge_user_nums[$date]++;
        }

        $list = [];
        $time = $stime;
        while(true){
            $st = $time;
            $et = strtotime(date('Y-m-d 23:59:59',$time));
            $date = date('Y-m-d',$time);
            // 访客数
            // $visitor_num = Db::name("bbfx_user_log")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$st,$et]],'platform'=>$platform])->group("user_id,utk")->count();
            // 用户数
            // $user_num = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$st,$et]],'platform'=>$platform])->count();
            // 成交用户数
            // $order_user_num = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$st,$et]],'is_pay'=>1,'platform'=>$platform])->group("user_id")->count();
            // 分销商用户数
            // $fenxiao_user_num = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'become_distributor_time'=>['between',[$st,$et]],'platform'=>$platform,'is_distributor'=>1,'distributor_status'=>1])->count();
            // 充值用户数
            // $recharge_user_num = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'platform'=>$platform,'createtime'=>['between',[$st,$et]],'is_pay'=>1])->group("user_id")->count();
            
            $visitor_num = intval($visitor_nums[$date]);
            $user_num = intval($user_nums[$date]);
            $order_user_num = intval($order_user_nums[$date]);
            $fenxiao_user_num = intval($fenxiao_user_nums[$date]);
            $recharge_user_num = intval($recharge_user_nums[$date]);
            
            $list[] = [
                'date'=>date('m-d',$time),
                'visitor_num'=>intval($visitor_num),
                'user_num'=>intval($user_num),
                'order_user_num'=>intval($order_user_num),
                'fenxiao_user_num'=>intval($fenxiao_user_num),
                'recharge_user_num'=>intval($recharge_user_num),
            ];

            $time = strtotime(date('Y-m-d 00:00:00',$time)." +1 day");
            
            if($time > $etime){
                break;
            }
            
        }

        $result['list'] = $list;

        __success("操作成功",$result);
    }

    public function profit(){
        $params = $this->param(['sdate/s','edate/s','platform/s','export/d']);
        $stime = strtotime(date("Y-m-d 00:00:00")." -29 day");
        $etime = strtotime(date("Y-m-d 23:59:59"));
        if(!empty($params['sdate'])){ 
            $stime = strtotime($params['sdate']." 00:00:00");
        }
        if(!empty($params['edate'])){
            $etime = strtotime($params['edate']." 23:59:59");
        }

        $export = intval($params['export']);

        // 收入
        $total_order_list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1])->field("id,price,profit_amount,cost_price,createtime")->select();
        // $order_list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'createtime'=>['between',[$stime,$etime]]])->field("id")->select();
        $total_oids = [-1];
        $total_income = 0;
        $income = 0;
        $total_amount = 0;
        $total_cost_price = 0;
        $total_profit_amount = 0;
        $amount = 0;
        $cost_price = 0;
        $profit_amount = 0;
        $order_list = [];
        $order_incomes = [];
        $order_amounts = [];
        $order_cost_prices = [];
        $order_profit_amounts = [];
        $order_ids = [];
        foreach ($total_order_list as $val){
            $total_income += $val['price'];
            $total_profit_amount += $val['profit_amount'];
            if($val['profit_amount'] == 0){
                $total_amount += $val['price'];
                $total_cost_price += $val['cost_price'];
                $total_oids[] = $val['id'];
            }
            if($stime < $val['createtime'] && $etime > $val['createtime']){
                $date = date("Y-m-d",$val['createtime']);
                $income += $val['price'];
                $profit_amount += $val['profit_amount'];
                if($val['profit_amount'] == 0){
                    $amount += $val['price'];
                    $cost_price += $val['cost_price'];
                    $oids[] = $val['id'];
                    if(empty($order_amounts[$date])){
                        $order_amounts[$date] = 0;
                    }
                    $order_amounts[$date] += $val['price'];
                    if(empty($order_cost_prices[$date])){
                        $order_cost_prices[$date] = 0;
                    }
                    $order_cost_prices[$date] += $val['cost_price'];
                }
                if(empty($order_incomes[$date])){
                    $order_incomes[$date] = 0;
                }
                $order_incomes[$date] += $val['price'];
                if(empty($order_profit_amounts[$date])){
                    $order_profit_amounts[$date] = 0;
                }
                $order_profit_amounts[$date] += $val['profit_amount'];
                if(empty($order_ids[$date])){
                    $order_ids[$date] = [];
                }
                $order_ids[$date][] = $val['id'];
            }
        }
        
        // $total_income = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1])->sum("price");
        // $income = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'createtime'=>['between',[$stime,$etime]]])->sum("price");
        // 佣金
        $total_commission = Db::name("bbfx_fenxiao_commission")->where(['uniacid'=>UNIACID,'money'=>['>',0],'type'=>['not in',[20,21]]])->sum("money");
        // $commission = Db::name("bbfx_fenxiao_commission")->where(['uniacid'=>UNIACID,'money'=>['>',0],'type'=>['not in',[20,21]],'createtime'=>['between',[$stime,$etime]]])->sum("money");
        $fenxiao_commission_list = Db::name("bbfx_fenxiao_commission")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]]])->field("money,createtime")->select();
        $commission = 0;
        $commission_unwithdrawn = 0;
        $fenxiao_commissions = [];
        $commission_unwithdrawns = [];
        foreach ($fenxiao_commission_list as $key => $val){
            $date = date("Y-m-d",$val['createtime']);
            if($val['money'] > 0 && !in_array($val['type'],[20,21])){
                if(empty($fenxiao_commissions[$date])){
                    $fenxiao_commissions[$date] = 0;
                }
                $fenxiao_commissions[$date] += $val['money'];
                $commission += $val['money'];
            }
            if(empty($commission_unwithdrawns[$date])){
                $commission_unwithdrawns[$date] = 0;
            }
            $commission_unwithdrawns[$date] += $val['money'];
            $commission_unwithdrawn += $val['money'];
        }
        
        // 未提现佣金
        $total_commission_unwithdrawn = Db::name("bbfx_user")->where(['uniacid'=>UNIACID])->sum("commission_ok");
        // $commission_unwithdrawn = Db::name("bbfx_fenxiao_commission")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$stime,$etime]]])->sum("money");
        // 余额
        $total_balance = Db::name("bbfx_user")->where(['uniacid'=>UNIACID])->sum("balance");
        // $balance = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'createtime'=>['between',[$stime,$etime]]])->sum("money");
        $user_balance_list = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'createtime'=>['between',[$stime,$etime]]])->field("money,createtime")->select();
        $balance = 0;
        $user_balances = [];
        foreach ($user_balance_list as $val){
            $balance += $val['money'];
            $date = date("Y-m-d",$val['createtime']);
            if(empty($user_balances[$date])){
                $user_balances[$date] = 0;
            }
            $user_balances[$date] += $val['money'];
        }
        // 利润
        // $total_amount = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0])->sum("price");
        // $total_cost_price = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0])->sum("cost_price");
        // $total_list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0])->field("id")->select();
        // $total_profit_amount = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1])->sum("profit_amount");
        // if(empty($total_list)) $total_list = [];
        // $total_oids = array_column($total_list,"id");
        $total_order_commission = Db::name("bbfx_fenxiao_order")->alias("a")->join("bbfx_order b","a.order_id=b.id","LEFT")->where(['a.uniacid'=>UNIACID,'a.user_id'=>['>',0],'a.order_id'=>['in',$total_oids],'a.status'=>1])->sum("money");
        $total_profit_amount += $total_amount - $total_cost_price - $total_order_commission;
        // $amount = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0,'createtime'=>['between',[$stime,$etime]]])->sum("price");
        // $cost_price = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0,'createtime'=>['between',[$stime,$etime]]])->sum("cost_price");
        // $list = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0,'createtime'=>['between',[$stime,$etime]]])->field("id")->select();
        // $profit_amount = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'createtime'=>['between',[$stime,$etime]]])->sum("profit_amount");
        // if(empty($list)) $list = [];
        // $oids = array_column($list,"id");
        $order_commission_list = Db::name("bbfx_fenxiao_order")->alias("a")->join("bbfx_order b","a.order_id=b.id","LEFT")->where(['a.uniacid'=>UNIACID,'a.user_id'=>['>',0],'a.order_id'=>['in',$oids],'a.status'=>1])->field("a.money,a.createtime")->select();
        $order_commission = 0;
        $order_commissions = [];
        foreach ($order_commission_list as $val){
            $order_commission += $val['money'];
            $date = date("Y-m-d",$val['createtime']);
            if(empty($order_commissions[$date])){
                $order_commissions[$date] = 0;
            }
            $order_commissions[$date] += $val['money'];
        }
        $profit_amount += $amount - $cost_price - $order_commission;
        $result = [
            'total_income'=>round($total_income,2),
            'income'=>round($income,2),
            'total_commission'=>round($total_commission,2),
            'commission'=>max(0,round($commission,2)),
            'total_profit_amount'=>round($total_profit_amount,2),
            'profit_amount'=>round($profit_amount,2),
            'total_balance'=>round($total_balance,2),
            'balance'=>max(0,round($balance,2)),
            'total_commission_unwithdrawn'=>round($total_commission_unwithdrawn,2),
            'commission_unwithdrawn'=>max(0,round($commission_unwithdrawn,2)),
        ];
        // print_r(111);die;
        
        

        $list = [];
        $time = $stime;
        $export_list = [];
        while(true){
            $st = $time;
            $et = strtotime(date('Y-m-d 23:59:59',$time));
            $date = date('Y-m-d',$time);
            // 收入
            // $income = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'createtime'=>['between',[$st,$et]]])->sum("price");
            $income = round($order_incomes[$date],2);
            // 佣金
            // $commission = Db::name("bbfx_fenxiao_commission")->where(['uniacid'=>UNIACID,'money'=>['>',0],'type'=>['not in',[20,21]],'createtime'=>['between',[$st,$et]]])->sum("money");
            $commission = round($fenxiao_commissions[$date],2);
            // 未提现佣金
            // $commission_unwithdrawn = Db::name("bbfx_fenxiao_commission")->where(['uniacid'=>UNIACID,'createtime'=>['between',[$st,$et]]])->sum("money");
            $commission_unwithdrawn = round($commission_unwithdrawns[$date],2);
            // 余额
            // $balance = Db::name("bbfx_money_log")->where(['uniacid'=>UNIACID,'type'=>1,'createtime'=>['between',[$st,$et]]])->sum("money");
            $balance = round($user_balances[$date],2);
            // 利润
            // $amount = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0,'createtime'=>['between',[$st,$et]]])->sum("price");
            $amount = round($order_amounts[$date],2);
            
            // $cost_price = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0,'createtime'=>['between',[$st,$et]]])->sum("cost_price");
            $cost_price = round($order_cost_prices[$date],2);
            
            
            // $profit_amount = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'createtime'=>['between',[$st,$et]]])->sum("profit_amount");
            $profit_amount = round($order_profit_amounts[$date],2);
            
            // $olist = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'is_pay'=>1,'profit_amount'=>0,'createtime'=>['between',[$st,$et]]])->field("id")->select();
            // if(empty($olist)) $olist = [];
            // $oids = array_column($olist,"id");
            // $order_commission = Db::name("bbfx_fenxiao_order")->where(['uniacid'=>UNIACID,'user_id'=>['>',0],'order_id'=>['in',$oids],'status'=>['in',[1]]])->sum("money");
            $order_commission = round($order_commissions[$date],2);
            
            $profit_amount += $amount - $cost_price - $order_commission;

            $data = [
                'date'=>date('m-d',$time),
                'income'=>round($income,2),
                'commission'=>max(0,round($commission,2)),
                'commission_unwithdrawn'=>max(0,round($commission_unwithdrawn,2)),
                'balance'=>max(0,round($balance,2)),
                'profit_amount'=>round($profit_amount,2),
            ];

            $list[] = $data;

            if($export == 1){
                $export_list[] = [
                    $data['date'],
                    $data['income'],
                    $data['commission'],
                    $data['commission_unwithdrawn'],
                    $data['balance'],
                    $data['profit_amount'],
                ];
            }

            $time = strtotime(date('Y-m-d 00:00:00',$time)." +1 day");
            
            if($time > $etime){
                break;
            }
            
        }

        if($export == 1){
    	    $headArr = ["日期","收入","佣金","未提现","余额","利润"];
    	    \app\common\model\PhpOffice::excelExport("profit-".date("Y-m-d"),$headArr,$export_list);
    	}

        $result['list'] = $list;

        __success("操作成功",$result);
    }
}