链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.6 KiB

  1. <?php
  2. namespace App\Admin\Forms\v3;
  3. use App\Models\v3\GoodsBanners;
  4. use Dcat\Admin\Widgets\Form;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use App\Models\v3\CouponReceiveType as ReceiveTypeModel;
  7. use App\Models\v3\CouponSetting as SettingModel;
  8. use App\Models\v3\Coupon as CouponModel;
  9. use Dcat\Admin\Contracts\LazyRenderable;
  10. use Dcat\Admin\Traits\LazyWidget;
  11. class CouponPublishForm extends Form implements LazyRenderable
  12. {
  13. use LazyWidget;
  14. /**
  15. * Handle the form request.
  16. *
  17. * @param array $input
  18. *
  19. * @return Response
  20. */
  21. public function handle(array $input)
  22. {
  23. // 获取外部传递参数
  24. $id = $input['id'];
  25. $receiveType = $input['receive_type'];
  26. $coupon = CouponModel::find($id);
  27. if(!$coupon){
  28. return $this->error('优惠券不存在或已删除!');
  29. }
  30. switch($coupon->status){
  31. case 0:
  32. case 3:
  33. $time = time();
  34. // 判断活动时间
  35. if($coupon->end_time <= $time + SettingModel::$publishTime){
  36. return $this->error('活动时间已经接近,请重新设置!');
  37. }
  38. if($coupon->status == 0){
  39. // 删除领取方式
  40. $delRes = ReceiveTypeModel::where('coupon_id',$id)->delete();
  41. if($delRes === false){
  42. return $this->error('发布失败!');
  43. }
  44. // 添加领取方式
  45. $receiveModel = new ReceiveTypeModel();
  46. $receiveModel->coupon_id = $id;
  47. $receiveModel->receive_type = $receiveType;
  48. // 获取一次可领取数量
  49. $receiveNumber = SettingModel::getSettingInfo($receiveType,'value');
  50. $receiveModel->one_receive_number = empty($receiveNumber) ? 1 : $receiveNumber->value;
  51. if(!$receiveModel->save()){
  52. return $this->error('发布失败!');
  53. }
  54. }
  55. $coupon->status = 1;
  56. if($coupon->save()){
  57. return $this->success('发布成功','/coupon');
  58. }
  59. break;
  60. case 1:
  61. return $this->error('优惠券已发布!');
  62. break;
  63. case 2:
  64. return $this->error('优惠券已领完!');
  65. break;
  66. case -1:
  67. return $this->error('优惠券已删除!');
  68. break;
  69. }
  70. return $this->error('发布失败!');
  71. }
  72. /**
  73. * Build a form here.
  74. */
  75. public function form()
  76. {
  77. $id = $this->payload['id'] ?? 0;
  78. $title = $this->payload['title'] ?? '';
  79. $status = $this->payload['status'] ?? -1;
  80. $this->hidden('id')->value($id);
  81. $this->display('title','标题')->value($title);
  82. $receiveTypeInfo = ReceiveTypeModel::getReceiveTypeOne([['coupon_id','=',$id]],'receive_type');
  83. $list = SettingModel::getSettingArray();
  84. $receiveType = empty($receiveTypeInfo->receive_type)? 0 :$receiveTypeInfo->receive_type;
  85. if($status == 0){
  86. $this->select('receive_type','领取方式')->required()->options($list)->value($receiveType);
  87. }else{
  88. $this->select('receive_type','领取方式')->options($list)->value($receiveType)->disable();
  89. }
  90. }
  91. /**
  92. * The data of the form.
  93. *
  94. * @return array
  95. */
  96. public function default()
  97. {
  98. return [];
  99. }
  100. }