UserModel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 static function getByParentId(int $uid): int
  33. {
  34. return self::where('id', $uid)->value("parent_id");
  35. }
  36. public function getAllAddress()
  37. {
  38. $all_list = $this->field('id,address')->select();
  39. $arr = [];
  40. foreach ($all_list as $item){
  41. $arr[$item['address']] = $item['id'];
  42. }
  43. return $arr;
  44. }
  45. //获取用户余额 冻结金额 和 折合金额
  46. public static function getUserAmount(int $uid): float
  47. {
  48. return self::where('id', $uid)->sum("balance-frozen_amount");
  49. }
  50. //获取用户RWA
  51. public static function getUserRwaNum(int $uid): int
  52. {
  53. return self::where('id', $uid)->value("rwa_num");
  54. }
  55. /**
  56. * RWA更新数据
  57. * @param int $uid
  58. * @param string $power
  59. * @return void
  60. */
  61. public static function updateForRwaNum(int $uid, int $pid, string $score, string $action)
  62. {
  63. if($action == '+'){
  64. //更新自己Rwa
  65. self::where('id', $uid)->setInc('rwa_num', $score);
  66. //更新直推Rwa
  67. return self::where('id', $pid)->setInc('direct_rwa', $score);
  68. }else{
  69. //更新自己Rwa
  70. self::where('id', $uid)->setDec('rwa_num', $score);
  71. //更新直推Rwa
  72. return self::where('id', $pid)->setDec('direct_rwa', $score);
  73. }
  74. }
  75. /**
  76. * 余额更新数据
  77. * @param int $uid
  78. * @param string $power
  79. * @return void
  80. */
  81. public static function updateForRBalance(int $uid, string $score, string $action)
  82. {
  83. if($action == '+'){
  84. return self::where('id', $uid)->setInc('balance', $score);
  85. }else{
  86. return self::where('id', $uid)->setDec('balance', $score);
  87. }
  88. }
  89. //社区向上发放奖励津贴
  90. public static function setCommunityRewards($uid, $pv, $token)
  91. {
  92. $paths = UserPathModel::where('user_id', $uid)->where('distance', '<', 11)->order('distance', 'asc')->column('distance,parent_id');
  93. if (!empty($paths)) {
  94. $model = new LedgerWalletModel();
  95. foreach ($paths as $kk=>$item) {
  96. if($kk== 1){
  97. $model->changeWalletAccount($item, $token, $pv, $model::Community, $uid);
  98. }else{
  99. //间接推荐有效会员大于层级
  100. if(self::where('parent_id', $item)->where('rwa_num','>', 0)->count() >= $kk){
  101. $model->changeWalletAccount($item, $token, $pv, $model::Community, $uid);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }