currency.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Notes:流水处理
  4. * User: wsc
  5. * Time: 2020/6/10 18:48
  6. * Class ModelCurrency
  7. */
  8. class ModelCurrency{
  9. private $attrType;
  10. public function __construct()
  11. {
  12. $this->attrType = oo::cfg('attr');
  13. }
  14. /**
  15. * Notes:
  16. * User: wsc
  17. * Time: 2020/7/8 15:35
  18. * @param $uid -用户id
  19. * @param $type -类型 如"money","spins",...
  20. * @param $t + 或 -
  21. * @param $count -变化值
  22. * @param $num -最终数量
  23. * @param $rid -来源id 明显见 config/common/config.reason.php
  24. * @param $reason -原因可扩展具体
  25. */
  26. public function log($uid,$type,$t,$count,$num,$rid,$reason){
  27. $userlevelInfo = oo::commonOprModel("member")->getUserLevel($uid);//获取用户信息
  28. $data = [
  29. 'uid'=>$uid,
  30. 'type'=>$type,
  31. 'num'=>(int)$count,
  32. 'crease'=>$t,
  33. 'reason'=>$reason,
  34. 'last_num'=>(int)$num,
  35. 'rid'=>(int)$rid,
  36. 'levelId'=>(int)$userlevelInfo['levelId'],
  37. 'time'=>time()
  38. ];
  39. if($type=='money'){
  40. if($t=="+"){
  41. oo::commonOprModel('steal')->toJoinQueue($uid,$userlevelInfo['levelId'],$num);
  42. $this->setUserDayData($uid,'obtain_coin',$count);
  43. }else{
  44. oo::commonOprModel('steal')->toOutQueue($uid,$userlevelInfo['levelId'],$num);
  45. $this->setUserDayData($uid,'cost_coin',$count);
  46. }
  47. }else if($type=='shield'){
  48. oo::commonOprModel('member')->checkAttackShieldByUid($uid,true);
  49. }else if($type=='spins'){
  50. if($t=="+"){
  51. $this->setUserDayData($uid,'obtain_spin',$count);
  52. }else{
  53. $this->setUserDayData($uid,'cost_spin',$count);
  54. }
  55. }
  56. $key = okeys::assetsLog(date("YmdH"));
  57. oo::commonOprRedis('Statistics')->lPush($key,json_encode($data));//流水入队
  58. $userlevelInfo['levelId'] > 1 && $this->coinsValueSta($data);
  59. $whileList = oo::commonOprRedis('common')->sMembers(okeys::slotWhileList());
  60. if($rid==999&&in_array($uid,$whileList)){//白名单后台发放不记录数数统计
  61. return ;
  62. }
  63. //数数事件-消耗发放
  64. $filtration =[1,2,3,4,5,9,10,11,30,50,81,82,86,87,205,208,213,214,217,219,221,353];
  65. $payfilt =[300,301,302,304,304,305,310,311,312,313,314,330,410,450,451,452,453,454];
  66. $other =[1003,1011,1012,1013,1014,1015,1016,1017,1018,1019,1020,1021,1022,1023,1024,1025,1026,1027,1028,2002];
  67. $filtration = array_merge($filtration,$payfilt,$other);
  68. if(!in_array($rid,$filtration)){
  69. if($t=="+"){
  70. $eventType = "resource_obtain";
  71. $eventData=[
  72. "resource_obtain_name"=>(string)$type,
  73. "resource_obtain_num"=>$count,
  74. "resource_obtain_newnum"=>$num,
  75. "resource_obtain_type"=>$rid,
  76. "resource_obtain_ext"=>[$reason],
  77. ];
  78. }else{
  79. $eventType = "resource_cost";
  80. $eventData=[
  81. "resource_cost_name"=>(string)$type,
  82. "resource_cost_num"=>$count,
  83. "resource_cost_newnum"=>$num,
  84. "resource_cost_type"=>$rid,
  85. "resource_cost_ext"=>[$reason],
  86. ];
  87. }
  88. oo::commonOprModel('ta')->setEventData($uid,$eventType,$eventData);
  89. }
  90. }
  91. /**
  92. * Notes:活动流水
  93. * User: wsc
  94. * Time: 2021/3/17 18:20
  95. * @param $aid
  96. * @param $uid
  97. * @param $type
  98. * @param $t
  99. * @param $count
  100. * @param $num
  101. * @param $rid
  102. * @param $reason
  103. */
  104. public function actlog($aid,$uid,$type,$t,$count,$num,$rid,$reason=''){
  105. $data = [
  106. 'aid'=>(int)$aid,
  107. 'uid'=>$uid,
  108. 'type'=>$type,
  109. 'num'=>(int)$count,
  110. 'crease'=>$t,
  111. 'reason'=>$reason,
  112. 'last_num'=>(int)$num,
  113. 'rid'=>$rid,
  114. 'time'=>time()
  115. ];
  116. $key = okeys::actAssetsLog(date("YmdH"));
  117. oo::commonOprRedis('Statistics')->lPush($key,json_encode($data));//流水入队
  118. }
  119. /**
  120. * 每日宠物免费/付费 偷/打参与数据
  121. * @param int $uid
  122. * @param string $type (coindozer:推金币)
  123. * @param string $flag 标记(免费参与:0,付费参与:1)
  124. * @param string $day 日期
  125. */
  126. public function participation($uid, $type, $flag = 0, $day = '') {
  127. if(empty($day)){
  128. $day = date('Ymd');
  129. }
  130. $uKey = okeys::participationUidKey($uid, $type, $flag);
  131. //今天已经统计过
  132. if(!oo::commonOprRedis('statistics')->setNx($uKey, 1)){
  133. return;
  134. }
  135. oo::commonOprRedis('statistics')->expireAt($uKey, strtotime('23:59:59'));
  136. $key = okeys::participationLog($day);
  137. oo::commonOprRedis('Statistics')->hIncrBy($key, okeys::participationSubKey($type, $flag), 1);
  138. //参与数据保留3天缓存
  139. if(oo::commonOprRedis('Statistics')->hLen($key) < 2){
  140. oo::commonOprRedis('Statistics')->expire($key,oo::redisRandomExpire(3*24*3600)); //保留3天
  141. }
  142. }
  143. /**
  144. * Notes:定时创建流水表
  145. * User: wsc
  146. * Time: 2020/6/12 19:44
  147. * @param string $do
  148. */
  149. public function cronCreatAssessTable($do="false"){
  150. if(date('w',time())==5||$do){
  151. $sql = oo::cfg('sql')['assetslog'];
  152. for($i=1;$i<8;$i++){
  153. $d = date("Ymd",time()+$i*86400);
  154. $tb = 'assetslog_'.$d;
  155. $query = str_replace('#table#', $tb, $sql);
  156. $res = funs::createTable("crazygod_log.".$tb,$query);
  157. oo::logs()->commlog("Create Table {$tb} Res:{$res}",'createAssessTable');
  158. }
  159. }
  160. }
  161. /**
  162. * Notes:统计个人每天金币,体力变化
  163. * User: wsc
  164. * Time: 2020/6/5 14:44
  165. * @param $data
  166. * @return bool
  167. */
  168. public function statisticsMS($data){
  169. $t = $data['crease'];
  170. if($t=="+"){
  171. //可能异常来源统计
  172. // $this->statisticsAbnormal($data);
  173. }
  174. //每日个人money.spans增减量统计
  175. $this->statisticsUserTodayData($data);
  176. $userlevelInfo = oo::commonOprModel("member")->getUserLevel($data['uid']);//获取用户信息
  177. //体力金币发放消耗关卡统计
  178. $LevelStatisticsT = $t=="-"?'Sub':'Add';
  179. oo::commonOprRedis('statistics')->hIncrBy(okeys::spinsMoneyLevelStatistics($data['type'],$LevelStatisticsT),$userlevelInfo['levelId'],$data['num']);
  180. //各变化来源分类合计统计
  181. oo::commonOprModel('member')->PropertyStatistics($t,$data['type'],$data['num'],$data['reason']);//用流水表做统计
  182. return true;
  183. }
  184. /**
  185. * Notes:每日个人money.spans增减量统计
  186. * User: wsc
  187. * Time: 2020/6/6 16:05
  188. * @param $data
  189. */
  190. public function statisticsUserTodayData($data){
  191. $type = $data['crease']=="-"?$data['type']:($data['type']=='money'?'addMoney':'addSpins');
  192. if($data['crease']=="+"){
  193. // //每日获得金币排行前100
  194. // $key = okeys::todayDataRank($type);
  195. // oo::commonOprRedis('statistics')->zAdd($key,$data['num'],$data['uid']);
  196. // $size = oo::commonOprRedis('statistics')->zSize($key);
  197. // if($size>100){
  198. // oo::commonOprRedis('statistics')->zDeleteRangeByScore($key,0,0);
  199. // }
  200. }
  201. //每日个人money.spins增减量统计
  202. oo::commonOprRedis('statistics')->hIncrBy(okeys::todayDataRank($type),$data['uid'],$data['num']);
  203. }
  204. //异常来源统计及警告
  205. public function statisticsAbnormal($data){
  206. $cfg = oo::commonOprModel('member')->moneyAddType();
  207. $reasonArr =explode(":",$data['reason']);
  208. $reason=!empty($reasonArr)?array_search($reasonArr[0],$cfg):'other';
  209. $akey = okeys::abnormalSta($data['uid']."_".$data['type']);
  210. $afterInfo = oo::commonOprRedis('statistics')->hGetAll($akey);
  211. if(isset($afterInfo['after_reason'])&&$afterInfo['after_reason']==$reason){//本次与上次是同一来源
  212. $sumlimit = oo::commonOprRedis('statistics')->hIncrBy($akey,$reason,1);
  213. if($sumlimit>=20){
  214. oo::commonOprModel('asynclog')->push("ABNORMAL",[
  215. 'uid' => $data['uid'],
  216. 'num' => $data['num'],
  217. 'reason' => $reasonArr[0],
  218. 'type' => $data['type'],
  219. 'abnormal'=> '连续20次增加来源一致',
  220. 'time' => time(),
  221. ]);
  222. oo::commonOprRedis('statistics')->delete($akey);
  223. }
  224. }else{
  225. oo::commonOprRedis('statistics')->delete($akey);
  226. $cache = ["after_reason"=>$reason,$reason=>1];
  227. oo::commonOprRedis('statistics')->hMset($akey,$cache);
  228. oo::commonOprRedis('statistics')->expire($akey,86400);
  229. }
  230. return true;
  231. }
  232. private function coinsValueSta($data){
  233. if($data['type'] == "money" || $data['type'] == 'spins'){
  234. if(in_array($data['uid'],oo::commonOprModel("config")->payWhiteList())){
  235. return true;
  236. }
  237. if($data['type'] == "spins" && $data['crease'] == "-"){
  238. oo::commonOprRedis('statistics')->hIncrBy(okeys::CoinsValueSta("spins"),$data['uid']."-".$data['levelId'],$data['num']);
  239. oo::commonOprRedis('statistics')->hIncrBy(okeys::CoinsValueSta("slot"),$data['uid'],1);
  240. oo::commonOprRedis('statistics')->hSet(okeys::CoinsValueSta("dau"),$data['uid'],$data['levelId']);
  241. }else if($data['type'] == "money" && $data['crease'] == "+" && in_array($data['rid'],[1,2,3,4,9,10])){
  242. oo::commonOprRedis('statistics')->hIncrBy(okeys::CoinsValueSta("coins"),$data['uid']."-".$data['levelId'],$data['num']);
  243. }
  244. $hashKey = $data['type'] == "money" ? $data['uid']."-".$data['levelId'] : $data['uid'];
  245. oo::commonOprRedis('statistics')->hIncrBy(okeys::CoinsValueSta($data['type'].$data['crease']),$hashKey,$data['num']);
  246. if($data['type'] == "spins"){
  247. if($data['crease'] == "-"){
  248. oo::commonOprModel('Statistics')->addUserStatistics($data['uid'],['costSpin'=>$data['num'],'spinTimes'=>1]);
  249. }else if($data['crease'] == "+"){
  250. oo::commonOprModel('Statistics')->addUserStatistics($data['uid'],['obtainSpin'=>$data['num']]);
  251. }
  252. }
  253. }
  254. return true;
  255. }
  256. /**
  257. * Notes:设置累计用户每日数据
  258. * User: wsc
  259. * Time: 2021/5/18 15:15
  260. * @param $uid
  261. * @param $type
  262. * @param $num
  263. * @return bool
  264. */
  265. public function setUserDayData($uid,$type,$num){
  266. $num = intval($num);
  267. $date = date( 'Ymd',time());
  268. $key = okeys::UserDayData($uid,$date);
  269. // $keyArr = array_keys(oo::commonOprModel('ta')->events['resource_list']);
  270. $keyArr = ["obtain_spin","cost_spin","obtain_coin","cost_coin","obtain_card","cost_card","spin_times"];
  271. if(!in_array($type,$keyArr)||$num<1){
  272. return false;
  273. }
  274. $needSet=false;
  275. if(!oo::commonOprRedis('statistics')->exists($key)){
  276. $needSet = true;
  277. }
  278. oo::commonOprRedis('statistics')->hIncrBy($key,$type,$num);
  279. if($needSet){
  280. oo::commonOprRedis('statistics')->expire($key,oo::redisRandomExpire(oo::todayDeadline()+7200));
  281. }
  282. }
  283. }