OrderBatchService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace addons\TinyShop\services\order;
  3. use Yii;
  4. use common\components\Service;
  5. use common\helpers\ArrayHelper;
  6. use common\forms\CreditsLogForm;
  7. use addons\TinyShop\common\enums\OrderStatusEnum;
  8. use addons\TinyShop\common\enums\OrderTypeEnum;
  9. use addons\TinyShop\common\models\order\Order;
  10. use addons\TinyShop\common\enums\CouponGetTypeEnum;
  11. use addons\TinyShop\common\enums\MarketingEnum;
  12. /**
  13. * Class OrderBatchService
  14. * @package addons\TinyShop\services\order
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class OrderBatchService extends Service
  18. {
  19. /**
  20. * 自动签收
  21. *
  22. * @return void
  23. */
  24. public function signAll()
  25. {
  26. $orderIds = Order::find()
  27. ->select('id')
  28. ->where(['order_status' => OrderStatusEnum::SHIPMENTS])
  29. ->andWhere(['>', 'auto_sign_time', 0])
  30. ->andWhere(['<', 'auto_sign_time', time()])
  31. ->column();
  32. try {
  33. foreach ($orderIds as $id) {
  34. Yii::$app->tinyShopService->order->takeDelivery($id);
  35. }
  36. } catch (\Exception $e) {
  37. }
  38. }
  39. /**
  40. * 批量发货
  41. *
  42. * @param $setting
  43. * @param $orders
  44. * @return void
  45. * @throws \yii\base\InvalidConfigException
  46. */
  47. public function finalizeAll($setting, $orders = [])
  48. {
  49. if (empty($orders)) {
  50. $orders = Order::find()
  51. ->where(['order_status' => OrderStatusEnum::SING])
  52. ->andWhere(['in', 'order_type', OrderTypeEnum::normal()])
  53. ->andWhere(['<=', 'auto_finish_time', time()])
  54. ->with(['marketingDetail', 'member'])
  55. ->all();
  56. }
  57. try {
  58. // 赠送优惠券列表
  59. $giveCouponTypeMap = [];
  60. foreach ($orders as $order) {
  61. // 赠送营销判断
  62. foreach ($order->marketingDetail as $marketingDetail) {
  63. if (empty($marketingDetail['give_coupon_type'])) {
  64. continue;
  65. }
  66. foreach ($marketingDetail['give_coupon_type'] as $giveCouponType) {
  67. if (!isset($giveCouponTypeMap[$giveCouponType['id']])) {
  68. $giveCouponTypeMap[$giveCouponType['id']] = 0;
  69. }
  70. $giveCouponTypeMap[$giveCouponType['id']] += $giveCouponType['number'];
  71. }
  72. }
  73. }
  74. $marketingCouponTypes = [];
  75. if (!empty($giveCouponTypeMap)) {
  76. $marketingCouponTypes = Yii::$app->tinyShopService->marketingCouponType->findByIds(array_keys($giveCouponTypeMap));
  77. $marketingCouponTypes = ArrayHelper::arrayKey($marketingCouponTypes, 'id');
  78. }
  79. // 会员折扣优惠
  80. $memberDiscountMoney = 0;
  81. /** @var Order $order */
  82. foreach ($orders as $order) {
  83. // 赠送优惠券和会员节省
  84. foreach ($order->marketingDetail as $value) {
  85. if (!empty($value['give_coupon_type'])) {
  86. foreach ($value['give_coupon_type'] as $giveCouponType) {
  87. if (isset($marketingCouponTypes[$giveCouponType['id']])) {
  88. Yii::$app->tinyShopService->marketingCoupon->giveByNewRecord(
  89. $marketingCouponTypes[$giveCouponType['id']],
  90. $order->buyer_id,
  91. $order->id,
  92. CouponGetTypeEnum::ORDER,
  93. $giveCouponTypeMap[$giveCouponType['id']]
  94. );
  95. }
  96. }
  97. }
  98. // 会员折扣优惠
  99. if ($value['marketing_type'] == MarketingEnum::MEMBER_DISCOUNT && $value['discount_money'] > 0) {
  100. $memberDiscountMoney += $value['discount_money'];
  101. }
  102. }
  103. Yii::$app->tinyShopService->order->finalize($order, $setting);
  104. // 加入节省
  105. if ($memberDiscountMoney > 0) {
  106. Yii::$app->services->memberCreditsLog->incrEconomizeMoney(new CreditsLogForm([
  107. 'member' => $order->member,
  108. 'num' => $memberDiscountMoney,
  109. 'group' => 'order',
  110. 'map_id' => $order->id,
  111. 'remark' => '订单-' . $order->order_sn,
  112. ]));
  113. }
  114. // 记录操作
  115. Yii::$app->services->actionLog->create('order', '自动完成', $order->id);
  116. }
  117. } catch (\Exception $e) {
  118. // 记录行为日志
  119. Yii::$app->services->log->push(500, 'tinyShopFinalizeAll', Yii::$app->services->base->getErrorInfo($e));
  120. }
  121. }
  122. /**
  123. * @return void
  124. * @throws \yii\base\InvalidConfigException
  125. */
  126. public function closeAll()
  127. {
  128. $orderIds = Order::find()
  129. ->select('id')
  130. ->where(['order_status' => OrderStatusEnum::NOT_PAY])
  131. ->andWhere(['<=', 'close_time', time()])
  132. ->column();
  133. try {
  134. foreach ($orderIds as $id) {
  135. Yii::$app->tinyShopService->order->close($id);
  136. // 记录操作
  137. Yii::$app->services->actionLog->create('order', '自动关闭订单', $id);
  138. }
  139. } catch (\Exception $e) {
  140. // 记录行为日志
  141. Yii::$app->services->log->push(500, 'tinyShopCloseAll', Yii::$app->services->base->getErrorInfo($e));
  142. }
  143. }
  144. }
粤ICP备19079148号