<?php


namespace app\api\controller\pabonus;

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

class User extends Base
{
    protected $noNeedLogin = ['getList'];
    protected $noNeedRight = ['*'];
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Pabonus;
    }

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

        $where = ['a.uniacid'=>UNIACID];

        if($pid > 0){
            $where['a.pid'] = $pid;
        }
        $abonus_nums = [];
        $list = Db::name("bbfx_pabonus_user")->alias("a")->join("bbfx_pabonus_goods b","a.pid=b.id","LEFT")->join("bbfx_user c","a.user_id=c.id","LEFT")->where($where)->field("a.*,b.name,c.nickname,c.avatar")->order("a.id desc")->limit(($page-1)*$limit,$limit)->select();
        foreach($list as $key => &$val){
            $val['avatar'] = cdnurl($val['avatar']);
            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
            if(!empty($abonus_nums[$val['pid']])){
                $abonus_num = $abonus_nums[$val['pid']];
            }
            else{
                $abonus_num = Db::name("bbfx_pabonus_user")->where(['uniacid'=>UNIACID,'pid'=>$val['pid']])->sum('num');
                $abonus_nums[$val['pid']] = $abonus_num;
            }
            $val['proportion'] = intval(round($val['num']/$abonus_num,2) * 100);
        }
        unset($val);

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