| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace App\Module\Mex\Services;
- use App\Module\Mex\Logic\MexMatchLogic;
- use Illuminate\Support\Facades\DB;
- /**
- * 农贸市场撮合服务
- *
- * 提供撮合相关的对外服务接口
- */
- class MexMatchService
- {
- /**
- * 执行用户买入物品撮合任务
- *
- * @param int|null $itemId 指定商品ID,null表示处理所有商品
- * @param int $batchSize 批处理大小
- * @return array 撮合结果
- */
- public static function executeUserBuyItemMatch(?int $itemId = null, int $batchSize = 100): array
- {
- return MexMatchLogic::executeUserBuyItemMatch($itemId, $batchSize);
- }
- /**
- * 执行用户卖出物品撮合任务
- *
- * @param int|null $itemId 指定商品ID,null表示处理所有商品
- * @param int $batchSize 批处理大小
- * @return array 撮合结果
- */
- public static function executeUserSellItemMatch(?int $itemId = null, int $batchSize = 100): array
- {
- return MexMatchLogic::executeUserSellItemMatch($itemId, $batchSize);
- }
- /**
- * 执行单个商品的用户买入物品撮合(带事务处理)
- *
- * @param int $itemId 商品ID
- * @param int $batchSize 批处理大小
- * @return array 撮合结果
- */
- public static function executeUserBuyItemMatchForItem(int $itemId, int $batchSize = 100): array
- {
- try {
- return DB::transaction(function () use ($itemId, $batchSize) {
- return MexMatchLogic::executeUserBuyItemMatchForItem($itemId, $batchSize);
- });
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '用户买入物品撮合执行失败:' . $e->getMessage(),
- 'matched_orders' => 0,
- 'total_amount' => '0.00000',
- ];
- }
- }
- /**
- * 执行单个商品的用户卖出物品撮合(带事务处理)
- *
- * @param int $itemId 商品ID
- * @param int $batchSize 批处理大小
- * @return array 撮合结果
- */
- public static function executeUserSellItemMatchForItem(int $itemId, int $batchSize = 100): array
- {
- try {
- return DB::transaction(function () use ($itemId, $batchSize) {
- return MexMatchLogic::executeUserSellItemMatchForItem($itemId, $batchSize);
- });
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '用户卖出物品撮合执行失败:' . $e->getMessage(),
- 'matched_orders' => 0,
- 'total_amount' => '0.00000',
- ];
- }
- }
- /**
- * 获取用户买入物品撮合统计信息
- *
- * @return array 统计信息
- */
- public static function getUserBuyItemMatchStats(): array
- {
- return MexMatchLogic::getUserBuyItemMatchStats();
- }
- /**
- * 获取用户卖出物品撮合统计信息
- *
- * @return array 统计信息
- */
- public static function getUserSellItemMatchStats(): array
- {
- return MexMatchLogic::getUserSellItemMatchStats();
- }
- /**
- * 检查用户买入物品撮合条件
- *
- * @param int $itemId 商品ID
- * @return array 检查结果
- */
- public static function checkUserBuyItemMatchConditions(int $itemId): array
- {
- return MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
- }
- /**
- * 检查用户卖出物品撮合条件
- *
- * @param int $itemId 商品ID
- * @return array 检查结果
- */
- public static function checkUserSellItemMatchConditions(int $itemId): array
- {
- return MexMatchLogic::checkUserSellItemMatchConditions($itemId);
- }
- /**
- * 执行单个用户卖出物品订单的撮合(带事务处理)
- *
- * @param \App\Module\Mex\Models\MexOrder $order 用户卖出物品订单
- * @return array 撮合结果
- */
- public static function executeUserSellItemOrderMatchWithTransaction(\App\Module\Mex\Models\MexOrder $order): array
- {
- try {
- return DB::transaction(function () use ($order) {
- return \App\Module\Mex\Logic\MexMatchLogic::executeUserSellItemOrderMatch($order);
- });
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '用户卖出物品订单撮合失败:' . $e->getMessage(),
- 'order_id' => $order->id,
- 'total_amount' => '0.00000',
- ];
- }
- }
- /**
- * 执行单个用户买入物品订单的撮合(带事务处理)
- *
- * @param \App\Module\Mex\Models\MexOrder $order 用户买入物品订单
- * @param \App\Module\Mex\Models\MexWarehouse $warehouse 仓库信息
- * @return array 撮合结果
- */
- public static function executeUserBuyItemOrderMatchWithTransaction(\App\Module\Mex\Models\MexOrder $order, \App\Module\Mex\Models\MexWarehouse $warehouse): array
- {
- try {
- return DB::transaction(function () use ($order, $warehouse) {
- return \App\Module\Mex\Logic\MexMatchLogic::executeUserBuyItemOrderMatch($order, $warehouse);
- });
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '用户买入物品订单撮合失败:' . $e->getMessage(),
- 'order_id' => $order->id,
- 'total_amount' => '0.00000',
- ];
- }
- }
- /**
- * 获取撮合统计信息(已废弃,请使用getUserBuyItemMatchStats)
- *
- * @deprecated 请使用getUserBuyItemMatchStats或getUserSellItemMatchStats
- * @return array 统计信息
- */
- public static function getMatchStats(): array
- {
- return self::getUserBuyItemMatchStats();
- }
- }
|