<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['banner'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function banner(){
        $list = Db::name('banner')->order('weigh desc')->select();
        $list = list_domain_image($list,['image']);
        $this->success(1,$list);
    }

    //我的邀请
    public function myintro(){
        $introme = Db::name('user')->field('id,nickname,avatar,jointime')->where('id',$this->auth->intro_uid)->find();
        $introme = info_domain_image($introme,['avatar']);

        $myintro = Db::name('user')->field('id,nickname,avatar,jointime')->where('intro_uid',$this->auth->id)->select();
        $myintro = list_domain_image($myintro,['avatar']);

        $rs = [
            'introme' => $introme,
            'myintro' => $myintro,
        ];

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

    //用户积分日志
    public function user_score_log(){
        $type = input('type',1);

        $where = [
            'user_id' => $this->auth->id,
        ];
        if($type == 1){
            $where['change_value'] = ['gt',0];
        }else{
            $where['change_value'] = ['lt',0];
        }

        $list = Db::name('user_score_log')->field('id,change_value,remark,createtime')->where($where)->order('id desc')->autopage()->select();
        if(!empty($list)){
            foreach($list as $key => &$val){
                $val['createdate'] = date('Y-m-d',$val['createtime']);
            }
        }

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

    //积分提现配置
    public function takecash_config(){
        $list = Db::name('exchmoney_config')->order('weigh desc')->select();
        $this->success(1,$list);
    }

    //积分提现
    public function takecash(){
        $con_id = input('config_id',0);
        $info = Db::name('exchmoney_config')->where('id',$con_id)->find();

        Db::startTrans();

        //日志
        $data = [
            'user_id' => $this->auth->id,
            'score'   => $info['score'],
            'money'   => $info['money'],
            'createtime' => time(),
            'status' => 0,
        ];
        $order_id = Db::name('user_exchangemoney_log')->insertGetId($data);
        if(!$order_id){
            Db::rollback();
            $this->error('申请提现失败');
        }

        //扣钱
        $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'score',-$info['score'],4,'提现','user_exchangemoney_log',$order_id);
        if($rs_wallet['status'] == false){
            Db::rollback();
            $this->error($rs_wallet['msg']);
        }

        Db::commit();
        $this->success('申请提现成功',$order_id);

    }

}