From 8b481f76df200744cba8331c07dfc2d4a25c55e9 Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Thu, 29 Oct 2020 10:08:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=91=E5=88=B8=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Activity/IndexController.php | 514 +++++++++--------- app/Admin/Controllers/HomeController.php | 164 ++++-- app/Models/Activity.php | 274 +++++----- packages/coupon/src/Action/ysd/YsdGrant.php | 11 +- 4 files changed, 546 insertions(+), 417 deletions(-) diff --git a/app/Admin/Controllers/Activity/IndexController.php b/app/Admin/Controllers/Activity/IndexController.php index 9508774..b7b7e02 100644 --- a/app/Admin/Controllers/Activity/IndexController.php +++ b/app/Admin/Controllers/Activity/IndexController.php @@ -1,247 +1,267 @@ -title; - } - - /** - * Index interface. - * @param Content $content - * @return Content - */ - public function index(Content $content) - { - return $content - ->title($this->title()) - ->description($this->description['index'] ?? trans('admin.list')) - ->body($this->grid()); - } - - /** - * Edit interface. - * @param mixed $id - * @param Content $content - * @return Content - */ - public function edit($id, Content $content) - { - return $content - ->title($this->title()) - ->description($this->description['edit'] ?? trans('admin.edit')) - ->body($this->form($id)->edit($id)); - } - - /** - * Create interface. - * @param Content $content - * @return Content - */ - public function create(Content $content) - { - return $content - ->title($this->title()) - ->description($this->description['create'] ?? trans('admin.create')) - ->body($this->form()); - } - - protected function grid() - { - $grid = new Grid(new Activity); - - $grid->actions(function ($actions) { - $actions->disableView(); - }); - - $grid->filter(function ($filter) { - $filter->column(1 / 2, function ($filter) { - $filter->equal('status', '状态')->select(Activity::STATUS); - $filter->equal('type', '类型')->select(Activity::TYPES); - - $users = User::whereHas('identity', function ($query) { - $query->where('identity_id', 1); - })->get()->pluck('nickname', 'id'); - - }); - - $filter->column(1 / 2, function ($filter) { - $filter->between('start_at', '开始时间')->datetime(); - $filter->between('end_at', '结束时间')->datetime(); - }); - - }); - - $grid->column('id', '#ID#'); - $grid->column('title', '活动名称'); - $grid->column('code', '活动编号'); - $grid->column('rule.code', '卡券规则'); - $grid->column('类型')->display(function () { - return $this->type_text; - }); - - $grid->column('days', '延期(天)'); - $grid->column('rule.full', '满足金额'); - $grid->column('rule.take', '扣除金额'); - $grid->column('发券') - ->display(function () { - return $this->grants->pluck('user_nickname'); - }) - ->label() - ->hide(); - - $grid->column('核券') - ->display(function () { - return $this->verifications->pluck('user_nickname'); - }) - ->label() - ->hide(); - - $grid->column('开始时间')->display(function () { - return $this->type == Activity::TYPE_SCOPE ? $this->start_at->format('Y-m-d') : '---'; - }); - $grid->column('结束时间')->display(function () { - return $this->type == Activity::TYPE_SCOPE ? $this->end_at->format('Y-m-d') : '---'; - }); - - $grid->status('状态')->switch([ - 'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'], - 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'], - ]); - - $grid->column('created_at', '创建时间'); - - return $grid; - } - - /** - * Make a form builder. - * @return Form - */ - protected function form($id = '') - { - $form = new Form(new Activity); - - $form->text('title', '活动名称')->required(); - $form->textarea('description', '活动说明')->required(); - - $form->select('activity_rule_id', '所属规则') - ->options(function ($option, $info) { - return ActivityRule::where('status', 1)->pluck('title', 'id'); - }) - ->required(); - - $form->radio('type', '类型') - ->options(Activity::TYPES) - ->when(Activity::TYPE_EXTEND, function (Form $form) { - $form->number('days', '延期天数')->default(60)->help('到期日期=发券日期+延期天数'); - }) - ->when(Activity::TYPE_SCOPE, function (Form $form) { - $form->dateRange('start_at', 'end_at', '有效时间'); - }) - ->required(); - - $form->switch('status', '状态')->default(1); - $form->switch('need_check', '多次校验') - ->default(1) - ->help('同一订单,多次核销时校验,订单每满100元可核销一笔。'); - - $grantdef = $verificationsdef = ''; - if ($id) { - $grantdef = Activity::find($id)->grants()->pluck('user_id')->toArray(); - $verificationsdef = Activity::find($id)->verifications()->pluck('user_id')->toArray(); - } - - $users = User::with('info')->whereHas('identity', function ($q) { - $q->where('identity_id', 1); - })->orderBy('id', 'desc')->get()->pluck('nickname', 'id'); - - $form->listbox('grants.user_id', '可发券渠道') - ->options($users) - ->default($grantdef) - ->required(); - - $form->listbox('verifications.user_id', '可核券渠道') - ->options($users) - ->default($verificationsdef) - ->required(); - - $form->saving(function (Form $form) { - $request = request(); - if ($request->type == Activity::TYPE_EXTEND && empty($request->days)) { - $error = new MessageBag([ - 'title' => '错误', - 'message' => '必须添加延期天数', - ]); - - return back()->withInput()->with(compact('error')); - } - - if ($request->type == Activity::TYPE_SCOPE && (empty($request->start_at) || empty($request->end_at))) { - $error = new MessageBag([ - 'title' => '错误', - 'message' => '必须添加延期天数', - ]); - - return back()->withInput()->with(compact('error')); - } - if (request()->start) { - $form->start_at = $form->start_at . ' 00:00:01'; - } - - if (request()->end_at) { - $form->end_at = $form->end_at . ' 23:59:59'; - } - }); - - $form->saved(function (Form $form) { - $users = []; - foreach ($form->grants['user_id'] as $key => $user_id) { - 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; - } - -} +title; + } + + /** + * Index interface. + * @param Content $content + * @return Content + */ + public function index(Content $content) + { + return $content + ->title($this->title()) + ->description($this->description['index'] ?? trans('admin.list')) + ->body($this->grid()); + } + + /** + * Edit interface. + * @param mixed $id + * @param Content $content + * @return Content + */ + public function edit($id, Content $content) + { + return $content + ->title($this->title()) + ->description($this->description['edit'] ?? trans('admin.edit')) + ->body($this->form($id)->edit($id)); + } + + /** + * Create interface. + * @param Content $content + * @return Content + */ + public function create(Content $content) + { + return $content + ->title($this->title()) + ->description($this->description['create'] ?? trans('admin.create')) + ->body($this->form()); + } + + protected function grid() + { + $grid = new Grid(new Activity); + $grid->model()->withCount('coupons'); + + $grid->actions(function ($actions) { + $actions->disableView(); + }); + + $grid->filter(function ($filter) { + $filter->column(1 / 2, function ($filter) { + $filter->equal('status', '状态')->select(Activity::STATUS); + $filter->equal('type', '类型')->select(Activity::TYPES); + + $users = User::whereHas('identity', function ($query) { + $query->where('identity_id', 1); + })->get()->pluck('nickname', 'id'); + + }); + + $filter->column(1 / 2, function ($filter) { + $filter->between('start_at', '开始时间')->datetime(); + $filter->between('end_at', '结束时间')->datetime(); + }); + + }); + + $grid->column('id', '#ID#'); + $grid->column('title', '活动名称'); + $grid->column('code', '活动编号'); + $grid->column('total', '可发券总数'); + $grid->column('coupons_count', '可发券总数'); + $grid->column('rule.code', '卡券规则'); + $grid->column('类型')->display(function () { + return $this->type_text; + }); + + $grid->column('days', '延期(天)'); + $grid->column('rule.full', '满足金额'); + $grid->column('rule.take', '扣除金额'); + $grid->column('发券') + ->display(function () { + return $this->grants->pluck('user_nickname'); + }) + ->label() + ->hide(); + + $grid->column('核券') + ->display(function () { + return $this->verifications->pluck('user_nickname'); + }) + ->label() + ->hide(); + + $grid->column('开始时间')->display(function () { + return $this->type == Activity::TYPE_SCOPE ? $this->start_at->format('Y-m-d') : '---'; + }); + $grid->column('结束时间')->display(function () { + return $this->type == Activity::TYPE_SCOPE ? $this->end_at->format('Y-m-d') : '---'; + }); + + $grid->status('状态')->switch([ + 'on' => ['value' => 1, 'text' => '正常', 'color' => 'primary'], + 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'], + ]); + + $grid->column('created_at', '创建时间'); + + return $grid; + } + + /** + * Make a form builder. + * @return Form + */ + protected function form($id = '') + { + $form = new Form(new Activity); + + $form->text('title', '活动名称')->required(); + $form->textarea('description', '活动说明')->required(); + + $form->select('activity_rule_id', '所属规则') + ->options(function ($option, $info) { + return ActivityRule::where('status', 1)->pluck('title', 'id'); + }) + ->required(); + + $form->number('total', '总数') + ->help('可发券总数') + ->default(10000) + ->required(); + + $form->radio('type', '类型') + ->options(Activity::TYPES) + ->when(Activity::TYPE_EXTEND, function (Form $form) { + $form->number('days', '延期天数')->default(60)->help('到期日期=发券日期+延期天数'); + }) + ->when(Activity::TYPE_SCOPE, function (Form $form) { + $form->dateRange('start_at', 'end_at', '有效时间'); + }) + ->required(); + + $form->switch('status', '状态')->default(1); + $form->switch('need_check', '多次校验') + ->default(1) + ->help('同一订单,多次核销时校验,订单每满100元可核销一笔。'); + + $grantdef = $verificationsdef = ''; + if ($id) { + $grantdef = Activity::find($id)->grants()->pluck('user_id')->toArray(); + $verificationsdef = Activity::find($id)->verifications()->pluck('user_id')->toArray(); + } + + $users = User::with('info')->whereHas('identity', function ($q) { + $q->where('identity_id', 1); + })->orderBy('id', 'desc')->get()->pluck('nickname', 'id'); + + $form->listbox('grants.user_id', '可发券渠道') + ->options($users) + ->default($grantdef) + ->required(); + + $form->listbox('verifications.user_id', '可核券渠道') + ->options($users) + ->default($verificationsdef) + ->required(); + + $form->saving(function (Form $form) { + $request = request(); + + if ($request->total < 0) { + $error = new MessageBag([ + 'title' => '错误', + 'message' => '可发券总数必须大于0', + ]); + + return back()->withInput()->with(compact('error')); + } + + if ($request->type == Activity::TYPE_EXTEND && empty($request->days)) { + $error = new MessageBag([ + 'title' => '错误', + 'message' => '必须添加延期天数', + ]); + + return back()->withInput()->with(compact('error')); + } + + if ($request->type == Activity::TYPE_SCOPE && (empty($request->start_at) || empty($request->end_at))) { + $error = new MessageBag([ + 'title' => '错误', + 'message' => '必须添加延期天数', + ]); + + return back()->withInput()->with(compact('error')); + } + + if (request()->start) { + $form->start_at = $form->start_at . ' 00:00:01'; + } + + if (request()->end_at) { + $form->end_at = $form->end_at . ' 23:59:59'; + } + + }); + + $form->saved(function (Form $form) { + $users = []; + foreach ($form->grants['user_id'] as $key => $user_id) { + 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; + } + +} diff --git a/app/Admin/Controllers/HomeController.php b/app/Admin/Controllers/HomeController.php index 4078efd..0de5e61 100644 --- a/app/Admin/Controllers/HomeController.php +++ b/app/Admin/Controllers/HomeController.php @@ -20,83 +20,183 @@ class HomeController extends Controller ->title('数据看版1') ->row(function (Row $row) { $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->column(2, function (Column $column) { - $column->append(new InfoBox('核销卡券总数', '', 'blue', '/admin/coupons', Coupon::whereIn('status', [2, 3])->count())); - }); - - $row->column(2, function (Column $column) { - $column->append(new InfoBox('核销成功', '', 'blue', '/admin/coupons', Coupon::where('status', 2)->count())); + $column->append(new InfoBox( + '核销卡券总数', + '', + 'blue', + '/admin/coupons', + Coupon::whereIn('status', [2, 3])->count() + )); }); $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) { - $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) { - $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) { - $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) { - $coupons = Coupon::where('status', 2) - ->whereDate('created_at', now()->format('Y-m-d')) - ->get(); $lists = [ - 'ysd10' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-10')->count(), - 'ysd25' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-25')->count(), - 'ysd50' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-50')->count(), - 'ysd100' => $coupons->where('thirdPartyGoodsId', 'YSD-full200-100')->count(), + 'ysd10' => Coupon::where('status', 2) + ->whereDate('created_at', now()->format('Y-m-d')) + ->where('thirdPartyGoodsId', 'YSD-full100-10') + ->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) { - $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) { - $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) { - $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) { - $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) { - $coupons = Coupon::where('status', 2)->get(); - $lists = [ - 'ysd10' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-10')->count(), - 'ysd25' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-25')->count(), - 'ysd50' => $coupons->where('thirdPartyGoodsId', 'YSD-full100-50')->count(), - 'ysd100' => $coupons->where('thirdPartyGoodsId', 'YSD-full200-100')->count(), + $lists = [ + 'ysd10' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full100-10')->count(), + 'ysd25' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full100-25')->count(), + 'ysd50' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full100-50')->count(), + 'ysd100' => Coupon::where('status', 2)->where('thirdPartyGoodsId', 'YSD-full200-100')->count(), ]; $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) { - $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) { - $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) { - $column->append(new InfoBox('200元减100元成功总数', '', 'blue', '/admin/coupons', $lists['ysd100'])); + $column->append(new InfoBox( + '200元减100元成功总数', + '', + 'blue', + '/admin/coupons', + $lists['ysd100'] + )); }); }); } diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 426dd5a..c074e8e 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -1,137 +1,137 @@ - '延期', - 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 = 'YSD' . 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(); - } - - } - -} + '延期', + 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 = 'YSD' . 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(); + } + + } + +} diff --git a/packages/coupon/src/Action/ysd/YsdGrant.php b/packages/coupon/src/Action/ysd/YsdGrant.php index 1c3b452..c414d2d 100644 --- a/packages/coupon/src/Action/ysd/YsdGrant.php +++ b/packages/coupon/src/Action/ysd/YsdGrant.php @@ -4,6 +4,7 @@ namespace XuanChen\Coupon\Action\ysd; use App\Models\Activity; use App\Models\User; +use Carbon\Carbon; use XuanChen\Coupon\Action\Init; class YsdGrant extends Init @@ -13,7 +14,7 @@ class YsdGrant extends Init { try { - $activity = Activity::where('code', $this->activityId)->first(); + $activity = Activity::withCount('coupons')->where('code', $this->activityId)->first(); if (!$activity) { return '发券失败,没有找到这个活动。'; } @@ -21,6 +22,14 @@ class YsdGrant extends Init if (!$activity->status) { 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(); if (!$outlet) {