Browse Source

1、订单增加了根据优惠券计算价格,2、订单提交完善,增加销量统计和资金流水;

dev
李可松 4 years ago
parent
commit
e0db6049ec
  1. 104
      app/Http/Controllers/Api/OrderController.php

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

@ -4,8 +4,10 @@ namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\UserMoneyLog;
use App\Models\Order; use App\Models\Order;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/** /**
* 订单 * 订单
@ -64,54 +66,106 @@ class OrderController extends Controller
'num.min' => '购买数量输入错误', 'num.min' => '购买数量输入错误',
]); ]);
$product = AgentProduct::where('id', $formData['id'])->with('product')->first();
if (!$product) {
$agent_product = AgentProduct::query()
->where('id', $formData['id'])
->with('coupon')
->with('product')
->first();
if (!$agent_product || !$agent_product->product) {
$this->error('产品不存在'); $this->error('产品不存在');
} }
$price = $product->price; //TODO 1、成交价格还要扣除优惠券;2、产品表加库存;3、生成资金流水;4、开启事务;
Order::create([
'user_id' => $this->user_id,
'order_no' => $this->getOrderNo(),
'num' => $formData['num'],
'price' => $price,
'name' => $formData['name'],
'mobile' => $formData['mobile'],
'title' => $product->product->title,
'agent_product_id' => $product->id,
'product_id' => $product->product_id,
'status' => 0,
'pay_type' => $formData['pay_type'],
]);
DB::beginTransaction();
try {
$price = $this->calc($agent_product, $formData['num']);
$title = $agent_product->product->title; //产品标题
//增加销量
$agent_product->increment('sale', $formData['num']); //代理商产品表销量
$agent_product->product->increment('sale', $formData['num']); //供应商产品表
// 存入订单表
$order = Order::query()->create([
'user_id' => $this->user_id,
'order_no' => $this->getOrderNo(),
'num' => $formData['num'],
'price' => $price,
'name' => $formData['name'],
'mobile' => $formData['mobile'],
'title' => $title,
'agent_product_id' => $agent_product->id,
'product_id' => $agent_product->product_id,
'status' => 0,
'pay_type' => $formData['pay_type'],
]);
//资金流水
UserMoneyLog::query()->create([
'user_id' => $this->user_id,
'agent_id' => $this->agent_id,
'money' => -$price,
'order_id' => $order->id,
'type' => 1,
'desc' => '购买产品:' . $title,
'created_at' => time(), //已关闭模型自动写入时间
]);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
return $e->getMessage();
}
return $this->success(); return $this->success();
} }
//获取应付金额 //获取应付金额
public function getPrice(Request $request) public function getPrice(Request $request)
{ {
$id = $request->input('id');
if (!$id || !ctype_digit($id)) {
return $this->error('未指定ID');
}
$id = (int)$request->input('id');
$num = (float)$request->input('num'); $num = (float)$request->input('num');
if (!$num || $num < 1) { if (!$num || $num < 1) {
return $this->error('未指定产品数量'); return $this->error('未指定产品数量');
} }
return $this->success(['price' => $this->calc($id, $num)]);
$agent_product = AgentProduct::query()
->with(['coupon' => fn($query) => $query->select('*')])
->find($id);
$final_price = $this->calc($agent_product, $num);
return $this->success(['price' => $final_price]);
} }
//计算价格
private function calc($agent_id, $num)
/**
* 计算最终价格(扣除优惠券之后的价格)
* @param $agent_product
* @param $num
* @return float
*/
private function calc($agent_product, $num)
{ {
return 2888.99; // TODO 此处需要详细计算,并扣除优惠价等之后的价格
$total_price = $agent_product->price * $num;
//没有任何优惠券时直接返回最终价
if ($agent_product->coupon->isEmpty()) {
return $total_price;
}
$coupon = $agent_product->coupon->toArray();
foreach ($coupon as $v) {
if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减
if ($total_price >= $v['detail']['full']) {
$total_price -= $v['detail']['reduction'];
}
} else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折
$total_price *= $v['detail']['discount'];
}
}
return round($total_price, 2);
} }
// 生成订单号 // 生成订单号
private function getOrderNo() private function getOrderNo()
{ {
list($micro, $sec) = explode(' ', microtime()); list($micro, $sec) = explode(' ', microtime());
return date('YmdHis', $sec) . str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT) . mt_rand(100, 999);
$micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT);
return date('YmdHis', $sec) . $micro . mt_rand(100, 999);
} }
} }
Loading…
Cancel
Save