UserModel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\common\model;
  3. use think\db\exception\DataNotFoundException;
  4. use think\db\exception\ModelNotFoundException;
  5. use think\Exception;
  6. use think\exception\DbException;
  7. use think\Model;
  8. /**
  9. * 会员模型
  10. */
  11. class UserModel extends Model
  12. {
  13. // // 开启自动写入时间戳字段
  14. // protected $autoWriteTimestamp = 'int';
  15. // // 定义时间戳字段名
  16. // protected $createTime = 'createtime';
  17. // protected $updateTime = 'updatetime';
  18. // // 追加属性
  19. // protected $append = [
  20. // 'url',
  21. // ];.
  22. protected $name = 'user';
  23. public function getById($userID)
  24. {
  25. return $this->where('id', $userID)->find();
  26. }
  27. public function getByAddress($address)
  28. {
  29. return $this->where("address", $address)->find();
  30. }
  31. public function getAllAddress()
  32. {
  33. $all_list = $this->field('id,address')->select();
  34. $arr = [];
  35. foreach ($all_list as $item){
  36. $arr[$item['address']] = $item['id'];
  37. }
  38. return $arr;
  39. }
  40. //获取用户余额 冻结金额 和 折合金额 frozen_amount convert_mount
  41. public static function getUserAmount(int $uid): float
  42. {
  43. return self::where('id', $uid)->sum("balance-frozen_amount");
  44. }
  45. /**
  46. * 余额更新数据
  47. * @param int $uid
  48. * @param string $power
  49. * @return void
  50. */
  51. public static function updateForRBalance(int $uid, string $score, string $action)
  52. {
  53. if($action == '+'){
  54. return self::where('id', $uid)->setInc('balance', $score);
  55. }else{
  56. return self::where('id', $uid)->setDec('balance', $score);
  57. }
  58. }
  59. /**
  60. * 添加上级的分享奖励和直推人数
  61. * @return void
  62. * @throws DbException
  63. * @throws Exception
  64. * @throws DataNotFoundException
  65. * @throws ModelNotFoundException
  66. */
  67. public static function updateParentData(string $uid, string $from_id)
  68. {
  69. $parent = self::where('id', $uid)->find();
  70. $amount = getConfig('direct_income');
  71. UserBalanceLog::create([
  72. 'user_id' => $uid,
  73. 'from_id' => $from_id,
  74. 'type_id' => UserBalanceLog::Share,
  75. 'amount' => $amount,
  76. 'before' => $parent['balance'],
  77. 'after' => $parent['balance'] + $amount,
  78. 'action' => '+'
  79. ]);
  80. $parent['balance'] = $parent['balance'] + $amount;
  81. $parent['team_num'] = $parent['team_num'] +1;
  82. $parent['direct_num'] = $parent['direct_num'] +1;
  83. $parent->save();
  84. }
  85. }