diff --git a/app/Amqp/Consumer/CancelOrderConsumer.php b/app/Amqp/Consumer/CancelOrderConsumer.php index bfbdbf2..1c4a63a 100644 --- a/app/Amqp/Consumer/CancelOrderConsumer.php +++ b/app/Amqp/Consumer/CancelOrderConsumer.php @@ -8,6 +8,7 @@ use App\Constants\Common\CouponCode; use App\Constants\Common\OrderCode; use App\Lib\Log; use App\Model\Order; +use App\Model\OrderGood; use App\Model\UserCoupon; use App\Service\ServiceTrait\Api\CouponTrait; use App\Service\ServiceTrait\Api\OrderTrait; @@ -54,6 +55,12 @@ class CancelOrderConsumer extends ConsumerMessage #[Inject] protected UserCoupon $userCouponModel; + /** + * @var OrderGood + */ + #[Inject] + protected OrderGood $orderGoodModel; + /** * @param $data * @param AMQPMessage $message @@ -92,7 +99,11 @@ class CancelOrderConsumer extends ConsumerMessage $this->rollbackCoupon($orderType, $orderInfo); $orderInfo->status = OrderCode::CANCEL; - if (!$orderInfo->save()) throw new Exception('CancelOrderConsumer:error:orderStatusSaveError:'.json_encode($orderInfo->toArray())); + + $updateGoods = $this->orderGoodModel->where('order_id',$orderInfo->id)->update([ + 'status' => OrderCode::CANCEL, + ]); + if (!$orderInfo->save() || !$updateGoods) throw new Exception('CancelOrderConsumer:error:orderStatusSaveError:'.json_encode($orderInfo->toArray())); Db::commit(); }catch (Exception $e){ diff --git a/app/Controller/Admin/OrderController.php b/app/Controller/Admin/OrderController.php index 591ae80..a5914f9 100644 --- a/app/Controller/Admin/OrderController.php +++ b/app/Controller/Admin/OrderController.php @@ -22,6 +22,11 @@ use Psr\Container\NotFoundExceptionInterface; ])] class OrderController extends AbstractController { + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ #[RequestMapping(path: "list", methods: "GET")] #[Scene(scene: "list")] public function list() diff --git a/app/Model/OrderGood.php b/app/Model/OrderGood.php index 3dd770c..17cdec0 100644 --- a/app/Model/OrderGood.php +++ b/app/Model/OrderGood.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Model; +use App\Constants\Common\OrderCode; use Hyperf\DbConnection\Model\Model; /** @@ -52,4 +53,15 @@ class OrderGood extends Model { return $this->where('order_id', $orderId)->select('sku_id')->selectRaw('SUM(`quantity`) as `quantity`')->groupBy('sku_id')->get()->toArray(); } + + /** + * @param array $orderIds + * @return int + */ + public function isCateringByOrderIds(array $orderIds): int + { + return $this->whereIn('order_id', $orderIds)->update([ + 'status' => OrderCode::PLAN + ]); + } } diff --git a/app/Service/Admin/Catering/CateringBaseService.php b/app/Service/Admin/Catering/CateringBaseService.php index a7a1a59..6a63458 100644 --- a/app/Service/Admin/Catering/CateringBaseService.php +++ b/app/Service/Admin/Catering/CateringBaseService.php @@ -37,7 +37,7 @@ abstract class CateringBaseService extends BaseService protected int $cycleId; /** - * @var int todo 厨房id 自选的需要 + * @var int */ protected int $kitchenId; diff --git a/app/Service/Admin/Catering/Meal/CheckService.php b/app/Service/Admin/Catering/Meal/CheckService.php index d74e04c..882ea3f 100644 --- a/app/Service/Admin/Catering/Meal/CheckService.php +++ b/app/Service/Admin/Catering/Meal/CheckService.php @@ -14,8 +14,11 @@ 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\Service\Admin\Catering\CateringBaseService; +use Exception; +use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; class CheckService extends CateringBaseService @@ -38,6 +41,12 @@ class CheckService extends CateringBaseService #[Inject] protected Order $orderModel; + /** + * @var OrderGood + */ + #[Inject] + protected OrderGood $orderGoodModel; + /** * @return array */ @@ -61,7 +70,10 @@ class CheckService extends CateringBaseService if (empty($orderIds)) throw new ErrException('数据错误'); - $this->orderModel->whereIn('id', $orderIds)->update(['status' => OrderCode::PLAN]); +// $this->orderModel->whereIn('id', $orderIds)->update(['status' => OrderCode::PLAN]); + Db::transaction(function () use ($orderIds) { + if (!$this->orderModel->isCateringByOrderIds($orderIds) || !$this->orderGoodModel->isCateringByOrderIds($orderIds)) throw new Exception('修改订单数据状态失败'); + }); return $this->return->success(); } diff --git a/app/Service/Admin/Catering/Option/CateringService.php b/app/Service/Admin/Catering/Option/CateringService.php index 63fa570..0a542a3 100644 --- a/app/Service/Admin/Catering/Option/CateringService.php +++ b/app/Service/Admin/Catering/Option/CateringService.php @@ -159,7 +159,7 @@ class CateringService extends CateringBaseService $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'); + if (!$this->orderModel->isCateringByOrderIds($this->orderIds) || !$this->orderGoodModel->isCateringByOrderIds($this->orderIds)) throw new Exception('配餐失败2'); Db::commit(); @@ -190,7 +190,7 @@ class CateringService extends CateringBaseService $currentDate = date('Y-m-d H:i:s'); foreach ($this->orderList as &$order) { foreach ($order['copies_list'] as &$copiesItem) { - if ($currentCode % $this->printBoxMaxNum === 0) { + if ($currentCode / $this->printBoxMaxNum === 0) { $startCode = $currentCode + 1; $endCode = min($currentCode + $this->printBoxMaxNum, $this->totalCopies); $codeGroups = $this->splitArrayIntoPartitions(range($startCode, $endCode)); diff --git a/app/Service/Admin/Good/SkuService.php b/app/Service/Admin/Good/SkuService.php index 2e2cb04..9080f3a 100644 --- a/app/Service/Admin/Good/SkuService.php +++ b/app/Service/Admin/Good/SkuService.php @@ -91,7 +91,7 @@ class SkuService extends BaseService $this->updateOssObjects(explode(',',$imageIds)); $insertModel->spu_id = $spuId; - $insertModel->title = $this->request->input('title'); + $insertModel->title = $spuInfo->title.$this->request->input('title'); $insertModel->price = $this->request->input('price'); $insertModel->image_ids = $imageIds; $insertModel->param = $this->request->input('param',''); diff --git a/app/Service/Admin/Order/OrderListService.php b/app/Service/Admin/Order/OrderListService.php index d87e34c..a8f1021 100644 --- a/app/Service/Admin/Order/OrderListService.php +++ b/app/Service/Admin/Order/OrderListService.php @@ -10,13 +10,145 @@ declare(strict_types=1); namespace App\Service\Admin\Order; +use App\Cache\Redis\Api\SiteCache; +use App\Model\Order; +use App\Model\OrderGood; +use App\Model\Sku; use App\Service\Admin\BaseService; +use Hyperf\Di\Annotation\Inject; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class OrderListService extends BaseService { - public function handle() + /** + * @var Order + */ + #[Inject] + protected Order $orderModel; + + /** + * @var OrderGood + */ + #[Inject] + protected OrderGood $orderGoodModel; + + /** + * @var Sku + */ + #[Inject] + protected Sku $skuModel; + + /** + * @var SiteCache + */ + #[Inject] + protected SiteCache $siteCache; + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function handle(): array { - //todo Write logic - return $this->return->success(); + $limit = (int)$this->request->input('limit', 20); + + //todo where + $orderList = $this->orderModel + ->when($this->request->input('search_user_id'), function ($query) { + $query->where('user_id', $this->request->input('search_user_id')); + }) + ->when($this->request->input('search_status'), function ($query) { + $query->where('status', $this->request->input('search_status')); + }) + ->select([ + 'id', + 'cycle_id', + 'order_sno', + 'user_id', + 'site_id', + 'copies', + 'type', + 'actual_price', + 'total_price', + 'status', + 'create_time', + 'pay_time' + ]) + ->orderByDesc('id') + ->paginate($limit) + ->toArray(); + + if (!empty($orderList['data'])) { + $this->buildData($orderList['data']); + } + + return $this->return->success('success', ['list' => $orderList]); + } + + /** + * 构建订单列表数据 + * @param array $orderList + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + private function buildData(array &$orderList): void + { + $orderIds = array_column($orderList, 'id'); + + $orderSkuList = $this->orderGoodModel->whereIn('order_id', $orderIds)->get()->toArray(); + $skuIds = []; + $newOrderSkuList = []; + foreach ($orderSkuList as $orderSku) { + if (empty($newOrderSkuList[$orderSku['order_id']])) { + $newOrderSkuList[$orderSku['order_id']] = []; + } + + if (empty($newOrderSkuList[$orderSku['order_id']][$orderSku['copies']])) { + $newOrderSkuList[$orderSku['order_id']][$orderSku['copies']] = []; + } + + $newOrderSkuList[$orderSku['order_id']][$orderSku['copies']][] = $orderSku; + + if (in_array($orderSku['sku_id'], $skuIds)) continue; + + $skuIds[] = $orderSku['sku_id']; + } + unset($orderSkuList); + + + $skuList = $this->skuModel->getDataArrByIds($skuIds); + $skuList = array_column($skuList, null,'id'); + + + foreach ($orderList as &$order) { + $orderCopiesList = []; + for ($i = 1; $i <= ($order['copies'] ?? 0); $i++) { + + $oneCopiesInfo = [ + 'total_price' => '0.00', + 'total_quantity' => 0, + 'sku_list' => [], + 'take_food_code' => [], //todo 取餐码 + ]; + + foreach ($newOrderSkuList[$order['id']][$i] as $item) { + if ($item['order_id'] != $order['id'] || $item['copies'] != $i) continue; + + $skuInfo = $skuList[$item['sku_id']] ?? []; + $oneCopiesInfo['total_price'] = bcadd($oneCopiesInfo['total_price'], bcmul($item['unit_price'],(string)$item['quantity'],2), 2); + $oneCopiesInfo['total_quantity'] += $item['quantity']; + + $oneCopiesInfo['sku_list'][] = $skuInfo; + } + + $orderCopiesList[] = $oneCopiesInfo; + } + + $order['copies_list'] = $orderCopiesList; + $order['site'] = $this->siteCache->getSiteInfo((int)$order['site_id']) ?? []; + } } } \ No newline at end of file diff --git a/app/Service/Api/Order/OrderListService.php b/app/Service/Api/Order/OrderListService.php index 5cc2dc4..cd4f522 100644 --- a/app/Service/Api/Order/OrderListService.php +++ b/app/Service/Api/Order/OrderListService.php @@ -49,10 +49,12 @@ class OrderListService extends BaseService /** * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function handle(): array { - $limit = $this->request->input('limit', 20); + $limit = (int)$this->request->input('limit', 20); $orderList = $this->orderModel ->where('user_id', $this->userId) diff --git a/app/Service/Api/Order/PlaceOrderService.php b/app/Service/Api/Order/PlaceOrderService.php index 0d21c18..79ea55c 100644 --- a/app/Service/Api/Order/PlaceOrderService.php +++ b/app/Service/Api/Order/PlaceOrderService.php @@ -170,6 +170,7 @@ class PlaceOrderService extends BaseOrderService 'is_comment' => OrderCode::GOOD_COMMENT_NULL, 'copies' => $copiesNum, 'type' => $one['type'], + 'status' => OrderCode::WAIT_PAY, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s'), ]; diff --git a/app/Service/ServiceTrait/Api/GoodOrderTrait.php b/app/Service/ServiceTrait/Api/GoodOrderTrait.php index 75648ba..681205f 100644 --- a/app/Service/ServiceTrait/Api/GoodOrderTrait.php +++ b/app/Service/ServiceTrait/Api/GoodOrderTrait.php @@ -81,7 +81,11 @@ trait GoodOrderTrait $this->orderInfo->status = OrderCode::PAYED; $this->orderInfo->pay_time = date('Y-m-d H:i:s'); - if (!$this->orderInfo->save()) { + $updateGoods = $this->orderGoodModel->where('order_id',$this->orderInfo->id)->update([ + 'status' => OrderCode::PAYED, + ]); + + if (!$this->orderInfo->save() || !$updateGoods) { $this->log->debug(__CLASS__.':Function:manageOrder:更新充值订单的状态失败:'.$this->orderInfo->id); throw new ErrException('更新充值订单的状态失败'); }