PointQueueService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2019/3/31
  5. * Time: 10:36 PM
  6. */
  7. namespace app\common\services\finance;
  8. use app\common\models\UniAccount;
  9. use app\Jobs\PointQueueJob;
  10. class PointQueueService
  11. {
  12. /**
  13. * 当前时间:商品赠送积分每月赠送验证参数
  14. *
  15. * @var string
  16. */
  17. private $nowTime;
  18. public function __construct()
  19. {
  20. $this->nowTime = $this->nowTime();
  21. }
  22. public function handle()
  23. {
  24. $uniAccount = UniAccount::getEnable() ?: [];
  25. foreach ($uniAccount as $u) {
  26. \Setting::$uniqueAccountId = \YunShop::app()->uniacid = $u->uniacid;
  27. $this->pointQueue();
  28. }
  29. }
  30. private function pointQueue()
  31. {
  32. if ($this->isRun()) {
  33. dispatch(new PointQueueJob(\YunShop::app()->uniacid));
  34. }
  35. }
  36. /**
  37. * 是否运行
  38. *
  39. * @return bool
  40. */
  41. private function isRun()
  42. {
  43. if (date('d') != 1) {
  44. return false;
  45. }
  46. if ($this->isFinish()) {
  47. return false;
  48. }
  49. $this->setTime();
  50. return true;
  51. }
  52. private function setTime()
  53. {
  54. $setLog['return_at'] = $this->nowTime;
  55. \Setting::set('point_queue.return_log', $setLog);
  56. }
  57. /**
  58. * 是否已经执行完成
  59. *
  60. * @return bool
  61. */
  62. private function isFinish()
  63. {
  64. $lastRunTime = $this->lastRunTime();
  65. return $this->nowTime == $lastRunTime;
  66. }
  67. /**
  68. * 最会一次执行时间
  69. *
  70. * @return string|null
  71. */
  72. private function lastRunTime()
  73. {
  74. $setLog = \Setting::get('point_queue.return_log');
  75. return $setLog['return_at'];
  76. }
  77. /**
  78. * 当前时间(验证格式)
  79. *
  80. * @return string
  81. */
  82. private function nowTime()
  83. {
  84. return date('y') . '-' . date('m') . '-' . date('d');
  85. }
  86. }