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.
|
|
<?php/** * 活动商品复制表单 */namespace App\Admin\Forms\v3;
use Dcat\Admin\Models\Administrator;use Dcat\Admin\Traits\LazyWidget;use Dcat\Admin\Widgets\Form;use Dcat\Admin\Contracts\LazyRenderable;use Symfony\Component\HttpFoundation\Response;use App\Models\v3\Store as StoreModel;use App\Models\v3\GoodsActivity as GoodsModel;use App\Models\v3\GoodsActivityBanners as GoodsBannerModel;
class GoodsActivityCopyForm extends Form implements LazyRenderable{ use LazyWidget;
/** * Handle the form request. * * @param array $input * * @return Response */ public function handle(array $input) { // 获取外部传递参数
$goodsId = $input['id']; $storeIds= $input['store_ids']; $expireTime = $input['expire_time']; $timeLimitDays = $input['time_limit_days']; $timeLimitNum = $input['time_limit_num']; $canUseCoupon = $input['can_use_coupon']; $type = $input['type']; $inventory = $input['inventory'];
if($expireTime <= time()){ $this->error('活动结束时间必须大于当前时间!'); }
$goods = GoodsModel::find($goodsId); $goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get(); $markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id'); foreach($storeIds as $key =>$storeId){ $marketId = $markets[$storeId]??0; $model = new GoodsModel();
$model->expire_time = $expireTime; $model->time_limit_days = $timeLimitDays; $model->time_limit_num = $timeLimitNum; $model->can_use_coupon = $canUseCoupon; $model->type = $type; $model->inventory = $inventory;
$model->store_id = $storeId; $model->market_id = $marketId; $model->market_ids = json_encode(["$marketId"]); $model->category_id = $goods->category_id;
$model->name = $goods->name; $model->cover_img = $goods->cover_img; $model->goods_unit = $goods->goods_unit;
$model->tags = $goods->tags; $model->spec = $goods->spec; $model->details_imgs = $goods->details_imgs; $model->content = $goods->content; $model->details = $goods->details;
$model->price = $goods->price; $model->original_price = $goods->original_price; $model->vip_price = $goods->vip_price;
$model->start_num = $goods->start_num; $model->restrict_num = $goods->restrict_num; $model->is_infinite = $goods->is_infinite;
$model->on_sale = 0; $model->sort = $goods->sort; $model->remark = $goods->remark;
if($model->save() && !empty($goodsBanners)){ $goodsId = $model->getKey(); $banners = []; foreach($goodsBanners as $kb => $vb){ $banners[] = [ 'goods_id' => $goodsId, 'type' => $vb->type, 'path' => $vb->path, 'sort' => $vb->sort, 'created_at' => time(), 'updated_at' => time(), ]; GoodsBannerModel::insert($banners); } }; }
return $this->success('复制成功', '/goods_activity'); }
/** * Build a form here. */ public function form() { $id = $this->payload['id'] ?? 0; $name = $this->payload['name'] ?? ''; $this->hidden('id')->value($id); $this->display('name')->value($name)->help('复制的活动商品默认下架状态,请核对信息后手动上架!'); $stores = StoreModel::getStoreArray(); $this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!'); $this->number('inventory','库存')->required()->attribute('min', 1)->default(1); $this->datetime('expire_time','活动结束时间')->required()->format('YYYY-MM-DD HH:mm:ss')->rules('after:now',['after'=>'活动结束时间必须大于当前时间!']); $this->number('time_limit_days','限制的天数')->default(1)->help('A时间段内限购'); $this->number('time_limit_num','限制购买数量')->default(1)->help('A时间段内限购的数量'); $this->switch('can_use_coupon','可同时使用优惠券')->default(0); $this->select('type','活动类型')->options(GoodsModel::$_TYPE)->default('flash_sale'); }
/** * The data of the form. * * @return array */ public function default() { return []; }
}
|