213 lines
7.8 KiB
PHP
213 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Service\ServiceTrait\Api;
|
|
|
|
use App\Cache\Redis\Api\SiteCache;
|
|
use App\Constants\Admin\CateringCode;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Model\OrderMealCateringLog;
|
|
use App\Model\OrderOptionCateringLog;
|
|
use App\Model\Sku;
|
|
use App\Model\Spu;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
trait CateringTrait
|
|
{
|
|
/**
|
|
* @var OrderOptionCateringLog
|
|
*/
|
|
#[Inject]
|
|
protected OrderOptionCateringLog $orderOptionCateringLogModel;
|
|
|
|
/**
|
|
* @var OrderMealCateringLog
|
|
*/
|
|
#[Inject]
|
|
protected OrderMealCateringLog $orderMealCateringLogModel;
|
|
|
|
/**
|
|
* @var SiteCache
|
|
*/
|
|
#[Inject]
|
|
protected SiteCache $siteCache;
|
|
|
|
/**
|
|
* @return bool 添加失败需要退款
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function manageAddCateringLog(): bool
|
|
{
|
|
return match ($this->orderInfo->type) {
|
|
OrderCode::ORDER_TYPE_MEAL => $this->manageAddMealCateringLog(),
|
|
OrderCode::ORDER_TYPE_OPTIONAL => $this->manageAddOptionCateringLog(),
|
|
default => true,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @return void 减少失败不影响退款数据
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function manageSubCateringLog(): void
|
|
{
|
|
match ($this->orderInfo->type) {
|
|
OrderCode::ORDER_TYPE_MEAL => $this->manageSubMealCateringLog(),
|
|
OrderCode::ORDER_TYPE_OPTIONAL => $this->manageSubOptionCateringLog(),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function manageSubOptionCateringLog(): void
|
|
{
|
|
$extraStapleArr = $this->spuModel->getCycleIdAddStapleFoodSkuIds($this->orderInfo->cycle_id);
|
|
$orderGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('copies','sku_id')->toArray();
|
|
|
|
$addStapleFoodNum = 0;
|
|
foreach ($orderGoods as $key => $copies) {
|
|
if (!in_array($key, $extraStapleArr)) continue;
|
|
|
|
$addStapleFoodNum = $copies + $addStapleFoodNum;
|
|
}
|
|
|
|
$siteInfo = $this->siteCache->getSiteInfo($this->orderInfo->site_id);
|
|
|
|
//需要绑定一个 kitchen_id
|
|
$logInfo = $this->orderOptionCateringLogModel->getInfoBySiteIdAndCycleIdAndSiteId($this->orderInfo->site_id, $this->orderInfo->cycle_id, (int)$siteInfo['kitchen_id']);
|
|
|
|
if (empty($logInfo)) {
|
|
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubOptionCateringLog:订单套餐配餐记录不存在,订单信息:'.json_encode($this->orderInfo->toArray()));
|
|
return;
|
|
}
|
|
|
|
if ($logInfo->status == CateringCode::CATERING_STATUS_FINISH) {
|
|
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubOptionCateringLog:已经截单不需要修改,订单信息:'.json_encode($this->orderInfo->toArray()));
|
|
return;
|
|
}
|
|
|
|
$logInfo->quantity = $logInfo->quantity - $this->orderInfo->copies;
|
|
$logInfo->add_staple_food_num = $this->orderInfo->add_staple_food_num - $addStapleFoodNum;
|
|
|
|
if (!$logInfo->save()) {
|
|
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubOptionCateringLog:修改配餐记录失败,订单信息:'.json_encode($this->orderInfo->toArray()));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function manageSubMealCateringLog(): void
|
|
{
|
|
$orderGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('copies','sku_id')->toArray();
|
|
|
|
foreach ($orderGoods as $key => $orderGood) {
|
|
$logInfo = $this->orderMealCateringLogModel->getInfoBySiteIdAndCycleIdAndSkuId($this->orderInfo->site_id,$this->orderInfo->cycle_id,(int)$key);
|
|
|
|
if (empty($logInfo)) {
|
|
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubMealCateringLog:订单套餐配餐记录不存在,订单信息:'.json_encode($this->orderInfo->toArray()).':订单商品信息:'.json_encode($orderGoods));
|
|
continue;
|
|
}
|
|
|
|
if ($logInfo->status == CateringCode::CATERING_STATUS_FINISH) {
|
|
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubMealCateringLog:已经截单不需要修改,订单信息:'.json_encode($this->orderInfo->toArray()).':订单商品信息:'.json_encode($orderGoods));
|
|
continue;
|
|
}
|
|
|
|
$logInfo->quantity = $logInfo->quantity - $orderGood;
|
|
|
|
if (!$logInfo->save()) {
|
|
$this->log->error(__CLASS__.':Function:refundCallBackHandle:manageSubMealCateringLog:修改配餐记录失败,订单信息:'.json_encode($this->orderInfo->toArray()).':订单商品信息:'.json_encode($orderGoods));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @var Spu
|
|
*/
|
|
#[Inject]
|
|
protected Spu $spuModel;
|
|
|
|
/**
|
|
* 添加自选配餐数据
|
|
* @return bool
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function manageAddOptionCateringLog(): bool
|
|
{
|
|
//根据订单 cycle_id 获取加一碗米饭 的数量
|
|
$extraStapleArr = $this->spuModel->getCycleIdAddStapleFoodSkuIds($this->orderInfo->cycle_id);
|
|
$orderGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('copies','sku_id')->toArray();
|
|
|
|
$addStapleFoodNum = 0;
|
|
foreach ($orderGoods as $key => $copies) {
|
|
if (!in_array($key, $extraStapleArr)) continue;
|
|
|
|
$addStapleFoodNum = $copies + $addStapleFoodNum;
|
|
}
|
|
|
|
$siteInfo = $this->siteCache->getSiteInfo($this->orderInfo->site_id);
|
|
//需要绑定一个 kitchen_id
|
|
$logInfo = $this->orderOptionCateringLogModel->getInfoBySiteIdAndCycleIdAndSiteId($this->orderInfo->site_id, $this->orderInfo->cycle_id,(int)$siteInfo['kitchen_id']);
|
|
|
|
if (empty($logInfo)) {
|
|
$logInfo = new OrderOptionCateringLog();
|
|
|
|
$logInfo->site_id = $this->orderInfo->site_id;
|
|
$logInfo->cycle_id = $this->orderInfo->cycle_id;
|
|
$logInfo->kitchen_id = (int)$siteInfo['kitchen_id'];
|
|
$logInfo->quantity = 0;
|
|
$logInfo->add_staple_food_num = 0;
|
|
$logInfo->status = CateringCode::CATERING_STATUS_UNDERWAY;
|
|
}
|
|
|
|
if ($logInfo->status == CateringCode::CATERING_STATUS_FINISH) return false;
|
|
|
|
$logInfo->quantity = $logInfo->quantity + $this->orderInfo->copies;
|
|
$logInfo->add_staple_food_num = $this->orderInfo->add_staple_food_num + $addStapleFoodNum;
|
|
|
|
if (!$logInfo->save()) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 添加套餐配餐数据
|
|
* @return bool
|
|
*/
|
|
private function manageAddMealCateringLog(): bool
|
|
{
|
|
$orderGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->pluck('copies','sku_id')->toArray();
|
|
|
|
foreach ($orderGoods as $key => $orderGood) {
|
|
$logInfo = $this->orderMealCateringLogModel->getInfoBySiteIdAndCycleIdAndSkuId($this->orderInfo->site_id,$this->orderInfo->cycle_id,(int)$key);
|
|
|
|
if (empty($logInfo)) {
|
|
$logInfo = new OrderMealCateringLog();
|
|
|
|
$logInfo->site_id = $this->orderInfo->site_id;
|
|
$logInfo->cycle_id = $this->orderInfo->cycle_id;
|
|
$logInfo->sku_id = (int)$key;
|
|
$logInfo->quantity = 0;
|
|
$logInfo->status = CateringCode::CATERING_STATUS_UNDERWAY;
|
|
}
|
|
|
|
if ($logInfo->status == CateringCode::CATERING_STATUS_FINISH) return false;
|
|
|
|
$logInfo->quantity = $logInfo->quantity + $orderGood;
|
|
|
|
if (!$logInfo->save()) return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |