AutoUnLockBrokerageListen.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\listens;
  12. use app\common\repositories\user\UserBillRepository;
  13. use crmeb\interfaces\ListenerInterface;
  14. use crmeb\services\TimerService;
  15. use Swoole\Timer;
  16. use think\facade\Db;
  17. class AutoUnLockBrokerageListen extends TimerService implements ListenerInterface
  18. {
  19. public function handle($event): void
  20. {
  21. //TODO 自动解冻佣金
  22. $this->tick(1000 * 60 * 20, function () {
  23. $userBill = app()->make(UserBillRepository::class);
  24. request()->clearCache();
  25. $timer = ((int)systemConfig('lock_brokerage_timer'));
  26. $time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time());
  27. $bills = $userBill->getTimeoutBrokerageBill($time);
  28. Db::transaction(function () use ($userBill, $bills) {
  29. foreach ($bills as $bill) {
  30. if ($bill->number > 0) {
  31. $refund = $userBill->refundBrokerage($bill->link_id, $bill->uid);
  32. $bill->user->brokerage_price = bcadd($bill->user->brokerage_price, bcsub($bill->number, $refund, 2), 2);
  33. $bill->user->save();
  34. }
  35. $bill->status = 1;
  36. $bill->balance = $bill->user->brokerage_price;
  37. $bill->save();
  38. }
  39. });
  40. });
  41. }
  42. }