NagentCountrecord.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace App\Models;
  3. use DB;
  4. class NagentCountrecord extends BaseModel {
  5. protected $table = "nagent_countrecord";
  6. public $timestamps = false;
  7. //验证游戏对应期号代理业绩是否统计
  8. protected function checkIscount($game,$no){
  9. $res = $this->where('game',$game)->where('no',$no)->first();
  10. if($res && $res->status==1)return -8505013002; //本期游戏已统计
  11. if(!$res)return array();
  12. return $res->toArray();
  13. }
  14. //获取对应游戏对应期代理业绩
  15. protected function getWaterBygn($game,$no,$uid='',$page=1,$total=1){
  16. DB::connection()->enableQueryLog();
  17. $buyobj = '\App\Models\Game' . ucfirst($game) . '_Buy'; //游戏注单表模型
  18. $buyinfo = $buyobj::select(DB::raw("cast(account_identity as uuid),sum(money) as money")); //投注数据获取
  19. if(!empty($uid)){
  20. $buyinfo = $buyinfo->where('account_identity',$uid); //获取指定用户注单数据
  21. }
  22. $nno = array();
  23. if(is_array($no)){ //多期游戏投注数据获取
  24. $redata = array();
  25. $tno = array();
  26. if(!empty($uid)){
  27. $iscountno = $this->select('no')->where('game',$game)->where('status',1)->whereIn('no',$no)->get();
  28. if($iscountno && count($iscountno->toArray())>0){
  29. foreach ($iscountno->toArray() as $k => $v) {
  30. $tno[] = $v['no'];
  31. }
  32. }
  33. foreach ($no as $k => $v) {
  34. if(in_array($v, $tno))continue;
  35. $nno[] = $v;
  36. }
  37. }
  38. //删除原已统计游戏期记录
  39. $res = $this->where('game',$game)->whereIn('no',$no)->delete();
  40. //添加新统计游戏期记录
  41. foreach ($no as $key => $value) {
  42. $redata[] = array(
  43. 'game' => $game,
  44. 'no' => $value,
  45. 'status' => 1,
  46. );
  47. }
  48. $res = $this->insert($redata);
  49. if($res<0)return -8505013102;
  50. //按用户分组获取投注金额
  51. $buyinfo = $buyinfo->whereIn('no',$no);
  52. }else{
  53. //验证并更新或添加游戏期统计记录
  54. if($page==1){
  55. $res = $this->checkUpinData($game,$no);
  56. if(is_numeric($res))return $res;
  57. }
  58. //按用户分组获取投注金额
  59. $buyinfo = $buyinfo->where('no',$no);
  60. $sumcount = $buyinfo->where('delstatus',1)->groupBy('account_identity')->count();
  61. if(empty($sumcount) || $sumcount<1)return 1;
  62. $total = (empty($total) || !is_numeric($total) || $total<1)?1:$total;
  63. $page = (empty($page) || !is_numeric($page) || $page<1)?1:$page;
  64. $limit = ceil($sumcount/$total);
  65. $offset = ($page-1) * $limit;
  66. $buyinfo = $buyinfo->limit($limit)->offset($offset);
  67. }
  68. $buyinfo = $buyinfo->where('delstatus',1)->groupBy('account_identity')->get();
  69. /*$queries = DB::getQueryLog();
  70. print_r($queries);*/
  71. if(!$buyinfo || count($buyinfo->toArray())<1)return 1;
  72. $buyinfo = $buyinfo->toArray();
  73. if(!empty($nno)){
  74. $tbinfo = $buyobj::select(DB::raw("account_identity,sum(money) as money"))->where('delstatus',1)->where('account_identity','<>',$uid)->whereIn('no',$nno)->groupBy('account_identity')->get();
  75. if($tbinfo && count($tbinfo->toArray())>0)$buyinfo = array_merge($buyinfo,$tbinfo->toArray());
  76. }
  77. return $buyinfo;
  78. }
  79. //是否统计游戏对应期验证及更新
  80. protected function checkUpinData($game,$no){
  81. $record = $this->checkIscount($game,$no); //验证当期游戏数据是否已统计
  82. if($record<0)return 1;
  83. //更新或添加将统计期游戏记录
  84. $redata = array(
  85. 'game' => $game,
  86. 'no' => $no,
  87. 'status' => 1,
  88. );
  89. $res = $this->addOrUpdate($record,$redata);
  90. if($res<0)return $res;
  91. return array();
  92. }
  93. //整理统一代理业绩数据
  94. protected function handleWater($gameinfo,$game,$arr,$uid=''){
  95. $sgi = array(); //按日期分组汇总年月日、对应日期期号组
  96. foreach ($gameinfo as $k => $v) {
  97. $date = date('Y-m-d',strtotime($v['open_time']));
  98. $sgi[$date]['open_time'] = $date;
  99. $sgi[$date]['no'][] = $v['info_no'];
  100. }
  101. //获取整理各用户对应日期业绩
  102. foreach ($sgi as $pk => $pv) {
  103. $buyinfo = $this->getWaterBygn($game,$pv['no'],$uid); //获取对应游戏指定期投注信息
  104. if(is_numeric($buyinfo))continue;
  105. foreach ($buyinfo as $k => $v) {
  106. if(empty($v['account_identity']))continue;
  107. $arr['buyinfo'][$v['account_identity']][$game] = $v['money'];
  108. if(!in_array($v['account_identity'], $arr['uid']))
  109. $arr['uid'][] = $v['account_identity'];
  110. $arr['pid'][$v['account_identity']] = array();
  111. }
  112. }
  113. return $arr;
  114. }
  115. //真人游戏代理业绩数据获取处理
  116. protected function getRealData($game,$arr,$stime,$etime,$uid=''){
  117. $date = date('Y-m-d',strtotime($stime));
  118. $buyobj = '\\App\\Model\\' . ucfirst($game) . '_betting_ogrbv'; //游戏注单表模型
  119. $stime = strtotime($stime);
  120. $etime = strtotime($etime);
  121. if($game=='kygame' || $game == 'lcqpgame'|| $game == 'lygame'){
  122. $tkey = 'GameEndTime';
  123. $buyinfo = $buyobj::select(DB::raw('cast(account_identity as uuid),sum("AllBet") as money')); //投注数据获取
  124. }else if($game=='aggame'){
  125. $tkey = 'recalcuTime';
  126. $buyinfo = $buyobj::select(DB::raw('cast(account_identity as uuid),sum("betAmount") as money')); //投注数据获取
  127. }else if($game=='hjgame'){
  128. $tkey = 'bet_time';
  129. $buyinfo = $buyobj::select(DB::raw('cast(account_identity as uuid),sum("bet_amount") as money')); //投注数据获取
  130. }else{
  131. $tkey = 'AddTime';
  132. $buyinfo = $buyobj::select(DB::raw('cast(account_identity as uuid),sum("BettingAmount") as money')); //投注数据获取
  133. }
  134. $updatestatus = $buyobj::where('count_status',2);
  135. if(!empty($uid)){
  136. $buyinfo = $buyinfo->where('account_identity',$uid); //获取指定用户注单数据
  137. $updatestatus = $updatestatus->where('account_identity',$uid);
  138. }
  139. //更新游戏统计状态为已统计
  140. $updatestatus = $updatestatus->update(['count_status'=>1]);
  141. if($updatestatus<0)return -8505013102;
  142. //按用户分组获取投注金额
  143. $buyinfo = $buyinfo->where($tkey,'>=',$stime)->where($tkey,'<=',$etime)->groupBy('account_identity')->get();
  144. /*$queries = DB::getQueryLog();
  145. print_r($queries);*/
  146. if(!$buyinfo || count($buyinfo->toArray())<1)return $arr;
  147. foreach ($buyinfo->toArray() as $k => $v) {
  148. if(empty($v['account_identity']))continue;
  149. $arr['buyinfo'][$v['account_identity']][$game] = $v['money'];
  150. if(!in_array($v['account_identity'], $arr['uid']))
  151. $arr['uid'][] = $v['account_identity'];
  152. $arr['pid'][$v['account_identity']] = array();
  153. }
  154. return $arr;
  155. }
  156. //代理业绩计算
  157. protected function CountWaters($uinfo,$ctime)
  158. {
  159. $nagent = '\App\Models\Nagent_detailed';
  160. $nagentwater = '\App\Models\Nagent_countwater';
  161. //获取已统计数据
  162. $waterInfo = $nagentwater::whereIn('count_time', array($ctime['wtime'],$ctime['ctime']))->whereIn('agent_identity', $uinfo['uid'])->get();
  163. $uwinfo = array();
  164. if($waterInfo && count($waterInfo->toArray())>0){
  165. foreach ($waterInfo->toArray() as $k => $v) {
  166. $uwinfo[$v['agent_identity']][$v['time_type']] = $v;
  167. }
  168. }
  169. //print_r($uwinfo);
  170. /*print_r($uinfo['uname']);
  171. print_r($uinfo['pid']);
  172. print_r($uinfo);exit;*/
  173. $indata = array();
  174. $udata = array();
  175. if (is_array($uinfo['pid']) && count($uinfo['pid']) > 0){
  176. foreach ($uinfo['pid'] as $pk => $pv) {
  177. //代理业绩统计
  178. $curmoney = $uinfo['buyinfo'][$pk];
  179. if(!isset($uwinfo[$pk][2])){
  180. $user = $uinfo['uname'][$pk];
  181. $type = 1;
  182. $indata = $this->getAddData($curmoney, $user, $ctime['ctime'], $pk, $type,$indata);
  183. if ($indata < 0)return $indata;
  184. $indata = $this->getAddData($curmoney, $user, $ctime['wtime'], $pk, $type,$indata);
  185. if ($indata < 0)return $indata;
  186. }else{
  187. //周统计更新数据处理
  188. $kwater = 'agent_water';
  189. $type = 1;
  190. //更新周统计
  191. $uwinfo[$pk][2] = $this->UpdateCountData($uwinfo[$pk][2], $kwater, $curmoney);
  192. if ($uwinfo[$pk][2] < 1)return $uwinfo[$pk][2];
  193. //日统计数据处理
  194. if (!isset($uwinfo[$pk][1])) {
  195. $indata = $this->getAddData($curmoney, $uwinfo[$pk][2]['agent_user'], $ctime['ctime'], $pk, $type,$indata);
  196. if ($indata < 0)return $indata;
  197. } else {
  198. //更新日统计
  199. $uwinfo[$pk][1] = $this->UpdateCountData($uwinfo[$pk][1], $kwater, $curmoney);
  200. if ($uwinfo[$pk][1] < 1)return $uwinfo[$pk][1];
  201. }
  202. }
  203. //代理父级业绩统计
  204. $allpi = count($pv);
  205. if($allpi>0){
  206. foreach ($pv as $k => $v) {
  207. //$pk:用户ID;$v:父级ID
  208. if(!isset($uwinfo[$v][2])){
  209. $user = $uinfo['uname'][$v];
  210. $type = (($k+1) == $allpi) ? 2 : 3;
  211. $indata = $this->getAddData($curmoney, $user, $ctime['ctime'], $v, $type,$indata);
  212. if ($indata < 0)return $indata;
  213. $indata = $this->getAddData($curmoney, $user, $ctime['wtime'], $v, $type,$indata);
  214. if ($indata < 0)return $indata;
  215. }else{
  216. //周统计更新数据处理
  217. $kwater = (($k+1) == $allpi) ? 'zt_water' : 'group_water';
  218. $type = (($k+1) == $allpi) ? 2 : 3;
  219. //更新周统计
  220. $uwinfo[$v][2] = $this->UpdateCountData($uwinfo[$v][2], $kwater, $curmoney);
  221. if ($uwinfo[$v][2] < 1)return $uwinfo[$v][2];
  222. //日统计数据处理
  223. if (!isset($uwinfo[$v][1])) {
  224. $indata = $this->getAddData($curmoney, $uwinfo[$v][2]['agent_user'], $ctime['ctime'], $v, $type,$indata);
  225. if ($indata < 0)return $indata;
  226. } else {
  227. //更新日统计
  228. $uwinfo[$v][1] = $this->UpdateCountData($uwinfo[$v][1], $kwater, $curmoney);
  229. if ($uwinfo[$v][1] < 1)return $uwinfo[$v][1];
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. //更新或添加统计数据:添加数据二维关联或索引数据;更新数据三维数据
  237. $res = $nagentwater::udadData($indata,$uwinfo);
  238. return $res;
  239. }
  240. //代理父级信息获取,处理代理业绩数据为ID=>金额形式
  241. protected function getNpinfo($arr){
  242. //获取代理父级信息
  243. $agent = \App\Models\Nagent_detailed::select('parent_path','agent_identity')->whereIn('agent_identity', $arr['uid'])->get();
  244. if (!$agent || count($agent->toArray()) < 1) {
  245. return 0;
  246. }
  247. foreach ($agent->toArray() as $k => $v) {
  248. $parent = explode(',', $v['parent_path']);
  249. foreach ($parent as $pk => $pv) {
  250. if(empty($pv) || $pv=='f615dac7-afd9-a3ad-7b73-030454bd159c' || is_numeric($pv))continue;
  251. $arr['pid'][$v['agent_identity']][] = $pv;
  252. if(!in_array($pv, $arr['uid']))$arr['uid'][] = $pv;
  253. }
  254. }
  255. $allainfo = \App\Models\Account::select('account','identity')->whereIn('identity', $arr['uid'])->get();
  256. if (!$allainfo || count($allainfo->toArray()) < 1) {
  257. return 0;
  258. }
  259. foreach ($allainfo->toArray() as $k => $v) {
  260. $arr['uname'][$v['identity']] = $v['account'];
  261. }
  262. return $arr;
  263. }
  264. //周统计、日统计数据更新
  265. protected function UpdateCountData($water,$kwater,$money,$dsum=0,$game=''){
  266. $gwater = json_decode($water['game_water'],1);
  267. if(is_array($money)){
  268. $allmoney = 0;
  269. foreach ($money as $k => $v) {
  270. $gwater[$k][$kwater] = isset($gwater[$k][$kwater])?$gwater[$k][$kwater]+$v:$v;
  271. if($kwater != 'agent_water'){
  272. $gwater[$k]['sum_water'] = isset($gwater[$k]['sum_water'])?$gwater[$k]['sum_water']+$v:$v;
  273. $dsum += $v;
  274. }
  275. $allmoney += $v;
  276. }
  277. $money = $allmoney;
  278. }else{
  279. $gwater[$game][$kwater] = isset($gwater[$game][$kwater])?$gwater[$game][$kwater]+$money:$money;
  280. $gwater[$game]['sum_water'] = isset($gwater[$game]['sum_water'])?$gwater[$game]['sum_water']+$dsum:$dsum;
  281. }
  282. $water[$kwater] += $money;
  283. $water['update_time'] = date('Y-m-d H:i:s');
  284. $water['game_water'] = json_encode($gwater);
  285. $water['sum_water'] += $dsum;
  286. return $water;
  287. }
  288. //统计数据处理
  289. private function handleCountdata($type,$gwater,$money){
  290. if ($type == 1) {
  291. if(!empty($gwater)){
  292. $gwater['agent_water'] += $money;
  293. }else{
  294. $gwater = array(
  295. 'agent_water' => $money,
  296. 'zt_water' => 0,
  297. 'group_water' => 0,
  298. 'sum_water' => 0,
  299. );
  300. }
  301. } else {
  302. if(empty($gwater)){
  303. $gwater = array(
  304. 'agent_water' => 0,
  305. 'zt_water' => ($type == 2) ? $money : 0,
  306. 'group_water' => ($type == 2) ? 0 : $money,
  307. 'sum_water' => $money,
  308. );
  309. }else{
  310. $gwater['zt_water'] += ($type == 2) ? $money : 0;
  311. $gwater['group_water'] += ($type == 2) ? 0 : $money;
  312. $gwater['sum_water'] += $money;
  313. }
  314. }
  315. return $gwater;
  316. }
  317. //获取周统计、日统计记录数据
  318. protected function getAddData($money,$user,$counttime,$uid,$type,$indata,$game=''){
  319. if(empty($user)){
  320. $auser = \App\Models\Nagent_detailed::select('agent_user')->where('agent_identity',$uid)->first();
  321. if(!isset($auser->agent_user))return -200102;
  322. $user = $auser->agent_user;
  323. }
  324. $gwater = array();
  325. $gswater = array();
  326. $uck = $uid.$counttime;
  327. if(isset($indata[$uck])){
  328. $gwater = array(
  329. 'agent_water' => $indata[$uck]['agent_water'],
  330. 'zt_water' => $indata[$uck]['zt_water'],
  331. 'group_water' => $indata[$uck]['group_water'],
  332. 'sum_water' => $indata[$uck]['sum_water'],
  333. );
  334. $gswater = json_decode($indata[$uck]['game_water'],1);
  335. }
  336. if(is_array($money)){
  337. $allmoney = 0;
  338. foreach ($money as $k => $v) {
  339. $allmoney += $v;
  340. $gswater[$k] = isset($gswater[$k])?$gswater[$k]:array();
  341. $gswater[$k] = $this->handleCountdata($type,$gswater[$k],$v);
  342. }
  343. $money = $allmoney;
  344. }
  345. $gwater = $this->handleCountdata($type,$gwater,$money);
  346. if(isset($indata[$uck])){
  347. $indata[$uck]['agent_water'] = $gwater['agent_water'];
  348. $indata[$uck]['zt_water'] = $gwater['zt_water'];
  349. $indata[$uck]['group_water'] = $gwater['group_water'];
  350. $indata[$uck]['sum_water'] = $gwater['sum_water'];
  351. $indata[$uck]['update_time'] = date('Y-m-d H:i:s');
  352. if(empty($game)){
  353. $indata[$uck]['game_water'] = json_encode($gswater);
  354. }else{
  355. $indata[$uck]['game_water'] = json_encode(array($game => $gwater));
  356. }
  357. }else{
  358. $indata[$uck] = $gwater;
  359. $indata[$uck]['agent_user'] = $user;
  360. $indata[$uck]['agent_identity'] = $uid;
  361. $indata[$uck]['count_time'] = $counttime;
  362. $indata[$uck]['time_type'] = count(explode('-', $counttime))==2?2:1;
  363. $indata[$uck]['update_time'] = date('Y-m-d H:i:s');
  364. if(empty($game)){
  365. $indata[$uck]['game_water'] = json_encode($gswater);
  366. }else{
  367. $indata[$uck]['game_water'] = json_encode(array($game => $gwater));
  368. }
  369. }
  370. return $indata;
  371. }
  372. //添加或更新统计游戏信息
  373. protected function addOrUpdate($record,$data){
  374. if(!empty($record) && isset($record['id'])){
  375. $res = $this->where('id',$record['id'])->update($data);
  376. }else{
  377. $res = $this->insert($data);
  378. }
  379. if($res<1)return -8505013102;
  380. return 1;
  381. }
  382. //格式化时间为日期和年周
  383. protected function getFormatTime($ctime){
  384. $time['ctime'] = date('Y-m-d',strtotime($ctime));
  385. $y = date('Y',strtotime($ctime));
  386. $m = date('m',strtotime($ctime));
  387. $w = date('W',strtotime($ctime));
  388. if($m==12 && $w==1)$y+=1;
  389. $time['wtime'] = $y . '-' . $w;
  390. return $time;
  391. }
  392. }