<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
//use app\common\model\wallet;
/**
 * 用户钱包
 */
class Userwallet extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //我的钱包余额
    public function my_wallet(){
        $wallet = Db::name('user_wallet')->field('money,gold,vip_endtime')->where(['user_id' => $this->auth->id])->find();
        $wallet['is_vip'] = $wallet['vip_endtime'] > time() ? 1 : 0;
        $this->success('success',$wallet);
    }

    //我的积分日志
    public function my_money_log(){
        $type = input_post('type',0);

        $map = [
           'user_id' => $this->auth->id,
        ];
//        if($type){
//            $map['log_type'] = $type;
//        }

        $list = Db::name('user_money_log')
            ->field('id,log_type,change_value,remain,createtime, remark log_text')
            ->where($map)->order('id desc')->autopage()->select();
//        $list = $this->list_appen_logtext($list);
        if(!empty($list)){
            foreach($list as $key => $val){
                $list[$key]['createtime'] = date('Y.m.d H:i', $val['createtime']);
            }
        }

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

    //金币日志
    public function my_gold_log(){
        $type = input_post('type',0);

        $map = [
            'user_id' => $this->auth->id,
        ];
        if($type){
            $map['log_type'] = $type;  //兑换记录
        }

        $list = Db::name('user_gold_log')
            ->field('id,log_type,change_value,remain,createtime, remark log_text')
            ->where($map)->order('id desc')->autopage()->select();
//        $list = $this->list_appen_logtext($list);
        if(!empty($list)){
            foreach($list as $key => $val){
                $list[$key]['createtime'] = date('Y.m.d H:i', $val['createtime']);
            }
        }

        $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]['createtime'] = date('Y.m.d H:i', $val['createtime']);
                $list[$key]['log_text'] = isset($conf[$val['log_type']]) ? $conf[$val['log_type']] : '';
            }
        }
        return $list;
    }


    //我的收益,三个数据
    public function my_income_count(){
        //累计收益,不计来源
        $map = [
            'user_id' => $this->auth->id,
            //'log_type'=> ['IN',[21,22,23]],
        ];
        $income_sum = Db::name('user_money_log')->where($map)->sum('change_value');

        //可提现总收益
        $money_remain = model('wallet')->getwallet($this->auth->id,'money');

        //今日收益
        $start = strtotime(date('Y-m-d'));
        $end = $start + 86399;
        $map['createtime'] = ['between',[$start,$end]];
        $today_income_sum = Db::name('user_money_log')->where($map)->sum('change_value');

        $result = [
            'income_sum' => $income_sum,
            'money_remain' => $money_remain,
            'today_income_sum' => $today_income_sum,
        ];

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


    //每日数据
    public function todayincome() {
        $start = input('time', 0, 'strtotime'); //时间: 2022-09-30
        if (!$start) {
            $start = strtotime(date('Y-m-d')); //默认今日
        }
        $end = $start + 86399;

        //收益type
        $profit_type = [21,22,23,54,58,60,67];

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

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

        $today_video_income = Db::name('user_match_video_log')->where($map)->sum('money');
        $today_video_income = $today_video_income ? : 0;

        $today_video_user = Db::name('user_match_video_log')->where($map)->column('user_id');
        $today_video_user = array_unique($today_video_user);
        $today_video_user_num = count($today_video_user);
        //今日语音时长/收益/人数
        $today_audio_min = Db::name('user_match_audio_log')->where($map)->sum('call_minutes');

        $today_audio_income = Db::name('user_match_audio_log')->where($map)->sum('money');
        $today_audio_income = $today_audio_income ? : 0;

        $today_audio_user = Db::name('user_match_audio_log')->where($map)->column('user_id');
        $today_audio_user = array_unique($today_audio_user);
        $today_audio_user_num = count($today_audio_user);
        //今日时长/收益/人数
        $today_time = $today_video_min + $today_audio_min;
        $today_time_income = number_format($today_video_income + $today_audio_income, 2, '.', '');
        $today_user_num = $today_video_user_num + $today_audio_user_num;

        //礼物收益
        $gitft_map = [
            'log_type' => ['IN',[54, 58, 60]],
            'user_id' => $this->auth->id,
            'createtime' => ['between',[$start,$end]],
        ];
        $today_gift_income = Db::name('user_money_log')->where($gitft_map)->sum('change_value');

        //私聊时长/收益/人数
        $today_chat_min = Db::name('user_match_typing_log')->where($map)->count('id');

        $today_chat_income = Db::name('user_match_typing_log')->where($map)->sum('money');
        $today_chat_income = $today_chat_income ? : 0;

        $today_chat_user = Db::name('user_match_typing_log')->where($map)->column('user_id');
        $today_chat_user = array_unique($today_chat_user);
        $today_chat_user_num = count($today_chat_user);

        //任务收益 客户要求由money改为gold
        $today_task_income = Db::name('user_gold_log')->where(['user_id' => $this->auth->id, 'log_type' => 67, 'createtime' => ['between',[$start,$end]]])->sum('change_value');
        $today_task_income = $today_task_income ? : 0;


        $result = [
            'today_profit' => $today_profit,
            'today_time_income' => $today_time_income,
            'today_gift_income' => $today_gift_income,
            'today_chat_income' => $today_chat_income,
            'today_task_income' => $today_task_income,
            'today_time' => $today_time,
            'today_user_num' => $today_user_num,
            'today_chat_min' => $today_chat_min,
            'today_chat_user_num' => $today_chat_user_num
        ];

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

    //每日数据礼物列表
    public function todaygiftlist() {
        $start = input('time', 0, 'strtotime'); //时间: 09-30
        if (!$start) {
            $start = strtotime(date('Y-m-d')); //默认今日
        }
        $end = $start + 86399;

        //今日收益
        $today_map = [
            'log_type' => ['IN', [54,/*58,*/60]],
            'user_id' => $this->auth->id,
            'createtime' => ['between',[$start,$end]],
        ];
        $list = Db::name('user_money_log')->where($today_map)->autopage()->order('id desc')->select();
        if (!$list) {
            $this->success('success', $list);
        }

        $mt_gift_user_typing = Db::name('gift_user_typing'); //54
//        $mt_gift_user_greet = Db::name('gift_user_greet'); //58
        $mt_gift_user_dongtai = Db::name('gift_user_dongtai'); //60
        foreach ($list as &$v) {
            if ($v['log_type'] == 54) {
                $table = $mt_gift_user_typing;
            }/* elseif ($v['log_type'] == 58) {
                $table = $mt_gift_user_greet;
            } */else {
                $table = $mt_gift_user_dongtai;
            }
            $info = $table->where(['id' => $v['table_id']])->find();

            $v['gift_name'] = $info['gift_name'];
            $v['number'] = $info['number'];
        }

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


}