增加发券规则

This commit is contained in:
2020-10-29 10:08:59 +08:00
parent a54e71a7ce
commit 8b481f76df
4 changed files with 546 additions and 417 deletions

View File

@@ -1,247 +1,267 @@
<?php <?php
namespace App\Admin\Controllers\Activity; namespace App\Admin\Controllers\Activity;
use App\Admin\Renderable\Activity\Grants; use App\Admin\Renderable\Activity\Grants;
use App\Models\Activity; use App\Models\Activity;
use App\Models\ActivityGrant; use App\Models\ActivityGrant;
use App\Models\ActivityRule; use App\Models\ActivityRule;
use App\Models\User; use App\Models\User;
use Encore\Admin\Controllers\HasResourceActions; use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form; use Encore\Admin\Form;
use Encore\Admin\Grid; use Encore\Admin\Grid;
use Encore\Admin\Layout\Content; use Encore\Admin\Layout\Content;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
use Illuminate\Routing\Controller as AdminController; use Illuminate\Routing\Controller as AdminController;
class IndexController extends AdminController class IndexController extends AdminController
{ {
use HasResourceActions; use HasResourceActions;
protected $title = '活动管理'; protected $title = '活动管理';
/** /**
* Get content title. * Get content title.
* @return string * @return string
*/ */
protected function title() protected function title()
{ {
return $this->title; return $this->title;
} }
/** /**
* Index interface. * Index interface.
* @param Content $content * @param Content $content
* @return Content * @return Content
*/ */
public function index(Content $content) public function index(Content $content)
{ {
return $content return $content
->title($this->title()) ->title($this->title())
->description($this->description['index'] ?? trans('admin.list')) ->description($this->description['index'] ?? trans('admin.list'))
->body($this->grid()); ->body($this->grid());
} }
/** /**
* Edit interface. * Edit interface.
* @param mixed $id * @param mixed $id
* @param Content $content * @param Content $content
* @return Content * @return Content
*/ */
public function edit($id, Content $content) public function edit($id, Content $content)
{ {
return $content return $content
->title($this->title()) ->title($this->title())
->description($this->description['edit'] ?? trans('admin.edit')) ->description($this->description['edit'] ?? trans('admin.edit'))
->body($this->form($id)->edit($id)); ->body($this->form($id)->edit($id));
} }
/** /**
* Create interface. * Create interface.
* @param Content $content * @param Content $content
* @return Content * @return Content
*/ */
public function create(Content $content) public function create(Content $content)
{ {
return $content return $content
->title($this->title()) ->title($this->title())
->description($this->description['create'] ?? trans('admin.create')) ->description($this->description['create'] ?? trans('admin.create'))
->body($this->form()); ->body($this->form());
} }
protected function grid() protected function grid()
{ {
$grid = new Grid(new Activity); $grid = new Grid(new Activity);
$grid->model()->withCount('coupons');
$grid->actions(function ($actions) {
$actions->disableView(); $grid->actions(function ($actions) {
}); $actions->disableView();
});
$grid->filter(function ($filter) {
$filter->column(1 / 2, function ($filter) { $grid->filter(function ($filter) {
$filter->equal('status', '状态')->select(Activity::STATUS); $filter->column(1 / 2, function ($filter) {
$filter->equal('type', '类型')->select(Activity::TYPES); $filter->equal('status', '状态')->select(Activity::STATUS);
$filter->equal('type', '类型')->select(Activity::TYPES);
$users = User::whereHas('identity', function ($query) {
$query->where('identity_id', 1); $users = User::whereHas('identity', function ($query) {
})->get()->pluck('nickname', 'id'); $query->where('identity_id', 1);
})->get()->pluck('nickname', 'id');
});
});
$filter->column(1 / 2, function ($filter) {
$filter->between('start_at', '开始时间')->datetime(); $filter->column(1 / 2, function ($filter) {
$filter->between('end_at', '结束时间')->datetime(); $filter->between('start_at', '开始时间')->datetime();
}); $filter->between('end_at', '结束时间')->datetime();
});
});
});
$grid->column('id', '#ID#');
$grid->column('title', '活动名称'); $grid->column('id', '#ID#');
$grid->column('code', '活动编号'); $grid->column('title', '活动名称');
$grid->column('rule.code', '卡券规则'); $grid->column('code', '活动编号');
$grid->column('类型')->display(function () { $grid->column('total', '可发券总数');
return $this->type_text; $grid->column('coupons_count', '可发券总数');
}); $grid->column('rule.code', '卡券规则');
$grid->column('类型')->display(function () {
$grid->column('days', '延期(天)'); return $this->type_text;
$grid->column('rule.full', '满足金额'); });
$grid->column('rule.take', '扣除金额');
$grid->column('发券') $grid->column('days', '延期(天)');
->display(function () { $grid->column('rule.full', '满足金额');
return $this->grants->pluck('user_nickname'); $grid->column('rule.take', '扣除金额');
}) $grid->column('发券')
->label() ->display(function () {
->hide(); return $this->grants->pluck('user_nickname');
})
$grid->column('核券') ->label()
->display(function () { ->hide();
return $this->verifications->pluck('user_nickname');
}) $grid->column('核券')
->label() ->display(function () {
->hide(); return $this->verifications->pluck('user_nickname');
})
$grid->column('开始时间')->display(function () { ->label()
return $this->type == Activity::TYPE_SCOPE ? $this->start_at->format('Y-m-d') : '---'; ->hide();
});
$grid->column('结束时间')->display(function () { $grid->column('开始时间')->display(function () {
return $this->type == Activity::TYPE_SCOPE ? $this->end_at->format('Y-m-d') : '---'; return $this->type == Activity::TYPE_SCOPE ? $this->start_at->format('Y-m-d') : '---';
}); });
$grid->column('结束时间')->display(function () {
$grid->status('状态')->switch([ return $this->type == Activity::TYPE_SCOPE ? $this->end_at->format('Y-m-d') : '---';
'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'], });
'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
]); $grid->status('状态')->switch([
'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
$grid->column('created_at', '创建时间'); 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
]);
return $grid;
} $grid->column('created_at', '创建时间');
/** return $grid;
* Make a form builder. }
* @return Form
*/ /**
protected function form($id = '') * Make a form builder.
{ * @return Form
$form = new Form(new Activity); */
protected function form($id = '')
$form->text('title', '活动名称')->required(); {
$form->textarea('description', '活动说明')->required(); $form = new Form(new Activity);
$form->select('activity_rule_id', '所属规则') $form->text('title', '活动名称')->required();
->options(function ($option, $info) { $form->textarea('description', '活动说明')->required();
return ActivityRule::where('status', 1)->pluck('title', 'id');
}) $form->select('activity_rule_id', '所属规则')
->required(); ->options(function ($option, $info) {
return ActivityRule::where('status', 1)->pluck('title', 'id');
$form->radio('type', '类型') })
->options(Activity::TYPES) ->required();
->when(Activity::TYPE_EXTEND, function (Form $form) {
$form->number('days', '延期天数')->default(60)->help('到期日期=发券日期+延期天数'); $form->number('total', '数')
}) ->help('可发券总数')
->when(Activity::TYPE_SCOPE, function (Form $form) { ->default(10000)
$form->dateRange('start_at', 'end_at', '有效时间'); ->required();
})
->required(); $form->radio('type', '类型')
->options(Activity::TYPES)
$form->switch('status', '状态')->default(1); ->when(Activity::TYPE_EXTEND, function (Form $form) {
$form->switch('need_check', '多次校验') $form->number('days', '延期天数')->default(60)->help('到期日期=发券日期+延期天数');
->default(1) })
->help('同一订单多次核销时校验订单每满100元可核销一笔。'); ->when(Activity::TYPE_SCOPE, function (Form $form) {
$form->dateRange('start_at', 'end_at', '有效时间');
$grantdef = $verificationsdef = ''; })
if ($id) { ->required();
$grantdef = Activity::find($id)->grants()->pluck('user_id')->toArray();
$verificationsdef = Activity::find($id)->verifications()->pluck('user_id')->toArray(); $form->switch('status', '状态')->default(1);
} $form->switch('need_check', '多次校验')
->default(1)
$users = User::with('info')->whereHas('identity', function ($q) { ->help('同一订单多次核销时校验订单每满100元可核销一笔。');
$q->where('identity_id', 1);
})->orderBy('id', 'desc')->get()->pluck('nickname', 'id'); $grantdef = $verificationsdef = '';
if ($id) {
$form->listbox('grants.user_id', '可发券渠道') $grantdef = Activity::find($id)->grants()->pluck('user_id')->toArray();
->options($users) $verificationsdef = Activity::find($id)->verifications()->pluck('user_id')->toArray();
->default($grantdef) }
->required();
$users = User::with('info')->whereHas('identity', function ($q) {
$form->listbox('verifications.user_id', '可核券渠道') $q->where('identity_id', 1);
->options($users) })->orderBy('id', 'desc')->get()->pluck('nickname', 'id');
->default($verificationsdef)
->required(); $form->listbox('grants.user_id', '可发券渠道')
->options($users)
$form->saving(function (Form $form) { ->default($grantdef)
$request = request(); ->required();
if ($request->type == Activity::TYPE_EXTEND && empty($request->days)) {
$error = new MessageBag([ $form->listbox('verifications.user_id', '可核券渠道')
'title' => '错误', ->options($users)
'message' => '必须添加延期天数', ->default($verificationsdef)
]); ->required();
return back()->withInput()->with(compact('error')); $form->saving(function (Form $form) {
} $request = request();
if ($request->type == Activity::TYPE_SCOPE && (empty($request->start_at) || empty($request->end_at))) { if ($request->total < 0) {
$error = new MessageBag([ $error = new MessageBag([
'title' => '错误', 'title' => '错误',
'message' => '必须添加延期天数', 'message' => '可发券总数必须大于0',
]); ]);
return back()->withInput()->with(compact('error')); return back()->withInput()->with(compact('error'));
} }
if (request()->start) {
$form->start_at = $form->start_at . ' 00:00:01'; if ($request->type == Activity::TYPE_EXTEND && empty($request->days)) {
} $error = new MessageBag([
'title' => '错误',
if (request()->end_at) { 'message' => '必须添加延期天数',
$form->end_at = $form->end_at . ' 23:59:59'; ]);
}
}); return back()->withInput()->with(compact('error'));
}
$form->saved(function (Form $form) {
$users = []; if ($request->type == Activity::TYPE_SCOPE && (empty($request->start_at) || empty($request->end_at))) {
foreach ($form->grants['user_id'] as $key => $user_id) { $error = new MessageBag([
if ($user_id) { 'title' => '错误',
$form->model()->grants()->updateOrCreate([ 'message' => '必须添加延期天数',
'user_id' => $user_id, ]);
]);
$users[] = $user_id; return back()->withInput()->with(compact('error'));
} }
}
$form->model()->grants()->whereNotIn('user_id', $users)->delete(); if (request()->start) {
$users = []; $form->start_at = $form->start_at . ' 00:00:01';
foreach ($form->verifications['user_id'] as $key => $user_id) { }
if ($user_id) {
$form->model()->verifications()->updateOrCreate([ if (request()->end_at) {
'user_id' => $user_id, $form->end_at = $form->end_at . ' 23:59:59';
]); }
$users[] = $user_id;
} });
}
$form->model()->verifications()->whereNotIn('user_id', $users)->delete(); $form->saved(function (Form $form) {
}); $users = [];
foreach ($form->grants['user_id'] as $key => $user_id) {
return $form; if ($user_id) {
} $form->model()->grants()->updateOrCreate([
'user_id' => $user_id,
} ]);
$users[] = $user_id;
}
}
$form->model()->grants()->whereNotIn('user_id', $users)->delete();
$users = [];
foreach ($form->verifications['user_id'] as $key => $user_id) {
if ($user_id) {
$form->model()->verifications()->updateOrCreate([
'user_id' => $user_id,
]);
$users[] = $user_id;
}
}
$form->model()->verifications()->whereNotIn('user_id', $users)->delete();
});
return $form;
}
}

