AutoUnlockMerchantMoneyListen.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace ln\listens;
  3. use app\common\repositories\system\merchant\MerchantRepository;
  4. use app\common\repositories\user\UserBillRepository;
  5. use ln\interfaces\ListenerInterface;
  6. use ln\services\TimerService;
  7. use think\facade\Db;
  8. class AutoUnlockMerchantMoneyListen extends TimerService implements ListenerInterface
  9. {
  10. public function handle($event): void
  11. {
  12. $this->tick(1000 * 60 * 20, function () {
  13. request()->clearCache();
  14. $userBill = app()->make(UserBillRepository::class);
  15. $timer = ((int)systemConfig('mer_lock_time'));
  16. $time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time());
  17. $bills = $userBill->getTimeoutMerchantMoneyBill($time);
  18. $merchant = app()->make(MerchantRepository::class);
  19. foreach ($bills as $bill) {
  20. $money = bcsub($bill->number, $userBill->refundMerchantMoney($bill->link_id, $bill->type, $bill->mer_id), 2);
  21. Db::transaction(function () use ($bill, $merchant, $money) {
  22. if ($money > 0) {
  23. $merchant->addMoney($bill->mer_id, $money);
  24. }
  25. $bill->status = 1;
  26. $bill->save();
  27. });
  28. }
  29. });
  30. }
  31. }