RetaineService.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. <?php
  2. namespace app\service;
  3. use app\admin\controller\RetaineGame;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Env;
  7. use think\Exception;
  8. use think\Log;
  9. /**
  10. * QuickSDK游戏同步加解密算法描述
  11. * @copyright quicksdk 2015
  12. * @author quicksdk
  13. * @version quicksdk v 0.0.1 2014/9/2
  14. */
  15. class RetaineService
  16. {
  17. /**
  18. * 需要统计的游戏
  19. * @return array|false|string
  20. */
  21. public function getGameIds()
  22. {
  23. return model('common/Game')->cache('retaine:game', 600)->where('cooperation_status', 1)->column('id');
  24. }
  25. /**
  26. * 当天注册账号
  27. * @param $day
  28. * @return array|false|string
  29. */
  30. public function getMembers($day)
  31. {
  32. return model('common/Members')->cache('retaine:members:' . strtotime($day), 600)->whereBetween('reg_time', [strtotime($day), strtotime($day . ' 23:59:59')])->column('id');
  33. }
  34. /**
  35. * 注册玩家总数
  36. *
  37. * @param $where
  38. * @return array|false|string
  39. */
  40. public function getMembersTotal($game_id, $channel_id)
  41. {
  42. $where = [];
  43. if($game_id){
  44. $where['gameid'] = $game_id;
  45. }
  46. if($channel_id){
  47. $where['channel_id'] = $channel_id;
  48. }
  49. return model('common/Members')
  50. ->cache('retaine:roleInfo_total:'.$game_id.'_'.$channel_id, 500)
  51. ->where($where)->fetchSql(false)->count();
  52. }
  53. /**
  54. * 新增玩家
  55. *
  56. * @param $day
  57. * @param array $mebmers 账户ID列表
  58. * @param int $server 是否区服数据
  59. * @param int $channel 是否渠道数据
  60. * @param int $platform 是否平台数据
  61. * @param string $type count=统计数据, sum=汇总数据
  62. *
  63. * @return array|bool|\PDOStatement|string|\think\Collection
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public function regNum($day, $mebmers, $server = 0, $channel = 0, $platform = 0, $type='reg_num')
  69. {
  70. // if (!$mebmers || $type == 'reg_total') {
  71. $sql = Db::table('cy_role_info')->whereIn('gameid', $this->getGameIds());
  72. // ->where('serverid', '<>', '')
  73. // ->where('channel_id', '<>', 0);
  74. if($type == 'reg_total'){
  75. $sql = $sql->where('create_time', '<', strtotime($day . ' 23:59:59'));
  76. }else{
  77. // ->where('userid', 'in', $mebmers)
  78. $sql = $sql->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')]);
  79. }
  80. // $sql->fetchSql(true);
  81. if ($platform) {
  82. $field = ' 0 as game_id,count(DISTINCT userid) as '.$type.', ';
  83. if ($channel) {
  84. $list = $sql->field($field . 'channel_id,0 as server_id')
  85. ->group('channel_id')->select();
  86. } else {
  87. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')
  88. ->select();
  89. }
  90. } else {
  91. $field = ' gameid as game_id,count(DISTINCT userid) as '.$type.', ';
  92. //当天登陆游戏
  93. if ($channel) {
  94. if ($server) {
  95. $list = $sql->field($field . 'channel_id,serverid as server_id')
  96. ->group('gameid,channel_id,serverid')->select();
  97. } else {
  98. $list = $sql->field($field . 'channel_id,0 as server_id')
  99. ->group('gameid,channel_id')->select();
  100. }
  101. } else {
  102. if ($server) {
  103. $list = $sql->field($field . ' 0 as channel_id,serverid as server_id')
  104. ->group('gameid,serverid')->select();
  105. } else {
  106. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')
  107. ->group('gameid')->select();
  108. }
  109. }
  110. }
  111. // dump($list);
  112. return $list??[];
  113. // }
  114. // return [];
  115. }
  116. public function regNumNew($day, $mebmers, $server = 0, $channel = 0, $platform = 0, $type='reg_num')
  117. {
  118. $sql = Db::table('nw_member_game_server')
  119. ->whereIn('game_id', $this->getGameIds())
  120. ->where('serverid', '<>', '')
  121. ->where('channel_id', '<>', '')
  122. ->where('roleid', '<>', '');
  123. if($type == 'reg_total'){
  124. $sql->where('create_time', '<=', strtotime($day . ' 23:59:59'));
  125. }else{
  126. if($mebmers){
  127. $sql->where('member_id', 'in', $mebmers);
  128. }
  129. $sql->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')]);
  130. }
  131. // $sql->fetchSql(true);
  132. if ($platform) {
  133. $field = ' 0 as game_id,count(DISTINCT member_id) as '.$type.', ';
  134. if ($channel) {
  135. $list = $sql->field($field . 'channel_id,0 as server_id')->group('channel_id')->select();
  136. } else {
  137. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->select();
  138. }
  139. } else {
  140. $field = ' game_id,count(DISTINCT member_id) as '.$type.', ';
  141. if ($channel) {
  142. if ($server) {
  143. $list = $sql->field($field . 'channel_id,serverid as server_id')->group('game_id,channel_id,serverid,member_id')->select();
  144. } else {
  145. $list = $sql->field($field . 'channel_id,0 as server_id')->group('game_id,channel_id')->select();
  146. }
  147. } else {
  148. if ($server) {
  149. $list = $sql->field($field . ' 0 as channel_id,serverid as server_id')->group('game_id,serverid,member_id')->select();
  150. } else {
  151. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->group('game_id')->select();
  152. }
  153. }
  154. }
  155. // dump($list);
  156. return $list??[];
  157. }
  158. /**
  159. * 新增玩家V2
  160. * @param $day
  161. * @param int $channel 是否渠道数据
  162. * @param int $platform 是否平台数据
  163. * @param string $type count=统计数据, sum=汇总数据
  164. *
  165. * @return array|bool|\PDOStatement|string|\think\Collection
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. * @throws \think\exception\DbException
  169. */
  170. public function regNumV2s($day, $channel = 0, $platform = 0, $type='reg_num')
  171. {
  172. $sql = Db::table('nw_subaccount')->where('game_id', 'in', $this->getGameIds());
  173. if($type == 'reg_total'){
  174. $sql = $sql->where('create_time', '<', strtotime($day . ' 23:59:59'));
  175. }else{
  176. $sql = $sql->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')]);
  177. }
  178. // $sql->fetchSql(true);
  179. if ($platform) {
  180. $field = ' 0 as game_id,count(id) as '.$type.', ';
  181. if ($channel) {
  182. $list = $sql->field($field . 'channel_id,0 as server_id')->group('channel_id')->select();
  183. } else {
  184. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->select();
  185. }
  186. } else {
  187. $field = ' game_id,count(id) as '.$type.', ';
  188. if ($channel) {
  189. $list = $sql->field($field . 'channel_id,0 as server_id')->group('game_id,channel_id')->select();
  190. } else {
  191. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->group('game_id')->select();
  192. }
  193. }
  194. // dump($list);
  195. return $list??[];
  196. }
  197. public function regNumV2($day, $channel = 0, $platform = 0, $type='reg_num')
  198. {
  199. $gameIds = implode(',', $this->getGameIds());
  200. $sql = Db::table('nw_subaccount')->alias('nw')->join('cy_game cg', "cg.id=nw.game_id", 'left')
  201. ->where('cg.id', 'in', $gameIds);
  202. if($type == 'reg_total'){
  203. $sql = $sql->where('nw.create_time', '<=', strtotime($day . ' 23:59:59'));
  204. }else{
  205. $sql = $sql->whereBetween('nw.create_time', [strtotime($day), strtotime($day . ' 23:59:59')]);
  206. }
  207. // $sql->fetchSql(true);
  208. if ($platform) {
  209. $field = ' 0 as game_id,count(nw.id) as '.$type.', ';
  210. if ($channel) {
  211. $list = $sql->field($field . 'nw.channel_id,0 as server_id')->group('nw.channel_id')->select();
  212. } else {
  213. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->select();
  214. }
  215. } else {
  216. $field = ' nw.game_id,count(nw.id) as '.$type.', ';
  217. if ($channel) {
  218. $list = $sql->field($field . 'nw.channel_id,0 as server_id')->group('nw.game_id,nw.channel_id')->select();
  219. } else {
  220. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->group('game_id')->select();
  221. }
  222. }
  223. // dump($list);
  224. return $list??[];
  225. }
  226. /**
  227. * 新增玩家留存
  228. *
  229. * @param $day
  230. * @param $server
  231. * @param $channel
  232. * @param $platform
  233. * @param $type
  234. *
  235. * @return array|bool|\PDOStatement|string|\think\Collection
  236. * @throws \think\db\exception\DataNotFoundException
  237. * @throws \think\db\exception\ModelNotFoundException
  238. * @throws \think\exception\DbException
  239. */
  240. public function regNumStay($day, $server = 0, $channel = 0, $platform = 0, $type='reg_num')
  241. {
  242. $sql = model('common/Subaccount')
  243. ->where('channel_id', '<>', 0);
  244. if($type == 'reg_total'){
  245. $sql = $sql->where('create_time', '<', strtotime($day . ' 23:59:59'));
  246. }else{
  247. $sql = $sql->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')]);
  248. }
  249. // $sql->fetchSql(true);
  250. if ($platform) {
  251. $field = ' 0 as game_id,count(id) as '.$type.', ';
  252. if ($channel) {
  253. $list = $sql->field($field . 'channel_id,0 as server_id')->group('channel_id')->select();
  254. } else {
  255. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->select();
  256. }
  257. } else {
  258. $field = ' game_id,count(id) as '.$type.', ';
  259. if ($channel) {
  260. $list = $sql->field($field . 'channel_id,0 as server_id')->group('game_id,channel_id')->select();
  261. } else {
  262. $list = $sql->field($field . ' 0 as channel_id,0 as server_id')->group('game_id')->select();
  263. }
  264. }
  265. // dump($list);
  266. return $list??[];
  267. }
  268. /**
  269. * 新增角色
  270. *
  271. * @param $day
  272. * @param int $server 是否根据游戏分组:0=否、1=是
  273. * @param int $channel 是否根据游戏分组:0=否、1=是
  274. * @param int $platform 是否根据游戏分组:0=否、1=是
  275. * @param string $type role_num=统计数据, reg_total=汇总数据
  276. *
  277. * @return bool|\PDOStatement|string|\think\Collection
  278. * @throws \think\db\exception\DataNotFoundException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. * @throws \think\exception\DbException
  281. */
  282. public function getFirstRoleMember($day, $server = 0, $channel = 0, $platform = 0, $type='role_num')
  283. {
  284. $sql = Db::table('nw_member_game_server')
  285. ->whereIn('game_id', $this->getGameIds())
  286. ->where('serverid', '<>', '')->where('channel_id', '<>', '');
  287. if($type == 'role_num'){
  288. $sql = $sql->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')]);
  289. }else{
  290. $sql = $sql->where('create_time', '<=', strtotime($day . ' 23:59:59'));
  291. }
  292. // $sql->fetchSql(true);
  293. if ($platform) {
  294. $field = ' 0 as game_id,count(DISTINCT game_id,member_id,serverid,roleid) as '.$type.', ';
  295. if ($channel) {
  296. $list = $sql->field($field . 'channel_id,0 as server_id')->group('channel_id')->select();
  297. } else {
  298. $list = $sql->field($field . '0 as channel_id,0 as server_id')->select();
  299. }
  300. } else {
  301. $field = ' game_id,count(DISTINCT game_id,member_id,serverid,roleid) as '.$type.', ';
  302. if ($channel) {
  303. if ($server) {
  304. $list = $sql->field($field . 'channel_id,serverid as server_id')->group('game_id,channel_id,serverid')->select();
  305. } else {
  306. $list = $sql->field($field . 'channel_id,0 as server_id')->group('game_id,channel_id')->select();
  307. }
  308. } else {
  309. if ($server) {
  310. $list = $sql->field($field . '0 as channel_id,serverid as server_id')->group('game_id,serverid')->select();
  311. } else {
  312. $list = $sql->field($field . '0 as channel_id,0 as server_id')->group('game_id')->select();
  313. }
  314. }
  315. }
  316. // dump($list);
  317. return $list;
  318. }
  319. /**
  320. * 活跃玩家
  321. *
  322. * @param $day
  323. * @param $param
  324. * @param int $server
  325. * @param int $channel
  326. * @param array $members
  327. * @param int $platform
  328. *
  329. * @return bool|\PDOStatement|string|\think\Collection
  330. * @throws \think\db\exception\DataNotFoundException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. * @throws \think\exception\DbException
  333. */
  334. public function activeMember($day, $param, $server = 0, $channel = 0, $members = [], $platform = 0)
  335. {
  336. $getGameIds = $this->getGameIds();
  337. $sql = Db::table('cy_role_info')
  338. ->whereBetween('update_time', [strtotime($day), strtotime($day . ' 23:59:59')])
  339. ->where('serverid', '<>', '')
  340. ->where('channel_id', '<>', '')
  341. ->where(function ($query) use ($members, $getGameIds) {
  342. if ($members) {
  343. $query->where('userid', 'in', $members);
  344. }
  345. if ($getGameIds) {
  346. $query->where('gameid', 'in', $getGameIds);
  347. }
  348. });
  349. // $sql->fetchSql(true);
  350. if ($platform) {
  351. $field = '0 as game_id,count(DISTINCT gameid, userid, serverid, roleid) as ' . $param . ', ';
  352. if ($channel) {
  353. $list = $sql->field($field . ' channel_id, 0 as server_id')->group('channel_id')->select();
  354. } else {
  355. $list = $sql->field($field . ' 0 as channel_id, 0 as server_id')->select();
  356. }
  357. }else{
  358. $field = 'gameid as game_id,count(DISTINCT gameid, userid, serverid) as ' . $param . ', ';
  359. if ($channel) {
  360. if ($server) {
  361. $list = $sql->field($field . ' channel_id, serverid as server_id')->group('gameid,channel_id,serverid')->select();
  362. } else {
  363. $list = $sql->field($field . ' channel_id, 0 as server_id')->group('gameid,channel_id')->select();
  364. }
  365. } else {
  366. if ($server) {
  367. $list = $sql->field($field . ' 0 as channel_id, serverid as server_id')->group('gameid,serverid')->select();
  368. } else {
  369. $list = $sql->field($field . ' 0 as channel_id, 0 as server_id')->group('gameid')->select();
  370. }
  371. }
  372. }
  373. // dump($list);
  374. return $list;
  375. }
  376. /**
  377. * 活跃玩家V2
  378. *
  379. * @param $day
  380. * @param $param
  381. * @param $channel
  382. * @param $members
  383. * @param $platform
  384. *
  385. * @return bool|\PDOStatement|string|\think\Collection
  386. * @throws \think\db\exception\DataNotFoundException
  387. * @throws \think\db\exception\ModelNotFoundException
  388. * @throws \think\exception\DbException
  389. */
  390. public function activeMemberV2($day, $param, $channel = 0, $members = [], $platform = 0, $wheres = [])
  391. {
  392. $sql = Db::table('cy_logininfo')->alias((['cy_logininfo' => 'cl', 'nw_subaccount' => 'ns']))
  393. ->join('nw_subaccount', 'cl.sub_id = ns.id', 'left')
  394. ->whereIn('gameid', $this->getGameIds())
  395. ->whereBetween('cl.login_time', [strtotime($day), strtotime($day . ' 23:59:59')])
  396. // ->where('channel_id', '<>', 0)
  397. ->where(function ($query) use ($members, $wheres) {
  398. if ($members) {
  399. $query->where('cl.userid', 'in', $members);
  400. }
  401. if ($wheres) {
  402. $query->where( $wheres);
  403. }
  404. });
  405. // $sql->fetchSql(true);
  406. if ($platform) {
  407. $field = '0 as game_id,count(DISTINCT cl.userid) as ' . $param . ', ';
  408. if ($channel) {
  409. $list = $sql->field($field . ' cl.channel_id, 0 as server_id')->group('cl.channel_id')->select();
  410. } else {
  411. $list = $sql->field($field . ' 0 as channel_id, 0 as server_id')->select();
  412. }
  413. }else{
  414. $field = 'cl.gameid as game_id,count(DISTINCT cl.gameid, cl.userid, cl.sub_id) as ' . $param . ', ';
  415. if ($channel) {
  416. $list = $sql->field($field . ' cl.channel_id, 0 as server_id')->group('cl.gameid,cl.channel_id')->select();
  417. } else {
  418. $list = $sql->field($field . ' 0 as channel_id, 0 as server_id')->group('cl.gameid')->select();
  419. }
  420. }
  421. // dump($list);
  422. return $list;
  423. }
  424. /**
  425. * 付费人数&充值金额
  426. *
  427. * @param $day
  428. * @param $server
  429. * @param $channel
  430. * @param $platform
  431. * @param $members
  432. * @param $param ['付费人数', '付费金额']
  433. *
  434. * @return bool|\PDOStatement|string|\think\Collection
  435. * @throws \think\db\exception\DataNotFoundException
  436. * @throws \think\db\exception\ModelNotFoundException
  437. * @throws \think\exception\DbException
  438. */
  439. public function rechargeMoney($day, $server = 0, $channel = 0, $platform = 0, $members = [], $param = ['pay_num', 'recharge_num'])
  440. {
  441. $sql = Db::table('cy_pay')
  442. ->whereBetween('pay_time', [strtotime($day), strtotime($day . ' 23:59:59')])
  443. ->where(['status' => 1])
  444. ->whereIn('gameid', $this->getGameIds())
  445. ->where(function ($query) use ($members) {
  446. if ($members) {
  447. $query->where('userid', 'in', $members);
  448. }
  449. });
  450. // $sql->fetchSql(true);
  451. if($platform){
  452. $field = "0 as game_id,sum(pay_amount) as {$param[1]},count(DISTINCT userid) as {$param[0]}, ";
  453. if ($channel) {
  454. $list = $sql->field($field . 'channel_id, 0 as server_id')->group('channel_id')->select();
  455. } else {
  456. $list = $sql->field($field . ' 0 as channel_id, 0 as server_id')->select();
  457. }
  458. }else{
  459. $field = "gameid as game_id,sum(pay_amount) as {$param[1]},count(DISTINCT userid) as {$param[0]}, ";
  460. if ($channel) {
  461. if ($server) {
  462. $list = $sql->field($field . 'channel_id,serverid as server_id')->group('gameid,channel_id,server_id')->select();
  463. } else {
  464. $list = $sql->field($field . 'channel_id, 0 as server_id')->group('gameid,channel_id')->select();
  465. }
  466. } else {
  467. if ($server) {
  468. $list = $sql->field($field . ' 0 as channel_id,serverid as server_id')->group('gameid,server_id')->select();
  469. } else {
  470. $list = $sql->field($field . ' 0 as channel_id, 0 as server_id')->group('gameid')->select();
  471. }
  472. }
  473. }
  474. // dump($list);
  475. return $list;
  476. }
  477. // 获取当天有新增角色的会员ID
  478. public function getFirstRoleMemberList($day)
  479. {
  480. $list = model('common/MemberGameServer')
  481. ->cache('retaine:getFirstRoleMemberList:' . strtotime($day), 500)
  482. ->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')])->column(' DISTINCT member_id');
  483. return $list;
  484. }
  485. // 获取当天有子账号注册的会员ID
  486. public function getSubIds($day)
  487. {
  488. $list = Db::table('nw_subaccount')
  489. ->cache('retaine:getSubIds:' . strtotime($day), 500)
  490. ->whereBetween('create_time', [strtotime($day), strtotime($day . ' 23:59:59')])
  491. ->where('game_id', 'in', $this->getGameIds())
  492. ->column('DISTINCT member_id');
  493. return $list;
  494. }
  495. public function insertOrUpdate($list, $type_id, $day, $param)
  496. {
  497. // Db::startTrans();
  498. try {
  499. foreach ($list as $k => $v) {
  500. // echo "retaine:insertOrUpdate:{$param}_" . $type_id . "_{$day}_" . $v['game_id'] . '_' . $v['channel_id'].PHP_EOL;
  501. $retaineGame = Db::table('cy_retaine_game')
  502. // 缓存查询会导致脏数据问题,先注释掉
  503. // ->cache("retaine:insertOrUpdate:{$param}_" . $type_id . '_' . strtotime($day) . '_' . $v['game_id'] . '_' . $v['channel_id'], 10)
  504. ->where(['type_id' => $type_id, 'day' => $day, 'game_id' => $v['game_id'], 'channel_id' => $v['channel_id']])
  505. ->find();
  506. if ($retaineGame) {
  507. $data = [];
  508. if ($retaineGame['s_channel_id'] == 0) {
  509. $data = $this->getChannel($v['channel_id']);
  510. }
  511. $data[$param] = $v[$param];
  512. if ($param == 'recharge_num') {
  513. $this->saveInfo($type_id, $day, $v['game_id'], $v['channel_id'], ['recharge_num' => $v['recharge_num'], 'pay_num' => $v['pay_num']]);
  514. } else {
  515. $this->saveInfo($type_id, $day, $v['game_id'], $v['channel_id'], $data);
  516. }
  517. } else {
  518. $data = $this->getChannel($v['channel_id']);
  519. $data[$param] = $v[$param];
  520. $this->insertInfo($type_id, $day, $v['game_id'], $v['channel_id'], $data);
  521. }
  522. }
  523. // Db::commit();
  524. } catch (Exception $e) {
  525. // Db::rollback();
  526. // curlDD('【风谷】数据统计异常.insertOrUpdate: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile(). $e->getLine().' - err_id:'.makeOrderid('ERR'), Env::get('dingtalk.warning_url'));
  527. log_message('【风谷】数据统计异常.insertOrUpdate: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile(). $e->getLine().' - err_id:'.makeOrderid('ERR'), 'error', LOG_PATH . '/complex_retaine/service');
  528. }
  529. }
  530. /**
  531. * 批量同步统计数据至留存表
  532. * @param array $data 统计好的数组数据
  533. * @param string $statDay 统计日期 e.g. '2026-01-30'
  534. * @param int $typeId 类型 1游戏 2推广员
  535. */
  536. /**
  537. * @param array $list 原始数据列表
  538. * @param int $type_id 类型
  539. * @param string $day 日期
  540. * @param string $param 对应数据库中的目标字段名 (如: 'reg_total' 或 'act_num')
  541. */
  542. public function syncGameStatData($list, $type_id, $day, $param)
  543. {
  544. if (empty($list) || empty($param)) return;
  545. // 1. 明确我们要更新的字段
  546. // 强制指定更新 $param 传入的字段名和 update_time
  547. $updateField = $param;
  548. // 2. 分批处理
  549. $chunks = array_chunk($list, 500);
  550. $now = time();
  551. foreach ($chunks as $item) {
  552. $values = [];
  553. foreach ($item as $row) {
  554. // 基础必有字段
  555. $gameId = intval($row['game_id']);
  556. $channelId = intval($row['channel_id']);
  557. /**
  558. * 重点:动态取值
  559. * 无论你的数组里那个数字的键名是什么(可能是 reg_total, 也可能是 count)
  560. * 我们通过 array_diff_key 排除掉固定键名,剩下的那个就是我们要的值
  561. */
  562. $tempRow = $row;
  563. unset($tempRow['game_id'], $tempRow['channel_id'], $tempRow['server_id']);
  564. $temp = [
  565. $type_id,
  566. "'$day'",
  567. $gameId,
  568. $channelId,
  569. $now,
  570. (float)$row[$param] // 对应 $param 的值
  571. ];
  572. $values[] = "(" . implode(',', $temp) . ")";
  573. }
  574. // 3. 动态拼装 SQL
  575. // 注意:这里手动指定 $updateField
  576. $fieldStr = "type_id, `day`, game_id, channel_id, update_time, `$updateField` ";
  577. $valStr = implode(',', $values);
  578. // 构建唯一索引冲突时的更新逻辑
  579. // 只有 update_time 和 你传入的这个 $param 字段会被更新
  580. $sql = "INSERT INTO cy_retaine_game ($fieldStr) VALUES $valStr
  581. ON DUPLICATE KEY UPDATE
  582. update_time = VALUES(update_time),
  583. `$updateField` = VALUES(`$updateField`)";
  584. // dump($sql);
  585. try {
  586. $res = \think\Db::execute($sql);
  587. // dump($res);
  588. } catch (\Exception $e) {
  589. \think\Log::error("同步字段 {$param} 失败: " . $e->getMessage());
  590. }
  591. }
  592. }
  593. public function insertOrUpdatev2($list, $type_id, $day, $param)
  594. {
  595. if (empty($list)) {
  596. return;
  597. }
  598. // 分批处理,每批100条记录
  599. $batchSize = 100;
  600. $batches = array_chunk($list, $batchSize);
  601. foreach ($batches as $batchIndex => $batch) {
  602. // Db::startTrans();
  603. try {
  604. // 1. 批量查询已存在的记录
  605. $gameIds = array_unique(array_column($batch, 'game_id'));
  606. $channelIds = array_unique(array_column($batch, 'channel_id'));
  607. $existingList = Db::table('cy_retaine_game')
  608. ->where('type_id', $type_id)
  609. ->where('day', $day)
  610. ->where('game_id', 'in', $gameIds)
  611. ->where('channel_id', 'in', $channelIds)
  612. ->select();
  613. // 构建已存在记录的键值映射
  614. $existingRecords = [];
  615. foreach ($existingList as $record) {
  616. $key = $record['game_id'] . '_' . $record['channel_id'];
  617. $existingRecords[$key] = $record;
  618. }
  619. // 2. 分离新增和更新数据
  620. $insertData = []; // 需要新增的
  621. $updateData = []; // 需要更新的
  622. foreach ($batch as $v) {
  623. $key = $v['game_id'] . '_' . $v['channel_id'];
  624. // 获取渠道信息(如果记录不存在,或者s_channel_id为0)
  625. $channelData = [];
  626. if (!isset($existingRecords[$key]) || (isset($existingRecords[$key]) && $existingRecords[$key]['s_channel_id'] == 0)) {
  627. $channelData = $this->getChannel($v['channel_id']);
  628. }
  629. // 统一数据结构:确保所有记录都有相同字段(避免 insertAll 字段不匹配错误)
  630. $data = [
  631. 's_channel_id' => isset($channelData['s_channel_id']) ? intval($channelData['s_channel_id']) : 0,
  632. 'b_channel_id' => isset($channelData['b_channel_id']) ? intval($channelData['b_channel_id']) : 0,
  633. 'bz_channel_id' => isset($channelData['bz_channel_id']) ? intval($channelData['bz_channel_id']) : 0,
  634. $param => $v[$param],
  635. 'update_time' => time()
  636. ];
  637. // 处理充值金额和付费人数
  638. if ($param == 'recharge_num') {
  639. $data['pay_num'] = $v['pay_num'];
  640. }
  641. // 判断是新增还是更新
  642. if (!isset($existingRecords[$key])) {
  643. // 新增记录:添加必要字段
  644. $data['type_id'] = $type_id;
  645. $data['day'] = $day;
  646. $data['game_id'] = $v['game_id'];
  647. $data['channel_id'] = $v['channel_id'];
  648. $insertData[] = $data;
  649. } else {
  650. // 更新记录:只更新非0的渠道字段
  651. $updateFields = [];
  652. if ($data['s_channel_id'] > 0) {
  653. $updateFields['s_channel_id'] = $data['s_channel_id'];
  654. }
  655. if ($data['b_channel_id'] > 0) {
  656. $updateFields['b_channel_id'] = $data['b_channel_id'];
  657. }
  658. if ($data['bz_channel_id'] > 0) {
  659. $updateFields['bz_channel_id'] = $data['bz_channel_id'];
  660. }
  661. $updateFields[$param] = $data[$param];
  662. $updateFields['update_time'] = $data['update_time'];
  663. if ($param == 'recharge_num') {
  664. $updateFields['pay_num'] = $data['pay_num'];
  665. }
  666. $updateFields['_where'] = [
  667. 'type_id' => $type_id,
  668. 'day' => $day,
  669. 'game_id' => $v['game_id'],
  670. 'channel_id' => $v['channel_id']
  671. ];
  672. $updateData[] = $updateFields;
  673. }
  674. }
  675. // 3. 批量插入新记录
  676. if (!empty($insertData)) {
  677. $insertResult = Db::table('cy_retaine_game')->insertAll($insertData);
  678. Log::write("## {$param}@insertOrUpdatev2.insertAll: data=".json_encode($insertData).", result=".$insertResult,'notice');
  679. }
  680. // 4. 批量更新已存在记录
  681. if (!empty($updateData)) {
  682. foreach ($updateData as $data) {
  683. $where = $data['_where'];
  684. unset($data['_where']);
  685. if (!empty($data)) {
  686. $updateResult = Db::table('cy_retaine_game')->where($where)->update($data);
  687. Log::write("## {$param}@insertOrUpdatev2.update: where=".json_encode($where).", data=".json_encode($data).", result=".$updateResult,'notice');
  688. }
  689. }
  690. }
  691. // Db::commit();
  692. } catch (Exception $e) {
  693. // Db::rollback();
  694. log_message('【风谷】数据统计异常.insertOrUpdatev2: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile(). $e->getLine().' - err_id:'.makeOrderid('ERR'), 'error', LOG_PATH . '/complex_retaine/service');
  695. }
  696. }
  697. }
  698. public function insertInfo($type_id, $day, $game_id, $channel_id, $param)
  699. {
  700. $data = array_merge($param, [
  701. 'type_id' => $type_id,
  702. 'day' => $day,
  703. 'game_id' => $game_id,
  704. 'channel_id' => $channel_id,
  705. 'update_time' => time(),
  706. ]);
  707. $result = model('common/RetaineGame')->insert($data);
  708. Log::write("## {$type_id}@insertInfo: data=".json_encode($data).", result=".$result,'notice');
  709. }
  710. public function saveInfo($type_id, $day, $game_id, $channel_id, $param)
  711. {
  712. $param = array_merge($param, ['update_time' => time()]);
  713. $result = model('common/RetaineGame')->where(['type_id' => $type_id, 'day' => $day, 'game_id' => $game_id, 'channel_id' => $channel_id])->update($param);
  714. Log::write("## {$type_id}@saveInfo: where=".json_encode(['type_id' => $type_id, 'day' => $day, 'game_id' => $game_id, 'channel_id' => $channel_id]).", data=".json_encode($param).", result=".$result,'notice');
  715. }
  716. public function insertOrUpdateServer($list, $type_id, $day, $param)
  717. {
  718. // Db::startTrans();
  719. try {
  720. foreach ($list as $k => $v) {
  721. // dump($v);
  722. // if (in_array($v['game_id'], [175])) {
  723. // if (!is_numeric($v['server_id'])) {
  724. // $gameServer = model('common/GameServer')->field('serverid')->cache('retaineService:insertOrUpdateServer:' . $v['game_id'] . "_" . $v['server_id'])->where(['game_id' => $v['game_id'], 'servername' => $v['server_id']])->where('serverid', '<>', $v['server_id'])->find();
  725. // $v['server_id'] = $gameServer['serverid'];
  726. // }
  727. // }
  728. if ($retaineGame = Db::table('cy_retaine_server')
  729. ->cache('retaine:insertOrUpdate:' . $type_id . '_' . strtotime($day) . '_' . $v['game_id'] . '_' . $v['channel_id'] . '_' . $v['server_id'], 60)
  730. ->where(['type_id' => $type_id, 'day' => $day, 'game_id' => $v['game_id'], 'channel_id' => $v['channel_id'], 'server_id' => $v['server_id']])->find()) {
  731. $data = [];
  732. // dump($retaineGame);
  733. // 判断是否商务
  734. if ($retaineGame['s_channel_id'] == 0) {
  735. $data = $this->getChannel($v['channel_id']);
  736. }
  737. // $data[$param] = $v[$param];
  738. // if ($param == 'recharge_num') {
  739. // $this->saveInfoServer($type_id, $day, $v['game_id'], $v['channel_id'], $v['server_id'], ['recharge_num' => $v['recharge_num'], 'pay_num' => $v['pay_num']]);
  740. // } else {
  741. // $this->saveInfoServer($type_id, $day, $v['game_id'], $v['channel_id'], $v['server_id'], $data);
  742. // }
  743. } else {
  744. //
  745. // $data = $this->getChannel($v['channel_id']);
  746. // $data[$param] = $v[$param];
  747. // $this->insertInfoServer($type_id, $day, $v['game_id'], $v['channel_id'], $v['server_id'], $data);
  748. }
  749. }
  750. // Db::commit();
  751. } catch (Exception $e) {
  752. curlDD('【风谷】数据统计异常.insertOrUpdateServer: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile().''. $e->getLine().' - err_id:'.makeOrderid('ERR'), Env::get('dingtalk.warning_url'));
  753. // Db::rollback();
  754. }
  755. }
  756. public function insertOrUpdateServerv2($list, $type_id, $day, $param)
  757. {
  758. if (empty($list)) {
  759. return;
  760. }
  761. // 分批处理,每批100条记录
  762. $batchSize = 500;
  763. $batches = array_chunk($list, $batchSize);
  764. $time = time();
  765. foreach ($batches as $batch) {
  766. Db::startTrans();
  767. try {
  768. // 1. 批量查询已存在的记录
  769. $gameIds = array_unique(array_column($batch, 'game_id'));
  770. $channelIds = array_unique(array_column($batch, 'channel_id'));
  771. $serverIds = array_unique(array_column($batch, 'server_id'));
  772. $existingList = Db::table('cy_retaine_server')
  773. ->where('type_id', $type_id)
  774. ->where('day', $day)
  775. ->where('game_id', 'in', $gameIds)
  776. ->where('channel_id', 'in', $channelIds)
  777. ->where('server_id', 'in', $serverIds)
  778. ->select();
  779. // 构建已存在记录的键值映射(包含 server_id)
  780. $existingRecords = [];
  781. foreach ($existingList as $record) {
  782. $key = $record['game_id'] . '_' . $record['channel_id'] . '_' . $record['server_id'];
  783. $existingRecords[$key] = $record;
  784. }
  785. // 2. 分离新增和更新数据
  786. $insertData = []; // 需要新增的
  787. $updateData = []; // 需要更新的
  788. foreach ($batch as $v) {
  789. $key = $v['game_id'] . '_' . $v['channel_id'] . '_' . $v['server_id'];
  790. // 获取渠道信息(如果记录不存在,或者s_channel_id为0)
  791. $channelData = [];
  792. if (!isset($existingRecords[$key]) || (isset($existingRecords[$key]) && $existingRecords[$key]['s_channel_id'] == 0)) {
  793. $channelData = $this->getChannel($v['channel_id']);
  794. }
  795. // 统一数据结构:确保所有记录都有相同字段
  796. $data = [
  797. 's_channel_id' => isset($channelData['s_channel_id']) ? intval($channelData['s_channel_id']) : 0,
  798. 'b_channel_id' => isset($channelData['b_channel_id']) ? intval($channelData['b_channel_id']) : 0,
  799. 'bz_channel_id' => isset($channelData['bz_channel_id']) ? intval($channelData['bz_channel_id']) : 0,
  800. $param => $v[$param],
  801. 'update_time' => $time
  802. ];
  803. // 处理充值金额和付费人数
  804. if ($param == 'recharge_num') {
  805. $data['pay_num'] = $v['pay_num'];
  806. }
  807. // 判断是新增还是更新
  808. if (!isset($existingRecords[$key])) {
  809. // 新增记录:添加必要字段
  810. $data['type_id'] = $type_id;
  811. $data['day'] = $day;
  812. $data['game_id'] = $v['game_id'];
  813. $data['channel_id'] = $v['channel_id'];
  814. $data['server_id'] = $v['server_id'];
  815. $insertData[] = $data;
  816. } else {
  817. // 更新记录:只更新非0的渠道字段
  818. $updateFields = [];
  819. if ($data['s_channel_id'] > 0) {
  820. $updateFields['s_channel_id'] = $data['s_channel_id'];
  821. }
  822. if ($data['b_channel_id'] > 0) {
  823. $updateFields['b_channel_id'] = $data['b_channel_id'];
  824. }
  825. if ($data['bz_channel_id'] > 0) {
  826. $updateFields['bz_channel_id'] = $data['bz_channel_id'];
  827. }
  828. $updateFields[$param] = $data[$param];
  829. $updateFields['update_time'] = $data['update_time'];
  830. if ($param == 'recharge_num') {
  831. $updateFields['pay_num'] = $data['pay_num'];
  832. }
  833. $updateFields['_where'] = [
  834. 'type_id' => $type_id,
  835. 'day' => $day,
  836. 'game_id' => $v['game_id'],
  837. 'channel_id' => $v['channel_id'],
  838. 'server_id' => $v['server_id']
  839. ];
  840. $updateData[] = $updateFields;
  841. }
  842. }
  843. // 3. 批量插入新记录
  844. if (!empty($insertData)) {
  845. Db::table('cy_retaine_server')->insertAll($insertData);
  846. }
  847. // 4. 批量更新已存在记录
  848. if (!empty($updateData)) {
  849. foreach ($updateData as $data) {
  850. $where = $data['_where'];
  851. unset($data['_where']);
  852. if (!empty($data)) {
  853. Db::table('cy_retaine_server')->where($where)->update($data);
  854. }
  855. }
  856. }
  857. Db::commit();
  858. } catch (Exception $e) {
  859. Db::rollback();
  860. log_message('【风谷】数据统计异常.insertOrUpdateServerv2: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile().''. $e->getLine().' - err_id:'.makeOrderid('ERR'), 'error', LOG_PATH . '/complex_retaine/service');
  861. }
  862. }
  863. }
  864. public function insertInfoServer($type_id, $day, $game_id, $channel_id, $server_id, $param)
  865. {
  866. model('common/RetaineServer')->insert(array_merge($param, [
  867. 'type_id' => $type_id,
  868. 'day' => $day,
  869. 'game_id' => $game_id,
  870. 'server_id' => $server_id,
  871. 'channel_id' => $channel_id,
  872. 'update_time' => time(),
  873. ]));
  874. }
  875. public function saveInfoServer($type_id, $day, $game_id, $channel_id, $server_id, $param)
  876. {
  877. $param = array_merge($param, ['update_time' => time()]);
  878. model('common/RetaineServer')->where(['type_id' => $type_id, 'day' => $day, 'game_id' => $game_id, 'channel_id' => $channel_id, 'server_id' => $server_id])->update($param);
  879. }
  880. public function getChannels($channel_id)
  881. {
  882. $data = [];
  883. if ($channel_id > 0) {
  884. return $data;
  885. }
  886. $channelList = model('common/Channel')->cache('retaine:channelList', 120)->column('id,name,id_path,level', 'id');
  887. if (isset($channelList[$channel_id])) {
  888. $channel = $channelList[$channel_id];
  889. $id_path = explode(',', $channel['id_path']);
  890. foreach ($id_path as $kk => $vv) {
  891. if (isset($vv) && isset($channelList[$vv]) && $channelList[$vv] && $vv != $channel_id) {
  892. $channel_ch = $channelList[$vv];
  893. if ($channel_ch['level'] == 0) {
  894. $data['s_channel_id'] = $channel_ch['id'];
  895. } elseif ($channel_ch['level'] == 1) {
  896. $data['b_channel_id'] = $channel_ch['id'];
  897. } elseif ($channel_ch['level'] == 2) {
  898. $data['bz_channel_id'] = $channel_ch['id'];
  899. }
  900. }
  901. }
  902. }
  903. return $data;
  904. }
  905. public function getChannel($channel_id)
  906. {
  907. // dump($channel_id);
  908. $data = [];
  909. if ($channel_id > 0) {
  910. return $data;
  911. }
  912. // 进程级静态缓存,一次请求只读一次文件
  913. static $channelList = null;
  914. if ($channelList === null) {
  915. $channelList = Cache::store('File')->get('cache_retaine_channel_list');
  916. if (!$channelList) {
  917. $channelList = model('common/Channel')->column('id,name,id_path,level', 'id');
  918. Cache::store('File')->set('cache_retaine_channel_list', $channelList, 3600);
  919. }
  920. }
  921. // dump($channelList);
  922. if (!isset($channelList[$channel_id])) {
  923. return [];
  924. }
  925. $data = [];
  926. $id_path = explode(',', $channelList[$channel_id]['id_path']);
  927. foreach ($id_path as $vv) {
  928. if ($vv == $channel_id || !isset($channelList[$vv])) {
  929. continue;
  930. }
  931. $ch = $channelList[$vv];
  932. if ($ch['level'] == 0) {
  933. $data['s_channel_id'] = $ch['id'];
  934. } elseif ($ch['level'] == 1) {
  935. $data['b_channel_id'] = $ch['id'];
  936. } elseif ($ch['level'] == 2) {
  937. $data['bz_channel_id'] = $ch['id'];
  938. }
  939. }
  940. // dump($data);
  941. return $data;
  942. }
  943. public function insertOrUpdatePlatform($list, $type_id, $day, $param)
  944. {
  945. Db::startTrans();
  946. try {
  947. foreach ($list as $k => $v) {
  948. $retaine = model('common/Retaine')
  949. // ->cache('retaine:insertOrUpdatePlatform:' . $type_id . '_' . strtotime($day) . '_' . $v['channel_id'], 60)
  950. ->where(['type_id' => $type_id, 'day' => $day, 'channel_id' => $v['channel_id']])->find();
  951. if ($retaine) {
  952. $data = [];
  953. if ($retaine['s_channel_id'] == 0) {
  954. $data = $this->getChannel($v['channel_id']);
  955. }
  956. $data[$param] = $v[$param];
  957. if ($param == 'recharge_num') {
  958. $this->saveInfoPlatform($type_id, $day, $v['channel_id'], ['recharge_num' => $v['recharge_num'], 'pay_num' => $v['pay_num']]);
  959. } else {
  960. $this->saveInfoPlatform($type_id, $day, $v['channel_id'], $data);
  961. }
  962. } else {
  963. $data = $this->getChannel($v['channel_id']);
  964. $data[$param] = $v[$param];
  965. $this->insertInfoPlatform($type_id, $day, $v['channel_id'], $data);
  966. }
  967. }
  968. Db::commit();
  969. } catch (Exception $e) {
  970. curlDD('【风谷】数据统计异常.insertOrUpdatePlatform: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile().''. $e->getLine().' - err_id:'.makeOrderid('ERR'), Env::get('dingtalk.warning_url'));
  971. Db::rollback();
  972. }
  973. }
  974. public function insertOrUpdatePlatformv2($list, $type_id, $day, $param)
  975. {
  976. if (empty($list)) {
  977. return;
  978. }
  979. // 分批处理,每批100条记录
  980. $batchSize = 100;
  981. $batches = array_chunk($list, $batchSize);
  982. foreach ($batches as $batch) {
  983. try {
  984. // 1. 批量查询已存在的记录
  985. $channelIds = array_unique(array_column($batch, 'channel_id'));
  986. $existingList = Db::table('cy_retaine')
  987. ->where('type_id', $type_id)
  988. ->where('day', $day)
  989. ->where('channel_id', 'in', $channelIds)
  990. ->select();
  991. // 构建已存在记录的键值映射
  992. $existingRecords = [];
  993. foreach ($existingList as $record) {
  994. $key = $record['channel_id'];
  995. $existingRecords[$key] = $record;
  996. }
  997. // 2. 分离新增和更新数据
  998. $insertData = []; // 需要新增的
  999. $updateData = []; // 需要更新的
  1000. foreach ($batch as $v) {
  1001. $key = $v['channel_id'];
  1002. // 获取渠道信息(如果记录不存在,或者s_channel_id为0)
  1003. $channelData = [];
  1004. if (!isset($existingRecords[$key]) || (isset($existingRecords[$key]) && $existingRecords[$key]['s_channel_id'] == 0)) {
  1005. $channelData = $this->getChannel($v['channel_id']);
  1006. }
  1007. // 统一数据结构:确保所有记录都有相同字段
  1008. $data = [
  1009. 's_channel_id' => isset($channelData['s_channel_id']) ? intval($channelData['s_channel_id']) : 0,
  1010. 'b_channel_id' => isset($channelData['b_channel_id']) ? intval($channelData['b_channel_id']) : 0,
  1011. 'bz_channel_id' => isset($channelData['bz_channel_id']) ? intval($channelData['bz_channel_id']) : 0,
  1012. $param => $v[$param],
  1013. 'update_time' => time()
  1014. ];
  1015. // 处理充值金额和付费人数
  1016. if ($param == 'recharge_num') {
  1017. $data['pay_num'] = $v['pay_num'];
  1018. }
  1019. // 判断是新增还是更新
  1020. if (!isset($existingRecords[$key])) {
  1021. // 新增记录:添加必要字段
  1022. $data['type_id'] = $type_id;
  1023. $data['day'] = $day;
  1024. $data['channel_id'] = $v['channel_id'];
  1025. $insertData[] = $data;
  1026. } else {
  1027. // 更新记录:只更新非0的渠道字段
  1028. $updateFields = [];
  1029. if ($data['s_channel_id'] > 0) {
  1030. $updateFields['s_channel_id'] = $data['s_channel_id'];
  1031. }
  1032. if ($data['b_channel_id'] > 0) {
  1033. $updateFields['b_channel_id'] = $data['b_channel_id'];
  1034. }
  1035. if ($data['bz_channel_id'] > 0) {
  1036. $updateFields['bz_channel_id'] = $data['bz_channel_id'];
  1037. }
  1038. $updateFields[$param] = $data[$param];
  1039. $updateFields['update_time'] = $data['update_time'];
  1040. if ($param == 'recharge_num') {
  1041. $updateFields['pay_num'] = $data['pay_num'];
  1042. }
  1043. $updateFields['_where'] = [
  1044. 'type_id' => $type_id,
  1045. 'day' => $day,
  1046. 'channel_id' => $v['channel_id']
  1047. ];
  1048. $updateData[] = $updateFields;
  1049. }
  1050. }
  1051. } catch (\Exception $e){
  1052. throw new \Exception('数据统计异常.insertOrUpdatePlatformv2: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage() . ' - file:' . $e->getFile() . ' - line:' . $e->getLine());
  1053. }
  1054. if(empty($insertData) && empty($updateData)){
  1055. break;
  1056. }
  1057. Db::startTrans();
  1058. try {
  1059. // 3. 批量插入新记录
  1060. if (!empty($insertData)) {
  1061. Db::table('cy_retaine')->insertAll($insertData);
  1062. }
  1063. // 4. 批量更新已存在记录
  1064. if (!empty($updateData)) {
  1065. foreach ($updateData as $data) {
  1066. $where = $data['_where'];
  1067. unset($data['_where']);
  1068. if (!empty($data)) {
  1069. Db::table('cy_retaine')->where($where)->update($data);
  1070. }
  1071. }
  1072. }
  1073. Db::commit();
  1074. } catch (Exception $e) {
  1075. Db::rollback();
  1076. log_message('【风谷】数据统计异常.insertOrUpdatePlatformv2: ' . $type_id . '_' . $day . '_' . $param . '_' . $e->getMessage().' - file:'. $e->getFile().''. $e->getLine().' - err_id:'.makeOrderid('ERR'), 'error', LOG_PATH . '/complex_retaine/service');
  1077. }
  1078. }
  1079. }
  1080. public function insertInfoPlatform($type_id, $day, $channel_id, $param)
  1081. {
  1082. model('common/Retaine')->insert(array_merge($param, [
  1083. 'type_id' => $type_id,
  1084. 'day' => $day,
  1085. 'channel_id' => $channel_id,
  1086. 'update_time' => time(),
  1087. ]));
  1088. }
  1089. public function saveInfoPlatform($type_id, $day, $channel_id, $param)
  1090. {
  1091. $param = array_merge($param, ['update_time' => time()]);
  1092. model('common/Retaine')->where(['type_id' => $type_id, 'day' => $day, 'channel_id' => $channel_id])->update($param);
  1093. }
  1094. }