<?php

namespace App\Models;

use App\Facade\TableName;
use App\Http\helper\NumConfig;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;

class Cpf extends Model
{

    /**
     * @param $uid
     * @param int $type 1 获取cpf 2 获取uids
     * @return array|false
     */
    public static function getCpf($uid,$type=2){
        $cpf = DB::connection('read')->table(TableName::agent() . 'mapping_cpf')
            ->where('UserID', $uid)
            ->pluck('cpf')->toArray();

        if($type == 1){
            return  $cpf;
        }
        if($cpf){
            return DB::connection('read')->table(TableName::agent() . 'mapping_cpf')
                ->whereIn('cpf', $cpf)
                ->distinct()->pluck('UserID')->toArray();
        }
        return false;
    }
    /**
     * @param $uid
     * @param int $type 1 获取cpf 2 获取uids
     * @return array|false
     */
    public static function getCpfCount($uid){
        $cpf = DB::connection('read')->table(TableName::agent() . 'mapping_cpf')
            ->where('UserID', $uid)
            ->pluck('cpf')->toArray();

        if($cpf){
            return DB::connection('read')->table(TableName::agent() . 'mapping_cpf')
                ->whereIn('cpf', $cpf)
                ->select('UserID')->distinct()->count();
        }
        return 0;
    }

    public static function insertCpf($uid,$cpf){
        $cpf=strval($cpf);
        $first = DB::connection('read')->table(TableName::agent() . 'mapping_cpf')
            ->where([['UserID',$uid],['cpf',$cpf]])
            ->first();
        if(!$first){
            DB::connection('read')->table(TableName::agent() . 'mapping_cpf')->insert(['UserID' => $uid,'cpf' => $cpf]);
        }
        return true;
    }

}
