<?php

namespace plugin\admin\app\controller;

use Illuminate\Support\Arr;
use support\Request;
use support\Response;
use plugin\admin\app\model\Stream;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 用户流水 
 */
class StreamController extends Crud
{


    /**
     * @var Stream
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Stream;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('stream/index');
    }


    public function select(Request $request): Response
    {
        $param=$request->all();
        $data=Stream::query()->where(function ($query)use ($param){
            if(is_numeric(Arr::get($param,'money.1')) &&  is_numeric(Arr::get($param,'money.0'))){
                $query->whereBetween('money',[$param['money'][0],$param['money'][1]]);
            }elseif (is_numeric(Arr::get($param,'money.0'))){
                $query->where('money','>=',$param['money'][0]);
            }elseif (is_numeric(Arr::get($param,'money.1'))){
                $query->where('money','<=',$param['money'][1]);
            }

            if(Arr::get($param,'created_at.0') &&  Arr::get($param,'created_at.1')){
                $query->whereBetween('created_at',[$param['created_at'][0],$param['created_at'][1]]);
            }elseif (Arr::get($param,'created_at.0')){
                $query->where('created_at','>=',$param['created_at'][0]);
            }elseif (Arr::get($param,'created_at.1')){
                $query->where('created_at','<=',$param['created_at'][1]);
            }

            if(Arr::get($param,'type')){
                $query->where('type',$param['type']);
            }
            if(Arr::get($param,'mold')){
                $query->where('mold',$param['mold']);
            }
        });
        if(Arr::get($param,'mobile') || Arr::get($param,'mobile')){
            $data->whereExists(function ($query)use ($param){
                $query->from('wa_users')->whereRaw('wa_users.id=wa_stream.user_id');
                if(Arr::get($param,'user_name')){
                    $query->where('name','like','%'.$param['user_name'].'%');
                }
                if(Arr::get($param,'mobile')){
                    $query->where('mobile','like','%'.$param['mobile'].'%');
                }
            });
        }
        $data=$data->with('userData:id,name,mobile');
        if(Arr::get($param,'field')){
            $order='asc';
            if(Arr::get($param,'order')){
                $order='desc';
            }
            $data=$data->orderBy($param['field'],$order);
        }else{
            $data=$data->orderByDesc('id');
        }
        $data=$data->paginate(Arr::get($param,'limit',10))->toArray();

        $arr=[];
        foreach ($data['data'] as $k=>$v){
            $arr[]=[
                'user_name'=>$v['user_data']['name'],
                'user_mobile'=>$v['user_data']['mobile'],
                'money'     =>$v['money'],
                'type'=>streamType($v['type']).'--'.moldType($v['mold']),
                'created_at'=>$v['created_at'],
                'total_money'=>$v['total_money']

            ];
        }
        return json(['code' => 0, 'data' => $arr, 'msg' => 'ok','count'=>$data['total']]);
    }

    /** 流水类型
     * @param Response $response
     * @return Response
     */
    public function addselect(Response $response)
    {

        return json(['code' => 0, 'data' => streamType(), 'msg' => 'ok','count'=>count(streamType())]);
    }

    /** 钱包类型
     * @param Response $response
     * @return Response
     */
    public function addmoldselect(Response $response)
    {

        return json(['code' => 0, 'data' => moldType(), 'msg' => 'ok','count'=>count(moldType())]);
    }

}
