<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 代理邀请中心
 */
class Userintro extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /*public function __construct(){
        parent::__construct();

        if($this->auth->group_id == 1){
            $this->error('无权进入');
        }
    }*/

    //邀请页
    public function introinfo(){

        $today = date('Y-m-d');
        $starttime = strtotime($today) - 86400;
        $endtime   = $starttime + 86399;
        $zuori_count = Db::name('user')->where('intro_uid',$this->auth->id)->where('yaoqingtime','BETWEEN',[$starttime,$endtime])->count();

        $total_count = Db::name('user')->where('intro_uid',$this->auth->id)->count();

        $register_url = config('h5register_url') .'?intro_no='. $this->auth->introcode;

        $result = [
            'zuori_count' => $zuori_count,
            'total_count' => $total_count,
            'register_url' => $register_url,
        ];
        $this->success(1,$result);
    }

    //生成视频分享海报
    public function shareposter() {
        $inviteimage = $this->introimage();

        $data = [
            [
                "left"=> "190px",
                "top"=> "320px",
                "type"=> "img",
                "width"=> "110px",
                "height"=> "110px",
                "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
            ],

        ];


        $data = json_encode($data, 320);

        $poster = [
            'id' => $this->auth->id,
            'title' => '珍友',
            'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
            'bg_image' => '/assets/img/posteruserbg.png',
            'data' => $data,
            'status' => 'normal',
            'weigh' => 0,
            'createtime' => 1653993709,
            'updatetime' => 1653994259,
        ];

        $image = new \addons\poster\library\Image();
        $imgurl = $image->createPosterImage($poster, $this->auth->getUser());

        if (!$imgurl) {
            $this->error('生成海报出错');
        }
        $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;

        //echo '<html><body><img src="'.$imgurl.'"></body></html>';

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

    //生成邀请码二维码图片
    private function introimage() {
        $params['text'] = config('h5register_url') .'?intro_no='. $this->auth->introcode;
        $qrcode_service = \addons\qrcode\library\Service::qrcode($params);

        $qrcodePath = ROOT_PATH . 'public/uploads/hbplayer/'.date('Ymd');
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if (is_really_writable($qrcodePath)) {
            $filename = 'intro_'.$this->auth->introcode . '.png';
            $filePath = $qrcodePath .'/'. $filename;
            $qrcode_service->writeFile($filePath);
        }

        return '/uploads/hbplayer/'.date('Ymd') .'/'. $filename;
    }

    //账户中心 api/withdraw/config
    //提现 api/withdraw/withdraw

    //账户明细
    public function agentjewel_log(){

        $map = [
            'user_id' => $this->auth->id,
        ];

        $list = Db::name('user_agentjewel_log')
            ->field('id,log_type,before,change_value,remain,remark,createtime')
            ->where($map)->order('id desc')->autopage()->select();
        $list = $this->list_appen_logtext($list);

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

    //追加log_text
    private function list_appen_logtext($list){
        if(!empty($list)){
            $conf = config('wallet.logtype');
            foreach($list as $key => $val){
                $list[$key]['log_text'] = isset($conf[$val['log_type']]) ? $conf[$val['log_type']] : '';
            }
        }
        return $list;
    }

    //申请绑定查询
    public function intro_search(){
        $username = input('username',0);

        $user_info = Db::name('user')->field('id,username,group_id,nickname,avatar')->where('username',$username)->find();
        if(empty($user_info)){
            $this->error('不存在的用户');
        }

        $user_info = info_domain_image($user_info,['avatar']);
        $this->success(1,$user_info);
    }

    //申请绑定
    public function apply_intro(){
        $down_uid = input('search_uid',0);
        $images  = input('images','');
        $remark = input('remark','');

        $where = [
            'user_id'   => $this->auth->id,
            'down_uid' => $down_uid,
            'status'    => 0,
        ];
        $check = Db::name('user_intro_apply')->where($where)->find();
        if($check){
            $this->error('您上次的申请还在审核中');
        }

        $data = [
            'user_id'   => $this->auth->id,
            'down_uid' => $down_uid,
            'images'    => $images,
            'remark'    => $remark,
            'createtime'=> time(),
        ];

        Db::name('user_intro_apply')->insertGetId($data);

        $this->success('申请成功');
    }
    //申请绑定记录
    public function apply_intro_log(){
        $where = [
            'user_id'   => $this->auth->id,
        ];
        $list = Db::name('user_intro_apply')->alias('log')
            ->field('log.*,user.username,user.nickname,user.avatar')
            ->join('user','log.down_uid = user.id','LEFT')
            ->where($where)->autopage()->select();
        $list = list_domain_image($list,['avatar']);

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

    //日报
    public function ribao(){
        $result = [];

        //日期
        $today = date('Y-m-d');
        $startday  = input('startday',$today);
        $starttime = strtotime($startday);
        $endtime   = $starttime + 86399;

        //传入代理
        $down_all_id = input('down_all_id',0);

        //代理名
        $result['agentname']   = $this->auth->agentname ?: $this->auth->nickname;
        $result['yaoqingtime'] = $this->auth->yaoqingtime;
        $result['startday']    = $startday;

        //一级代理  下拉  可选二级代理
        $down_all = $this->select_agent();
        $result['down_all'] = $down_all;
        //dump($down_all);

        //确定收益统计范围
        if(!empty($down_all_id)){
            $downall_uid = Db::name('user')->where('intro_uid',$down_all_id)->column('id');//其下直推,A1下B0+B1 或 B1下C1
            //$downall_uid[] = $down_all_id;
        }else{
            $down_uid = array_column($down_all,'id');
            //dump($down_uid);
            $downall_uid = Db::name('user')->where('intro_uid','IN',$down_uid)->column('id');//其下直推B0+代理下直推C1 或 C1
        }
        //dump($downall_uid);

        //邀请奖励收益
        $recharge_sum = Db::name('user_agentjewel_log')->where('user_id','IN',$downall_uid)->where('log_type',51)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('change_value');
        $result['recharge_sum'] = $recharge_sum;
        //视频收益
        $video_sum = Db::name('user_match_video_log')->where('to_user_id','IN',$downall_uid)->where('updatetime','BETWEEN',[$starttime,$endtime])->sum('money');
        $result['video_sum'] = $video_sum;
        //语音收益
        $audio_sum = Db::name('user_match_audio_log')->where('to_user_id','IN',$downall_uid)->where('updatetime','BETWEEN',[$starttime,$endtime])->sum('money');
        $result['audio_sum'] = $audio_sum;
        //私信收益
        $typing_sum= Db::name('user_match_typing_log')->where('to_user_id','IN',$downall_uid)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('money');
        $result['typing_sum'] = $typing_sum;
        //礼物收益
        $gift_sum  = Db::name('gift_user_typing')->where('user_to_id','IN',$downall_uid)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('getvalue');
        $result['gift_sum'] = $gift_sum;
        //总收益
        $result['total'] = $recharge_sum + $video_sum + $audio_sum + $typing_sum + $gift_sum;
        //在线用户
        $active_num = Db::name('user_active')->where('user_id','IN',$downall_uid)->where('requesttime','BETWEEN',[$starttime,$endtime])->count();
        $result['active_num'] = $active_num;
        //收益用户
        $a = Db::name('user_agentjewel_log')->where('user_id','IN',$downall_uid)->where('log_type',51)->where('createtime','BETWEEN',[$starttime,$endtime])->distinct(true)->field('user_id')->select();
        $a1 = array_column($a,'user_id');

        $b = Db::name('user_match_video_log')->where('to_user_id','IN',$downall_uid)->where('updatetime','BETWEEN',[$starttime,$endtime])->distinct(true)->field('to_user_id')->select();
        $b1 = array_column($b,'to_user_id');

        $c = Db::name('user_match_audio_log')->where('to_user_id','IN',$downall_uid)->where('updatetime','BETWEEN',[$starttime,$endtime])->distinct(true)->field('to_user_id')->select();
        $c1 = array_column($c,'to_user_id');

        $d = Db::name('user_match_typing_log')->where('to_user_id','IN',$downall_uid)->where('createtime','BETWEEN',[$starttime,$endtime])->distinct(true)->field('to_user_id')->select();
        $d1 = array_column($d,'to_user_id');

        $e = Db::name('gift_user_typing')->where('user_to_id','IN',$downall_uid)->where('createtime','BETWEEN',[$starttime,$endtime])->distinct(true)->field('user_to_id')->select();
        $e1 = array_column($e,'user_to_id');
//        dump($a);
//        dump($a1);
//        dump($b);
//        dump($b1);
//        dump($c);
//        dump($c1);
//        dump($d);
//        dump($d1);
//        dump($e);
//        dump($e1);

        $abcde = array_merge(array_merge($a1,$b1,$c1),$d1,$e1);
        $shouyi_user = array_flip(array_flip($abcde));
        $result['shouyi_num'] = count($shouyi_user);

        $this->success(1,$result);
    }

    //下拉选择代理
    private function select_agent(){
        //一级代理下的二级代理
        $down_agent_users = [];
        if($this->auth->group_id == 3){
            $down_agent_users = Db::name('user')->field('id,nickname,agentname')->where('agent_id',$this->auth->id)->select();
        }

        //追加自己。二级代理的,就只有自己
        $self_agent = [['id'=>$this->auth->id,'nickname'=>$this->auth->nickname,'agentname'=>$this->auth->agentname]];

        $down_all = array_merge($self_agent,$down_agent_users);

        //没有代理名的用昵称
        foreach($down_all as $key => $val){
            $val['agentname'] = $val['agentname'] ?: $val['nickname'];
            unset($val['nickname']);
            $down_all[$key] = $val;
        }

        return $down_all;
    }
    //成员管理
    public function all_user(){
        $result = [];

        //传入代理
        $down_all_id = input('down_all_id',0);

        //传入用户id
        $get_username = input('get_username',0);

        //一级代理  下拉  可选二级代理
        $down_all = $this->select_agent();
        $result['down_all'] = $down_all;
        //dump($down_all);

        //确定收益统计范围
        if(!empty($down_all_id)){
            $where = ['intro_uid' => $down_all_id];
            if($get_username){
                $where['username']= $get_username;
            }
            $downall_uid = Db::name('user')->where($where)->field('id,username,avatar,nickname,is_active,intro_uid,yaoqingtime')->order('yaoqingtime desc')->autopage()->select();//其下直推,A1下B0 或 B1下C1
            //$downall_uid[] = $down_all_id;
        }else{
            $down_uid = array_column($down_all,'id');
            $where = ['intro_uid' => ['IN',$down_uid]];
            if($get_username){
                $where['username']= $get_username;
            }
            //dump($down_uid);
            $downall_uid = Db::name('user')->where($where)->field('id,username,avatar,nickname,is_active,intro_uid,yaoqingtime')->order('yaoqingtime desc')->autopage()->select();//其下直推B0+代理下直推C1 或 C1
        }
        //dump($downall_uid);

        foreach($downall_uid as $key => &$user){
            $user['avatar'] = one_domain_image($user['avatar']);

            $user['agentname'] = Db::name('user')->where('id',$user['intro_uid'])->value('agentname');
            //邀请奖励收益
            $recharge_sum = Db::name('user_agentjewel_log')->where('user_id',$user['id'])->where('log_type',51)->sum('change_value');
            $user['recharge_sum'] = $recharge_sum;
            //视频收益
            $video_sum = Db::name('user_match_video_log')->where('to_user_id',$user['id'])->sum('money');
            $user['video_sum'] = $video_sum;
            //语音收益
            $audio_sum = Db::name('user_match_audio_log')->where('to_user_id',$user['id'])->sum('money');
            $user['audio_sum'] = $audio_sum;
            //私信收益
            $typing_sum= Db::name('user_match_typing_log')->where('to_user_id',$user['id'])->sum('money');
            $user['typing_sum'] = $typing_sum;
            //礼物收益
            $gift_sum  = Db::name('gift_user_typing')->where('user_to_id',$user['id'])->sum('getvalue');
            $user['gift_sum'] = $gift_sum;
            //总收益
            $user['total'] = $recharge_sum + $video_sum + $audio_sum + $typing_sum + $gift_sum;
        }

        $result['user_list'] = $downall_uid;

        $this->success(1,$result);
    }
    //成员收入
    public function user_shouyi(){
        $result = [];

        //日期
        $today = date('Y-m-d');
        $startday  = input('startday',$today);
        $starttime = strtotime($startday);
        $endtime   = $starttime + 86399;

        //传入代理
        $down_all_id = input('down_all_id',0);

        //传入用户id
        $get_username = input('get_username',0);

        $result['startday']    = $startday;

        //一级代理  下拉  可选二级代理
        $down_all = $this->select_agent();
        $result['down_all'] = $down_all;
        //dump($down_all);

        //确定收益统计范围
        if(!empty($down_all_id)){
            $where = ['intro_uid' => $down_all_id];
            if($get_username){
                $where['username']= $get_username;
            }
            $downall_uid = Db::name('user')->where($where)->field('id,username,avatar,nickname,intro_uid')->order('yaoqingtime desc')->autopage()->select();//其下直推,A1下B0 或 B1下C1
            //$downall_uid[] = $down_all_id;
        }else{
            $down_uid = array_column($down_all,'id');
            $where = ['intro_uid' => ['IN',$down_uid]];
            if($get_username){
                $where['username']= $get_username;
            }
            //dump($down_uid);
            $downall_uid = Db::name('user')->where($where)->field('id,username,avatar,nickname,intro_uid')->order('yaoqingtime desc')->autopage()->select();//其下直推B0+代理下直推C1 或 C1
        }
        //dump($downall_uid);
        foreach($downall_uid as $key => &$user){
            $user['agentname'] = Db::name('user')->where('id',$user['intro_uid'])->value('agentname');
            //邀请奖励收益
            $recharge_sum = Db::name('user_agentjewel_log')->where('user_id',$user['id'])->where('log_type',51)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('change_value');
//            $user['recharge_sum'] = $recharge_sum;
            //视频收益
            $video_sum = Db::name('user_match_video_log')->where('to_user_id',$user['id'])->where('updatetime','BETWEEN',[$starttime,$endtime])->sum('money');
//            $user['video_sum'] = $video_sum;
            //语音收益
            $audio_sum = Db::name('user_match_audio_log')->where('to_user_id',$user['id'])->where('updatetime','BETWEEN',[$starttime,$endtime])->sum('money');
//            $user['audio_sum'] = $audio_sum;
            //私信收益
            $typing_sum= Db::name('user_match_typing_log')->where('to_user_id',$user['id'])->where('createtime','BETWEEN',[$starttime,$endtime])->sum('money');
//            $user['typing_sum'] = $typing_sum;
            //礼物收益
            $gift_sum  = Db::name('gift_user_typing')->where('user_to_id',$user['id'])->where('createtime','BETWEEN',[$starttime,$endtime])->sum('getvalue');
//            $user['gift_sum'] = $gift_sum;
            //总收益
            $user['total'] = $recharge_sum + $video_sum + $audio_sum + $typing_sum + $gift_sum;

        }

        $result['user_list'] = $downall_uid;

        $this->success(1,$result);
    }

    //成员详情
    public function user_info(){
        $user_id = input('user_id',0);

        $user_info = Db::name('user')->where('id',$user_id)->field('id,username,avatar,nickname,intro_uid,yaoqingtime')->find();

        //邀请奖励收益
        $recharge_sum = Db::name('user_agentjewel_log')->where('user_id',$user_id)->where('log_type',51)->sum('change_value');
        $user_info['recharge_sum'] = $recharge_sum;
        //视频收益
        $video_sum = Db::name('user_match_video_log')->where('to_user_id',$user_id)->sum('money');
        $user_info['video_sum'] = $video_sum;
        //语音收益
        $audio_sum = Db::name('user_match_audio_log')->where('to_user_id',$user_id)->sum('money');
        $user_info['audio_sum'] = $audio_sum;
        //私信收益
        $typing_sum= Db::name('user_match_typing_log')->where('to_user_id',$user_id)->sum('money');
        $user_info['typing_sum'] = $typing_sum;
        //礼物收益
        $gift_sum  = Db::name('gift_user_typing')->where('user_to_id',$user_id)->sum('getvalue');
        $user_info['gift_sum'] = $gift_sum;
        //总收益
        $user_info['total'] = $recharge_sum + $video_sum + $audio_sum + $typing_sum + $gift_sum;

        $this->success(1,$user_info);
    }

    //成员详情-日历数据
    public function user_info_date(){
        $user_id = input('user_id',0);

        //日期
        $todaytime = strtotime(date('Y-m-d'));//今天凌晨

        $datelist = [];
        for($i=0;$i<$this->listrow;$i++){ //循环10次

            $day = ($this->page-1)*$this->listrow + $i;//往前推的天数

            $starttime = $todaytime - $day*86400;
            $endtime   = $starttime + 86399;

            $thisday = [
                'date' => date('m-d',$starttime),
            ];

            //邀请奖励收益
            $recharge_sum = Db::name('user_agentjewel_log')->where('user_id',$user_id)->where('log_type',51)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('change_value');
            $thisday['recharge_sum'] = $recharge_sum;
            //视频收益
            $video_sum = Db::name('user_match_video_log')->where('to_user_id',$user_id)->where('updatetime','BETWEEN',[$starttime,$endtime])->sum('money');
            $thisday['video_sum'] = $video_sum;
            //语音收益
            $audio_sum = Db::name('user_match_audio_log')->where('to_user_id',$user_id)->where('updatetime','BETWEEN',[$starttime,$endtime])->sum('money');
            $thisday['audio_sum'] = $audio_sum;
            //私信收益
            $typing_sum= Db::name('user_match_typing_log')->where('to_user_id',$user_id)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('money');
            $thisday['typing_sum'] = $typing_sum;
            //礼物收益
            $gift_sum  = Db::name('gift_user_typing')->where('user_to_id',$user_id)->where('createtime','BETWEEN',[$starttime,$endtime])->sum('getvalue');
            $thisday['gift_sum'] = $gift_sum;
            //总收益
            $thisday['total'] = $recharge_sum + $video_sum + $audio_sum + $typing_sum + $gift_sum;

            //
            $datelist[] = $thisday;
        }

        $this->success(1,$datelist);

    }









}