<?php
/**
 * File UserController.php
 *
 * @author Tuan Duong <bacduong@gmail.com>
 * @package Laravue
 * @version 1.0
 */

namespace App\Http\Controllers;

use App\Laravue\Models\WxUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Validator;

class WxuserController extends Controller
{
    //获取wx用户列表
    public function wxlist(Request $request){
        /*
         * get请求返回wx用户列表
         * */

        $limit = $request->input('limit');
        $pagenNum = $limit * ($request->input('page')-1);//页数
        if($pagenNum === '' || $limit == ''){
            return $this->jsonErrorData(105,'页数或limit不能为空');
        }
        $where = [];
        $keys = $request->all();
        if(isset($keys['sex'])) {
            $keys['sex'] = $keys['sex'] == 3 ? 0 : $keys['sex'];
            $where[] = ['sex', '=', $keys['sex']];
        }
        if(isset($keys['state'])) {
            $where[] = ['state', '=', $keys['state']];
        }
        if(isset($keys['mobile'])) {
            $where[] = ['mobile', 'like', "%$keys[mobile]%"];
        }
        $city = '';
        if(isset($keys['city'])) {
            $city = $keys['city'];
        }
        $cont =  WxUser::where($where)
            ->whereRaw("concat(country, province, city) like '%{$city}%'")->count();
        $Wxuser = WxUser::select('nickname','id','openid','sex','province','city','mobile','country','headimgurl','created_at','state')
            ->where($where)
            ->whereRaw("concat(country, province, city) like '%{$city}%'")
            ->offset($pagenNum)
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->get();
        $data = ['Wxuser'=>$Wxuser,'cont'=>$cont];
        if ($Wxuser){
            return $this->jsonSuccessData($data);
        }else{
            return $this->jsonErrorData('105','获取数据失败');
        }
    }
    //用户搜索
    public  function seek (Request $request){
        $keys = $request->all();
        $where = [];

        if(isset($keys['sex'])) {
            $keys['sex'] = $keys['sex'] == 3 ? 0 : $keys['sex'];
            $where[] = ['sex', '=', $keys['sex']];
        }
        if(isset($keys['state'])) {
            $where[] = ['state', '=', $keys['state']];
        }
        if(isset($keys['mobile'])) {
            $where[] = ['mobile', 'like', "%$keys[mobile]%"];
        }
        $city = '';
        if(isset($keys['city'])) {
           $city = $keys['city'];
        }
        $datas = DB::table('wx_user')
            ->where($where)
            ->whereRaw("concat(country, province, city) like '%{$city}%'")
            ->limit($keys['limit'])
            ->offset($keys['limit'] * ($keys['page'] - 1))
            ->orderBy('id', 'DESC')
            ->get();
        $cont = DB::table('wx_user')->where($where)->whereRaw("concat(country, province, city) like '%{$city}%'")->count();
        $data = ['Wxuser'=>$datas,'cont'=>$cont];
        return $this->jsonSuccessData($data);
    }
}