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

135 lines
4.8 KiB

  1. <?php
  2. /**
  3. * 活动商品复制表单
  4. */
  5. namespace App\Admin\Forms\v3;
  6. use Dcat\Admin\Models\Administrator;
  7. use Dcat\Admin\Traits\LazyWidget;
  8. use Dcat\Admin\Widgets\Form;
  9. use Dcat\Admin\Contracts\LazyRenderable;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use App\Models\v3\Store as StoreModel;
  12. use App\Models\v3\GoodsActivity as GoodsModel;
  13. use App\Models\v3\GoodsActivityBanners as GoodsBannerModel;
  14. class GoodsActivityCopyForm extends Form implements LazyRenderable
  15. {
  16. use LazyWidget;
  17. /**
  18. * Handle the form request.
  19. *
  20. * @param array $input
  21. *
  22. * @return Response
  23. */
  24. public function handle(array $input)
  25. {
  26. // 获取外部传递参数
  27. $goodsId = $input['id'];
  28. $storeIds= $input['store_ids'];
  29. $expireTime = $input['expire_time'];
  30. $timeLimitDays = $input['time_limit_days'];
  31. $timeLimitNum = $input['time_limit_num'];
  32. $canUseCoupon = $input['can_use_coupon'];
  33. $type = $input['type'];
  34. $inventory = $input['inventory'];
  35. if($expireTime <= time()){
  36. $this->error('活动结束时间必须大于当前时间!');
  37. }
  38. $goods = GoodsModel::find($goodsId);
  39. $goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get();
  40. $markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id');
  41. foreach($storeIds as $key =>$storeId){
  42. $marketId = $markets[$storeId]??0;
  43. $model = new GoodsModel();
  44. $model->expire_time = $expireTime;
  45. $model->time_limit_days = $timeLimitDays;
  46. $model->time_limit_num = $timeLimitNum;
  47. $model->can_use_coupon = $canUseCoupon;
  48. $model->type = $type;
  49. $model->inventory = $inventory;
  50. $model->store_id = $storeId;
  51. $model->market_id = $marketId;
  52. $model->market_ids = json_encode(["$marketId"]);
  53. $model->category_id = $goods->category_id;
  54. $model->name = $goods->name;
  55. $model->cover_img = $goods->cover_img;
  56. $model->goods_unit = $goods->goods_unit;
  57. $model->tags = $goods->tags;
  58. $model->spec = $goods->spec;
  59. $model->details_imgs = $goods->details_imgs;
  60. $model->content = $goods->content;
  61. $model->details = $goods->details;
  62. $model->price = $goods->price;
  63. $model->original_price = $goods->original_price;
  64. $model->vip_price = $goods->vip_price;
  65. $model->start_num = $goods->start_num;
  66. $model->restrict_num = $goods->restrict_num;
  67. $model->is_infinite = $goods->is_infinite;
  68. $model->on_sale = 0;
  69. $model->sort = $goods->sort;
  70. $model->remark = $goods->remark;
  71. if($model->save() && !empty($goodsBanners)){
  72. $goodsId = $model->getKey();
  73. $banners = [];
  74. foreach($goodsBanners as $kb => $vb){
  75. $banners[] = [
  76. 'goods_id' => $goodsId,
  77. 'type' => $vb->type,
  78. 'path' => $vb->path,
  79. 'sort' => $vb->sort,
  80. 'created_at' => time(),
  81. 'updated_at' => time(),
  82. ];
  83. GoodsBannerModel::insert($banners);
  84. }
  85. };
  86. }
  87. return $this->success('复制成功', '/goods_activity');
  88. }
  89. /**
  90. * Build a form here.
  91. */
  92. public function form()
  93. {
  94. $id = $this->payload['id'] ?? 0;
  95. $name = $this->payload['name'] ?? '';
  96. $this->hidden('id')->value($id);
  97. $this->display('name')->value($name)->help('复制的活动商品默认下架状态,请核对信息后手动上架!');
  98. $stores = StoreModel::getStoreArray();
  99. $this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!');
  100. $this->number('inventory','库存')->required()->attribute('min', 1)->default(1);
  101. $this->datetime('expire_time','活动结束时间')->required()->format('YYYY-MM-DD HH:mm:ss')->rules('after:now',['after'=>'活动结束时间必须大于当前时间!']);
  102. $this->number('time_limit_days','限制的天数')->default(1)->help('A时间段内限购');
  103. $this->number('time_limit_num','限制购买数量')->default(1)->help('A时间段内限购的数量');
  104. $this->switch('can_use_coupon','可同时使用优惠券')->default(0);
  105. $this->select('type','活动类型')->options(GoodsModel::$_TYPE)->default('flash_sale');
  106. }
  107. /**
  108. * The data of the form.
  109. *
  110. * @return array
  111. */
  112. public function default()
  113. {
  114. return [];
  115. }
  116. }