Order.php 2.8 KB

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