feat : catering

This commit is contained in:
2025-03-12 17:26:11 +08:00
parent 3858bc4a5d
commit 467f44f847
12 changed files with 1114 additions and 54 deletions

View File

@@ -12,6 +12,7 @@ namespace App\Service\Admin\Catering\Option;
use App\Cache\Redis\Api\SiteCache;
use App\Constants\Admin\CateringCode;
use App\Constants\Admin\PrintCode;
use App\Constants\Common\OrderCode;
use App\Constants\ConfigCode;
use App\Exception\ErrException;
@@ -19,9 +20,11 @@ use App\Extend\StringUtil;
use App\Model\Order;
use App\Model\OrderGood;
use App\Model\OrderOptionCateringLog;
use App\Model\PickupCode;
use App\Model\Site;
use App\Model\User;
use App\Service\Admin\Catering\CateringBaseService;
use App\Service\Admin\Print\PrintOrderFactory;
use App\Service\ServiceTrait\Admin\Catering\PrintTrait;
use Exception;
use Hyperf\DbConnection\Db;
@@ -92,6 +95,11 @@ class CateringService extends CateringBaseService
*/
protected int $partitionCount;
/**
* @var int
*/
protected int $copiesCount;
public function __construct()
{
parent::__construct();
@@ -99,44 +107,91 @@ class CateringService extends CateringBaseService
$this->partitionCount = (int)$this->configCache->getConfigValueByKey(ConfigCode::MAXIMUM_WHOLE_CASE_SPLIT);
}
public function handle()
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$id = (int)$this->request->input('id');
// 获取数据源
$this->logInfo = $this->orderOptionCateringLogModel->find($id);
$this->siteInfo = $this->siteModel->find($this->logInfo->site_id);
// 检测数据
$this->check();
// 截单 打印按钮置灰
$this->closePrintAndPlaceOrder();
// 获取订单数据
$this->getOrderData();
$this->buildPickupCode();
// 构建取餐码
$this->copiesCount = $this->buildPickupCode();
if (empty($this->orderList)) throw new ErrException('无数据打印');
// 打印或者喷码
$res = match ($this->request->input('type')) {
CateringCode::OPTION_PRINT_YLY => $this->printOrderByYly(),
CateringCode::CATERING_STATUS_FINISH => $this->printOrderByCoding(),
CateringCode::OPTION_PRINT_CODING => $this->printOrderByCoding(),
};
return $this->return->success('success', $res);
// 关闭配餐
$this->catering();
return $this->return->success('success', ['list' => $res]);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function catering(): void
{
if ($this->isCateringByCache()) return;
try {
Db::beginTransaction();
$this->logInfo->status = CateringCode::CATERING_STATUS_FINISH;
if (!$this->logInfo->save()) throw new Exception('配餐失败1');
if (!$this->orderModel->isCateringByOrderIds($this->orderIds)) throw new Exception('配餐失败2');
Db::commit();
$this->joinCateringCache();
} catch (Exception $exception) {
Db::rollback();
throw new ErrException($exception->getMessage());
}
}
/**
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function buildPickupCode(): int
{
//todo 没写完
// if ($this->isBuildPickupCode()) {
//// return $this->getPickupCode();
// }
if ($this->isBuildPickupCode()) {
return $this->getPickupCode();
}
$currentCode = 0;
$prefix = strtoupper(StringUtil::randStr(3));
$codeRanges = [];
$takeFoodCodes = [];
$currentDate = date('Y-m-d H:i:s');
foreach ($this->orderList as &$order) {
foreach ($order['good_list'] as &$goodItem) {
foreach ($order['copies_list'] as &$copiesItem) {
if ($currentCode % $this->printBoxMaxNum === 0) {
$startCode = $currentCode + 1;
$endCode = min($currentCode + $this->printBoxMaxNum, $this->totalCopies);
@@ -146,50 +201,88 @@ class CateringService extends CateringBaseService
$currentCode++;
$paddedCode = str_pad((string)$currentCode, 3, '0', STR_PAD_LEFT);
$fullCode = "$prefix-$paddedCode";
$goodItem['take_food_code'] = $fullCode;
$goodItem['heapsort'] = $codeRanges[$currentCode] ?? '';
$copiesItem['take_food_code'] = $fullCode;
$copiesItem['heapsort'] = $codeRanges[$currentCode] ?? '';
$takeFoodCodes[] = [
'order_id' => $order['id'],
'box_num' => $goodItem['box_num'],
'take_food_code' => $fullCode,
'heapsort' => $goodItem['heapsort']
'copies' => $copiesItem['copies'],
'pickup_code' => $fullCode,
'heapsort' => $copiesItem['heapsort'],
'create_time' => $currentDate
];
}
}
// $this->insertTakeFoodCode($takeFoodCodes);
$this->insertTakeFoodCode($takeFoodCodes);
unset($currentCode,$prefix,$codeRanges,$currentDate,$startCode,$endCode,$codeGroups,$paddedCode,$fullCode);
return count($takeFoodCodes);
}
// private function insertTakeFoodCode(array $codeRanges)
// {
// try {
// Db::beginTransaction();
//
// /**
// * @var TakeFoodCode $takeFoodCodeModel
// */
// $takeFoodCodeModel = app(TakeFoodCode::class);
// $takeFoodCodeModel->whereIn('order_id',$this->orderId)->delete();
// if (!$takeFoodCodeModel->insertAll($takeFoodCodeArr)) throw new Exception('取餐码生成失败');
//
// if (!$this->isBuildTakeFoodToday($this->todayDate,$this->cacheLockValue)) throw new Exception('取餐码生成失败-缓存已添加');
//
// Db::commit();
// return count($takeFoodCodeArr);
// }catch (Exception $e) {
// Db::rollback();
// throw new ErrException($e->getMessage());
// }
// }
/**
* @var PickupCode
*/
#[Inject]
protected PickupCode $pickupCodeModel;
/**
* @return int
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getPickupCode(): int
{
$pickupCodeList = $this->pickupCodeModel
->whereIn('order_id',$this->orderIds)
->get(['take_food_code', 'order_id', 'box_num', 'heapsort', 'id'])
->toArray();
if (empty($pickupCodeList)) {
$this->delPickUpCodeByIdCache();
throw new ErrException('找不到数据,请重新生成');
}
$pickupCodeArrList = $this->buildPickupCodeListData($pickupCodeList);
foreach ($this->orderList as &$order) {
foreach ($order['copies_list'] as &$copiesItem) {
$copiesItem['take_food_code'] = $pickupCodeArrList[$copiesItem['id']][$copiesItem]['take_food_code'];
$copiesItem['heapsort'] = $pickupCodeArrList[$copiesItem['id']][$copiesItem]['heapsort'];
}
}
unset($pickupCodeArrList);
return count($pickupCodeList);
}
/**
* @param array $codeRanges
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function insertTakeFoodCode(array $codeRanges): void
{
try {
Db::beginTransaction();
$delRes = $this->pickupCodeModel->whereIn('order_id',$this->orderIds)->delete();
$insertModel = new PickupCode();
$insertRes = $insertModel->insert($codeRanges);
if (!$insertRes || !$delRes) throw new Exception('取餐码生成失败');
$this->buildPickupCodeCache();
Db::commit();
}catch (Exception $e) {
Db::rollback();
throw new ErrException($e->getMessage());
}
}
/**
* 将数组尽可能均匀地分割为指定份数
* @param array $array 输入数组
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function splitArrayIntoPartitions(array $array): array
{
@@ -275,7 +368,7 @@ class CateringService extends CateringBaseService
$one['driver_num'] = $driverInfo->driver_num;
$one['site_order'] = $this->siteInfo->sequence;
$one['real_name'] = $driverInfo->driver_name;
$one['good_list'] = array_values($orderGoodArr[$one['id']]);
$one['copies_list'] = array_values($orderGoodArr[$one['id']]);
}
$this->orderList = $orderList;
@@ -323,6 +416,38 @@ class CateringService extends CateringBaseService
return $result;
}
/**
* @param array $data
* @return array
*/
private function buildPickupCodeListData(array $data): array
{
$result = [];
foreach ($data as $item) {
// 提取关键字段
$orderId = $item["order_id"];
$copies = $item["copies"];
$pickupCode = $item["pickup_code"];
$heapsort = $item["heapsort"];
// 按层级构建数组结构
if (!isset($result[$orderId])) {
$result[$orderId] = [];
}
if (!isset($result[$orderId][$copies])) {
$result[$orderId][$copies] = [];
}
$result[$orderId][$copies][] = [
'pickup_code' => $pickupCode,
'heapsort' => $heapsort,
];
}
return $result;
}
/**
* 打印置灰和截单
* @return void
@@ -355,13 +480,123 @@ class CateringService extends CateringBaseService
if ($this->logInfo->quantity <= 0) throw new ErrException('该配餐数量为0不可配餐');
}
/**
* @var PrintOrderFactory
*/
#[Inject]
protected PrintOrderFactory $printOrderFactory;
/**
* @return true
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function printOrderByYly(): true
{
$service = $this->printOrderFactory->handle($this->request->input('type'));
$service->printId = (int)$this->request->input('print_id');
$service->handle();
$printBoxNum = 0;
$printCurrentNum = 0;
foreach ($this->orderList as $one) {
foreach ($one['copies_list'] as $copiesItem) {
// 先加当前数量
$printBoxNum++;
$service->data = [
'pickup_code' => $copiesItem['pickup_code'],
'driver_num' => '',
'site_order' => '',
'site_text' => '',
'order_sno' => '',
'username' => '',
'date' => '',
'mobile' => '',
'heapsort' => $copiesItem['heapsort'],
'sku' => $copiesItem['sku'],
];
// 打印单份
$service->printList();
// 当前数量等于最大值 归零
if ($printBoxNum == $this->printBoxMaxNum || $this->printBoxMaxNum * $printCurrentNum+$printBoxNum == $this->copiesCount) {
$printBoxNum = 0;
$printCurrentNum++;
}
//不归零跳出 当前循环
if ($printBoxNum != 0) continue;
$service->data = [
'driver_num' => '',
'site_order' => '',
'site_text' => '',
'driver_name' => '',
'current_num' => $printCurrentNum,
'date_text' => date('Y-m-d'),
'copies_num' => $this->copiesCount,
];
// 打印标签
$service->printBoxLabel();
}
}
return true;
}
private function printOrderByCoding()
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function printOrderByCoding(): array
{
return [];
$service = $this->printOrderFactory->handle($this->request->input('type'));
$printBoxNum = 0;
$printCurrentNum = 0;
$data=[];
foreach ($this->orderList as $one) {
foreach ($one['copies_list'] as $copiesItem) {
$service->data = [
'pickup_code' => $copiesItem['pickup_code'],
'driver_num' => '',
'site_order' => '',
'site_text' => '',
'order_sno' => '',
'username' => '',
'date' => '',
'mobile' => '',
'heapsort' => $copiesItem['heapsort'],
'sku' => $copiesItem['sku'],
];
$data[] = $service->printList();
//先加当前数量
$printBoxNum++;
//当前数量等于最大值 归零
if ($printBoxNum == $this->printBoxMaxNum || $this->printBoxMaxNum*$printCurrentNum+$printBoxNum == $this->copiesCount) {
$printBoxNum = 0;
$printCurrentNum++;
}
//不归零跳出 当前循环
if ($printBoxNum != 0) continue;
//归零打印
// $service->printInfo['current_num'] = $printCurrentNum;
// $service->printInfo['date_text'] = date('Y-m-d',$this->todayDate);
// $service->printInfo['box_num'] = $boxNum;
// $service->printBoxLabel();
}
}
return $data;
}
}