155 lines
5.0 KiB
PHP
155 lines
5.0 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Http\Controllers;
|
|
|
|
use App\Dicts;
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Exceptions\NotExistException;
|
|
use App\Domains\Config\Services\ConfigService;
|
|
use App\Domains\Virtual\Services\OrderService;
|
|
use App\Domains\Virtual\Services\CommonService;
|
|
use App\Domains\Virtual\Services\CompanyService;
|
|
use App\Domains\Virtual\Services\PackageService;
|
|
use App\Domains\Virtual\Repositories\OrderRepository;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
protected $request;
|
|
protected $orderService;
|
|
|
|
/**
|
|
* 构造函数,自动注入.
|
|
*/
|
|
public function __construct(Request $request, OrderService $orderService)
|
|
{
|
|
$this->request = $request;
|
|
$this->orderService = $orderService;
|
|
}
|
|
|
|
/**
|
|
* 列表.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Dicts $dicts)
|
|
{
|
|
$conditions = $this->request->all();
|
|
$conditions['limit'] = $this->request->get('limit', 20);
|
|
|
|
$orders = $this->orderService->paginate($conditions);
|
|
|
|
$carrierOperators = $dicts->get('carrier_operator');
|
|
$orderStatues = $dicts->get('order_status');
|
|
$transactionStatuses = $dicts->get('transaction_status');
|
|
|
|
$orders->transform(function ($item) use ($carrierOperators, $orderStatues, $transactionStatuses) {
|
|
return collect([
|
|
'id' => $item->id,
|
|
'sn' => $item->sn,
|
|
'transaction_no' => $item->transaction_no,
|
|
'package_name' => $item->package->name,
|
|
'company_name' => $item->company->name,
|
|
'pay_channel' => CommonService::namePayChannel($item->pay_channel),
|
|
'carrier_operator' => $carrierOperators[$item->package->carrier_operator],
|
|
'unit_price' => $item->unit_price,
|
|
'counts' => $item->counts,
|
|
'total_price' => $item->total_price,
|
|
'custom_price' => $item->custom_price,
|
|
'shipments' => $item->shipments,
|
|
'order_status' => $item->order_status,
|
|
'order_status_name' => $orderStatues[$item->order_status],
|
|
'transaction_status' => $item->transaction_status,
|
|
'transaction_status_name' => $transactionStatuses[$item->transaction_status],
|
|
'order_at' => (string) $item->order_at,
|
|
'deleted_at' => (string) $item->deleted_at,
|
|
]);
|
|
});
|
|
|
|
return res($orders, '订单列表', 201);
|
|
}
|
|
|
|
|
|
/**
|
|
* 订单详情
|
|
*/
|
|
public function show(Dicts $dicts, $id)
|
|
{
|
|
if (!$order = app(OrderRepository::class)->find($id)) {
|
|
throw new NotExistException('订单不存在或已删除');
|
|
}
|
|
|
|
$logistics = app(ConfigService::class)->get('logistics');
|
|
|
|
$carrierOperators = $dicts->get('carrier_operator');
|
|
$orderStatues = $dicts->get('order_status');
|
|
$transactionStatuses = $dicts->get('transaction_status');
|
|
|
|
$company = CompanyService::load($order->company_id);
|
|
$package = PackageService::load($order->package_id);
|
|
|
|
$order->company_name = $company['name'];
|
|
$order->package_name = $package['name'];
|
|
$order->pay_channel = CommonService::namePayChannel($order->pay_channel);
|
|
$order->carrier_operator = $package['carrier_operator'];
|
|
$order->carrier_operator_name = $carrierOperators[$package['carrier_operator']];
|
|
$order->order_status_name = $orderStatues[$order->order_status];
|
|
$order->transaction_status_name = $transactionStatuses[$order->transaction_status];
|
|
$order->logistics_company_name = $logistics[$order->logistics_company] ?? '';
|
|
$order->unit_price = sprintf('%.02f', $order->unit_price/100);
|
|
$order->total_price = sprintf('%.02f', $order->total_price/100);
|
|
$order->custom_price = sprintf('%.02f', $order->custom_price/100);
|
|
|
|
$order->extends = [
|
|
'cancel_remark' => $order->extends['cancel_remark'] ?? '',
|
|
'refund_channel' => $order->extends['refund_channel'] ?? '',
|
|
'refund_account' => $order->extends['refund_account'] ?? '',
|
|
'refund_remark' => $order->extends['refund_remark'] ?? '',
|
|
];
|
|
|
|
return res($order, '订单详情', 201);
|
|
}
|
|
|
|
/**
|
|
* 下单
|
|
*/
|
|
public function create()
|
|
{
|
|
$attributes = $this->request->all();
|
|
$attributes['source'] = 1;
|
|
|
|
$res = $this->orderService->store($attributes);
|
|
|
|
return res($res, '下单成功');
|
|
}
|
|
|
|
|
|
/**
|
|
* 编辑.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update($id)
|
|
{
|
|
$attributes = $this->request->all();
|
|
$attributes['id'] = $id;
|
|
|
|
$order = $this->orderService->store($attributes);
|
|
|
|
return res($order, '修改成功');
|
|
}
|
|
|
|
/**
|
|
* 删除.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy()
|
|
{
|
|
$ids = $this->request->ids();
|
|
|
|
$this->orderService->destroy($ids);
|
|
|
|
return res(true, '删除成功');
|
|
}
|
|
}
|