|
|
<?php
namespace App\Service\v3\Implementations;
use App\Commons\Log;use App\Constants\v3\ErrorCode;use App\Constants\v3\LogLabel;use App\Constants\v3\OrderState;use App\Constants\v3\OrderType;use App\Exception\ErrorCodeException;use App\Model\v3\Order;use App\Model\v3\OrderMain;use App\Model\v3\Store;use App\Service\v3\Interfaces\OrderOfflineServiceInterface;use App\Service\v3\Interfaces\PaymentServiceInterface;use Hyperf\DbConnection\Db;use Hyperf\Snowflake\IdGeneratorInterface;use Hyperf\Utils\ApplicationContext;use Hyperf\Di\Annotation\Inject;
class OrderOfflineService implements OrderOfflineServiceInterface{
/** * @Inject * @var PaymentServiceInterface */ protected $paymentService;
/** * @Inject * @var Log */ protected $log;
public function do($storeId, $userId, $money, $plat ='') { try {
$store = Store::find($storeId);
// 获取分布式全局ID
$generator = ApplicationContext::getContainer()->get(IdGeneratorInterface::class); $globalOrderId = $generator->generate();
$dataMain = [ 'market_id' => $store->market_id, 'order_num' => $globalOrderId, 'global_order_id' => $globalOrderId, 'user_id' => $userId, 'type' => OrderType::OFFLINE, 'money' => $money, 'total_money' => $money, 'services_money' => 0, 'coupon_money' => 0, 'delivery_money' => 0, 'state' => OrderState::UNPAID, 'tel' => '', 'address' => '', 'lat' => '', 'lng' => '', 'name' => '', 'plat' => $plat, 'delivery_time_note' => '' ];
$orderMain = OrderMain::query()->create($dataMain); $orderMainId = $orderMain->id;
// 店铺今天的订单数
$count = Order::query() ->join('lanzu_order_main as main', 'main.id', '=', 'lanzu_order.order_main_id') ->where(['lanzu_order.store_id' => $storeId, 'main.type' => OrderType::OFFLINE]) ->whereBetween('lanzu_order.created_at', [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))]) ->count();
// 子订单数据
$dataChildren = [ 'order_main_id' => $orderMainId, 'user_id' => $userId, 'store_id' => $storeId, 'money' => $money, 'order_num' => date('YmdHis').mt_rand(1000, 9999), 'note' => '', 'oid' => $count + 1 ];
$orderChild = Order::query()->create($dataChildren); $orderChildId = $orderChild->id;
Db::commit(); // 支付
return $this->paymentService->do($globalOrderId, $money, $userId, config('wechat.notify_url.offline')); } catch (\Exception $e) { Db::rollBack(); $this->log->event(LogLabel::ORDER_OFFLINE_LOG, ['exception_msg' => $e->getMessage()]); throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL); } }
public function check() { // TODO: Implement check() method.
}
public function undo() { // TODO: Implement undo() method.
}
public function doPaid($orderMainId) { Db::beginTransaction(); try {
// 主订单状态更新
$orderMain = OrderMain::query() ->where(['id' => $orderMainId, 'type' => OrderType::OFFLINE]) ->first();
if (empty($orderMain)) {
$this->log->event( LogLabel::ORDER_OFFLINE_PAID_LOG, ['order_not_found' => $orderMain->global_order_id] ); Db::rollBack(); return false; }
$currentTime = time(); $orderMain->state = OrderState::COMPLETED; $orderMain->pay_time = $currentTime; $orderMain->save();
Db::commit(); return true; } catch (\Exception $e) {
$this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]); Db::rollBack(); return false; } }}
|