You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

127 lines
3.9 KiB

<?php
namespace App\Controller;
use App\Constants\ErrorCode;
use App\Constants\LogLabel;
use App\Model\OrderMain;
use App\Request\OrderOfflineRequest;
use App\Request\OrderOnlineRequest;
use App\Request\UserOrdersRequest;
use App\Service\SeparateAccountsServiceInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use App\Service\OrderServiceInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Validation\ValidationException;
class OrderController extends BaseController
{
/**
* @Inject
* @var OrderServiceInterface
*/
protected $orderService;
/**
* @Inject
* @var SeparateAccountsServiceInterface
*/
protected $separateAccountsService;
public function addOnlineOrder(OrderOnlineRequest $request)
{
$orderMainId = $this->orderService->addOnlineOrder($request->validated());
if (!is_int($orderMainId)) {
return $this->result(ErrorCode::ORDER_FAILURE, '', $orderMainId);
}
return $this->success(['order_id' => $orderMainId]);
}
public function addOfflineOrder(OrderOfflineRequest $request)
{
$orderMainId = $this->orderService->addOfflineOrder($request->validated());
if (!is_int($orderMainId)) {
return $this->result(ErrorCode::ORDER_FAILURE, '', $orderMainId);
}
return $this->success(['order_id' => $orderMainId]);
}
/**
* 用户完成订单-确认收货
*/
public function userComp()
{
$validator = $this->validationFactory->make(
$this->request->all(),
[
'user_id' => 'required|nonempty|integer',
'order_id' => 'required|nonempty|numeric',
],
[
'*.*' => ':attribute 参数不正确',
]
);
if ($validator->fails()) {
// Handle exception
throw new ValidationException($validator);
return;
}
$userId = $this->request->input('user_id');
$orderId = $this->request->input('order_id'); // TODO 等新订单列表接口处理完毕后全面转换成global_order_id
$orderMain = OrderMain::query()
->where(['id' => $orderId, 'state' => OrderMain::ORDER_STATE_DELIVERY, 'user_id' => $userId])
->first();
if (empty($orderMain)) {
$this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['order_not_found' => 'order_not_found']);
return $this->result(ErrorCode::SEPARATE_ACCOUNTS_ERROR, '', '操作失败,订单异常或不存在');
}
Db::beginTransaction();
try {
$this->orderService->onlineCompleted($orderMain->global_order_id);
$this->separateAccountsService->orderOnlineCompleted($orderMain->global_order_id);
Db::commit();
return $this->success('');
} catch (\Exception $e) {
Db::rollBack();
$this->log->event(LogLabel::ONLINE_COMPLETE_LOG, ['exception' => $e->getMessage()]);
return $this->result(ErrorCode::SEPARATE_ACCOUNTS_ERROR, '', '操作失败,请稍后重试');
}
}
/**
* 用户取消订单
*/
public function onlineCancel(){
$orderId = $this->request->input('order_id');
$orderMain = OrderMain::where('id',$orderId)
->select('global_order_id')
->first();
return $this->success($this->orderService->onlineCancel($orderMain->global_order_id));
}
/**
* 用户订单
* @param UserOrdersRequest $request
* @return \Psr\Http\Message\ResponseInterface
*/
public function userOnlineOrders(UserOrdersRequest $request)
{
$params = $request->validated();
$this->orderService->onlineAutoCancelByUserId($params['user_id']);
$result = $this->orderService->userOnlineOrders($params['user_id'], $params['state'], $params['page'], $params['pagesize']);
return $this->success($result);
}
}