<?php
namespace App\Repositories;

use App\Exceptions\ResponseException;
use App\Models\Category;
use App\Models\Organization;
use App\Models\MembersLog;
use App\Validators\Rules\MobileRule;
use Illuminate\Container\Container as Application;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Support\Facades\DB;

class OrganizationRepository extends BaseRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */


    public function __construct(Application $app)
    {
        parent::__construct($app);
    }

    public function model()
    {
        return Organization::class;
    }

    public function save($data, $id)
    {
        return $this->update($data, $id);
    }


    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    public function getOrganizationByAccount($account)
    {
        if (validator_check($account, 'email')) {
            return $this->model->where('email', $account)->first();
        }

        if (validator_check($account, new MobileRule())) {
            return $this->model->where('mobile', $account)->first();
        }

        return $this->model->where('username', $account)->orWhere('organization_code', $account)->first();
    }

    public function updateLoginStatus($company)
    {
        $company->last_login_ip=ip2long(request()->ip);
        $company->last_login_time=time();
        $company->refresh_time=time();
        $company->save();
    }

    public function getOrgInfoByID($id)
    {
        $orgInfo = $this->model->find($id, ['*']);
        if (!$orgInfo) {
            throw new ResponseException('参数错误', '400');
        }

        $landline = $orgInfo->landline_tel?explode('-', $orgInfo->landline_tel):array();
        if (is_array($landline)) {
            $orgInfo->landline_first = isset($landline[0]) ? ($landline[0] == 0 ? '' : $landline[0]) : "";
            $orgInfo->landline_next = isset($landline[1]) ? ($landline[1] == 0 ? '' : $landline[1]) : "";
            $orgInfo->landline_last = isset($landline[2]) ? ($landline[2] == 0 ? '' : $landline[2]) : "";
        }
        return $orgInfo;
    }

    public function getOrganizationColumn($id, $column = ['*'])
    {
        return $this->model->where('id', $id)->select($column)->first()->toArray();
    }

    public function changeEmail($id, $email)
    {
        $this->model->where('id', $id)->update(['email'=>$email, 'email_audit'=>1]);
    }

    public function checkUniaue($key, $value, $id = 0)
    {
        if (!Schema::hasColumn($this->model->getTable(), $key)) {
            return true;
        }
        if ($this->model->withTrashed()->where($key, $value)
            ->when($id>0, function ($query) use ($id) {
                return $query->where('id', '<>', $id);
            })->first()) {
            return false;
        }
        return true;
    }



}