<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 22/04/26
 * Time: 14:41
 */

namespace App\Services\Api;

use App\Models\User;

class UserService
{
   /**
     * 模型
     *
     * @var object
     * 
     */
    private $Model;

    /**
     * 初始化
     * @access public
     */
    public function __construct()
    {
      if( ! $this->Model) $this->Model = new User;
    }

    public function search(array $search,array $where = [],array $orderby=['id'=>'desc'],$pagesize=10)
    {
        //查询
        $currentQuery = $this->Model;
        if(isset($search['keyword']) && ! empty($search['keyword'])) {
            $keywords = '%' . $search['keyword'] . '%';
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('id'  , 'like', $keywords)
                ->orwhere('nickname', 'like', $keywords)
                ->orwhere('password', 'like', $keywords)
                ->orwhere('email', 'like', $keywords)
                ->orwhere('mobile', 'like', $keywords);
            });
        }

        //条件
        if($where && is_array($where)){
            foreach ($where as $field => $value){
              $currentQuery = $currentQuery->where($field, $value);
            }
        }
        //排序
        if($orderby && is_array($orderby)){
            foreach ($orderby as $field => $value){
                $currentQuery = $currentQuery->orderBy($field, $value);
            }
        }
        
        $currentQuery = $currentQuery->paginate($pagesize);
        $currentQuery->makeHidden('password','deleted_at');

        return $currentQuery;
    }

    //查询用户信息
    public function show($id) {
        return $this->Model->findOrFail($id);
    }

    /**
     * 添加
     * @param unknown $data
     */
    public function create($data)
    {
        return $this->Model->create($data);
    }

    /**
     * 更新
     * @param unknown $id
     * @param unknown $data
     */
    public function update($id,$data)
    {
        $obj = $this->Model->findOrFail($id);
        if(!$obj) {
            return false;
        }
        $ok = $obj->update($data);
        return $ok;
    }
    /**
     * 更新状态
     */
    public function updateStatus($id,$status) {
        $data = $this->Model->findOrFail($id);
        $data->status = $status;
        return $data->save();
    }

    /**
     * 删除
     * @param unknown $id
     */
    public function destroy($id)
    { 
        $data = $this->Model->findOrFail($id);
        $data->deleted_at = date('Y-m-d H:i:s');
        return $data->save();
    }

    //获取用户信息（需要更多信息的话在这里追加）
    public static function getUserInfoById($id)
    {
        $info = User::query()->where('id', $id)->whereNull('deleted_at')->first();
        $info->makeHidden(['updated_at', 'deleted_at']);

        $info = !empty($info) ? $info->toArray() : [];

        return $info;
    }

    //通过手机号查用户
    public static function checkUserByMobile($mobile)
    {
        $user = User::where('mobile', $mobile)->whereNull('deleted_at')->first();
        if ($user) {
            return true;
        }

        return false;
    }

    //通过邮箱查找用户
    public static function checkUserByEmail($email)
    {
        $user = User::where('email', $email)->whereNull('deleted_at')->first();
        if ($user) {
            return true;
        }

        return false;
    }

    //收藏数+1
    public static function collectNumInc($userId)
    {
        User::query()->where('id', $userId)->increment('collect_num');
    }

    //收藏数-1
    public static function collectNumDec($userId)
    {
        User::query()->where('id', $userId)->decrement('collect_num');
    }




}