<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use app\common\model\wallet;
/**
 * 用户钱包
 *
 * @icon fa fa-circle-o
 */
class Userwallet extends Backend
{
    
    /**
     * Userwallet模型对象
     * @var \app\admin\model\Userwallet
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Userwallet;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['username','nickname','mobile']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 充值金币
     */
    public function updategold(){
        $id = input('id');
        $info = Db::name('user_wallet')
            ->where('id',$id)
            ->find();

        if ($this->request->isPost()) {

            $user_id = input_post('user_id');
            $gold = input_post('gold', 0, 'intval');

            $user_info = Db::name('user')->find($user_id);
            if (!$user_info) {
                $this->error('用户不存在');
            }
            if ($gold > 0) {
                $is_pay_get = 1;
            } else {
                $is_pay_get = 0;
            }
            Db::startTrans();

            $rs = model('wallet')->lockChangeAccountRemain($user_id,0,'gold',$gold,1, '系统调节','',0, $is_pay_get);
            if($rs['status'] === false){
                Db::rollback();
                $this->error($rs['msg']);
            }

            //若有上级,给上级返利
            if ($user_info['intro_uid'] && $gold > 0) {
                //获取返利比率
                $agent_info = Db::name('user')->where(['id' => $user_info['intro_uid']])->field('is_agent,h_intro_recharge_rebate_rate')->find();
                $intro_recharge_rebate_rate = ($agent_info['is_agent'] == 1) ? $agent_info['h_intro_recharge_rebate_rate'] : (int)config('site.intro_recharge_rebate_rate');
                if ($intro_recharge_rebate_rate > 0 && $intro_recharge_rebate_rate <= 100) {
                    // 添加赠送用户余额
                    $money_to_gold = config('site.money_to_gold');
                    $money = bcdiv($gold,$money_to_gold,2);
                    //上级获得金额数量
                    $intro_uid_money = number_format($money * $intro_recharge_rebate_rate / 100, 2, '.', '');
                    if ($intro_uid_money > 0) {
                        $intro_result = model('wallet')->lockChangeAccountRemain($user_info['intro_uid'],$user_id,'money',$intro_uid_money,65,'邀请人充值奖励');
                        if($intro_result['status']===false)
                        {
                            Db::rollback();
                            $this->error($intro_result['msg']);
                        }
                    }
                }
            }

            Db::commit();
            $this->success('充值完成');
        }

        $this->assign('row',$info);
        return $this->view->fetch();
    }
    /**
     * 充值积分
     */
    public function updatemoney(){
        $id = input('id');
        $info = Db::name('user_wallet')
            ->where('id',$id)
            ->find();

        if ($this->request->isPost()) {

            $user_id = input_post('user_id');
            $money = input_post('money', 0, 'intval');

            $user_info = Db::name('user')->find($user_id);
            if (!$user_info) {
                $this->error('用户不存在');
            }

            Db::startTrans();

            $rs = model('wallet')->lockChangeAccountRemain($user_id,0,'money',$money,2, '系统调节','',0, 0);
            if($rs['status'] === false){
                Db::rollback();
                $this->error($rs['msg']);
            }

            Db::commit();
            $this->success('充值完成');
        }

        $this->assign('row',$info);
        return $this->view->fetch();
    }

    /**
     * 充值钻石
     */
    public function updatejewel(){
        $id = input('id');
        $info = Db::name('user_wallet')
            ->where('id',$id)
            ->find();

        if ($this->request->isPost()) {

            $user_id = input_post('user_id');
            $jewel = input_post('jewel', 0, 'intval');

            $user_info = Db::name('user')->find($user_id);
            if (!$user_info) {
                $this->error('用户不存在');
            }

            Db::startTrans();

            $rs = model('wallet')->lockChangeAccountRemain($user_id,0,'jewel',$jewel,3, '系统调节','',0, 0);
            if($rs['status'] === false){
                Db::rollback();
                $this->error($rs['msg']);
            }

            Db::commit();
            $this->success('充值完成');
        }

        $this->assign('row',$info);
        return $this->view->fetch();
    }

}