'加', self::TYPE_DEC => '减', ]; function newPlatformBalance($platform_id, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|bool { $resModel = $this->findItemByWhere(['platform_id' => $platform_id, 'currency_code' => $currency_code]); if ($resModel) return $resModel; $oWalletCurrencyModel = new WalletCurrencyModel(); $resWalletCurrencyModel = $oWalletCurrencyModel->findByCode($currency_code); if (!$resWalletCurrencyModel) throw new ModelException('currency_code error'); $insert = [ 'platform_id' => $platform_id, 'currency_id' => $oWalletCurrencyModel->id, 'currency_code' => $oWalletCurrencyModel->code, 'created_at' => Times::getNowDateTime(), 'updated_at' => Times::getNowDateTime(), ]; return $this->addItem($insert); } function findPlatformBalance($platform_id, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null { return $this->findItemByWhere(['platform_id' => $platform_id, 'currency_code' => $currency_code]); } function findPlatformBalanceOrCreate($platform_id, $currency_code): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|bool|array|null { $resModel = $this->findItemByWhere(['platform_id' => $platform_id, 'currency_code' => $currency_code]); if (!$resModel) { //不存在就创建一个 $resModel = $this->newPlatformBalance($platform_id, $currency_code); } return $resModel; } //增加平台余额 function incAvailableBalance($id, $amount): bool { $amount = abs($amount); $oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'available_amount']); if (!$oModel) throw new ModelException('找不到id'); $oModel->frozen_amount = Db::raw('total_amount + ' . $amount); $oModel->available_amount = Db::raw('available_amount + ' . $amount); return $oModel->save(); } //扣除平台余额 function decAvailableBalance($id, $amount): bool { $amount = abs($amount); $oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'available_amount']); if (!$oModel) throw new ModelException('找不到id'); $oModel->frozen_amount = Db::raw('total_amount - ' . $amount); $oModel->available_amount = Db::raw('available_amount - ' . $amount); return $oModel->save(); } function decFrozenAmountById($id, $amount): int { $amount = abs($amount); $oModel = $this->newQuery()->where('id', $id)->first(['total_amount', 'frozen_amount']); if (!$oModel) throw new ModelException('找不到id'); $oModel->frozen_amount = Db::raw('frozen_amount - ' . $amount); $oModel->total_amount = Db::raw('total_amount - ' . $amount); return $oModel->save(); } function unFrozenAmountById($id, $amount): int { $amount = abs($amount); $oModel = $this->newQuery()->where('id', $id)->first(['frozen_amount', 'available_amount']); if (!$oModel) throw new ModelException('找不到id'); $oModel->frozen_amount = Db::raw('frozen_amount - ' . $amount); $oModel->available_amount = Db::raw('available_amount + ' . $amount); return $oModel->save(); } function frozenAmountById($id, $amount): int { $amount = abs($amount); $oModel = $this->newQuery()->where('id', $id)->first(['frozen_amount', 'available_amount']); if (!$oModel) throw new ModelException('找不到id'); $oModel->frozen_amount = Db::raw('frozen_amount + ' . $amount); $oModel->available_amount = Db::raw('available_amount - ' . $amount); return $oModel->save(); } }