Order.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\task\behavior\sharp;
  3. use think\Cache;
  4. use app\task\model\Order as OrderModel;
  5. use app\task\model\sharp\Setting as SettingModel;
  6. use app\task\service\Order as OrderService;
  7. use app\common\enum\order\OrderSource as OrderSourceEnum;
  8. /**
  9. * 订单行为管理
  10. * Class Order
  11. * @package app\task\behavior
  12. */
  13. class Order
  14. {
  15. /* @var \app\task\model\Order $model */
  16. private $model;
  17. // 小程序id
  18. private $wxappId;
  19. /**
  20. * 执行函数
  21. * @param $model
  22. * @return bool
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. */
  27. public function run($model)
  28. {
  29. if (!$model instanceof OrderModel)
  30. return new OrderModel and false;
  31. if (!$model::$wxapp_id) return false;
  32. // 绑定订单模型
  33. $this->model = $model;
  34. $this->wxappId = $model::$wxapp_id;
  35. // 秒杀订单行为管理
  36. $this->sharp();
  37. return true;
  38. }
  39. /**
  40. * 秒杀订单行为管理
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. private function sharp()
  46. {
  47. // 未支付订单自动关闭
  48. $this->close();
  49. }
  50. /**
  51. * 未支付订单自动关闭
  52. * @return bool
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @throws \think\exception\DbException
  56. * @throws \Exception
  57. */
  58. private function close()
  59. {
  60. $key = "__task_space__sharp_order__{$this->wxappId}";
  61. if (Cache::has($key)) return true;
  62. // 取消n分钟以前的的未付款订单
  63. $minute = SettingModel::getItem('basic')['order']['order_close'];
  64. if ($minute < 1) return false;
  65. // 截止时间
  66. $deadlineTime = time() - ((int)$minute * 60);
  67. // 执行自动关闭
  68. $service = new OrderService;
  69. $service->close($deadlineTime, ['order_source' => OrderSourceEnum::SHARP]);
  70. // 记录日志
  71. $this->dologs('close', [
  72. 'close_minute' => (int)$minute,
  73. 'deadline_time' => $deadlineTime,
  74. 'orderIds' => json_encode($service->getCloseOrderIds()),
  75. ]);
  76. return true;
  77. }
  78. /**
  79. * 记录日志
  80. * @param $method
  81. * @param array $params
  82. * @return bool|int
  83. */
  84. private function dologs($method, $params = [])
  85. {
  86. $value = 'behavior sharp Order --' . $method;
  87. foreach ($params as $key => $val)
  88. $value .= ' --' . $key . ' ' . $val;
  89. return log_write($value);
  90. }
  91. }