| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace app\common\model;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\exception\DbException;
- use think\Model;
- /**
- * 会员模型
- */
- class UserModel extends Model
- {
- // // 开启自动写入时间戳字段
- // protected $autoWriteTimestamp = 'int';
- // // 定义时间戳字段名
- // protected $createTime = 'createtime';
- // protected $updateTime = 'updatetime';
- // // 追加属性
- // protected $append = [
- // 'url',
- // ];.
- protected $name = 'user';
- public function getById($userID)
- {
- return $this->where('id', $userID)->find();
- }
- public function getByAddress($address)
- {
- return $this->where("address", $address)->find();
- }
- public function getAllAddress()
- {
- $all_list = $this->field('id,address')->select();
- $arr = [];
- foreach ($all_list as $item){
- $arr[$item['address']] = $item['id'];
- }
- return $arr;
- }
- //获取用户余额 冻结金额 和 折合金额 frozen_amount convert_mount
- public static function getUserAmount(int $uid): float
- {
- return self::where('id', $uid)->sum("balance-frozen_amount");
- }
-
- /**
- * 余额更新数据
- * @param int $uid
- * @param string $power
- * @return void
- */
- public static function updateForRBalance(int $uid, string $score, string $action)
- {
- if($action == '+'){
- return self::where('id', $uid)->setInc('balance', $score);
- }else{
- return self::where('id', $uid)->setDec('balance', $score);
- }
- }
- /**
- * 添加上级的分享奖励和直推人数
- * @return void
- * @throws DbException
- * @throws Exception
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public static function updateParentData(string $uid, string $from_id)
- {
- $parent = self::where('id', $uid)->find();
- $amount = getConfig('direct_income');
- UserBalanceLog::create([
- 'user_id' => $uid,
- 'from_id' => $from_id,
- 'type_id' => UserBalanceLog::Share,
- 'amount' => $amount,
- 'before' => $parent['balance'],
- 'after' => $parent['balance'] + $amount,
- 'action' => '+'
- ]);
- $parent['balance'] = $parent['balance'] + $amount;
- $parent['team_num'] = $parent['team_num'] +1;
- $parent['direct_num'] = $parent['direct_num'] +1;
- $parent->save();
- }
- }
|