链街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.

99 lines
3.0 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. class CouponPublishForm extends Form
  10. {
  11. /**
  12. * Handle the form request.
  13. *
  14. * @param array $input
  15. *
  16. * @return Response
  17. */
  18. public function handle(array $input)
  19. {
  20. // 获取外部传递参数
  21. $id = $input['id'];
  22. $receiveType = $input['receive_type'];
  23. $coupon = CouponModel::find($id);
  24. if(!$coupon){
  25. return $this->error('优惠券不存在或已删除!');
  26. }
  27. switch($coupon->status){
  28. case 0:
  29. case 3:
  30. $time = time();
  31. // 判断活动时间
  32. if($coupon->end_time <= $time + SettingModel::$publishTime){
  33. return $this->error('活动时间已经接近,请重新设置!');
  34. }
  35. $coupon->status = 1;
  36. if(!$coupon->save()){
  37. return $this->error('发布失败!');
  38. }
  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->success('发布成功','/coupon');
  53. }
  54. break;
  55. case 1:
  56. return $this->error('优惠券已发布!');
  57. break;
  58. case 2:
  59. return $this->error('优惠券已领完!');
  60. break;
  61. case -1:
  62. return $this->error('优惠券已删除!');
  63. break;
  64. }
  65. return $this->error('发布失败!');
  66. }
  67. /**
  68. * Build a form here.
  69. */
  70. public function form()
  71. {
  72. $id = $this->getKey();
  73. $receiveTypeInfo = ReceiveTypeModel::getReceiveTypeInfo($id,'receive_type');
  74. $list = SettingModel::getSettingArray();
  75. $receiveType = empty($receiveTypeInfo->receive_type)? 0 :$receiveTypeInfo->receive_type;
  76. $this->hidden('id')->value($id);
  77. $this->select('receive_type','领取方式')->required()->options($list)->value($receiveType);
  78. }
  79. /**
  80. * The data of the form.
  81. *
  82. * @return array
  83. */
  84. public function default()
  85. {
  86. return [];
  87. }
  88. }