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

157 lines
5.6 KiB

  1. <?php
  2. /**
  3. * 活动商品复制表单
  4. */
  5. namespace App\Admin\Forms\v3;
  6. use App\Admin\Common\CustomFileController;
  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. $ossImageDir = config('admin.upload.directory.image');
  39. $goods = GoodsModel::find($goodsId);
  40. $goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get();
  41. $markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id');
  42. foreach($storeIds as $key =>$storeId){
  43. $newCoverImg = $this->copyImage($ossImageDir,$goods->cover_img);
  44. $marketId = $markets[$storeId]??0;
  45. $model = new GoodsModel();
  46. $model->expire_time = $expireTime;
  47. $model->time_limit_days = $timeLimitDays;
  48. $model->time_limit_num = $timeLimitNum;
  49. $model->can_use_coupon = $canUseCoupon;
  50. $model->type = $type;
  51. $model->inventory = $inventory;
  52. $model->goods_id = 0;
  53. $model->store_id = $storeId;
  54. $model->market_id = $marketId;
  55. $model->market_ids = ["$marketId"];
  56. $model->category_id = $goods->category_id;
  57. $model->name = $goods->name;
  58. $model->cover_img = $newCoverImg;
  59. $model->goods_unit = $goods->goods_unit;
  60. $model->tags = $goods->tags;
  61. $model->spec = $goods->spec;
  62. $model->details_imgs = $goods->details_imgs;
  63. $model->content = $goods->content;
  64. $model->details = $goods->details;
  65. $model->price = $goods->price;
  66. $model->original_price = $goods->original_price;
  67. $model->vip_price = $goods->vip_price;
  68. $model->start_num = $goods->start_num;
  69. $model->restrict_num = $goods->restrict_num;
  70. $model->is_infinite = $goods->is_infinite;
  71. $model->on_sale = 0;
  72. $model->sort = $goods->sort;
  73. $model->remark = $goods->remark;
  74. if($model->save() && !empty($goodsBanners)){
  75. $goodsId = $model->getKey();
  76. foreach($goodsBanners as $kb => $vb){
  77. $newBanner = $this->copyImage($ossImageDir, $vb->path);
  78. if(!empty($newBanner)){
  79. $banners = [
  80. 'goods_id' => $goodsId,
  81. 'type' => $vb->type,
  82. 'path' => $newBanner,
  83. 'sort' => $vb->sort,
  84. 'created_at' => time(),
  85. 'updated_at' => time(),
  86. ];
  87. GoodsBannerModel::insert($banners);
  88. }
  89. }
  90. };
  91. }
  92. return $this->success('复制成功', '/goods_activity');
  93. }
  94. /**
  95. * Build a form here.
  96. */
  97. public function form()
  98. {
  99. $id = $this->payload['id'] ?? 0;
  100. $name = $this->payload['name'] ?? '';
  101. $this->hidden('id')->value($id);
  102. $this->display('name')->value($name)->help('复制的活动商品默认下架状态,请核对信息后手动上架!');
  103. $stores = StoreModel::getStoreArray();
  104. $this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!');
  105. $this->number('inventory','库存')->required()->attribute('min', 1)->default(1);
  106. $this->datetime('expire_time','活动结束时间')->required()->format('YYYY-MM-DD HH:mm:ss')->rules('after:now',['after'=>'活动结束时间必须大于当前时间!']);
  107. $this->number('time_limit_days','限制的天数')->default(1)->help('A时间段内限购');
  108. $this->number('time_limit_num','限制购买数量')->default(1)->help('A时间段内限购的数量');
  109. $this->switch('can_use_coupon','可同时使用优惠券')->default(0);
  110. $this->select('type','活动类型')->options(GoodsModel::$_TYPE)->default('flash_sale');
  111. $this->disableResetButton();
  112. }
  113. /**
  114. * The data of the form.
  115. *
  116. * @return array
  117. */
  118. public function default()
  119. {
  120. return [];
  121. }
  122. /**
  123. * 复制的商品图片
  124. * @param $ossImageDir oss 存储文件夹
  125. * @param $imagePath 图片绝对路径
  126. */
  127. public function copyImage($ossImageDir, $imagePath)
  128. {
  129. $return = '';
  130. $oldCoverImg = $imagePath;
  131. $customFile = new CustomFileController();
  132. $result = $customFile->autoCopyFile($ossImageDir, $oldCoverImg);
  133. if($result['status']){
  134. $return = $result['path'];
  135. }
  136. return $return;
  137. }
  138. }