123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\task\behavior\sharp;
- use think\Cache;
- use app\task\model\Order as OrderModel;
- use app\task\model\sharp\Setting as SettingModel;
- use app\task\service\Order as OrderService;
- use app\common\enum\order\OrderSource as OrderSourceEnum;
- /**
- * 订单行为管理
- * Class Order
- * @package app\task\behavior
- */
- class Order
- {
- /* @var \app\task\model\Order $model */
- private $model;
- // 小程序id
- private $wxappId;
- /**
- * 执行函数
- * @param $model
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function run($model)
- {
- if (!$model instanceof OrderModel)
- return new OrderModel and false;
- if (!$model::$wxapp_id) return false;
- // 绑定订单模型
- $this->model = $model;
- $this->wxappId = $model::$wxapp_id;
- // 秒杀订单行为管理
- $this->sharp();
- return true;
- }
- /**
- * 秒杀订单行为管理
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- private function sharp()
- {
- // 未支付订单自动关闭
- $this->close();
- }
- /**
- * 未支付订单自动关闭
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \Exception
- */
- private function close()
- {
- $key = "__task_space__sharp_order__{$this->wxappId}";
- if (Cache::has($key)) return true;
- // 取消n分钟以前的的未付款订单
- $minute = SettingModel::getItem('basic')['order']['order_close'];
- if ($minute < 1) return false;
- // 截止时间
- $deadlineTime = time() - ((int)$minute * 60);
- // 执行自动关闭
- $service = new OrderService;
- $service->close($deadlineTime, ['order_source' => OrderSourceEnum::SHARP]);
- // 记录日志
- $this->dologs('close', [
- 'close_minute' => (int)$minute,
- 'deadline_time' => $deadlineTime,
- 'orderIds' => json_encode($service->getCloseOrderIds()),
- ]);
- return true;
- }
- /**
- * 记录日志
- * @param $method
- * @param array $params
- * @return bool|int
- */
- private function dologs($method, $params = [])
- {
- $value = 'behavior sharp Order --' . $method;
- foreach ($params as $key => $val)
- $value .= ' --' . $key . ' ' . $val;
- return log_write($value);
- }
- }
|