<?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 = model('wallet')->getwallet($this->auth->id);
        $this->success('success',$wallet);
    }



    //充值记录
    public function gold_recharge_log(){
        $map = [
            'user_id' => $this->auth->id,
            'log_type'=> 10,
        ];
        $list = Db::name('user_gold_log')->field('id,change_value,remain,createtime')->where($map)->order('id desc')->autopage()->select();

        $this->success('success',$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);
    }

    //追加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 hudong_money(){
        $map = [
            'user_id' => $this->auth->id,
            'log_type'=> ['IN',[21,22,23,54]],
        ];
        $list = Db::name('user_money_log')
            ->field('id,log_type,change_value,remain,remark,createtime')
            ->where($map)->order('id desc')->autopage()->select();

        $list = $this->list_appen_logtext($list);
        $this->success('success',$list);
    }
    //音聊收益,语聊间礼物
    public function party_money(){
        $map = [
            'user_id' => $this->auth->id,
            'log_type'=> 52,
        ];
        $list = Db::name('user_money_log')
            ->field('id,log_type,change_value,remain,remark,createtime')
            ->where($map)->order('id desc')->autopage()->select();

        $list = $this->list_appen_logtext($list);
        $this->success('success',$list);
    }
    //直播收益,直播间礼物
    public function livebc_money(){
        $map = [
            'user_id' => $this->auth->id,
            'log_type'=> 56,
        ];
        $list = Db::name('user_money_log')
            ->field('id,log_type,change_value,remain,remark,createtime')
            ->where($map)->order('id desc')->autopage()->select();

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

    //我的余额日志
    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,remark,createtime')
            ->where($map)->order('id desc')->autopage()->select();
        $list = $this->list_appen_logtext($list);

        $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')->where($map)->order('id desc')->autopage()->select();
        $list = $this->list_appen_logtext($list);

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

    //提现配置
    public function take_cash_config(){
        $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->find();

        $data = [
            'money' => model('wallet')->getwallet($this->auth->id,'money'),
            'alipay_account' => ($this->auth->idcard_status == 1 && isset($idcard_confirm['alipay_account'])) ? $idcard_confirm['alipay_account'] : '',
            'min' => 1,
            'max' => 1000,
        ];

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

    //提现
    public function take_cash(){
        $money = floatval(input_post('money',0));

        if(empty($money)){
            $this->error();
        }

        if(empty($this->auth->idcard_status)){
            $this->error('请先完成实名认证');
        }

        $check = Db::name('take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
        if($check){
            $this->error('您已经申请了提现,请等待审核');
        }

        $user_money = model('wallet')->getwallet($this->auth->id,'money');
        if($money > $user_money){
            $this->error('提现金额不能大于可提现余额');
        }

        $idcard_confirm = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->find();
        $data = [
            'user_id' => $this->auth->id,
            'number' => $money,
            'alipay_account' => $idcard_confirm['alipay_account'],
            'status' => 0,
            'createtime' => time(),
            'updatetime' => time(),
        ];

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

        //审核时候再扣，或者这里先扣，等需求方确认
        $this->success('申请成功请等待审核');
    }

    //提现记录
    public function take_cash_log(){
        $list = Db::name('take_cash')->where(['user_id'=>$this->auth->id])->autopage()->select();

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

}
