Files
hyperf_service/app/Service/Admin/Order/FinishService.php
2025-04-01 18:06:10 +08:00

83 lines
2.1 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Order;
use App\Amqp\Producer\Statement\FinancesProducer;
use App\Constants\Common\OrderCode;
use App\Exception\ErrException;
use App\Model\Order;
use App\Model\OrderGood;
use App\Service\Admin\BaseService;
use Hyperf\Amqp\Producer;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class FinishService extends BaseService
{
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var Producer
*/
#[Inject]
protected Producer $producer;
/**
* @return array
*/
public function handle(): array
{
$kitchenId = (int)$this->request->input('kitchenId');
$cycleId = (int)$this->request->input('cycleId');
$orderIds = $this->orderModel
->where('kitchen_id', $kitchenId)
->where('cycle_id', $cycleId)
->whereIn('status', [
OrderCode::PLAN,
OrderCode::DEPART,
OrderCode::ARRIVE,
])
->pluck('id')
->toArray();
Db::transaction(function () use ($orderIds) {
foreach (array_chunk($orderIds, 100) as $chunk) {
$orderFinish = $this->orderModel->whereIn('id', $chunk)->update(['status' => OrderCode::FINISH]);
$orderGoodFinish = $this->orderGoodModel->whereIn('order_id', $chunk)->update(['status' => OrderCode::FINISH]);
if (!$orderFinish && !$orderGoodFinish) throw new ErrException('更新失败,数据回滚,请重新操作');
}
});
// 财务结算节点 消息队列 - 生成财务结算数据 完成订单数据
$financesMessage = new FinancesProducer([
'cycle_id' => $cycleId,
'kitchen_id' => $kitchenId,
]);
$this->producer->produce($financesMessage);
return $this->return->success();
}
}