diff --git a/MySQL_change.sql b/MySQL_change.sql index 6375bcc..0dce124 100644 --- a/MySQL_change.sql +++ b/MySQL_change.sql @@ -140,3 +140,8 @@ ALTER TABLE `order_product_items` ADD COLUMN `product_type` TINYINT NOT NULL DEFAULT 0 COMMENT '供应商产品的type' AFTER `agent_product_spec_id`, CHANGE COLUMN `product_spec_id` `product_spec_id` BIGINT NOT NULL DEFAULT 0 COMMENT '供应商产品规格ID' AFTER `product_type`; +# 17:03 2021/9/26 +ALTER TABLE `order_product_items` + CHANGE COLUMN `agent_product_spec_id` `agent_product_spec_id` JSON NOT NULL COMMENT '代理商产品规格ID' AFTER `price`, + CHANGE COLUMN `product_spec_id` `product_spec_id` JSON NOT NULL COMMENT '供应商产品规格ID' AFTER `product_type`; + diff --git a/app/Http/Controllers/Api/OrderController.php b/app/Http/Controllers/Api/OrderController.php index 9c229d9..c4afd90 100644 --- a/app/Http/Controllers/Api/OrderController.php +++ b/app/Http/Controllers/Api/OrderController.php @@ -112,7 +112,7 @@ class OrderController extends Controller public function create(Request $request) { $formData = $request->input(); - $formData = array_map(fn($v) => trim($v), $formData); //过滤,删除首尾空 + $formData = array_map(fn($v) => is_string($v) ? trim($v) : $v, $formData); //过滤,删除首尾空 //表单验证 $pay_type_values = join(',', array_keys(PayType::array())); @@ -122,7 +122,7 @@ class OrderController extends Controller 'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'], 'pay_type' => ['required', 'in:' . $pay_type_values], 'num' => ['required', 'min:1'], - 'spec_id' => ['required', 'min:1', 'integer'], + 'spec_id' => ['required', 'array'], 'id_card' => ['regex:/^\d{17}[\dXx]$/'], ], [ 'id.required' => '未指定产品ID', @@ -135,7 +135,8 @@ class OrderController extends Controller 'pay_type.in' => '不存在此支付方式', 'num.required' => '请输入购买数量', 'num.min' => '购买数量输入错误', - 'spec_id.*' => '请选择产品规格', + 'spec_id.required' => '请选择产品规格', + 'spec_id.array' => '产品规格必须是数组', 'id_card.regex' => '身份证号输入不正确', ]); @@ -158,15 +159,27 @@ class OrderController extends Controller if ($ap->type == 0) { $spec = AgentProductSpec::with('productSpec') ->where('agent_product_id', $formData['id']) - ->find($formData['spec_id']); - if (!$spec) { + ->whereIn('id', $formData['spec_id']) + ->get(); + if ($spec->isEmpty()) { return $this->error('你选择的产品规格不存在'); } - $order_price = $spec->price * $formData['num']; //订单总价 + $spec = $spec->toArray(); + $spec = array_map(function ($v) { + $v = array_merge($v['product_spec'] ?? [], $v); + unset($v['product_spec']); + return $v; + }, $spec); + + $order_price = 0; //订单总价 + foreach ($spec as $v) { + $order_price += $v['price'] * $formData['num']; + } //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type') switch ($ap->product->type) { case 0: + case 2: case 3: case 4: case 5: @@ -198,25 +211,13 @@ class OrderController extends Controller if (empty($formData['id_card'])) { return $this->error('请输入身份证号'); } - $order_info['check_in_time'] = $formData['departure_time']; //入住时间 - $order_info['check_out_time'] = $formData['return_time']; //离店时间 + $order_info['check_in_time'] = $formData['check_in_time']; //入住时间 + $order_info['check_out_time'] = $formData['check_out_time']; //离店时间 $order_info['arrival_time'] = $formData['arrival_time']; //到店时间 $order_info['id_card'] = $formData['id_card']; //身份证号 $order_info['sex'] = $formData['sex'] ?? 0; //性别 $order_info['age'] = $formData['age'] ?? 0; //年龄 break; - case 2: - if (empty($formData['enter_time']) || !strtotime($formData['enter_time'])) { - return $this->error('请选择入园时间'); - } - if (empty($formData['id_card'])) { - return $this->error('请输入身份证号'); - } - $order_info['enter_time'] = $formData['enter_time']; //到店时间 - $order_info['id_card'] = $formData['id_card']; //身份证号 - $order_info['sex'] = $formData['sex'] ?? 0; //性别 - $order_info['age'] = $formData['age'] ?? 0; //年龄 - break; } } else { return $this->error('不存在此类型产品'); @@ -247,11 +248,12 @@ class OrderController extends Controller } //供应商产品规格减表减库存 + $product_spec_ids = array_column($spec, 'product_spec_id'); $affect_row = ProductSpec::query() ->where('stock', '>=', $formData['num']) //乐观锁 - ->where('id', $spec['product_spec_id']) + ->whereIn('id', $product_spec_ids) ->decrement('stock', $formData['num']); - if (!$affect_row) { + if ($affect_row != count($product_spec_ids)) { throw new \Exception('你选择的供应产品规格库存不足'); } @@ -265,11 +267,12 @@ class OrderController extends Controller } //代理商产品规格表减库存 + $agent_product_spec_ids = array_column($spec, 'id'); $affect_row = AgentProductSpec::query() ->where('stock', '>=', $formData['num']) //乐观锁 - ->where('id', $spec['id']) + ->whereIn('id', $agent_product_spec_ids) ->decrement('stock', $formData['num']); - if (!$affect_row) { + if ($affect_row != count($agent_product_spec_ids)) { throw new \Exception('你选择的商户产品规格库存不足'); } @@ -325,9 +328,9 @@ class OrderController extends Controller 'product_id' => $ap->product->id, 'num' => $formData['num'], 'price' => $ap->product->price, - 'agent_product_spec_id' => $formData['spec_id'], + 'agent_product_spec_id' => json_encode($agent_product_spec_ids), 'product_type' => $ap->product->type, - 'product_spec_id' => $spec->productSpec->id, + 'product_spec_id' => json_encode($product_spec_ids), 'service_persons' => $ap->product->service_persons, ]); diff --git a/app/Models/OrderProductItem.php b/app/Models/OrderProductItem.php index a0ee23e..8309a29 100644 --- a/app/Models/OrderProductItem.php +++ b/app/Models/OrderProductItem.php @@ -9,6 +9,7 @@ class OrderProductItem extends BaseModel use HasFactory; protected $guarded = ['id']; //不允许批量赋值的字段 + protected $casts = ['agent_product_spec_id' => 'json', 'product_spec_id' => 'json']; public function __construct(array $attributes = []) {