<?php
namespace app\api\controller\user;
use app\admin\model\order\StoreOrderCartInfo;
use app\admin\model\user\User;
use app\models\store\StoreOrder;
use app\models\user\UserCode;
use app\Request;
use crmeb\services\{UtilService,JsonService};
class CodeController
{
    private function init(Request $request)
    {
        $userinfo = $request->user();
        if(intval($userinfo['level'])<6)
        {
            return app('json')->fail('非总代不能进入');
        }
    }
    public function index(Request $request)
    {
        $this->init();
        $user = $request->user();
        $data['user'] = $user;
        $data['user_code'] = UserCode::where('uid',$request->uid())->where('code',$user['code'])->find();
       return app('json')->successful($data);

    }
    public function goods(Request $request)
    {
        $this->init();
        $user = $request->user();
        return app('json')->successful(UserCode::where('uid',$request->uid())->where('code',$user['code'])->select()->toArray());
    }
    public function order(Request $request)
    {
        $where = UtilService::postMore([
            ['page',1],
            ['limit',20],
        ],$request);
        $this->init();
        $user = $request->user();
        $count = StoreOrder::where('paid',1)->where('code',$user['code'])->count();
        $data = StoreOrder::where('paid',1)->where('code',$user['code'])->page($where['page'],$where['limit'])->select()->toArray();
        foreach ($data as &$v)
        {
            $v['user'] = User::where('uid',$v['uid'])->field('uid,real_name,nickname,avatar');
            $v['goods_name'] = StoreOrderCartInfo::getProductNameList($v['id']);
        }
        return app('json')->successful(compact('count','data'));
    }
    public function users(Request $request)
    {
        $this->init();
        $user = $request->user();
        return app('json')->successful(User::where('code',$user['code'])->order('team_cts desc')->page(input('page',1),input('limit',100))->field('uid,real_name,nickname,avatar,level,team_cts')->select()->toArray());
    }
}