| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Jobs;
- use App\Constants\ChangeLogConstant;
- use App\Model\ChangeLogModel;
- use App\Model\UserModel;
- use Hyperf\AsyncQueue\Job;
- /**
- * 帐变记录保存
- *
- * 这块用户查询的余额数据
- */
- class SaveChangeLogJob extends Job
- {
- public $params;
- public function __construct($params)
- {
- $this->params = $params;
- }
- public function handle()
- {
- $itemId = data_get($this->params, 'item_id', null);
- try {
- $userInfo = UserModel::query()->where('code', $this->params['user_uid'])->first(['coins', 'balance', 'code']);
- // 1:金币;2:现金
- if ($this->params['currency_type'] == 1) {
- $amount = $userInfo->coins;
- } else {
- $amount = $userInfo->balance;
- }
- $beforeAmount = 0;
- if ($amount != 0) {
- switch ($this->params['way']) {
- case 1:
- if ($amount < $this->params['amount']) {
- $beforeAmount = $amount;
- $amount = $amount + $this->params['amount'];
- } else {
- $beforeAmount = $amount - $this->params['amount'];
- }
- break;
- case 2:
- $beforeAmount = $amount + $this->params['amount'];
- break;
- }
- } else {
- if ($this->params['way'] == 2) {
- $beforeAmount = $amount + $this->params['amount'];
- }
- }
- $saveData = [
- 'user_uid' => $userInfo->code,
- 'way' => $this->params['way'],
- 'currency_type' => $this->params['currency_type'],
- 'amount' => $this->params['amount'],
- 'before_amount' => $beforeAmount,
- 'after_amount' => $amount,
- 'type' => $this->params['type'],
- 'remark' => ChangeLogConstant::TYPE_TEXT[$this->params['type']],
- ];
- if (!is_null($itemId)) {
- // 关联ID
- $saveData['item_id'] = $itemId;
- }
- $res = ChangeLogModel::query()->create($saveData);
- if (!$res) {
- logger('SaveChangeLogJob-储存帐变失败', $saveData);
- }
- } catch (\Exception $exception) {
- logger('SaveChangeLogJob', [$exception->getMessage()]);
- }
- }
- }
|