AssembleBill.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\job\event;
  3. use think\facade\Cache;
  4. use app\job\model\plus\assemble\Bill as BillModel;
  5. use app\common\library\helper;
  6. /**
  7. * 拼团任务行为管理
  8. */
  9. class AssembleBill
  10. {
  11. private $model;
  12. /**
  13. * 执行函数
  14. */
  15. public function handle()
  16. {
  17. try {
  18. $this->model = new BillModel();
  19. $cacheKey = "task_space_assemble_bill_task";
  20. if (!Cache::has($cacheKey)) {
  21. // 将已过期的拼团任务标记为已结束
  22. $this->closeAssemble();
  23. Cache::set($cacheKey, time(), 10);
  24. }
  25. } catch (\Throwable $e) {
  26. echo 'ERROR AssembleBill: ' . $e->getMessage() . PHP_EOL;
  27. log_write('AssembleBill TASK : ' . '__ ' . $e->getMessage(), 'task');
  28. }
  29. return true;
  30. }
  31. /**
  32. * 到期订单未拼团成功自动关闭,并退款
  33. */
  34. private function closeAssemble()
  35. {
  36. // 获取到期未拼团成功的订单
  37. $billList = $this->model->getCloseIds(0);
  38. $billIds = helper::getArrayColumn($billList, 'assemble_bill_id');
  39. if (!empty($billIds)) {
  40. //关闭订单
  41. $this->model->close($billIds);
  42. }
  43. // 记录日志
  44. $this->dologs('closeAssemble fail', [
  45. 'billIds' => json_encode($billIds),
  46. 'error' => $this->model->getError()
  47. ]);
  48. // 获取到期未拼团成功的订单,自动成团
  49. $billList = $this->model->getCloseIds(1);
  50. $billIds = helper::getArrayColumn($billList, 'assemble_bill_id');
  51. if (!empty($billIds)) {
  52. //关闭订单
  53. $this->model->success($billIds);
  54. }
  55. // 退款
  56. $this->model->orderRefund();
  57. // 记录日志
  58. $this->dologs('closeAssemble success', [
  59. 'billIds' => json_encode($billIds),
  60. 'error' => $this->model->getError()
  61. ]);
  62. return true;
  63. }
  64. /**
  65. * 记录日志
  66. */
  67. private function dologs($method, $params = [])
  68. {
  69. $value = 'behavior assemble_bill Task --' . $method;
  70. foreach ($params as $key => $val)
  71. $value .= ' --' . $key . ' ' . $val;
  72. return log_write($value, 'task');
  73. }
  74. }