feat : chef statement

This commit is contained in:
2025-03-24 11:43:15 +08:00
parent 3c91ac4077
commit 071b0fc445
8 changed files with 246 additions and 6 deletions

View File

@@ -4,15 +4,24 @@ declare(strict_types=1);
namespace App\Amqp\Consumer\Statement;
use App\Lib\Log;
use App\Service\Amqp\Statement\ChefService;
use Exception;
use Hyperf\Amqp\Message\Type;
use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Di\Annotation\Inject;
use PhpAmqpLib\Message\AMQPMessage;
#[Consumer(exchange: 'hyperf', routingKey: 'hyperf', queue: 'hyperf', name: "ChefConsumer", nums: 1)]
#[Consumer(exchange: 'chef', routingKey: 'chef', queue: 'chef.statement', name: "ChefConsumer", nums: 1)]
class ChefConsumer extends ConsumerMessage
{
/**
* @var Log
*/
#[Inject]
protected Log $log;
/**
* @var Type|string 消息类型
@@ -20,6 +29,22 @@ class ChefConsumer extends ConsumerMessage
protected Type|string $type = Type::DIRECT;
public function consumeMessage($data, AMQPMessage $message): Result
{
if (!$data['kitchen_id'] || !$data['cycle_id']) {
$this->log->error('ChefConsumer:error:NoData:'.json_encode($data));
return Result::ACK;
}
try {
$service = new ChefService();
$service->cycleId = (int)$data['cycle_id'];
$service->kitchenId = (int)$data['kitchen_id'];
$service->handle();
} catch (Exception $e) {
$this->log->error('ChefConsumer:error:'.$e->getMessage().':data:'.json_encode($data));
}
return Result::ACK;
}
}

View File

@@ -8,7 +8,7 @@ use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Amqp\Message\Type;
#[Producer(exchange: 'hyperf', routingKey: 'hyperf')]
#[Producer(exchange: 'chef', routingKey: 'chef')]
class ChefProducer extends ProducerMessage
{

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $date
* @property int $cycle_id
* @property int $kitchen_id
* @property int $chef_id
* @property int $sku_id
* @property int $sale
* @property int $refund
* @property int $cancel
* @property string $create_time
* @property string $update_time
*/
class ChefStatement extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'chef_statement';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'cycle_id' => 'integer', 'kitchen_id' => 'integer', 'chef_id' => 'integer', 'sku_id' => 'integer', 'sale' => 'integer', 'refund' => 'integer', 'cancel' => 'integer'];
}

View File

@@ -10,7 +10,8 @@ use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $order_id
* @property int $cycle_id
* @property int $cycle_id
* @property int $kitchen_id
* @property int $sku_id
* @property int $spu_id
* @property int $user_id
@@ -38,7 +39,19 @@ class OrderGood extends Model
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'order_id' => 'integer', 'cycle_id' => 'integer', 'sku_id' => 'integer', 'spu_id' => 'integer', 'user_id' => 'integer', 'quantity' => 'integer', 'is_comment' => 'integer', 'copies' => 'integer', 'type' => 'integer'];
protected array $casts = [
'id' => 'integer',
'order_id' => 'integer',
'cycle_id' => 'integer',
'kitchen_id' => 'integer',
'sku_id' => 'integer',
'spu_id' => 'integer',
'user_id' => 'integer',
'quantity' => 'integer',
'is_comment' => 'integer',
'copies' => 'integer',
'type' => 'integer'
];
const string CREATED_AT = 'create_time';

View File

@@ -161,7 +161,6 @@ class DispenseAddService extends BaseService
'coupon_dispense_id' => $this->dispenseId,
'user_id' => $userId,
]);
// $producer = ApplicationContext::getContainer()->get(Producer::class);
$this->producer->produce($message);
}

View File

@@ -10,11 +10,13 @@ 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;
@@ -32,7 +34,16 @@ class FinishService extends BaseService
#[Inject]
protected OrderGood $orderGoodModel;
public function handle()
/**
* @var Producer
*/
#[Inject]
protected Producer $producer;
/**
* @return array
*/
public function handle(): array
{
$kitchenId = (int)$this->request->input('kitchenId');
$cycleId = (int)$this->request->input('cycleId');
@@ -58,7 +69,14 @@ class FinishService extends BaseService
}
});
// 财务结算节点 消息队列 - 生成财务结算数据 完成订单数据
$financesMessage = new FinancesProducer([
'cycle_id' => $cycleId,
'kitchen_id' => $kitchenId,
]);
$this->producer->produce($financesMessage);
//
return $this->return->success();
}

