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

namespace app\admin\controller\bbfxshop\salequota;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class User extends Base
{

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

    public function getList(){
        $page = max(1,$this->param("page/d"));
        $limit = max(10,$this->param("limit/d"));
        $type = $this->param("type/d");
        $keyword = $this->param("keyword/s");

        $config = $this->model->getSet();

        $stock_list = $config['stock'];
        $types = [];
        foreach($stock_list as $key => $val){
            $types[] = $key;
        }

        $where = ["a.uniacid"=>UNIACID,'a.type'=>['in',$types]];
        if(!empty($keyword)){
            if($type == 2){
                $where['a.user_id'] = intval($keyword);
            }
            else{
                $where['b.nickname'] = ['like','%'.$keyword.'%'];
            }
        }
        $list = Db::name("bbfx_salequota_user_stock")
            ->alias("a")->join("bbfx_user b","a.user_id = b.id","LEFT")
            ->where($where)->order("a.id desc")
            ->field("a.*,b.nickname,b.avatar")
            ->limit(($page-1)*$limit,$limit)->select();
        
        foreach($list as $key=>&$val){
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            $val['avatar'] = cdnurl($val['avatar']);
            $val['type_text'] = $stock_list[$val['type']]['title'];
        }
        unset($val);

        $total = Db::name("bbfx_salequota_user_stock")
            ->alias("a")->join("bbfx_user b","a.user_id = b.id","LEFT")
            ->where($where)->count();

        $config = $this->model->getSet();
        $stock_list = $config['stock'];
        foreach($stock_list as $key => &$val){
            $val['stock_total'] = Db::name("bbfx_salequota_user_stock")->where(["type"=>$key])->sum("stock");
        }
        unset($val);

        __success("操作成功",['list'=>$list,'total'=>$total,'stock_list'=>$stock_list]);
    }
}