<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller\v1;


use app\BaseController;
use app\model\api\MemberDetail;
use app\model\api\Order;
use app\model\system\AllTotal;
use app\model\system\OrderInfo;
use app\model\system\SiteDetail;
use app\model\system\Tx;
use app\Request;
use library\services\UtilService;
use think\Cache;

class Index extends  BaseController
{

    /**
     * 获取统计数据
     */
    public function getIndex(){
        $reids = \think\facade\Cache::store('redis');
        $postStr = $reids->get('system_totalv1');
        if(empty($postStr)) {
            //今日订单量
            $post['today_order_count'] = Order::where('status', '>', 0)->whereTime('pay_time', 'today')->count();
            //订单总量
            $post['all_order_count'] = Order::where('status', '>', 0)->count();

            //今日子订单量
            $post['today_t_order_count'] = OrderInfo::where('status', '>', 0)->whereTime('time', 'today')->count();
            //子订单总量
            $post['all_t_order_count'] = OrderInfo::where('status', '>', 0)->count();

            //今日销售额
            $post['today_money'] = Order::where('status', '>', 0)->whereTime('pay_time', 'today')->sum('all_price');
            //总销售额
            $post['all_money'] = Order::where('status', '>', 0)->sum('all_price');

            //新增会员
            $post['today_user'] = (new \app\model\system\Member)->whereDay('regtime', 'today')->count();
            //总会员
            $post['user_count'] = (new \app\model\system\Member)->count();

            //分站佣金
            $post['fz_total'] = (new SiteDetail)->where('type', 1)->sum('v');
            //今日分站佣金
            $post['today_fz_total'] = (new SiteDetail)->whereTime('time', 'today')->where('type', 1)->sum('v');

            //分站已提现
            $post['fz_tx'] = (new Tx)->where('type', 2)->sum('money');
            //分站今日已提现
            $post['today_fz_tx'] = (new Tx)->whereTime('time', 'today')->where('type', 2)->sum('money');

            //会员总佣金
            $post['user_commission'] = (new MemberDetail)->where('code', 'income')->sum('v');
            //今日佣金
            $post['today_user_commission'] = (new MemberDetail)->whereTime('time', 'today')->where('code', 'income')->sum('v');

            //会员已提现
            $post['user_tx'] = (new Tx)->where('type', 1)->sum('money');
            //会员今日已提现
            $post['today_user_tx'] = (new Tx)->whereTime('time', 'today')->where('type', 1)->sum('money');

            //未打单
            $post['no_order_count'] = Order::where('status', '=', 1)->count();
            //已打单
            $post['order_count'] = Order::where('status', '>', 1)->count();

            $reids->set('system_totalv1',serialize($post),60 * 5);
        } else {
            $post = unserialize($postStr);
        }
        return app('json')->success($post);
    }

    /**
     * site状态
     */
    public function getSite(Request $request){
        $pageCount = 10;
        [$page] = UtilService::getMore([
            ['page',1]
        ],$request,true);
        $siteData = (new \app\model\system\Site)
            ->field("s.name,s.sassid,s.money,(SELECT count(*) from table_order where sassid = s.sassid AND status > 0) as orderCount,
                (SELECT count(*) FROM table_member where sassid = s.sassid) as memberCount,
                (SELECT SUM(v) FROM table_recharge where sassid = s.sassid and status = 1) as recharge_count
            ")
            ->alias("s")
            ->order("orderCount","desc")
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        $post = [];
        $post['pageSize'] = $pageCount;
        $post['pageCount'] = $siteData['total'];
        $post['data'] = (array)$siteData['data'];
        return app('json')->success($post);
    }

    public function getOrderTotal(Request $request){
        [$visitDate] = UtilService::getMore([
            ['visitDate','thirtyday']
        ],$request,true);
        $order = (new \app\model\system\Order());
        $orderInfo = (new OrderInfo());
        //30天
        $tAr = ['name'=>[],'order_money'=>[],'order_info_count'=>[]];
        if($visitDate == 'thirtyday') {
            for ($i = 30;$i>=0;$i--) {
                $time = date('Y-m-d',strtotime('-'.$i.' day'));
                $tAr['name'][] = $time;
                $tAr['order_money'][] = $order->whereDay('time',$time)->where('status','>',0)->sum('all_price');
                $tAr['order_info_count'][] = $orderInfo->where('status','>',0)->whereDay('time',$time)->count();
            }
        }
        return app('json')->success($tAr);
    }

    public function xh(Request $request){
        $pageCount = 14;
        [$page] = UtilService::getMore([
            ['page',1]
        ],$request,true);
        $siteData = (new AllTotal)
            ->order("time","desc")
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        $post = [];
        $post['pageSize'] = $pageCount;
        $post['pageCount'] = $siteData['total'];
        $post['data'] = UtilService::getParam([
            'khs_total',
            'dr_total',
            'ls_lv',
            'dr_money',
            'khs_money',
            'dr_order',
            'dr_avg',
            ['time','time',"date('Y-m-d',$1)"]
        ],$siteData['data']);
        return app('json')->success($post);
    }

    public function xhDetail(Request $request){
        $pageCount = 20;
        [$page,$date,$start,$end] = UtilService::getMore([
            ['page',1],
            ['date',''],
            ['start',''],
            ['end','']
        ],$request,true);
        if($date!=''){
            $time = strtotime($date);
            $endTime = $time + 3600 * 24;
        }else{
            $time = strtotime($start);
            $endTime = strtotime($end) + 3600 * 24;
        }
        $data = OrderInfo::alias("i")
            ->leftjoin('order o','o.id=i.o_id')
            ->leftjoin('member m','m.uid=i.uid')
            ->field('m.uid,m.mobile,m.weixin_name,m.nickname,m.label,sum(i.money) as money,count(i.id) as count')
            ->where('i.status', '>', 0)
            ->where('o.pay_time','between',[$time,$endTime])
            ->group('i.uid')
            ->order('money desc')
            ->paginate(['list_rows'=>$pageCount,'page'=>$page])
            ->toArray();
        $post = [];
        $post['pageSize'] = $pageCount;
        $post['pageCount'] = $data['total'];
        $post['data'] = UtilService::getParam([
            'uid',
            'mobile',
            'weixin_name',
            'nickname',
            'money',
            'count',
            'label'
        ],$data['data']);
        return app('json')->success($post);
    }

    public function totalRecharge(Request $request){
        $recharge = new \app\model\system\Recharge;
        $tAr = ['label'=>[],'total'=>[]];
        for ($i = 30;$i>=0;$i--) {
            $time = date('Y-m-d',strtotime('-'.$i.' day'));
            $tAr['label'][] = $time;
            $sum = $recharge
                ->whereDay('time',$time)
                ->where('status',1)
                ->sum('v');
            $tAr['total'][] = $sum > 0 ? $sum : 0;
        }

        return app('json')->success($tAr);
    }

    public function totalUser(Request $request) {
        $Member = new \app\model\system\Member;
        $tAr = ['label'=>[],'total'=>[]];
        for ($i = 30;$i>=0;$i--) {
            $time = date('Y-m-d',strtotime('-'.$i.' day'));
            $tAr['label'][] = $time;
            $sum = $Member
                ->whereDay('regtime',$time)
                ->count();
            $tAr['total'][] = $sum > 0 ? $sum : 0;
        }
        return app('json')->success($tAr);
    }

}