<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 用户推荐
 */
class Userintro extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    //我的收益
    public function myprofit(){
        //今日
        $start = strtotime(date('Y-m-d'));
        $end   = $start + 86399;

        //收益type
        $profit_type = [21,22,23,52,54,56];

        //今日收益
        $map = [
            'log_type' => ['IN',$profit_type],
            'user_id' => $this->auth->id,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_profit = Db::name('user_money_log')->where($map)->sum('change_value');

        //今日视频时长
        $map = [
            'user_id|to_user_id' => $this->auth->id,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_video_min = Db::name('user_match_video_log')->where($map)->sum('call_minutes');

        //今日语音时长
        $map = [
            'user_id|to_user_id' => $this->auth->id,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_audio_min = Db::name('user_match_audio_log')->where($map)->sum('call_minutes');

        //本周收益
        $week_start = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
        $week_end   = $week_start + 604799;

        $map = [
            'log_type' => ['IN',$profit_type],
            'user_id' => $this->auth->id,
            'createtime' => ['between',[$week_start,$week_end]],
        ];
        $week_profit = Db::name('user_money_log')->where($map)->sum('change_value');

        //我的累计收益
        $map = [
            'log_type' => ['IN',$profit_type],
            'user_id' => $this->auth->id,
        ];
        $all_profit = Db::name('user_money_log')->where($map)->sum('change_value');

        //我的邀请
        $my_intro_number = Db::name('user')->where('intro_uid',$this->auth->id)->count('id');


        $result = [
            'avatar' => $this->auth->avatar,
            'nickname' => $this->auth->nickname,
            'username' => $this->auth->username,

            'today_profit' => $today_profit,
            'today_video_min' => $today_video_min,
            'today_audio_min' => $today_audio_min,
            'week_profit' => $week_profit,
            'all_profit' => $all_profit,
            'my_intro_number' => $my_intro_number,
        ];

        $this->success('success',$result);

    }

    //我邀请的,成员今日收益
    public function myintro(){
        $map = [
            'intro_uid' => $this->auth->id,
        ];

        $list = Db::name('user')->field('id,nickname,username,avatar')->where($map)->page($this->page,$this->listrow)->select();

        $rs = [];
        foreach($list as $key => $user){
            $rs[] = $this->profit($user);
        }
        //dump($rs);
        $this->success('success',$rs);
    }

    //收益数据
    private function profit($userinfo){
        $uid = $userinfo['id'];
        //今日
        $start = strtotime(date('Y-m-d'));
        $end   = $start + 86399;

        //收益type
        $profit_type = [21,22,23,52,54,56];

        //今日收益
        $map = [
            'log_type' => ['IN',$profit_type],
            'user_id' => $uid,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_profit = Db::name('user_money_log')->where($map)->sum('change_value');

        //今日视频时长
        $map = [
            'user_id|to_user_id' => $uid,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_video_min = Db::name('user_match_video_log')->where($map)->sum('call_minutes');

        //今日语音时长
        $map = [
            'user_id|to_user_id' => $uid,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_audio_min = Db::name('user_match_audio_log')->where($map)->sum('call_minutes');

        //本周收益
        $week_start = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
        $week_end   = $week_start + 604799;

        $map = [
            'log_type' => ['IN',$profit_type],
            'user_id' => $uid,
            'createtime' => ['between',[$week_start,$week_end]],
        ];
        $week_profit = Db::name('user_money_log')->where($map)->sum('change_value');

        $result = [
            'today_profit' => $today_profit,
            'today_video_min' => $today_video_min,
            'today_audio_min' => $today_audio_min,
            'week_profit' => $week_profit,
        ];

        $result = array_merge($result,$userinfo);

        return $result;
    }
    


    //我的邀请顶部信息
    public function myintrotop() {
        //获取本周开始时间
        $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', [65, 68]],
            'user_id' => $this->auth->id,
            'createtime' => ['egt', $wday_start],
        ];
        //本周收益
        $week_profit = Db::name('user_money_log')->where($week_map)->sum('change_value');
        //上周收益
        $week_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
        $last_week_profit = Db::name('user_money_log')->where($week_map)->sum('change_value');
        //本周邀请人数
        $week_count = Db::name('user')->where(['intro_uid' => $this->auth->id, 'invite_time' => ['egt', $wday_start]])->count('id');
        //上周邀请人数
        $last_week_count = Db::name('user')->where(['intro_uid' => $this->auth->id, 'invite_time' => ['between', [$wday_start - 86400 * 7, $wday_start]]])->count('id');
        //总邀请人数
        $total_count = Db::name('user')->where(['intro_uid' => $this->auth->id])->count('id');

        $list = [
            'week_profit' => $week_profit,
            'last_week_profit' => $last_week_profit,
            'week_count' => $week_count,
            'last_week_count' => $last_week_count,
            'total_count' => $total_count,
        ];

        $this->success('success',$list);
    }

    //我的邀请
    public function myintrolist() {
        $map = [
            'intro_uid' => $this->auth->id,
        ];

        $list = Db::name('user')->field('id,nickname,avatar,invite_time createtime')->where($map)->autopage()->order('invite_time desc')->select();
        if (!$list) {
            $this->success('success', $list);
        }

        foreach ($list as &$v) {
            $v['avatar'] = one_domain_image($v['avatar']);
            $v['createtime'] = date('Y.m.d H:i', $v['createtime']);
        }

        $this->success('success',$list);
    }

    //邀请收益
    public function introincome() {
        $list = Db::name('user_money_log')->field('user_id, sum(change_value) sum')->where(['log_type' => ['in', [65, 66]]])->group('user_id')->order('sum desc')->autopage()->select();
        if (!$list) {
            $this->success('success', $list);
        }

        $mt_user = Db::name('user'); //绑定邀请码

        foreach ($list as &$v) {
            $user_info = $mt_user->field('nickname, avatar')->where(['id' => $v['user_id']])->find();
            $v['nickname'] = $user_info['nickname'];
            $v['avatar'] = one_domain_image($user_info['avatar']);
            $v['sum'] = number_format($v['sum'], 2, '.', '');
            $v['intro_num'] = $mt_user->where(['intro_uid' => $v['user_id']])->count('id');
        }

        $this->success('success', $list);
    }
}