View File

@@ -0,0 +1,146 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Amqp\Statement;
use App\Constants\Common\OrderCode;
use App\Model\ChefStatement;
use App\Model\Cycle;
use App\Model\Kitchen;
use App\Model\OrderGood;
use App\Model\Sku;
use App\Model\Spu;
use Exception;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class ChefService
{
/**
* @var int
*/
public int $kitchenId;
/**
* @var int
*/
public int $cycleId;
/**
* @var Cycle
*/
#[Inject]
protected Cycle $cycleModel;
/**
* @var Kitchen
*/
#[Inject]
protected Kitchen $kitchenModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @var Spu
*/
#[Inject]
protected Spu $spuModel;
/**
* @var ChefStatement
*/
#[Inject]
protected ChefStatement $chefStatementModel;
/**
* @return void
* @throws Exception
*/
public function handle(): void
{
$kitchen = $this->kitchenModel->where('id',$this->kitchenId)->first();
$cycle = $this->cycleModel->where('id',$this->cycleId)->first();
if (empty($kitchen) || empty($cycle)) {
throw new Exception('kitchenOrCycleError:'.json_encode([
'kitchen_id' => $this->kitchenId,
'cycle_id' => $this->cycleId,
'cycle' => $cycle,
'kitchen' => $kitchen,
]));
}
$orderGoods = $this->orderGoodModel
->where('cycle_id',$cycle->id)
->where('kitchen_id',$this->kitchenId)
->whereIn('status',[
OrderCode::FINISH,OrderCode::CANCEL,OrderCode::FINISH_REFUND
])
->get();
if ($orderGoods->isEmpty()) throw new Exception('orderGoodError:dataNull');
$orderGoods = $orderGoods->toArray();
$skuIds = array_unique(array_column($orderGoods, 'sku_id'));
$skuInfo = $this->skuModel->whereIn('id',$skuIds)->pluck('spu_id','id')->toArray();
if (empty($skuInfo)) throw new Exception('skuInfoError:dataNull');
$spuInfo = $this->spuModel->whereIn('id',array_unique(array_values($skuInfo)))->pluck('chef_id','id')->toArray();
$currentDate = date('Y-m-d H:i:s');
$insertData = [];
foreach ($orderGoods as $orderGood) {
if (empty($insertData[$orderGood['sku_id']])) {
$insertData[$orderGood['sku_id']] = [
'date' => $cycle->dates,
'chef_id' => $spuInfo[$skuInfo[$orderGood['sku_id']]]['chef_id'] ?? 0,
'sku_id' => $orderGood['sku_id'],
'cycle_id' => $this->cycleId,
'kitchen_id' => $this->kitchenId,
'sale' => 0,
'refund' => 0,
'cancel' => 0,
'create_time' => $currentDate,
'update_time' => $currentDate
];
}
match ($orderGoods['status']) {
OrderCode::FINISH => $insertData[$orderGood['sku_id']]['sale'] += $orderGood['quantity'],
OrderCode::FINISH_REFUND => $insertData[$orderGood['sku_id']]['refund'] += $orderGood['quantity'],
OrderCode::CANCEL => $insertData[$orderGood['sku_id']]['cancel'] += $orderGood['quantity'],
};
}
if (empty($insertData)) throw new Exception('insertDataError:dataNull');
Db::transaction(function () use ($insertData) {
$delFlag = $this->chefStatementModel->where('cycle_id',$this->cycleId)->where('kitchen_id',$this->kitchenId)->delete();
$insertFlag = (new ChefStatement)->insert($insertData);
if (!$delFlag || !$insertFlag) {
throw new Exception('insertDataError:'.json_encode([
'data' => $insertData,
'cycle_id' => $this->cycleId,
'kitchen_id' => $this->kitchenId,
]));
}
});
}
}

View File

@@ -162,6 +162,7 @@ class PlaceOrderService extends BaseOrderService
$orderGoodInsertArr[] = [
'order_id' => $this->orderId,
'cycle_id' => $this->cycleId,
'kitchen_id' => $this->orderRes['site_info']['kitchen_id'],
'sku_id' => $one['id'],
'spu_id' => $one['spu_id'],
'user_id' => $this->userId,