Task.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\task\behavior\bargain;
  3. use think\Cache;
  4. use app\task\model\bargain\Task as TaskModel;
  5. use app\common\library\helper;
  6. /**
  7. * 砍价任务行为管理
  8. * Class Task
  9. * @package app\task\behavior\bargain
  10. */
  11. class Task
  12. {
  13. /* @var TaskModel $model */
  14. private $model;
  15. /**
  16. * 执行函数
  17. * @param $model
  18. * @return bool
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. */
  23. public function run($model)
  24. {
  25. if (!$model instanceof TaskModel) {
  26. return new TaskModel and false;
  27. }
  28. $this->model = $model;
  29. if (!$model::$wxapp_id) {
  30. return false;
  31. }
  32. if (!Cache::has('__task_space__bargain_task__')) {
  33. // 将已过期的砍价任务标记为已结束
  34. $this->onSetIsEnd();
  35. Cache::set('__task_space__bargain_task__', time(), 10);
  36. }
  37. return true;
  38. }
  39. /**
  40. * 将已过期的砍价任务标记为已结束
  41. * @return bool
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @throws \think\exception\DbException
  45. */
  46. private function onSetIsEnd()
  47. {
  48. // 获取已过期但未结束的砍价任务
  49. $list = $this->model->getEndList();
  50. $taskIds = helper::getArrayColumn($list, 'task_id');
  51. // 将砍价任务标记为已结束(批量)
  52. !empty($taskIds) && $this->model->setIsEnd($taskIds);
  53. // 记录日志
  54. $this->dologs('close', [
  55. 'orderIds' => json_encode($taskIds),
  56. ]);
  57. return true;
  58. }
  59. /**
  60. * 记录日志
  61. * @param $method
  62. * @param array $params
  63. * @return bool|int
  64. */
  65. private function dologs($method, $params = [])
  66. {
  67. $value = 'behavior bargain Task --' . $method;
  68. foreach ($params as $key => $val)
  69. $value .= ' --' . $key . ' ' . $val;
  70. return log_write($value);
  71. }
  72. }