180 lines
4.7 KiB
PHP
180 lines
4.7 KiB
PHP
<?php
|
||
/**
|
||
* This service file is part of item.
|
||
*
|
||
* @author ctexthuang
|
||
* @contact ctexthuang@qq.com
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Service\Admin\Catering\Meal;
|
||
|
||
use App\Constants\Admin\CateringCode;
|
||
use App\Constants\Common\OrderCode;
|
||
use App\Exception\ErrException;
|
||
use App\Model\Order;
|
||
use App\Model\OrderGood;
|
||
use App\Model\OrderMealCateringLog;
|
||
use App\Model\Site;
|
||
use App\Service\Admin\Catering\CateringBaseService;
|
||
use App\Service\ServiceTrait\Admin\Catering\PrintTrait;
|
||
use Exception;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Di\Annotation\Inject;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
class CateringService extends CateringBaseService
|
||
{
|
||
use PrintTrait;
|
||
|
||
/**
|
||
* @var OrderMealCateringLog
|
||
*/
|
||
#[Inject]
|
||
protected OrderMealCateringLog $orderMealCateringLogModel;
|
||
|
||
/**
|
||
* @var Order
|
||
*/
|
||
#[Inject]
|
||
protected Order $orderModel;
|
||
|
||
/**
|
||
* @var OrderGood
|
||
*/
|
||
#[Inject]
|
||
protected OrderGood $orderGoodModel;
|
||
|
||
/**
|
||
* @var OrderMealCateringLog
|
||
*/
|
||
protected OrderMealCateringLog $logInfo;
|
||
|
||
/**
|
||
* @var Site
|
||
*/
|
||
protected Site $siteInfo;
|
||
|
||
/**
|
||
* @var int
|
||
*/
|
||
protected int $resCount = 0;
|
||
|
||
/**
|
||
* @var int 关闭站点标识 0 未关闭 1 关闭
|
||
*/
|
||
protected int $closeSiteFlag = 0;
|
||
|
||
/**
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function handle(): array
|
||
{
|
||
$id = (int)$this->request->input('id');
|
||
|
||
// 获取数据源
|
||
$this->logInfo = $this->orderMealCateringLogModel->find($id);
|
||
$this->siteInfo = $this->siteModel->find($this->logInfo->site_id);
|
||
|
||
// 检测数据
|
||
$this->check();
|
||
|
||
// 生成 key
|
||
$this->__initMealRedisKey();
|
||
|
||
try {
|
||
Db::beginTransaction();
|
||
|
||
// 配餐
|
||
$this->catering();
|
||
|
||
// 查询该点所有套餐是否已经配餐 如果是就缓存关闭该点
|
||
$this->isCloseMealSite();
|
||
|
||
Db::commit();
|
||
}catch (Exception|ErrException $e) {
|
||
Db::rollBack();
|
||
throw new ErrException($e->getMessage());
|
||
}
|
||
// 加缓存
|
||
$this->joinMealCateringCache();
|
||
|
||
// 关闭线路缓存
|
||
if ($this->closeSiteFlag == 1) $this->closeMealWholeLine();
|
||
|
||
return $this->return->success('success',['num' => $this->resCount]);
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
private function isCloseMealSite(): void
|
||
{
|
||
$res = $this->orderMealCateringLogModel
|
||
->where('site_id', $this->siteInfo->id)
|
||
->where('cycle_id', $this->logInfo->cycle_id)
|
||
->where('quantity','>',0)
|
||
->count() ?? 0;
|
||
|
||
if ($res > 0) return; // 证明还没完结 配餐 跳出
|
||
|
||
$this->closeMealSite();
|
||
|
||
$orderIds = $this->orderModel
|
||
->where('site_id', $this->siteInfo->id)
|
||
->where('cycle_id', $this->logInfo->cycle_id)
|
||
->where('type',OrderCode::ORDER_TYPE_MEAL)
|
||
->pluck('order_id')
|
||
->toArray();
|
||
if (empty($orderIds)) throw new ErrException('配餐失败,更新逻辑失败');
|
||
|
||
foreach (array_chunk($orderIds, 100) as $chunk) {
|
||
if (!$this->orderModel->isCateringByOrderIds($chunk) || !$this->orderGoodModel->isCateringByOrderIds($chunk)) throw new ErrException('修改订单数据状态失败');
|
||
}
|
||
|
||
$this->closeSiteFlag = 1;
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
private function catering(): void
|
||
{
|
||
$this->logInfo->status = CateringCode::CATERING_STATUS_FINISH;
|
||
if (!$this->logInfo->save()) throw new ErrException('配餐失败1');
|
||
}
|
||
|
||
/**
|
||
* @return void
|
||
*/
|
||
private function check(): void
|
||
{
|
||
if (empty($this->logInfo)) throw new ErrException('配餐数据不存在');
|
||
|
||
if ($this->logInfo->quantity <= 0) throw new ErrException('该配餐数量为0,不可配餐');
|
||
|
||
$this->resCount = $this->logInfo->quantity;
|
||
|
||
$allOrderIds = $this->orderModel
|
||
->where('site_id', $this->logInfo->site_id)
|
||
->where('cycle_id', $this->cycleId)
|
||
->where('type',OrderCode::ORDER_TYPE_MEAL)
|
||
->where('status',OrderCode::PAYED)
|
||
->pluck('id')
|
||
->toArray();
|
||
|
||
$totalCopies = $this->orderGoodModel
|
||
->whereIn('order_id', $allOrderIds)
|
||
->where('cycle_id', $this->cycleId)
|
||
->where('sku_id', $this->logInfo->sku_id)
|
||
->sum('copies') ?? 0;
|
||
|
||
if ($totalCopies != $this->logInfo->quantity) $this->resCount = (int)$totalCopies;
|
||
}
|
||
} |