PointToLoveQueue.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /****************************************************************
  3. * Author: king -- LiBaoJia
  4. * Date: 2020/4/14 6:11 PM
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * IDE: PhpStorm
  8. * User: 芸众商城 www.yunzshop.com
  9. ****************************************************************/
  10. namespace app\common\services\point;
  11. use app\common\facades\Setting;
  12. use app\common\models\UniAccount;
  13. use app\framework\Support\Facades\Log;
  14. use app\Jobs\PointToLoveJob;
  15. class PointToLoveQueue
  16. {
  17. private $pointSet;
  18. private $transferLoveSet;
  19. public function handle()
  20. {
  21. $uniAccount = UniAccount::getEnable() ?: [];
  22. foreach ($uniAccount as $u) {
  23. Setting::$uniqueAccountId = \YunShop::app()->uniacid = $u->uniacid;
  24. $this->pointSet = $this->pointSet();
  25. $this->transferLoveSet = $this->transferLoveSet();
  26. if ($this->isRun()) {
  27. (new PointToLoveJob($u->uniacid))->handle();
  28. //更新最后以及转入时间记录
  29. Setting::set('point.transfer_love', [
  30. 'last_month' => date('m'),
  31. 'last_week' => date('W'),
  32. 'last_day' => date('d')
  33. ]);
  34. }
  35. }
  36. }
  37. /**
  38. * 积分基础设置
  39. *
  40. * @return array
  41. */
  42. private function pointSet()
  43. {
  44. return Setting::get('point.set');
  45. }
  46. /**
  47. * 积分转入爱心值最后转入时间记录
  48. *
  49. * @return array
  50. */
  51. private function transferLoveSet()
  52. {
  53. return Setting::get('point.transfer_love');
  54. }
  55. /**
  56. * @return bool
  57. */
  58. private function isRun()
  59. {
  60. //爱心值插件是否开启
  61. if (!$this->lovePluginStatus()) {
  62. return false;
  63. }
  64. //是否开启积分转入爱心值
  65. if (!$this->transferLoveStatus()) {
  66. return false;
  67. }
  68. //转入周期:每天 / 每周,默认每天
  69. switch ($this->pointSet['transfer_cycle']) {
  70. case 1:
  71. return $this->weekly();
  72. break;
  73. default:
  74. return $this->everyDay();
  75. }
  76. }
  77. /**
  78. * 是否开启自动转入爱心值
  79. *
  80. * @return bool
  81. */
  82. private function transferLoveStatus()
  83. {
  84. return isset($this->pointSet['transfer_love']) && $this->pointSet['transfer_love'] == 1;
  85. }
  86. private function lovePluginStatus()
  87. {
  88. return app('plugins')->isEnabled('love');
  89. }
  90. /**
  91. * 每天转入爱心值
  92. *
  93. * @return bool
  94. */
  95. private function everyDay()
  96. {
  97. return $this->transferHour();
  98. }
  99. /**
  100. * 是否满足每周转入爱心值
  101. *
  102. * @return bool
  103. */
  104. private function weekly()
  105. {
  106. $lastWeek = $this->transferLoveSet['last_week'];
  107. if (isset($lastWeek) && $lastWeek == date('W')) {
  108. Log::info('========积分转入爱心值UNIACID:' . Setting::$uniqueAccountId . ',本年度第' . date('W') . '周已经激活========');
  109. return false;
  110. }
  111. $setWeek = $this->pointSet['transfer_time_week'];
  112. if ($setWeek != date('w')) {
  113. Log::info('========积分转入爱心值UNIACID:' . Setting::$uniqueAccountId . ',转入时间周' . $setWeek . '========');
  114. return false;
  115. }
  116. return $this->transferHour();
  117. }
  118. /**
  119. * 是否满足转入时间
  120. *
  121. * @return bool
  122. */
  123. private function transferHour()
  124. {
  125. $transferHour = $this->pointSet['transfer_time_hour'] - 1;
  126. if ($transferHour != (int)date('H')) {
  127. Log::info('========积分转入爱心值UNIACID:' . Setting::$uniqueAccountId . ',转入时间' . $transferHour . '点========');
  128. return false;
  129. }
  130. return $this->isTransferred();
  131. }
  132. /**
  133. * 今日是否已经转入
  134. *
  135. * @return bool
  136. */
  137. private function isTransferred()
  138. {
  139. $lastDay = $this->transferLoveSet['last_day'];
  140. if ($lastDay && $lastDay == date('d')) {
  141. Log::info('========积分转入爱心值UNIACID:' . \YunShop::app()->uniacid . ',' . date('d') . '日已经转入========');
  142. return false;
  143. }
  144. return true;
  145. }
  146. }