<?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);
    }

    //我的余额日志
    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')->where($map)->order('id desc')->autopage()->select();
        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']] : '';
            }
        }

        $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();
        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']] : '';
            }
        }

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

    //提现配置
    public function take_cash_config(){
        $data = [
            'money' => model('wallet')->getwallet($this->auth->id,'money'),
            'alipay_account' => $this->auth->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->alipay_account)){
            $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('提现金额不能大于可提现余额');
        }

        $data = [
            'user_id' => $this->auth->id,
            'number' => $money,
            'alipay_account' => $this->auth->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);
    }

}