AutoUnLockBrokerageListen.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 AutoUnLockBrokerageListen 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('lock_brokerage_timer'));
  17. $time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time());
  18. $bills = $userBill->getTimeoutBrokerageBill($time);
  19. Db::transaction(function () use ($userBill, $bills) {
  20. foreach ($bills as $bill) {
  21. if ($bill->number > 0) {
  22. $brokerage = bcsub($bill->number, $userBill->refundBrokerage($bill->link_id, $bill->uid), 2);
  23. if ($brokerage > 0) {
  24. $bill->user->brokerage_price = bcadd($bill->user->brokerage_price, $brokerage, 2);
  25. $bill->user->save();
  26. }
  27. }
  28. $bill->status = 1;
  29. $bill->balance = $bill->user->brokerage_price;
  30. $bill->save();
  31. }
  32. });
  33. });
  34. }
  35. }