UserModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. //0未开始 1进行中 2已结束 Not started
  24. const NOTSTOP = 0;
  25. const NORMAL = 1;
  26. const STOP = 2;
  27. protected $name = 'user';
  28. public function getById($userID)
  29. {
  30. return $this->where('id', $userID)->find();
  31. }
  32. public function getByAddress($address)
  33. {
  34. return $this->where("address", $address)->find();
  35. }
  36. public static function getByParentId(int $uid): int
  37. {
  38. return self::where('id', $uid)->value("parent_id");
  39. }
  40. public function getAllAddress()
  41. {
  42. $all_list = $this->field('id,address')->select();
  43. $arr = [];
  44. foreach ($all_list as $item){
  45. $arr[$item['address']] = $item['id'];
  46. }
  47. return $arr;
  48. }
  49. //获取用户RWA
  50. public static function getUserRwaNum(int $uid): int
  51. {
  52. return self::where('id', $uid)->value("rwa_num");
  53. }
  54. /**
  55. * RWA更新数据
  56. * @param int $uid
  57. * @param string $power
  58. * @return void
  59. */
  60. public static function updateForRwaNum(int $uid, int $pid, string $score, string $action)
  61. {
  62. if($action == '+'){
  63. //更新自己Rwa
  64. self::where('id', $uid)->setInc('rwa_num', $score);
  65. //更新直推Rwa
  66. return self::where('id', $pid)->setInc('direct_rwa', $score);
  67. }else{
  68. //更新自己Rwa
  69. self::where('id', $uid)->setDec('rwa_num', $score);
  70. //更新直推Rwa
  71. return self::where('id', $pid)->setDec('direct_rwa', $score);
  72. }
  73. }
  74. /**
  75. * 添加用户上级标识
  76. * @param int $uid
  77. * @param string $parentId
  78. * @return void
  79. */
  80. public static function updateUserSuper(int $uid, int $parentId)
  81. {
  82. self::where('id', $uid)->update(['is_super'=>self::NORMAL]);
  83. return self::where('id', $parentId)->setInc('direct_super', self::NORMAL);
  84. }
  85. /**
  86. * 获取直推用户黄金标识人数 direct_super >= 10
  87. * @param int $uid
  88. * @return void
  89. */
  90. public static function getDirectGoldNum(int $uid)
  91. {
  92. return self::where('parent_id', $uid)->where('direct_super', '>=' , 10)->count();
  93. }
  94. //社区向上发放奖励津贴
  95. public static function setCommunityRewards($uid, $pv, $token)
  96. {
  97. $paths = UserPathModel::where('user_id', $uid)->where('distance', '<', 11)->order('distance', 'asc')->column('distance,parent_id');
  98. if (!empty($paths)) {
  99. $model = new LedgerWalletModel();
  100. foreach ($paths as $kk=>$item) {
  101. if($kk== 1){
  102. $model->changeWalletAccount($item, $token, $pv, $model::Community, $uid);
  103. }else{
  104. //间接推荐有效会员大于层级
  105. if(self::where('parent_id', $item)->where('rwa_num','>', 0)->count() >= $kk){
  106. $model->changeWalletAccount($item, $token, $pv, $model::Community, $uid);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }