| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace App\Module\Fund\Models;
- use App\Module\User\Models\User;
- use UCore\ModelCore;
- /**
- * 资金表
- *
- * field start
- * @property int $id 自增
- * @property int $user_id 用户ID
- * @property int $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
- /**
- *
- * @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);
- }
- }
|