UserModel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\model;
  3. use think\db\exception\DataNotFoundException;
  4. use think\db\exception\ModelNotFoundException;
  5. use app\common\model\UserPathModel;
  6. use think\Exception;
  7. use app\common\model\LedgerWalletModel;
  8. use think\Model;
  9. /**
  10. * 会员模型
  11. */
  12. class UserModel extends Model
  13. {
  14. // // 开启自动写入时间戳字段
  15. // protected $autoWriteTimestamp = 'int';
  16. // // 定义时间戳字段名
  17. // protected $createTime = 'createtime';
  18. // protected $updateTime = 'updatetime';
  19. // // 追加属性
  20. // protected $append = [
  21. // 'url',
  22. // ];.
  23. protected $name = 'user';
  24. public function getById($userID)
  25. {
  26. return $this->where('id', $userID)->find();
  27. }
  28. public function getByAddress($address)
  29. {
  30. return $this->where("address", $address)->find();
  31. }
  32. public function getAllAddress()
  33. {
  34. $all_list = $this->field('id,address')->select();
  35. $arr = [];
  36. foreach ($all_list as $item){
  37. $arr[$item['address']] = $item['id'];
  38. }
  39. return $arr;
  40. }
  41. //获取用户余额 冻结金额 和 折合金额
  42. public static function getUserAmount(int $uid): float
  43. {
  44. return self::where('id', $uid)->sum("balance-frozen_amount");
  45. }
  46. //获取用户RWA
  47. public static function getUserRwaNum(int $uid): int
  48. {
  49. return self::where('id', $uid)->value("rwa_num");
  50. }
  51. /**
  52. * RWA更新数据
  53. * @param int $uid
  54. * @param string $power
  55. * @return void
  56. */
  57. public static function updateForRwaNum(int $uid, string $score, string $action)
  58. {
  59. if($action == '+'){
  60. return self::where('id', $uid)->setInc('rwa_num', $score);
  61. }else{
  62. return self::where('id', $uid)->setDec('rwa_num', $score);
  63. }
  64. }
  65. /**
  66. * 余额更新数据
  67. * @param int $uid
  68. * @param string $power
  69. * @return void
  70. */
  71. public static function updateForRBalance(int $uid, string $score, string $action)
  72. {
  73. if($action == '+'){
  74. return self::where('id', $uid)->setInc('balance', $score);
  75. }else{
  76. return self::where('id', $uid)->setDec('balance', $score);
  77. }
  78. }
  79. //社区向上发放奖励津贴
  80. public static function setCommunityRewards($uid, $pv, $token)
  81. {
  82. $paths = UserPathModel::where('user_id', $uid)->where('distance', '<', 11)->order('distance', 'asc')->column('distance,parent_id');
  83. if (!empty($paths)) {
  84. $model = new LedgerWalletModel();
  85. foreach ($paths as $kk=>$item) {
  86. if($kk== 1){
  87. $model->changeWalletAccount($item, $token, $pv, $model::Community, $uid);
  88. }else{
  89. //间接推荐有效会员大于层级
  90. if(self::where('parent_id', $item)->where('rwa_num','>', 0)->count() >= $kk){
  91. $model->changeWalletAccount($item, $token, $pv, $model::Community, $uid);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }