<?php
namespace app\admin\controller;

use app\model\Admins;
use app\model\Onlines;
use app\model\Orders;
use app\model\Sales;
use app\model\Works;
use support\Log;
use support\Request;

class DataController extends base
{
    public function index(Request $request) {
        $times = $request->get('times');
        $os = $request->get('os');

        $query = Orders::alias('o')->with('admin')
            ->leftJoin('admins a','a.id = o.admin_id')
            ->order('o.admin_id','desc')
            ->group('a.id,a.name,o.admin_id')
            ->having('o.admin_id>0')
            ->fieldRaw('count(o.id) as orders, SUM(o.total_price) as total_price, SUM(IF(o.asset_price>0 AND o.status=2,1,0)) as assets, SUM(IF(o.asset_price>0 AND o.status=2,o.asset_price,0)) as asset_price, sum(if(o.order_status=0,1,0)) as nopays, sum(if(o.order_status=0,o.total_price,0)) as nopay_price, o.admin_id,a.name');

        if($times) {
            $query->whereBetween('o.create_at',[strtotime($times[0])*1000,strtotime($times[1])*1000+999]);
        }
        if($os) {
            $query->where('o.os', $os);
        }

        $list = $query->select();

        return $this->success($list, null, ['oss' => Orders::OSS]);
    }

    /**
     * 在线时长
     */
    public function online(Request $request) {
        $times = $request->get('times');
        $admin = $request->get('admin');

        $online = new Onlines();
        $list = $online->online($admin, $times);
        
        return $this->success($list);
    }

    public function anchor(Request $request) {
        $times = $request->get('times');
        $admin = $request->get('admin');
        if(!empty($times) && is_array($times) && count($times) >= 2) {
            $start = date('Y-m-d 00:00:00',strtotime($times[0]));
            $end = date('Y-m-d 23:59:59',strtotime($times[1]));
        }else{
            $start = date('Y-m-01 00:00:00');
            $end = date('Y-m-d 23:59:59');
        }

        if($admin) {
            $admins = Admins::where('is_anchor', 1)->where('username', $admin)->select();
        }else{
            $admins = Admins::where('is_anchor', 1)->select();
        }
        foreach($admins as $admin) {
            $admin->works = Works::where('admin_id', $admin->id)->where('status',1)->whereBetween('start',[$start, $end])->order('start','asc')->select()->append(['total']);
        }

        return $this->success($admins->hidden(['password','remember_token']));
    }

    public function sale(Request $request) {
        $query = Sales::with(['admin'])->order('date','desc')->order("id",'asc');

        $admin = $request->get('admin');
        if($admin) {
            $admin_id = Admins::where('username', $admin)->value('id');
            $query->where('admin_id', $admin_id);
        }
        $times = $request->get('times');
        if($times) {
            $start = date('Ymd', strtotime($times[0]));
            $end = date('Ymd', strtotime($times[1]));
            $query->whereBetween('date',[$start,$end]);
        }
        
        $list = $query->paginate($request->get('limit'));

        return $this->success($list);
    }
}