146 lines
4.0 KiB
PHP
146 lines
4.0 KiB
PHP
<?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'));
|
|
$chefList = $this->skuModel->whereIn('id',$skuIds)->pluck('chef_id','id')->toArray();
|
|
if (empty($chefList)) throw new Exception('chefInfoError:dataNull');
|
|
|
|
|
|
$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' => $chefList[$orderGood['sku_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 ($orderGood['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) {
|
|
$this->chefStatementModel->where('cycle_id',$this->cycleId)->where('kitchen_id',$this->kitchenId)->delete();
|
|
|
|
$insertFlag = (new ChefStatement)->insert($insertData);
|
|
|
|
if (!$insertFlag) {
|
|
throw new Exception('insertDataError:'.json_encode([
|
|
'data' => $insertData,
|
|
'cycle_id' => $this->cycleId,
|
|
'kitchen_id' => $this->kitchenId,
|
|
]));
|
|
}
|
|
});
|
|
}
|
|
} |