SaveChangeLogJob.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Jobs;
  3. use App\Constants\ChangeLogConstant;
  4. use App\Model\ChangeLogModel;
  5. use App\Model\UserModel;
  6. use Hyperf\AsyncQueue\Job;
  7. /**
  8. * 帐变记录保存
  9. *
  10. * 这块用户查询的余额数据
  11. */
  12. class SaveChangeLogJob extends Job
  13. {
  14. public $params;
  15. public function __construct($params)
  16. {
  17. $this->params = $params;
  18. }
  19. public function handle()
  20. {
  21. $itemId = data_get($this->params, 'item_id', null);
  22. try {
  23. $userInfo = UserModel::query()->where('code', $this->params['user_uid'])->first(['coins', 'balance', 'code']);
  24. // 1:金币;2:现金
  25. if ($this->params['currency_type'] == 1) {
  26. $amount = $userInfo->coins;
  27. } else {
  28. $amount = $userInfo->balance;
  29. }
  30. $beforeAmount = 0;
  31. if ($amount != 0) {
  32. switch ($this->params['way']) {
  33. case 1:
  34. if ($amount < $this->params['amount']) {
  35. $beforeAmount = $amount;
  36. $amount = $amount + $this->params['amount'];
  37. } else {
  38. $beforeAmount = $amount - $this->params['amount'];
  39. }
  40. break;
  41. case 2:
  42. $beforeAmount = $amount + $this->params['amount'];
  43. break;
  44. }
  45. } else {
  46. if ($this->params['way'] == 2) {
  47. $beforeAmount = $amount + $this->params['amount'];
  48. }
  49. }
  50. $saveData = [
  51. 'user_uid' => $userInfo->code,
  52. 'way' => $this->params['way'],
  53. 'currency_type' => $this->params['currency_type'],
  54. 'amount' => $this->params['amount'],
  55. 'before_amount' => $beforeAmount,
  56. 'after_amount' => $amount,
  57. 'type' => $this->params['type'],
  58. 'remark' => ChangeLogConstant::TYPE_TEXT[$this->params['type']],
  59. ];
  60. if (!is_null($itemId)) {
  61. // 关联ID
  62. $saveData['item_id'] = $itemId;
  63. }
  64. $res = ChangeLogModel::query()->create($saveData);
  65. if (!$res) {
  66. logger('SaveChangeLogJob-储存帐变失败', $saveData);
  67. }
  68. } catch (\Exception $exception) {
  69. logger('SaveChangeLogJob', [$exception->getMessage()]);
  70. }
  71. }
  72. }