AutoUnLockIntegralListen.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace ln\listens;
  3. use app\common\repositories\user\UserBillRepository;
  4. use ln\interfaces\ListenerInterface;
  5. use ln\services\TimerService;
  6. use Swoole\Timer;
  7. use think\facade\Db;
  8. class AutoUnLockIntegralListen extends TimerService implements ListenerInterface
  9. {
  10. public function handle($event): void
  11. {
  12. //TODO 自动解冻积分
  13. $this->tick(1000 * 60 * 20, function () {
  14. $userBill = app()->make(UserBillRepository::class);
  15. request()->clearCache();
  16. $timer = ((int)systemConfig('integral_freeze'));
  17. $time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time());
  18. $bills = $userBill->getTimeoutIntegralBill($time);
  19. Db::transaction(function () use ($userBill, $bills) {
  20. foreach ($bills as $bill) {
  21. if ($bill->number > 0) {
  22. $integral = bcsub($bill->number, $userBill->refundIntegral($bill->link_id, $bill->uid), 2);
  23. if ($integral > 0) {
  24. $bill->user->integral = bcadd($bill->user->integral, $integral, 2);
  25. $bill->user->save();
  26. }
  27. }
  28. $bill->status = 1;
  29. $bill->balance = $bill->user->integral;
  30. $bill->save();
  31. }
  32. });
  33. });
  34. }
  35. }