<?php

namespace app\admin\controller\agent;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        //推广链接 推广二维码
        $extend_link = config('site.extend_link');
        $extend_qrcode = config('site.domain_cdnurl') . config('site.extend_qrcode');

        $user_info = Db::name('user')->where(['id' => $this->auth->user_id])->find();
        if (!$user_info) {
            $user_info['id'] = '';
            $user_info['introcode'] = '';
        } else {
            $extend_link = $extend_link . '?code=' . $user_info['introcode'];
        }

        $this->assign('user', $user_info);
        $this->assign('extend_link', $extend_link);
        $this->assign('extend_qrcode', $extend_qrcode);
        return $this->view->fetch();
    }
    
    //统计
    public function statistics() {
        $user_id = input('ids', 0, 'intval');
        if (!$user_id) {
            $user_id = $this->auth->user_id;
        }

        $user_info = Db::name('user')->where(['id' => $user_id])->find();
        if ($user_info && $user_info['is_agent'] == 1) { //高级代理
            $intro_recharge_rebate_rate = config('site.h_intro_recharge_rebate_rate'); //高级邀请人充值返利比率
            $intro_income_rebate_rate = config('site.h_intro_income_rebate_rate'); //高级邀请人收益返利比率
        } else {
            $intro_recharge_rebate_rate = config('site.intro_recharge_rebate_rate');
            $intro_income_rebate_rate = config('site.intro_income_rebate_rate');
        }
        if ($user_info) {
            $start = strtotime(date('Y-m-d')); //默认今日
            $end = $start + 86399;

            $mt_user_money_log = Db::name('user_money_log');
            //下级id集合
            $lower_user_ids = Db::name('user')->where(['intro_uid' => $user_id])->column('id');
            $lower_user_ids = $lower_user_ids ? : [];
            //收益type
            $income_type = [21, 22, 23, 54, 60, 82];

            /**
             * 今日概览
             */
            //新增营业额
            $lower_new_income = $mt_user_money_log->where(['log_type' => ['in', $income_type], 'user_id' => ['in', $lower_user_ids], 'createtime' => ['egt', $start]])->sum('change_value');
            //家族新增收益
            $lower_new_my_income = $mt_user_money_log->where(['log_type' => 68, 'user_id' => $user_id, 'createtime' => ['egt', $start]])->sum('change_value');
            //主播总人数
            $lower_total_count = Db::name('user')->where(['intro_uid' => $user_id, 'gender' => 0])->count('id');
            //邀请充值奖励
            $lower_total_invite_profit = $mt_user_money_log->where(['log_type' => 65, 'user_id' => $user_id, 'createtime' => ['egt', $start]])->sum('change_value');

            /**
             * 总览
             */
            //营业额
            //昨日收入
            $last_day_map = [
                'log_type' => ['in', $income_type],
                'user_id' => ['in', $lower_user_ids],
                'createtime' => ['between', [$start - 86400, $start - 1]],
            ];
            $lower_last_day_profit = $mt_user_money_log->where($last_day_map)->sum('change_value');
            //获取本周开始时间
            $day = 1; // 一周的第一天
            $wday = date('w'); // 星期几的数字表示    0(周日)到 6(周六)
            $wday_start = strtotime(date('Y-m-d', strtotime('-' . ($wday ? $wday - $day : 6) . ' day'))); // 本周开始日期

            $week_map = [
                'log_type' => ['in', $income_type],
                'user_id' => ['in', $lower_user_ids],
                'createtime' => ['between', [$wday_start - 86400 * 7, $wday_start]]
            ];
            //上周收入
            $lower_last_week_profit = $mt_user_money_log->where($week_map)->sum('change_value');
            //近30天收入
            $last_day_map['createtime'] = ['egt', $start - 86400 * 30];
            $lower_thirty_day_profit = $mt_user_money_log->where($last_day_map)->sum('change_value');
            //主播总收入
            unset($week_map['createtime']);
            $lower_total_point_profit = $mt_user_money_log->where($week_map)->sum('change_value');

            //收益
            //昨日收益
            $my_last_day_map = [
                'log_type' => 68,
                'user_id' => $user_id,
                'createtime' => ['between', [$start - 86400, $start - 1]],
            ];
            $my_yesterday_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
            //上周收益
            $my_last_day_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
            $my_last_week_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
            //近30天收益
            $my_last_day_map['createtime'] = ['egt', $start - 86400 * 30];
            $my_thirty_day_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
            //家族总收益
            unset($my_last_day_map['createtime']);
            $my_total_point_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');

            //昨日邀请充值收益
            $my_intive_map = [
                'log_type' => 65,
                'user_id' => $user_id,
                'createtime' => ['between', [$start - 86400, $start - 1]],
            ];
            $my_yesterday_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
            //上周邀请充值收益
            $my_intive_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
            $my_last_week_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
            //近30天充值总收益
            $my_intive_map['createtime'] = ['egt', $start - 86400 * 30];
            $my_thirty_day_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
            //邀请充值总收益
            unset($my_intive_map['createtime']);
            $my_total_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
        } else {
            $lower_new_income = 0;
            $lower_new_my_income = 0;
            $lower_total_count = 0;
            $lower_total_invite_profit = 0;
            $lower_last_day_profit = 0;
            $lower_last_week_profit = 0;
            $lower_thirty_day_profit = 0;
            $lower_total_point_profit = 0;
            $my_yesterday_profit = 0;
            $my_last_week_profit = 0;
            $my_thirty_day_profit = 0;
            $my_total_point_profit = 0;
            $my_yesterday_invite_profit = 0;
            $my_last_week_invite_profit = 0;
            $my_thirty_day_invite_profit = 0;
            $my_total_invite_profit = 0;
        }

        $this->assign('lower_new_income', $lower_new_income);
        $this->assign('lower_new_my_income', $lower_new_my_income);
        $this->assign('lower_total_count', $lower_total_count);
        $this->assign('lower_total_invite_profit', $lower_total_invite_profit);
        $this->assign('lower_last_day_profit', $lower_last_day_profit);
        $this->assign('lower_last_week_profit', $lower_last_week_profit);
        $this->assign('lower_thirty_day_profit', $lower_thirty_day_profit);
        $this->assign('lower_total_point_profit', $lower_total_point_profit);
        $this->assign('my_yesterday_profit', $my_yesterday_profit);
        $this->assign('my_last_week_profit', $my_last_week_profit);
        $this->assign('my_thirty_day_profit', $my_thirty_day_profit);
        $this->assign('my_total_point_profit', $my_total_point_profit);
        $this->assign('my_yesterday_invite_profit', $my_yesterday_invite_profit);
        $this->assign('my_last_week_invite_profit', $my_last_week_invite_profit);
        $this->assign('my_thirty_day_invite_profit', $my_thirty_day_invite_profit);
        $this->assign('my_total_invite_profit', $my_total_invite_profit);
        $this->assign('intro_recharge_rebate_rate', $intro_recharge_rebate_rate);
        $this->assign('intro_income_rebate_rate', $intro_income_rebate_rate);

        return $this->view->fetch();
    }

}