目录
1. 新增商品操作文章来源:https://www.toymoban.com/news/detail-730535.html
2. 修改商品操作文章来源地址https://www.toymoban.com/news/detail-730535.html
1. 新增商品操作
// 原则,所有服务类只有一个public入口,或者多个public入口,但是他们做都是同一件事情
Class CreateService {
// 创建类的入口, 根据dto去新建
public function create(Dto $dto){
$user = User::find($dto->getUserId());
// 这里做一些系列的参数验证,此处省略...
// 先构建商品model对象, 不要在事务期间构建,减少事务等待
$good = $this->buildGood($user, $dto);
// 构建商品的属性(一对多)
$good->setRelation('attributes', $this->buildAttributes($dto));
// 构建商品的活动(一对多)
$good->setRelation('activities', $this->buildActivities($dto));
DB::transaction(function () use (
$good
) {
// 保存商品model对象
// 主表
$good->save();
// 批量插入商品属性表
$good->attributes()->saveMany(
$good->attributes->all()
);
// 批量插入商品活动表
$good->activities()->saveMany(
$good->activities->all()
);
// 触发商品新增事件,要delay延迟事件,确保事务提交后触发
delay_event(new GoodCreated());
});
}
// 根据Good类去新建
public function createByGood(Good $good) {
// ....
}
/** 构建商品属性对象
* @param Dto $dto
* @return Collection
*/
protected function buildAttributes(
Dto $dto
): Collection {
$newCollection = new Collection();
// 获取页面提交的属性名称列表
$$list = $dto->getAttributes();
if (empty($list)) {
return $newCollection;
}
foreach ($list as $name) {
$newCollection->add(new GoodAttribute([
'name' => $name,
]));
}
return $newCollection;
}
// 构建商品活动对象
protected function buildActivities(
Dto $dto
): Collection {
$newCollection = new Collection();
if (!empty($dto->getAtivityId())) {
$newCollection->add(
new GoodActivity(
[
'activity_id' => $dto->getAtivityId(),
]
)
);
}
return $newCollection;
}
/** 构建商品对象
* @param User $user
* @param Dto $dto
* @return Good
*/
protected function buildGood(
User $user,
Dto $dto
): Good {
return new Good([
'updated_at' => $dto->getDataUpdatedAt(),
'user_id' => $user->id,
'user_name' => $user->username,
]);
}
}
class Dto
{
protected $attributes;
protected $dataUpdatedAt;
protected $activityId;
protected $userId;
public static function build(array $params): Dto {
$self = new self();
$self->attributes = (array)($params['attributes'] ?? []);
$self->dataUpdatedAt = Carbon::now()->toDatetimeString();
$self->activityId = intval($params['activity_id'] ?? 0);
$self->userId = intval($params['user_id'] ?? 0);
return $self;
}
}
// controller
GoodController {
// 商品新增执行入口
public function create(Request $request, CreateService $createService): array {
$service->create(
Dto::build($request->validated())
);
return ['code' => 200];
}
}
2. 修改商品操作
<?php
// 原则,所有服务类只有一个public入口,或者多个public入口,但是他们做都是同一件事情
Class UpdateService {
// 创建类的入口, 根据dto去新建
public function update(Dto $dto){
$good = Good::find($dto->getGoodId());
$good->loadMissing([
'attributes',
'activities',
]);
$user = User::find($dto->getUserId());
// 这里做一些系列的参数验证,此处省略...
// 先构建商品model对象, 不要在事务期间构建,减少事务等待
$good = $this->buildGood($good, $user, $dto);
// 构建商品的属性(一对多)
$good->setRelation('attributes', $this->buildAttributes($good, $dto));
// 构建商品的活动(一对多)
$good->setRelation('activities', $this->buildActivities($good, $dto));
DB::transaction(function () use (
$good
) {
// 保存商品model对象
// 主表
$good->save();
// 批量插入商品属性表
$good->attributes()->saveMany(
$good->attributes->all()
);
// 删除没选中的商品属性
$good->attributes()->whereNotIn(
'id',
$good->attributes->pluck('id')->filter()->unique()->all()
)->delete();
// 批量插入商品活动表
$good->activities()->saveMany(
$good->activities->all()
);
// 删除没选中的商品活动
$good->activities->whereNotIn(
'id',
$good->activities->pluck('id')->filter()->unique()->all()
);
// 触发商品修改事件,要delay延迟事件,确保事务提交后触发
delay_event(new GoodUpdated());
});
}
// 根据product类去新建
public function updateByGood(Good $product, array $params) {
}
/** 构建商品属性对象
* @param Dto $dto
* @return Collection
*/
protected function buildAttributes(
Good $good,
Dto $dto
): Collection {
$newCollection = new Collection();
// 获取页面提交的属性名称列表
$list = $dto->getAttributes();
if (empty($list)) {
return $newCollection;
}
$oldAttributes = $good->attributes->keyBy('name');
foreach ($list as $name) {
// 按商品属性名字取查找,如果已经添加,就不需要新建了
if ($attribute = $oldAttributes->get($name)) {
$newCollection->add($attribute);
} else {
// 没有添加,则新建
$newCollection->add(new GoodAttribute([
'name' => $name,
]));
}
}
return $newCollection;
}
// 构建商品活动对象
protected function buildActivities(
Good $good,
Dto $dto
): Collection {
$newCollection = new Collection();
$oldActivities = $good->activities->keyBy('activity_id');
if (empty($dto->getAtivityId())) {
return $newCollection;
}
if ($activity = $oldActivities->get($dto->getAtivityId())) {
// 已经添加过活动,就无需再新增
$newCollection->add($activity);
} else {
// 新增活动
$newCollection->add(
new GoodActivity(
[
'activity_id' => $dto->getAtivityId(),
]
)
);
}
return $newCollection;
}
/** 构建商品对象
* @param User $user
* @param Dto $dto
* @return Good
*/
protected function buildGood(
Good $good,
User $user,
Dto $dto
): Good {
return $good->fill([
'updated_at' => $dto->getDataUpdatedAt(),
'user_id' => $user->id,
'user_name' => $user->username,
]);
}
}
class Dto
{
protected $attributes;
protected $dataUpdatedAt;
protected $activityId;
protected $userId;
protected $goodId;
public static function build(array $params): Dto {
$self = new self();
$self->attributes = (array)($params['attributes'] ?? []);
$self->dataUpdatedAt = Carbon::now()->toDatetimeString();
$self->activityId = intval($params['activity_id'] ?? 0);
$self->userId = intval($params['user_id'] ?? 0);
$self->goodId = intval($params['good_id'] ?? 0);
return $self;
}
// ...一下实现获取属性值的方法,此处省略
}
// controller
class GoodController {
// 商品修改执行入口
public function update(Request $request, UpdateService $updateService): array {
$service->update(
Dto::build($request->validated())
);
return ['code' => 200];
}
}
到了这里,关于Laravel一些优雅的写法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!