View File

@@ -20,83 +20,183 @@ class HomeController extends Controller
->title('数据看版1') ->title('数据看版1')
->row(function (Row $row) { ->row(function (Row $row) {
$row->column(2, function (Column $column) { $row->column(2, function (Column $column) {
$column->append(new InfoBox('渠道商', 'users', 'yellow', '/admin/users?identity[identity_id]=1', User::whereHas('identity', function ($q) {$q->where('identity_id', 1);})->count())); $column->append(new InfoBox(
'渠道商',
'users',
'yellow',
'/admin/users?identity[identity_id]=1',
User::whereHas('identity', function ($q) {
$q->where('identity_id', 1);
})->count()
));
}); });
}) })
->row(function (Row $row) { ->row(function (Row $row) {
$row->column(2, function (Column $column) { $row->column(2, function (Column $column) {
$column->append(new InfoBox('核销卡券总数', '', 'blue', '/admin/coupons', Coupon::whereIn('status', [2, 3])->count())); $column->append(new InfoBox(
}); '核销卡券总数',
'',
$row->column(2, function (Column $column) { 'blue',
$column->append(new InfoBox('核销成功', '', 'blue', '/admin/coupons', Coupon::where('status', 2)->count())); '/admin/coupons',
Coupon::whereIn('status', [2, 3])->count()
));
}); });
$row->column(2, function (Column $column) { $row->column(2, function (Column $column) {
$column->append(new InfoBox('核销失败', '', 'black', '/admin/coupons', Coupon::where('status', 3)->count())); $column->append(new InfoBox(
'核销成功',
'',
'blue',
'/admin/coupons',
Coupon::where('status', 2)->count()
));
}); });
$row->column(2, function (Column $column) { $row->column(2, function (Column $column) {
$column->append(new InfoBox('资金通道结算', '', 'red', '/admin/coupons', Coupon::where('status', 2)->sum('price'))); $column->append(new InfoBox(
'核销失败',
'',
'black',
'/admin/coupons',
Coupon::where('status', 3)->count()
));
}); });
$row->column(2, function (Column $column) { $row->column(2, function (Column $column) {
$column->append(new InfoBox('应打款金额', '', 'green', '/admin/coupons', Coupon::where('status', 2)->sum('profit'))); $column->append(new InfoBox(
'资金通道结算',
'',
'red',
'/admin/coupons',
Coupon::where('status', 2)->sum('price')
));
}); });
$row->column(2, function (Column $column) { $row->column(2, function (Column $column) {
$column->append(new InfoBox('已打款金额', '', 'green', '/admin/coupons', Coupon::where('status', 2)->where('is_profit', 1)->sum('profit'))); $column->append(new InfoBox(
'应打款金额',
'',
'green',
'/admin/coupons',
Coupon::where('status', 2)->sum('profit')
));
});
$row->column(2, function (Column $column) {
$column->append(new InfoBox(
'已打款金额',
'',
'green',
'/admin/coupons',
Coupon::where('status', 2)
->where('is_profit', 1)
->sum('profit')
));
}); });
}) })
->row(function (Row $row) { ->row(function (Row $row) {
$coupons = Coupon::where('status', 2)
->whereDate('created_at', now()->format('Y-m-d'))
->get();
$lists = [ $lists = [
'ysd10' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-10')->count(), 'ysd10' => Coupon::where('status', 2)
'ysd25' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-25')->count(), ->whereDate('created_at', now()->format('Y-m-d'))
'ysd50' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-50')->count(), ->where('thirdPartyGoodsId', 'YSD-full100-10')
'ysd100' => $coupons->where('thirdPartyGoodsId', 'YSD-full200-100')->count(), ->count(),
'ysd25' => Coupon::where('status', 2)
->whereDate('created_at', now()->format('Y-m-d'))
->where('thirdPartyGoodsId', 'YSD-full100-25')
->count(),
'ysd50' => Coupon::where('status', 2)
->whereDate('created_at', now()->format('Y-m-d'))
->where('thirdPartyGoodsId', 'YSD-full100-50')
->count(),
'ysd100' => Coupon::where('status', 2)
->whereDate('created_at', now()->format('Y-m-d'))
->where('thirdPartyGoodsId', 'YSD-full200-100')
->count(),
]; ];
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('100元减10元成功今日总数', '', 'blue', '/admin/coupons', $lists['ysd10'])); $column->append(new InfoBox(
'100元减10元成功今日总数',
'',
'blue',
'/admin/coupons',
$lists['ysd10']
));
}); });
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('100元减25元成功今日总数', '', 'blue', '/admin/coupons', $lists['ysd25'])); $column->append(new InfoBox(
'100元减25元成功今日总数',
'',
'blue',
'/admin/coupons',
$lists['ysd25']
));
}); });
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('100元减50元成功今日总数', '', 'blue', '/admin/coupons', $lists['ysd50'])); $column->append(new InfoBox(
'100元减50元成功今日总数',
'',
'blue',
'/admin/coupons',
$lists['ysd50']
));
}); });
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('200元减100元成功今日总数', '', 'blue', '/admin/coupons', $lists['ysd100'])); $column->append(new InfoBox(
'200元减100元成功今日总数',
'',
'blue',
'/admin/coupons',
$lists['ysd100']
));
}); });
}) })
->row(function (Row $row) { ->row(function (Row $row) {
$coupons = Coupon::where('status', 2)->get(); $lists = [
$lists = [ 'ysd10' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full100-10')->count(),
'ysd10' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-10')->count(), 'ysd25' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full100-25')->count(),
'ysd25' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-25')->count(), 'ysd50' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full100-50')->count(),
'ysd50' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-50')->count(), 'ysd100' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full200-100')->count(),
'ysd100' => $coupons->where('thirdPartyGoodsId', 'YSD-full200-100')->count(),
]; ];
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('100元减10元成功总数', '', 'blue', '/admin/coupons', $lists['ysd10'])); $column->append(new InfoBox(
'100元减10元成功总数',
'',
'blue',
'/admin/coupons',
$lists['ysd10']
));
}); });
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('100元减25元成功总数', '', 'blue', '/admin/coupons', $lists['ysd25'])); $column->append(new InfoBox(
'100元减25元成功总数',
'',
'blue',
'/admin/coupons',
$lists['ysd25']
));
}); });
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('100元减50元成功总数', '', 'blue', '/admin/coupons', $lists['ysd50'])); $column->append(new InfoBox(
'100元减50元成功总数',
'',
'blue',
'/admin/coupons',
$lists['ysd50']
));
}); });
$row->column(2, function (Column $column) use ($lists) { $row->column(2, function (Column $column) use ($lists) {
$column->append(new InfoBox('200元减100元成功总数', '', 'blue', '/admin/coupons', $lists['ysd100'])); $column->append(new InfoBox(
'200元减100元成功总数',
'',
'blue',
'/admin/coupons',
$lists['ysd100']
));
}); });
}); });
} }

