Browse Source

创建订单增加agent_cloud_pid和agent_cloud_price字段的保存,并优化库存判断

develop
李可松 4 years ago
parent
commit
70fea770c6
  1. 12
      app/Http/Controllers/Api/OrderController.php
  2. 5
      app/Models/AgentProduct.php

12
app/Http/Controllers/Api/OrderController.php

@ -128,17 +128,12 @@ class OrderController extends Controller
$ap = AgentProduct::query() $ap = AgentProduct::query()
->where('id', $formData['id']) ->where('id', $formData['id'])
->with('coupon')
->with('product')
->with(['coupon', 'product', 'agentCloudProduct:id,price'])
->has('product') ->has('product')
->first(); ->first();
if (!$ap || !$ap->product) { if (!$ap || !$ap->product) {
$this->error('产品不存在或已下架'); $this->error('产品不存在或已下架');
} }
//TODO 供应商产品需要判断组合产品库存
if ($ap->stock < $formData['num'] || $ap->product->stock < $formData['num']) {
$this->error('对不起,库存不足');
}
$coupon_ids = []; $coupon_ids = [];
if ($ap->coupon) { if ($ap->coupon) {
@ -150,7 +145,6 @@ class OrderController extends Controller
DB::beginTransaction(); DB::beginTransaction();
try { try {
$price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap); $price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap);
$title = $ap->title; //产品标题
//供应商产品表减库存 //供应商产品表减库存
$product_ids = explode(',', $ap->product_ids); $product_ids = explode(',', $ap->product_ids);
@ -184,7 +178,7 @@ class OrderController extends Controller
'price' => $price, 'price' => $price,
'name' => $formData['name'], 'name' => $formData['name'],
'mobile' => $formData['mobile'], 'mobile' => $formData['mobile'],
'title' => $title,
'title' => $ap->title,
'picture' => $ap->picture, 'picture' => $ap->picture,
'agent_product_id' => $ap->id, 'agent_product_id' => $ap->id,
'product_id' => $ap->product_id, 'product_id' => $ap->product_id,
@ -194,6 +188,8 @@ class OrderController extends Controller
'coupon_id' => join(',', $coupon_ids), 'coupon_id' => join(',', $coupon_ids),
'guide_id' => $ap->guide_id, 'guide_id' => $ap->guide_id,
'timeout' => $timeout, 'timeout' => $timeout,
'agent_cloud_pid' => $ap->agent_cloud_pid,
'agent_cloud_price' => $ap->agentCloudProduct->price,
]); ]);
//存入订单产品表 //存入订单产品表

5
app/Models/AgentProduct.php

@ -52,6 +52,11 @@ class AgentProduct extends BaseModel
return $this->hasMany(AgentProductItem::class); return $this->hasMany(AgentProductItem::class);
} }
public function agentCloudProduct()
{
return $this->belongsTo(self::class, 'agent_cloud_pid', 'id');
}
public function setChannelIdAttribute($value) public function setChannelIdAttribute($value)
{ {
$this->attributes['channel_id'] = is_array($value) ? join(',', array_filter($value)) : $value; $this->attributes['channel_id'] = is_array($value) ? join(',', array_filter($value)) : $value;

Loading…
Cancel
Save