You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

131 lines
4.8 KiB

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class OverSpeedRedEquipment extends Model
{
use HasFactory;
use HasFactory, SoftDeletes;
protected $table = 'overspeedred_equipment';
protected $dates = ['deleted_at'];
protected $fillable = [
'serialnumber',
'brand',
'model',
'custodyunit_id',
'assetownership_id',
'buydate',
'activatedate',
'locationid',
'location',
'comment',
'precinct',
'station',
];
// append fields
protected $appends = [
'custodyunit_name',
'assetownership_name',
'short_name',
];
// custome fields
public function getShortNameAttribute()
{
$match = [
'桃園區國際路二段與大興西路三段路口' => '國際路二段與大興西三段',
'桃園區復興路與民生路口' => '復興路與民生路',
'桃園區三民路二段與春日路口' => '三民二段與春日路',
'桃園區中山路與上海路口' => '中山路與上海路',
'桃園區三民路二段與民生路口' => '三民路二段與民生路',
'桃園區春日路與民光東路口' => '春日路與民光東路',
'桃園區三民路三段與永安路口' => '三民路三段與永安路',
'蘆竹區中正路與南山路一段路口' => '蘆竹中正路與南山路一段',
'八德區中華路與永豐路口' => '八德中華路與永豐路',
'八德區建德路與豐德路口' => '八德建德路與豐德路',
'大溪區台七乙線9K+100' => '台七乙線9K+100處',
'桃園區大興西路三段與正光路口(往交流道)' =>'大興西與正光路',
'龍潭區台3線50.8K處(往龍潭)' =>'龍潭台3線50.8K',
'龜山區萬壽路二段472號前' =>'龜山萬壽路472號前',
'中壢區民權路五段65號前' =>'民權路65號前',
'楊梅區台1線50K+536處' =>'楊梅台1線50K+536',
'大溪區仁和路2段517巷' =>'大溪仁和路2段517巷',
'觀音區濱海路廣興段845號前' =>'觀音濱海路與廣興段',
'龍潭區中正路三林段330號' =>'龍潭中正路三林段',
'桃園區中山路輿上海路口' =>'中山路與上海路口',
];
$name = $match[$this->location] ?? $this->location;
return $name;
}
public function getCustodyunitNameAttribute()
{
$name = CustodyUnit::find($this->custodyunit_id)->name ?? '無';
return $name;
}
public function getAssetownershipNameAttribute()
{
$name = AssetOwnership::find($this->assetownership_id)->name ?? '無';
return $name;
}
// 有關聯到設備保管單位
public function custodyunit()
{
return $this->belongsTo(CustodyUnit::class, 'custodyunit_id', 'id');
}
// 有關聯到設備財產權屬
public function assetownership()
{
return $this->belongsTo(AssetOwnership::class, 'assetownership_id', 'id');
}
// 有關聯到
public static function getTypeStation($options = [])
{
// 获取所有的类型
$types = self::select('station')->distinct()->pluck('station')->toArray();
// 构建一个关联数组来存储每个类型对应的代码
$typeCodes = [];
foreach ($types as $type) {
if ($options != []){
if (in_array($type, $options)) {
$codes = self::where('station', $type)->pluck('serialnumber')->toArray();
$typeCodes[$type] = $codes;
}
}else{
$codes = self::where('station', $type)->pluck('serialnumber')->toArray();
$typeCodes[$type] = $codes;
}
}
return $typeCodes;
}
public static function getTypePrecinct($options = [])
{
// 获取所有的类型
$types = self::select('precinct')->distinct()->pluck('precinct')->toArray();
// 构建一个关联数组来存储每个类型对应的代码
$typeCodes = [];
foreach ($types as $type) {
// $type in $options
if ($options != []){
if (in_array($type, $options)) {
$codes = self::where('precinct', $type)->pluck('serialnumber')->toArray();
$typeCodes[$type] = $codes;
}
}else{
$codes = self::where('precinct', $type)->pluck('serialnumber')->toArray();
$typeCodes[$type] = $codes;
}
}
return $typeCodes;
}
}