2025-05-19 16:54:25 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service\CodeGeneration;
|
|
|
|
|
|
|
|
|
|
|
|
use App\BaseApp\BaseService;
|
2025-05-21 17:04:10 +08:00
|
|
|
|
use App\Enum\order\OrderStatusEnum;
|
|
|
|
|
|
use App\Models\OrderDetailModel;
|
2025-05-20 16:52:47 +08:00
|
|
|
|
use App\Models\OrderModel;
|
2025-05-21 17:04:10 +08:00
|
|
|
|
use App\Models\ProductModel;
|
|
|
|
|
|
use App\Models\SkuModel;
|
|
|
|
|
|
use App\Models\UserModel;
|
|
|
|
|
|
use App\Models\UserReceivingAddressModel;
|
2025-05-19 16:54:25 +08:00
|
|
|
|
use Exception;
|
2025-05-21 17:04:10 +08:00
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2025-05-19 16:54:25 +08:00
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
|
|
|
2025-05-20 16:52:47 +08:00
|
|
|
|
class OrderService extends BaseService
|
2025-05-19 16:54:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
|
{
|
|
|
|
|
|
parent::__construct();
|
2025-05-20 16:52:47 +08:00
|
|
|
|
$this->selectField = ["id", "user_id", "order_no", "order_status", "total_amount", "status", "created_at", "updated_at", "deleted_at"];
|
|
|
|
|
|
$this->queryField = ['user_id' => '=','order_no' => 'like','order_status' => '=,status='];
|
|
|
|
|
|
$this->model = OrderModel::class;
|
2025-05-21 17:04:10 +08:00
|
|
|
|
$this->with = [
|
|
|
|
|
|
'user:id,nick_name,avatar'
|
|
|
|
|
|
];
|
2025-05-19 16:54:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 16:52:47 +08:00
|
|
|
|
* 获取订单管理列表
|
2025-05-19 16:54:25 +08:00
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function list(): array
|
|
|
|
|
|
{
|
2025-05-21 17:04:10 +08:00
|
|
|
|
$result = $this->getPageList();
|
|
|
|
|
|
foreach ($result['items'] as &$v) {
|
|
|
|
|
|
$v['status_text'] = OrderStatusEnum::from($v['status'])->description();
|
|
|
|
|
|
$v['status_color'] = OrderStatusEnum::from($v['status'])->getColor();
|
|
|
|
|
|
}
|
|
|
|
|
|
return $result;
|
2025-05-19 16:54:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 16:52:47 +08:00
|
|
|
|
* 获取订单管理详情
|
2025-05-19 16:54:25 +08:00
|
|
|
|
* @param $id
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function detail($id)
|
|
|
|
|
|
{
|
2025-05-21 17:04:10 +08:00
|
|
|
|
$this->selectField[] = 'recipient';
|
|
|
|
|
|
$this->with[] = 'detail:id,order_id,product_id,sku_id,price,quantity,subtotal,snapshot';
|
|
|
|
|
|
$result = $this->getDetail($id);
|
|
|
|
|
|
$result['status_text'] = OrderStatusEnum::from($result['status'])->description();
|
|
|
|
|
|
$result['status_color'] = OrderStatusEnum::from($result['status'])->getColor();
|
|
|
|
|
|
$result['recipient'] = json_decode($result['recipient'], true);
|
|
|
|
|
|
foreach ($result['detail'] as &$v) {
|
|
|
|
|
|
$v['snapshot'] = !empty($v['snapshot']) ? json_decode($v['snapshot'], true) : [];
|
|
|
|
|
|
}
|
|
|
|
|
|
return $result;
|
2025-05-19 16:54:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 16:52:47 +08:00
|
|
|
|
* 订单管理下拉列表
|
2025-05-19 16:54:25 +08:00
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function option()
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->optionField = ['id', 'name'];
|
|
|
|
|
|
return $this->getOption();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 16:52:47 +08:00
|
|
|
|
* 创建订单管理
|
2025-05-19 16:54:25 +08:00
|
|
|
|
* @param $params
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function create($params): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->insert($params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 16:52:47 +08:00
|
|
|
|
* 编辑订单管理
|
2025-05-19 16:54:25 +08:00
|
|
|
|
* @param $id
|
|
|
|
|
|
* @param $params
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function update($id, $params): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->save($id, $params);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 16:52:47 +08:00
|
|
|
|
* 删除订单管理
|
2025-05-19 16:54:25 +08:00
|
|
|
|
* @param $ids
|
|
|
|
|
|
* @return mixed|true
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function delete($ids): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->del($ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-21 17:04:10 +08:00
|
|
|
|
public function generateOrder($params)
|
|
|
|
|
|
{
|
|
|
|
|
|
$userIds = UserModel::pluck('id');
|
|
|
|
|
|
$userId = $userIds->random();
|
|
|
|
|
|
|
|
|
|
|
|
$skuIds = array_column($params['products'], 'sku_id');
|
|
|
|
|
|
$skuQuantity = array_column($params['products'], 'number', 'sku_id');
|
|
|
|
|
|
|
|
|
|
|
|
$skus = SkuModel::with('product')->whereIn('id', $skuIds)->get();
|
|
|
|
|
|
if (empty($skus)) {
|
|
|
|
|
|
$this->utils->errorThrow('商品sku不存在!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$skus = $skus->toArray();
|
|
|
|
|
|
|
|
|
|
|
|
$skuGetIds = array_column($skus, 'id');
|
|
|
|
|
|
// 对比skuIds和skuGetIds如果传入的skuId不存在于skuGetIds中,则抛出异常
|
|
|
|
|
|
$diffSkuIds = array_diff($skuIds, $skuGetIds);
|
|
|
|
|
|
if ($diffSkuIds) {
|
|
|
|
|
|
$this->utils->errorThrow('商品sku不存在!SkuId:'. implode(',', $diffSkuIds));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取收件人地址
|
|
|
|
|
|
$recipient = $this->getRecipientInfo($params['address_id']?? 0);
|
|
|
|
|
|
|
|
|
|
|
|
// 生成订单号
|
|
|
|
|
|
$orderNo = $this->utils->genOrderNo();
|
|
|
|
|
|
|
|
|
|
|
|
$orderInsertData = [
|
|
|
|
|
|
'user_id' => $userId,
|
|
|
|
|
|
'order_no' => $orderNo,
|
|
|
|
|
|
'total_amount' => 0,
|
|
|
|
|
|
'recipient' => $recipient,
|
|
|
|
|
|
'user_remarks' => $params['user_remarks'] ?? '',
|
|
|
|
|
|
'status' => 0,
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
try {
|
|
|
|
|
|
$totalAmount = 0;
|
|
|
|
|
|
$orderId = $this->insert($orderInsertData);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$orderId) {
|
|
|
|
|
|
$this->utils->errorThrow('订单生成失败!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$orderProducts = [];
|
|
|
|
|
|
foreach ($skus as $v) {
|
|
|
|
|
|
$subtotal = bcmul($v['price'], $skuQuantity[$v['id']], 2);
|
|
|
|
|
|
$orderProducts[] = [
|
|
|
|
|
|
'order_id' => $orderId,
|
|
|
|
|
|
'product_id' => $v['product_id'],
|
|
|
|
|
|
'sku_id' => $v['id'],
|
|
|
|
|
|
'price' => $v['price'],
|
|
|
|
|
|
'quantity' => $skuQuantity[$v['id']],
|
|
|
|
|
|
'subtotal' => $subtotal,
|
|
|
|
|
|
'snapshot' => json_encode($v),
|
|
|
|
|
|
];
|
|
|
|
|
|
$totalAmount = bcadd($totalAmount, $subtotal, 2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$orderDetailInsertModel = OrderDetailModel::insert($orderProducts);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$orderDetailInsertModel) {
|
|
|
|
|
|
$this->utils->errorThrow('订单商品生成失败!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$orderUpdateModel = $this->update($orderId, [
|
|
|
|
|
|
'total_amount' => $totalAmount,
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
if (!$orderUpdateModel) {
|
|
|
|
|
|
$this->utils->errorThrow('订单生成失败!');
|
|
|
|
|
|
}
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
$this->utils->errorThrow($e->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $orderId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getRecipientInfo($id = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (empty($id)) {
|
|
|
|
|
|
$this->utils->errorThrow('请选择收件人!');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$recipientInfo = UserReceivingAddressModel::where('id', $id)->first();
|
|
|
|
|
|
$recipientInfo['copy_text'] = "{$recipientInfo['name']} {$recipientInfo['phone']} {$recipientInfo['complete_address']}";
|
|
|
|
|
|
return json_encode($recipientInfo);
|
|
|
|
|
|
}
|
2025-05-19 16:54:25 +08:00
|
|
|
|
}
|