<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\queue_mode;

use app\api\controller\Base;
use think\Db;


class User extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\QueueMode;
    }

    public function index(){
        $rank = Db::name("bbfx_queue_mode_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'surplus_money'=>['>',0]])->value("rank");
        $money_total = Db::name("bbfx_queue_mode_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id']])->sum("money");
        $money_total = round($money_total,2);
        $money_wait = Db::name("bbfx_queue_mode_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'surplus_money'=>['>',0]])->sum("surplus_money");
        $money_wait = round($money_wait,2);
        $money = round($money_total - $money_wait,2);
        if(!empty($rank)){
            $rank_num = $rank - 1;
            
        }
        else{
            $rank = '未排队';
            $rank_num = 0;
        }

        $where = ['b.uniacid'=>UNIACID,'b.user_id'=>$this->user['id']];
        $list1 = Db::name("bbfx_queue_mode_user_record")->alias("a")->join("bbfx_queue_mode_user b","a.pid=b.id","LEFT")->where($where)->field("a.id,a.pid,a.money,a.order_id,a.createtime")->order("id desc")->select();
        $list2 = Db::name("bbfx_queue_mode_user")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'surplus_money'=>['>',0]])->field("info",true)->order("rank asc")->select();
        
        foreach($list1 as $key => &$val){
            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
        }
        unset($val);
        foreach($list2 as $key => &$val){
            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
        }
        unset($val);
        
        $result = [
            'rank'=>$rank,
            'money_total'=>$money_total,
            'money_wait'=>$money_wait,
            'money'=>$money,
            'rank_num'=>$rank_num,
            'list1'=>$list1,
            'list2'=>$list2
        ];
        __success("操作成功",$result);
    }
}