'延期', 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(100, 999); }); } //活动类型 固定期限 延期 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); } /** * Notes: 关联可发券渠道 * @Author: 玄尘 * @Date : 2020/8/21 11:09 */ public function grants() { return $this->hasMany(ActivityGrant::class); } /** * Notes: 关联可核销渠道 * @Author: 玄尘 * @Date : 2020/8/21 11:09 */ public function verifications() { return $this->hasMany(ActivityVerification::class); } public function canGrant() { return $this->status; } //发券 public function grant($mobile, $outletId = null) { try { $code = '66406' . date('ymd') . mt_rand(100000, 999999); 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(); } } }