OrderClose.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\command;
  20. use app\common\enum\OrderEnum;
  21. use app\common\enum\OrderLogEnum;
  22. use app\common\enum\PayEnum;
  23. use app\common\enum\YesNoEnum;
  24. use app\common\model\Goods;
  25. use app\common\model\GoodsItem;
  26. use app\common\model\Order;
  27. use app\common\model\OrderLog;
  28. use app\common\service\after_sale\AfterSaleService;
  29. use app\common\service\ConfigService;
  30. use think\console\Command;
  31. use think\console\Input;
  32. use think\console\Output;
  33. use think\facade\Db;
  34. use think\facade\Log;
  35. /**
  36. * 关闭超时待付款订单
  37. * Class OrderClose
  38. * @package app\common\command
  39. */
  40. class OrderClose extends Command
  41. {
  42. protected function configure()
  43. {
  44. $this->setName('order_close')
  45. ->setDescription('系统关闭超时未付款订单');
  46. }
  47. protected function execute(Input $input, Output $output)
  48. {
  49. $now = time();
  50. $ableClose = ConfigService::get('transaction', 'cancel_unpaid_orders');
  51. $cancelTime = ConfigService::get('transaction', 'cancel_unpaid_orders_times') * 60;
  52. if ($ableClose == YesNoEnum::NO) {
  53. return true;
  54. }
  55. $orders = Order::with('order_goods')
  56. ->whereRaw("create_time+$cancelTime < $now")
  57. ->where([
  58. 'order_status' => OrderEnum::STATUS_WAIT_PAY,
  59. 'pay_status' => PayEnum::UNPAID,
  60. ])->select();
  61. if (empty($orders)) {
  62. return true;
  63. }
  64. try{
  65. foreach ($orders as $order) {
  66. //回退订单商品库存
  67. $this->rollbackGoods($order);
  68. //更新订单状态
  69. $this->updateOrderSattus($order);
  70. //如有使用优惠券, 返回优惠券
  71. AfterSaleService::returnCoupon($order);
  72. }
  73. } catch(\Exception $e) {
  74. Log::write('订单自动关闭失败,失败原因:' . $e->getMessage());
  75. }
  76. }
  77. /**
  78. * @notes 回退库存
  79. * @param $order
  80. * @author 段誉
  81. * @date 2021/9/15 14:32
  82. */
  83. protected function rollbackGoods($order)
  84. {
  85. foreach ($order['order_goods'] as $good) {
  86. Goods::update([
  87. 'total_stock' => Db::raw('total_stock+' . $good['goods_num'])
  88. ], ['id' => $good['goods_id']]);
  89. //补充规格表库存
  90. GoodsItem::update([
  91. 'stock' => Db::raw('stock+' . $good['goods_num'])
  92. ], ['id' => $good['item_id']]);
  93. }
  94. }
  95. /**
  96. * @notes 更新订单状态
  97. * @param $order
  98. * @author 段誉
  99. * @date 2021/9/15 14:32
  100. */
  101. protected function updateOrderSattus($order)
  102. {
  103. //更新订单状态
  104. Order::update(['order_status' => OrderEnum::STATUS_CLOSE], ['id' => $order['id']]);
  105. // 订单日志
  106. (new OrderLog())->record([
  107. 'type' => OrderLogEnum::TYPE_SYSTEM,
  108. 'channel' => OrderLogEnum::SYSTEM_CANCEL_ORDER,
  109. 'order_id' => $order['id'],
  110. 'operator_id' => $order['user_id'],
  111. ]);
  112. }
  113. }