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

@@ -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,
]));
}
});
}
}