81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use App\Models\Server\CollageGroup;
|
|
use App\Contracts\Collageable;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
|
|
trait CanCollage
|
|
{
|
|
/**
|
|
* 添加拼团
|
|
* @param Collageable $collageable
|
|
* @param $collage_array
|
|
* @return CollageGroup
|
|
*/
|
|
public function addCollageGroup(Collageable $collageable,$collage_array):CollageGroup
|
|
{
|
|
$array = [
|
|
'm_id' => $this->getPrimaryId(),
|
|
];
|
|
$array = array_merge($array,$collage_array);
|
|
$collageGroup = new CollageGroup($array);
|
|
$collageable->collageGroup()->save($collageGroup);
|
|
return $collageGroup;
|
|
}
|
|
|
|
public function checkCollageGroup(Collageable $collageable){
|
|
$now = date('Y-m-d H:i:s');
|
|
$result = CollageGroup::where('collageable_id',$collageable->getPrimaryId())
|
|
->where('collageable_type',$this->getCurrentClass($collageable))
|
|
->where('start_time','>',$now)
|
|
->where('end_time', '<',$now)
|
|
->exists();
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function collageGroup(): MorphMany
|
|
{
|
|
return $this->morphMany(CollageGroup::class,'collageable');
|
|
}
|
|
|
|
|
|
public function getPrimaryId(): string
|
|
{
|
|
return (string)$this->getAttribute($this->primaryKey);
|
|
}
|
|
|
|
|
|
public function getCurrentClass(Collageable $collageable){
|
|
return get_class($collageable);
|
|
}
|
|
|
|
/**
|
|
* 是否开启拼团
|
|
*/
|
|
public function hasGroup(Collageable $collageable){
|
|
$now = date('Y-m-d H:i:s');
|
|
$result = CollageGroup::where('collageable_id',$collageable->getPrimaryId())
|
|
->where('collageable_type',$this->getCurrentClass($collageable))
|
|
->where('start_time','<=',$now)
|
|
->where('end_time','>',$now)
|
|
->exists();
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 拼团信息
|
|
*/
|
|
public function groupInformation(Collageable $collageable){
|
|
$result = CollageGroup::where('collageable_id',$collageable->getPrimaryId())
|
|
->where('collageable_type',$this->getCurrentClass($collageable))
|
|
->first();
|
|
return $result;
|
|
}
|
|
|
|
}
|