attr.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Notes:用户属性管理
  4. * User: wsc
  5. * Time: 2020/6/4 15:52
  6. * Class attr
  7. */
  8. class ModelAttr
  9. {
  10. private $attrType;
  11. public function __construct()
  12. {
  13. $this->redis = oo::commonOprRedis('user');
  14. $this->attrType = oo::cfg('attr');
  15. }
  16. /**
  17. * Notes:新增属性
  18. * User: wsc
  19. * Time: 2020/6/4 17:14
  20. * @param $uid
  21. * @param $type
  22. * @param $count
  23. * @param string $reason
  24. * @param string $ext
  25. * @return int
  26. */
  27. public function add($uid,$type,$count,$rid,$reason=""){
  28. $count = abs(intval($count));
  29. if($count==0){
  30. return 0;
  31. }
  32. $num = $this->change($uid,$type,$count);
  33. //写流水,成就,活动
  34. oo::commonOprModel('currency')->log($uid,$type,"+",$count,$num,$rid,$reason);//流水,统计
  35. // if($type=="money"){
  36. // oo::commonOprModel('member')->saveStatistics($uid,'money',$count); //个人基本统计(历史总获得金币数)
  37. // }
  38. return $num;
  39. }
  40. /**
  41. * Notes:减除属性
  42. * User: wsc
  43. * Time: 2020/6/4 17:14
  44. * @param $uid
  45. * @param $type
  46. * @param $count
  47. * @param $reason
  48. * @param string $ext
  49. * @return int
  50. */
  51. public function del($uid,$type,$count,$rid,$reason=''){
  52. $value = -abs(intval($count));
  53. $num = $this->change($uid,$type,$value);
  54. if($num==-1){
  55. $hcount = $this->redis->hIncrBy(okeys::userAttr($uid),$type,0);
  56. oo::logs()->debug3(["uid"=>$uid,"type"=>$type,"count"=>$count,"haveCount"=>$hcount,"rid"=>$rid],"delMoneyErr.txt");
  57. return $num;
  58. }
  59. //写流水,成就,活动
  60. oo::commonOprModel('currency')->log($uid,$type,"-",$count,$num,$rid,$reason);
  61. if($type=='spins'){
  62. //体力大师活动判别
  63. oo::commonOprModel('activitynew')->addCommonScore($uid,'spins_master',-$value);
  64. }
  65. return $num;
  66. }
  67. /**
  68. * Notes:获取用户属性
  69. * User: wsc
  70. * Time: 2020/6/4 17:15
  71. * @param $uid
  72. * @param $type
  73. * @return int|array
  74. */
  75. public function get2($uid,$type=""){
  76. $key = okeys::userAttr($uid);
  77. if($type){
  78. $res = $this->redis->hIncrBy($key,$type,0);
  79. }else{
  80. $res =$this->redis->hGetAll($key);
  81. }
  82. return $res;
  83. }
  84. public function get($uid,$type="",$limit=0){
  85. $key = okeys::userAttr($uid);
  86. $attr = oo::cfg('attr');
  87. $res = $this->redis->hGetAll($key);
  88. if(empty($res)){//再查一次
  89. $res = $this->redis->hGetAll($key);
  90. }
  91. $needRecover = [];//需要恢复的字段
  92. if(empty($res)){
  93. $needRecover = array_filter(array_column($attr,"name"));
  94. }else{
  95. if (isset($res['uid'])) unset($res['uid']);
  96. if(count($res)<count($attr)){
  97. foreach ($attr as $v){
  98. if(!isset($res[$v['name']])){
  99. $needRecover[] = $v['name'];
  100. }
  101. }
  102. }
  103. }
  104. if(!empty($needRecover)&&$limit==0){
  105. oo::commonOprModel('recoverdata')->recoverAttr($uid,$needRecover);//从新拉取写缓存
  106. $res = $this->get($uid,$type,1);
  107. }else{
  108. if($type){
  109. $res = $res[$type];
  110. }
  111. }
  112. return $res;
  113. }
  114. /**
  115. * Notes:改变用户属性
  116. * User: wsc
  117. * Time: 2020/6/4 17:15
  118. * @param $uid
  119. * @param $type
  120. * @param $count
  121. * @return int
  122. */
  123. public function change($uid,$type,$count){
  124. $this->get($uid);
  125. $key = okeys::userAttr($uid);
  126. $num = $this->redis->hIncrBy($key,$type,$count);
  127. if($num<0){//不能为负数,容错回滚
  128. $this->redis->hIncrBy($key,$type,abs($count));
  129. return -1;
  130. }
  131. $this->joinAttrLand($uid);
  132. return $num;
  133. }
  134. public function joinAttrLand($uid){
  135. $key = okeys::Landing("GAMEINFO", $uid);
  136. oo::commonOprRedis('Usercache')->zAdd($key,time(),$uid);
  137. }
  138. }