php - 重用 Laravel 模型创建代码的代码
问题描述
我使用这个自定义类代码来处理 Laravel 中不同类型的订单。此问题包括有关订单类型 A 的代码。
我想知道如何在创建对象时重用代码。因为我看到代码中有很多重复。
我有不同的订单类型,功能相同但行为不同。这就是我使用抽象类来扩展所有订单类型的原因。
abstract class BaseOrder
{
abstract public function getOrder(Order $order);
abstract public function storeOrder(Request $request);
...
}
class OrderTypeA extends BaseOrder
{
private $type = 'A';
public function storeOrder($request)
{
$returnedValues = DB::transaction(function () use ($request) {
$order = new Order;
$order->type = $this->type;
$order->from = $request->json('from');
$order->amount = $request->json('amount');
$order->status = 2;
$order->save();
foreach ($request->json('items') as $item) {
$order->items()->create([
'product_id' => $item['product_id'],
'quantity' => $item['quantity'],
'total' => $item['total'],
]);
}
return compact('order');
});
return $returnedValues['order']->load('items');
}
public function updateOrder($request)
{
$returnedValues = DB::transaction(function () use ($request) {
if ($request->id) {
$order = Order::findOrFail($request->id);
$order->amount = $request->json('amount');
$order->status = 1;
$order->save();
$order->items()->delete();
foreach ($request->json('items') as $item) {
$order->items()->create([
'product_id' => $item['product_id'],
'quantity' => $item['quantity'],
'total' => $item['total'],
]);
}
} else {
$order = new Order;
$order->type = $this->type;
$order->from = $request->json('from');
$order->to = null;
$order->amount = $request->json('amount');
$order->status = 1;
$order->save();
foreach ($request->json('items') as $item) {
$order->items()->create([
'product_id' => $item['product_id'],
'quantity' => $item['quantity'],
'total' => $item['total'],
]);
}
}
return compact('order');
});
return $returnedValues['order']->load('items');
}
public function generateOrder($customer)
{
$order = new stdClass();
$order->id = null;
$order->type = $this->type;
$order->from = $customer->id;
$order->to = null;
$order->status = 1;
$totalAmount = 0;
$items = [];
$stocks = Stock::where('type', 1)
->where('customer_id', $customer->id)
->whereRaw('stock <= reorder_level')
->get();
if (count($stocks) > 0) {
foreach ($stocks as $key => $stock) {
if ($stock->minimum_order_quantity != 0) {
$priceListItem = PriceList::where('product_id', $stock->product_id)->latest()->first();
$item = new stdClass;
$item->id = $key;
$item->product_id = $stock->product_id;
$item->product = Product::findOrFail($stock->product_id);
$item->order_id = null;
$item->quantity = (int) $stock->minimum_order_quantity;
$item->total = ((int) $stock->minimum_order_quantity * ((float) $priceListItem->price));
$item->is_checked = true;
$item->created_at = null;
$item->updated_at = null;
$totalAmount += $item->total;
$items[] = $item;
}
}
}
$order->amount = $totalAmount;
$order->items = $items;
return $order;
}
}
解决方案
推荐阅读
- javascript - 你能在 Javascript 的 forEach 方法中调用 for 循环吗?
- javascript - 无法加载和显示作为第三方脚本加载的前端 React 元素
- python - 将对象数据类型列转换为整数错误
- c++ - 函数 alnum 未显示正确的金额
- python - 如何将时间字符串合并到分类器中
- javascript - 如何使用两个回调(WIX corvid)在 JavaScript 中返回异步调用
- r - 如何在ggplot中为每个离散变量生成3个箱线图?
- version-control - 控制跟踪的外部依赖版本
- reactjs - 使用 Spring Boot 后端在 React 应用程序中创建报告和仪表板
- if-statement - JAGS/BUGS 中的 if-else 条件