| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace App\Module\Fund\Models;
- use App\Module\Fund\Enums\FUND_TYPE;
- use App\Module\User\Models\User;
- use App\Module\User\Models\UserInfo;
- use UCore\ModelCore;
- /**
- * 用户账户表
- *
- * 存储用户的各种账户信息及余额。每个用户可以拥有多个不同种类的账户,
- * 每个账户对应一种账户种类(FundConfigModel)。
- * 同一币种可以有多个不同用途的账户,如可用账户、冻结账户等。
- *
- * field start
- * @property int $id 自增
- * @property int $user_id 用户ID
- * @property \App\Module\Fund\Enums\FUND_TYPE $fund_id 资金ID
- * @property int $balance 余额
- * @property int $update_time 更新时间
- * @property int $create_time 创建时间
- * field end
- */
- class FundModel extends ModelCore
- {
- protected $table = 'fund';
- public $timestamps = false;
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'fund_id',
- 'balance',
- 'update_time',
- 'create_time',
- ];
- // attrlist end
- protected $casts = [
- 'fund_id' => FUND_TYPE::class
- ];
- public static function userAccount($user_id)
- {
- $q = self::query()->where([
- 'user_id' => $user_id,
- ])->lockForUpdate()->get();
- return $q;
- }
- /**
- *
- * @param $user_id
- * @param $fund_id
- * @return false|FundModel
- */
- static public function getAccount($user_id, $fund_id)
- {
- $q = self::query()->where([
- 'user_id' => $user_id,
- 'fund_id' => $fund_id
- ])->lockForUpdate()->first();
- return $q;
- }
- /**
- * @param $userId
- * @param $fundIds
- * @return \Illuminate\Database\Eloquent\Collection
- * 获取用户全部资金账户
- */
- public static function getAllAccount($userId, $fundIds)
- {
- $query = self::query();
- $query->where('user_id', $userId);
- $query->whereIn('fund_id', $fundIds);
- return $query->get();
- }
- public function user()
- {
- return $this->hasOne(User::class, 'id', 'user_id');
- }
- //
- public function userInfo()
- {
- return $this->hasOne(UserInfo::class, 'user_id', 'user_id');
- }
- /**
- * @param $userId
- * @param $fundId
- * @param $amount
- * @return int
- * 减少资金
- */
- public static function dec($userId, $fundId, $amount)
- {
- $query = self::query();
- $query->where('user_id', $userId);
- $query->where('fund_id', $fundId);
- return $query->decrement('balance', $amount);
- }
- /**
- * @param $userId
- * @param $fundId
- * @param $amount
- * @return int
- * 增加资金
- */
- public static function inc($userId, $fundId, $amount)
- {
- $query = self::query();
- $query->where('user_id', $userId);
- $query->where('fund_id', $fundId);
- return $query->increment('balance', $amount);
- }
- }
|