Order.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\console\service;
  13. use app\console\library\Tools;
  14. use app\console\model\Order as OrderModel;
  15. use app\common\service\BaseService;
  16. use app\common\service\order\Complete as OrderCompleteService;
  17. use app\common\service\Order as OrderService;
  18. use app\common\enum\order\OrderStatus as OrderStatusEnum;
  19. use app\common\library\helper;
  20. /**
  21. * 服务类:订单模块
  22. * Class Order
  23. * @package app\console\service
  24. */
  25. class Order extends BaseService
  26. {
  27. /**
  28. * 未支付订单自动关闭
  29. * @param int $storeId
  30. * @param int $closeDays
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function closeEvent(int $storeId, int $closeDays)
  36. {
  37. // 截止时间
  38. $deadlineTime = time() - ((int)$closeDays * 86400);
  39. // 查询截止时间未支付的订单
  40. $model = new OrderModel;
  41. $list = $model->getListByClose($storeId, $deadlineTime);
  42. // 订单ID集
  43. $orderIds = helper::getArrayColumn($list, 'order_id');
  44. if (!empty($orderIds)) {
  45. // 取消订单事件
  46. foreach ($list as $order) {
  47. OrderService::cancelEvent($order);
  48. }
  49. // 批量更新订单状态为已取消
  50. $model->onBatchUpdate($orderIds, ['order_status' => OrderStatusEnum::CANCELLED]);
  51. }
  52. // 记录日志
  53. Tools::taskLogs('Order', 'closeEvent', [
  54. 'storeId' => $storeId,
  55. 'closeDays' => $closeDays,
  56. 'deadlineTime' => $deadlineTime,
  57. 'orderIds' => helper::jsonEncode($orderIds)
  58. ]);
  59. }
  60. /**
  61. * 已发货订单自动确认收货
  62. * @param int $storeId
  63. * @param int $receiveDays
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function receiveEvent(int $storeId, int $receiveDays)
  69. {
  70. // 截止时间
  71. $deadlineTime = time() - ((int)$receiveDays * 86400);
  72. // 查询截止时间未支付的订单ID集
  73. $model = new OrderModel;
  74. $orderIds = $model->getOrderIdsByReceive($storeId, $deadlineTime);
  75. // 更新订单收货状态
  76. if (!empty($orderIds)) {
  77. // 批量更新订单状态为已收货
  78. $model->onUpdateReceived($orderIds);
  79. // 批量处理已完成的订单
  80. $this->onReceiveCompleted($storeId, $orderIds);
  81. }
  82. // 记录日志
  83. Tools::taskLogs('Order', 'receiveEvent', [
  84. 'storeId' => $storeId,
  85. 'receiveDays' => $receiveDays,
  86. 'deadlineTime' => $deadlineTime,
  87. 'orderIds' => helper::jsonEncode($orderIds)
  88. ]);
  89. }
  90. /**
  91. * 批量处理已完成的订单
  92. * @param int $storeId 商城ID
  93. * @param array $orderIds 订单ID集
  94. * @return bool
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. private function onReceiveCompleted(int $storeId, array $orderIds)
  100. {
  101. // 获取已完成的订单列表
  102. $model = new OrderModel;
  103. $list = $model->getListByCompleted($storeId, $orderIds);
  104. // 执行订单完成后的操作
  105. if (!$list->isEmpty()) {
  106. $OrderCompleteService = new OrderCompleteService();
  107. $OrderCompleteService->complete($list, $storeId);
  108. }
  109. return true;
  110. }
  111. }