View File

@@ -1,137 +1,137 @@
<?php <?php
namespace App\Models; namespace App\Models;
use App\Models\Traits\BelongsToUser; use App\Models\Traits\BelongsToUser;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class Activity extends Model class Activity extends Model
{ {
use BelongsToUser; use BelongsToUser;
const TYPE_EXTEND = 1; const TYPE_EXTEND = 1;
const TYPE_SCOPE = 2; const TYPE_SCOPE = 2;
const TYPES = [ const TYPES = [
self::TYPE_EXTEND => '延期', self::TYPE_EXTEND => '延期',
self::TYPE_SCOPE => '固定', self::TYPE_SCOPE => '固定',
]; ];
const STATUS_OPEN = 1; const STATUS_OPEN = 1;
const STATUS_CLOSE = 0; const STATUS_CLOSE = 0;
const STATUS = [ const STATUS = [
self::STATUS_OPEN => '正常', self::STATUS_OPEN => '正常',
self::STATUS_CLOSE => '关闭', self::STATUS_CLOSE => '关闭',
]; ];
protected $dates = [ protected $dates = [
'start_at', 'start_at',
'end_at', 'end_at',
]; ];
/** /**
* 默认加载的关联 * 默认加载的关联
* @var array * @var array
*/ */
protected $with = ['rule']; protected $with = ['rule'];
protected static function boot(): void protected static function boot(): void
{ {
parent::boot(); parent::boot();
self::creating(function ($model) { self::creating(function ($model) {
$model->code = 'ysd' . date('Ym') . mt_rand(100, 999); $model->code = 'ysd' . date('Ym') . mt_rand(100, 999);
}); });
} }
//活动类型 固定期限 延期 //活动类型 固定期限 延期
public function getTypeTextAttribute() public function getTypeTextAttribute()
{ {
return self::TYPES[$this->type] ?? '未知'; return self::TYPES[$this->type] ?? '未知';
} }
//活动状态 //活动状态
public function getStatusTextAttribute() public function getStatusTextAttribute()
{ {
return self::STATUS[$this->status] ?? '未知'; return self::STATUS[$this->status] ?? '未知';
} }
//关联卡券规则 ysd-full100-10 //关联卡券规则 ysd-full100-10
public function rule() public function rule()
{ {
return $this->belongsTo(ActivityRule::class, 'activity_rule_id'); return $this->belongsTo(ActivityRule::class, 'activity_rule_id');
} }
//关联券表 //关联券表
public function coupons() public function coupons()
{ {
return $this->hasMany(ActivityCoupon::class); return $this->hasMany(ActivityCoupon::class);
} }
/** /**
* Notes: 关联可发券渠道 * Notes: 关联可发券渠道
* @Author: 玄尘 * @Author: 玄尘
* @Date : 2020/8/21 11:09 * @Date : 2020/8/21 11:09
*/ */
public function grants() public function grants()
{ {
return $this->hasMany(ActivityGrant::class); return $this->hasMany(ActivityGrant::class);
} }
/** /**
* Notes: 关联可核销渠道 * Notes: 关联可核销渠道
* @Author: 玄尘 * @Author: 玄尘
* @Date : 2020/8/21 11:09 * @Date : 2020/8/21 11:09
*/ */
public function verifications() public function verifications()
{ {
return $this->hasMany(ActivityVerification::class); return $this->hasMany(ActivityVerification::class);
} }
public function canGrant() public function canGrant()
{ {
return $this->status; return $this->status;
} }
//发券 //发券
public function grant($mobile, $outletId = null) public function grant($mobile, $outletId = null)
{ {
try { try {
$code = 'YSD' . date('ymd') . mt_rand(100000, 999999); $code = 'YSD' . date('ymd') . mt_rand(100000, 999999);
if ($this->type == SELF::TYPE_EXTEND) { if ($this->type == SELF::TYPE_EXTEND) {
$start_at = now(); $start_at = now();
$end_at = now()->addDays($this->days); $end_at = now()->addDays($this->days);
} else { } else {
$start_at = $this->start_at->startOfDay(); $start_at = $this->start_at->startOfDay();
$end_at = $this->end_at->endOfDay(); $end_at = $this->end_at->endOfDay();
} }
DB::beginTransaction(); DB::beginTransaction();
$coupon = ActivityCoupon::create([ $coupon = ActivityCoupon::create([
'activity_id' => $this->id, 'activity_id' => $this->id,
'code' => $code, 'code' => $code,
'mobile' => $mobile, 'mobile' => $mobile,
'outletId' => $outletId, 'outletId' => $outletId,
'full' => $this->rule->full, 'full' => $this->rule->full,
'price' => $this->rule->take, 'price' => $this->rule->take,
'start_at' => $start_at, 'start_at' => $start_at,
'end_at' => $end_at, 'end_at' => $end_at,
'status' => ActivityCoupon::STATUS_INIT, 'status' => ActivityCoupon::STATUS_INIT,
]); ]);
DB::commit(); DB::commit();
return $coupon; return $coupon;
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollback(); DB::rollback();
return $e->getMessage(); return $e->getMessage();
} }
} }
} }

