MexMatchService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Module\Mex\Services;
  3. use App\Module\Mex\Logic\MexMatchLogic;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 农贸市场撮合服务
  7. *
  8. * 提供撮合相关的对外服务接口
  9. */
  10. class MexMatchService
  11. {
  12. /**
  13. * 执行用户买入物品撮合任务
  14. *
  15. * @param int|null $itemId 指定商品ID,null表示处理所有商品
  16. * @param int $batchSize 批处理大小
  17. * @return array 撮合结果
  18. */
  19. public static function executeUserBuyItemMatch(?int $itemId = null, int $batchSize = 100): array
  20. {
  21. return MexMatchLogic::executeUserBuyItemMatch($itemId, $batchSize);
  22. }
  23. /**
  24. * 执行用户卖出物品撮合任务
  25. *
  26. * @param int|null $itemId 指定商品ID,null表示处理所有商品
  27. * @param int $batchSize 批处理大小
  28. * @return array 撮合结果
  29. */
  30. public static function executeUserSellItemMatch(?int $itemId = null, int $batchSize = 100): array
  31. {
  32. return MexMatchLogic::executeUserSellItemMatch($itemId, $batchSize);
  33. }
  34. /**
  35. * 执行单个商品的用户买入物品撮合(带事务处理)
  36. *
  37. * @param int $itemId 商品ID
  38. * @param int $batchSize 批处理大小
  39. * @return array 撮合结果
  40. */
  41. public static function executeUserBuyItemMatchForItem(int $itemId, int $batchSize = 100): array
  42. {
  43. try {
  44. return DB::transaction(function () use ($itemId, $batchSize) {
  45. return MexMatchLogic::executeUserBuyItemMatchForItem($itemId, $batchSize);
  46. });
  47. } catch (\Exception $e) {
  48. return [
  49. 'success' => false,
  50. 'message' => '用户买入物品撮合执行失败:' . $e->getMessage(),
  51. 'matched_orders' => 0,
  52. 'total_amount' => '0.00000',
  53. ];
  54. }
  55. }
  56. /**
  57. * 执行单个商品的用户卖出物品撮合(带事务处理)
  58. *
  59. * @param int $itemId 商品ID
  60. * @param int $batchSize 批处理大小
  61. * @return array 撮合结果
  62. */
  63. public static function executeUserSellItemMatchForItem(int $itemId, int $batchSize = 100): array
  64. {
  65. try {
  66. return DB::transaction(function () use ($itemId, $batchSize) {
  67. return MexMatchLogic::executeUserSellItemMatchForItem($itemId, $batchSize);
  68. });
  69. } catch (\Exception $e) {
  70. return [
  71. 'success' => false,
  72. 'message' => '用户卖出物品撮合执行失败:' . $e->getMessage(),
  73. 'matched_orders' => 0,
  74. 'total_amount' => '0.00000',
  75. ];
  76. }
  77. }
  78. /**
  79. * 获取用户买入物品撮合统计信息
  80. *
  81. * @return array 统计信息
  82. */
  83. public static function getUserBuyItemMatchStats(): array
  84. {
  85. return MexMatchLogic::getUserBuyItemMatchStats();
  86. }
  87. /**
  88. * 获取用户卖出物品撮合统计信息
  89. *
  90. * @return array 统计信息
  91. */
  92. public static function getUserSellItemMatchStats(): array
  93. {
  94. return MexMatchLogic::getUserSellItemMatchStats();
  95. }
  96. /**
  97. * 检查用户买入物品撮合条件
  98. *
  99. * @param int $itemId 商品ID
  100. * @return array 检查结果
  101. */
  102. public static function checkUserBuyItemMatchConditions(int $itemId): array
  103. {
  104. return MexMatchLogic::checkUserBuyItemMatchConditions($itemId);
  105. }
  106. /**
  107. * 检查用户卖出物品撮合条件
  108. *
  109. * @param int $itemId 商品ID
  110. * @return array 检查结果
  111. */
  112. public static function checkUserSellItemMatchConditions(int $itemId): array
  113. {
  114. return MexMatchLogic::checkUserSellItemMatchConditions($itemId);
  115. }
  116. /**
  117. * 执行单个用户卖出物品订单的撮合(带事务处理)
  118. *
  119. * @param \App\Module\Mex\Models\MexOrder $order 用户卖出物品订单
  120. * @return array 撮合结果
  121. */
  122. public static function executeUserSellItemOrderMatchWithTransaction(\App\Module\Mex\Models\MexOrder $order): array
  123. {
  124. try {
  125. return DB::transaction(function () use ($order) {
  126. return \App\Module\Mex\Logic\MexMatchLogic::executeUserSellItemOrderMatch($order);
  127. });
  128. } catch (\Exception $e) {
  129. return [
  130. 'success' => false,
  131. 'message' => '用户卖出物品订单撮合失败:' . $e->getMessage(),
  132. 'order_id' => $order->id,
  133. 'total_amount' => '0.00000',
  134. ];
  135. }
  136. }
  137. /**
  138. * 执行单个用户买入物品订单的撮合(带事务处理)
  139. *
  140. * @param \App\Module\Mex\Models\MexOrder $order 用户买入物品订单
  141. * @param \App\Module\Mex\Models\MexWarehouse $warehouse 仓库信息
  142. * @return array 撮合结果
  143. */
  144. public static function executeUserBuyItemOrderMatchWithTransaction(\App\Module\Mex\Models\MexOrder $order, \App\Module\Mex\Models\MexWarehouse $warehouse): array
  145. {
  146. try {
  147. return DB::transaction(function () use ($order, $warehouse) {
  148. return \App\Module\Mex\Logic\MexMatchLogic::executeUserBuyItemOrderMatch($order, $warehouse);
  149. });
  150. } catch (\Exception $e) {
  151. return [
  152. 'success' => false,
  153. 'message' => '用户买入物品订单撮合失败:' . $e->getMessage(),
  154. 'order_id' => $order->id,
  155. 'total_amount' => '0.00000',
  156. ];
  157. }
  158. }
  159. /**
  160. * 获取撮合统计信息(已废弃,请使用getUserBuyItemMatchStats)
  161. *
  162. * @deprecated 请使用getUserBuyItemMatchStats或getUserSellItemMatchStats
  163. * @return array 统计信息
  164. */
  165. public static function getMatchStats(): array
  166. {
  167. return self::getUserBuyItemMatchStats();
  168. }
  169. }