first commit
This commit is contained in:
110
app/Models/Activity.php
Normal file
110
app/Models/Activity.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\BelongsToUser;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class Activity extends Model
|
||||
{
|
||||
|
||||
use BelongsToUser;
|
||||
|
||||
const TYPE_EXTEND = 1;
|
||||
const TYPE_SCOPE = 2;
|
||||
const TYPES = [
|
||||
self::TYPE_EXTEND => '延期',
|
||||
self::TYPE_SCOPE => '固定',
|
||||
];
|
||||
|
||||
const STATUS_OPEN = 1;
|
||||
const STATUS_CLOSE = 0;
|
||||
const STATUS = [
|
||||
self::STATUS_OPEN => '正常',
|
||||
self::STATUS_CLOSE => '关闭',
|
||||
];
|
||||
|
||||
protected $dates = [
|
||||
'start_at',
|
||||
'end_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* 默认加载的关联
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['rule'];
|
||||
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
self::creating(function ($model) {
|
||||
$model->code = 'ysd' . date('Ym') . mt_rand(11, 99);
|
||||
});
|
||||
}
|
||||
|
||||
//活动类型 固定期限 延期
|
||||
public function getTypeTextAttribute()
|
||||
{
|
||||
return self::TYPES[$this->type] ?? '未知';
|
||||
}
|
||||
|
||||
//活动状态
|
||||
public function getStatusTextAttribute()
|
||||
{
|
||||
return self::STATUS[$this->status] ?? '未知';
|
||||
}
|
||||
|
||||
//关联卡券规则 ysd-full100-10
|
||||
public function rule()
|
||||
{
|
||||
return $this->belongsTo(ActivityRule::class, 'activity_rule_id');
|
||||
}
|
||||
|
||||
//关联券表
|
||||
public function coupons()
|
||||
{
|
||||
return $this->hasMany(ActivityCoupon::class);
|
||||
}
|
||||
|
||||
//发券
|
||||
public function grant($mobile, $outletId = null)
|
||||
{
|
||||
|
||||
try {
|
||||
$code = 'YSD' . date('ymd') . mt_rand(100, 999);
|
||||
|
||||
if ($this->type == SELF::TYPE_EXTEND) {
|
||||
$start_at = now();
|
||||
$end_at = now()->addDays($this->days);
|
||||
} else {
|
||||
$start_at = $this->start_at->startOfDay();
|
||||
$end_at = $this->end_at->endOfDay();
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
$coupon = ActivityCoupon::create([
|
||||
'activity_id' => $this->id,
|
||||
'code' => $code,
|
||||
'mobile' => $mobile,
|
||||
'outletId' => $outletId,
|
||||
'full' => $this->rule->full,
|
||||
'price' => $this->rule->take,
|
||||
'start_at' => $start_at,
|
||||
'end_at' => $end_at,
|
||||
'status' => ActivityCoupon::STATUS_INIT,
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $coupon;
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user