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
157 lines
5.6 KiB
<?php
|
|
/**
|
|
* 活动商品复制表单
|
|
*/
|
|
namespace App\Admin\Forms\v3;
|
|
|
|
use App\Admin\Common\CustomFileController;
|
|
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('活动结束时间必须大于当前时间!');
|
|
}
|
|
$ossImageDir = config('admin.upload.directory.image');
|
|
$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){
|
|
$newCoverImg = $this->copyImage($ossImageDir,$goods->cover_img);
|
|
$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->goods_id = 0;
|
|
$model->store_id = $storeId;
|
|
$model->market_id = $marketId;
|
|
$model->market_ids = ["$marketId"];
|
|
$model->category_id = $goods->category_id;
|
|
|
|
$model->name = $goods->name;
|
|
$model->cover_img = $newCoverImg;
|
|
$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();
|
|
foreach($goodsBanners as $kb => $vb){
|
|
$newBanner = $this->copyImage($ossImageDir, $vb->path);
|
|
if(!empty($newBanner)){
|
|
$banners = [
|
|
'goods_id' => $goodsId,
|
|
'type' => $vb->type,
|
|
'path' => $newBanner,
|
|
'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');
|
|
$this->disableResetButton();
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* 复制的商品图片
|
|
* @param $ossImageDir oss 存储文件夹
|
|
* @param $imagePath 图片绝对路径
|
|
*/
|
|
public function copyImage($ossImageDir, $imagePath)
|
|
{
|
|
$return = '';
|
|
$oldCoverImg = $imagePath;
|
|
|
|
$customFile = new CustomFileController();
|
|
$result = $customFile->autoCopyFile($ossImageDir, $oldCoverImg);
|
|
if($result['status']){
|
|
$return = $result['path'];
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
}
|