View File

@@ -4,6 +4,7 @@ namespace XuanChen\Coupon\Action\ysd;
use App\Models\Activity; use App\Models\Activity;
use App\Models\User; use App\Models\User;
use Carbon\Carbon;
use XuanChen\Coupon\Action\Init; use XuanChen\Coupon\Action\Init;
class YsdGrant extends Init class YsdGrant extends Init
@@ -13,7 +14,7 @@ class YsdGrant extends Init
{ {
try { try {
$activity = Activity::where('code', $this->activityId)->first(); $activity = Activity::withCount('coupons')->where('code', $this->activityId)->first();
if (!$activity) { if (!$activity) {
return '发券失败,没有找到这个活动。'; return '发券失败,没有找到这个活动。';
} }
@@ -21,6 +22,14 @@ class YsdGrant extends Init
if (!$activity->status) { if (!$activity->status) {
return '发券失败,活动已经关闭。'; return '发券失败,活动已经关闭。';
} }
if ($activity->type == Activity::TYPE_SCOPE && Carbon::now()->gt($activity->end_at)) {
return '发券失败,此活动已经结束。';
}
if ($activity->total > 0 && $activity->coupons_count >= $activity->total) {
return '发券失败,已达到可发券总数。';
}
$outlet = User::where('outlet_id', $this->outletId)->first(); $outlet = User::where('outlet_id', $this->outletId)->first();
if (!$outlet) { if (!$outlet) {