Stakeholder.php 1.43 KB
<?php

namespace App\Models\Legal;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
 * Class Stakeholder
 * @package App\Models\Legal
 */
class Stakeholder extends BaseModel
{
    use HasFactory;

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function detail()
    {
        return $this->hasMany(StakeholderBankDetail::class, 'stakeholder_id', 'id');
    }

    /**
     * 获取用户的所有关联id
     * @param object $identifier
     * @return array
     */
    public static function stakeholderIds(object $identifier)
    {
        $stakeholders_model = Stakeholder::query();

        switch (intval($identifier->type)) {
            case 1:
                //个人
                $s_table = Stakeholder::table();
                $sbd_table = StakeholderBankDetail::table();
                $stakeholder = $stakeholders_model->where(['type' => 1, "{$sbd_table}.card_no"=>$identifier->identifier])
                    ->join($sbd_table, "{$s_table}.id", '=', "{$sbd_table}.stakeholder_id")
                    ->pluck("{$s_table}.id");
                break;
            case 2:
                $stakeholder = $stakeholders_model->where(['type' => 2, 'company_no' => $identifier->identifier])->pluck('id');
                break;
            default:
                return [];
                break;
        }

        return array_unique($stakeholder->toArray());
    }
}