截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrderClose.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\common\command;
  3. use app\common\enum\OrderEnum;
  4. use app\common\enum\PayEnum;
  5. use app\common\model\CouponList;
  6. use app\common\model\goods\Goods;
  7. use app\common\model\goods\GoodsItem;
  8. use app\common\model\order\Order;
  9. use app\common\server\ConfigServer;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\Output;
  13. use think\facade\Db;
  14. use think\facade\Log;
  15. class OrderClose extends Command
  16. {
  17. protected function configure()
  18. {
  19. $this->setName('order_close')
  20. ->setDescription('关闭订单');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. try {
  25. $time = time();
  26. $order_cancel_time = ConfigServer::get('transaction', 'unpaid_order_cancel_time', 60);
  27. // 配置0或为空时不取消订单
  28. if (empty($order_cancel_time)) {
  29. return true;
  30. }
  31. $order_cancel_time = $order_cancel_time * 60;
  32. $model = new Order();
  33. $order_list = $model->field(true)
  34. ->whereRaw("create_time+$order_cancel_time < $time")
  35. ->where([
  36. ['order_type', '<>', OrderEnum::TEAM_ORDER],
  37. ['order_status', '=', OrderEnum::ORDER_STATUS_NO_PAID],
  38. ['pay_status', '=', OrderEnum::PAY_STATUS_NO_PAID]
  39. ])->with(['orderGoods'])
  40. ->select()->toArray();
  41. $order_ids = []; //更新的订单
  42. $update_total_stock = []; //更新总库存
  43. $update_stock = []; //更新规格库存
  44. $total_stock_num = []; //总库存
  45. $stock_num = []; //规格库存
  46. $update_coupon_ids = []; //更新优惠券状态
  47. foreach ($order_list as $order) {
  48. $order_ids[] = $order['id'];
  49. //返回优惠券
  50. if ($order['coupon_list_id']) {
  51. $update_coupon_ids[] = $order['coupon_list_id'];
  52. }
  53. foreach ($order['orderGoods'] as $order_goods) {
  54. //更新商品总库存数据
  55. if (isset($update_total_stock[$order_goods['goods_id']])) {
  56. $total_stock_num[$order_goods['goods_id']] = $total_stock_num[$order_goods['goods_id']] + $order_goods['goods_num'];
  57. $update_total_stock[$order_goods['goods_id']]['stock'] = Db::raw('stock+' . $total_stock_num[$order_goods['goods_id']]);
  58. } else {
  59. $total_stock_num[$order_goods['goods_id']] = $order_goods['goods_num'];
  60. $update_total_stock[$order_goods['goods_id']] = [
  61. 'id' => $order_goods['goods_id'],
  62. 'stock' => Db::raw('stock+' . $total_stock_num[$order_goods['goods_id']])
  63. ];
  64. }
  65. //更新商品规格库存数据
  66. if (isset($update_stock[$order_goods['item_id']])) {
  67. $stock_num[$order_goods['item_id']] = $stock_num[$order_goods['item_id']] + $order_goods['goods_num'];
  68. $update_stock[$order_goods['item_id']]['stock'] = Db::raw('stock+' . $stock_num[$order_goods['item_id']]);
  69. } else {
  70. $stock_num[$order_goods['item_id']] = $order_goods['goods_num'];
  71. $update_stock[$order_goods['item_id']] = [
  72. 'id' => $order_goods['item_id'],
  73. 'stock' => Db::raw('stock+' . $stock_num[$order_goods['item_id']])
  74. ];
  75. }
  76. }
  77. }
  78. // 更新订单状态为关闭
  79. if ($order_ids) {
  80. $update_data = [
  81. 'order_status' => OrderEnum::ORDER_STATUS_DOWN,
  82. 'update_time' => $time,
  83. ];
  84. $model->where(['id' => $order_ids])->update($update_data);
  85. }
  86. //批量更新库存
  87. if($update_total_stock){
  88. (new Goods())->saveAll(array_values($update_total_stock));
  89. (new GoodsItem())->saveAll(array_values($update_stock));
  90. }
  91. if($update_coupon_ids){
  92. $update_coupon = [
  93. 'status' => 0,
  94. 'use_time' => '',
  95. 'order_id' => '',
  96. 'update_time' => $time,
  97. ];
  98. (new CouponList())->where(['id'=>$update_coupon_ids])->update($update_coupon);
  99. }
  100. return true;
  101. } catch (\Exception $e) {
  102. Log::write('自动关闭订单异常:'.$e->getMessage());
  103. return false;
  104. }
  105. }
  106. }