ComplexMember.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. <?php
  2. /**
  3. * 聚合玩家用户管理控制器
  4. */
  5. namespace app\admin\controller;
  6. use app\common\library\MakeReportGo;
  7. use app\common\model\ComplexMembers as MembersModel;
  8. use app\common\model\ComplexChannelModel;
  9. use app\common\model\ComplexPay;
  10. use app\common\model\CompanyGameBind;
  11. use GuzzleHttp\Exception\RequestException;
  12. use think\Db;
  13. use app\common\library\MakeReport;
  14. use GuzzleHttp\Client;
  15. class ComplexMember extends Admin
  16. {
  17. protected $membersModel;
  18. protected $where;
  19. protected $start_time;
  20. protected $end_time;
  21. protected $login_start_time;
  22. protected $login_end_time;
  23. protected $pay_start_time;
  24. protected $pay_end_time;
  25. /**
  26. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  27. */
  28. protected function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->membersModel = new MembersModel;
  32. $this->where = [];
  33. }
  34. public function getWhere()
  35. {
  36. $where = [];
  37. $start_time = input('request.start_time');
  38. $end_time = input('request.end_time');
  39. $channel_id = input('request.channel_id');
  40. $gameid = input('request.gameid');
  41. $username = input('request.username');
  42. $complex_username = input('request.complex_username');
  43. //开始时间和结束时间不为空时
  44. if ($start_time != '' && $end_time != '') {
  45. $where['reg_time'] = [
  46. ['>=', strtotime($start_time)],
  47. ['<=', strtotime($end_time . ' 23:59:59')],
  48. ];
  49. } //开始时间不为空时
  50. elseif ($start_time != '') {
  51. $where['reg_time'] = ['>=', strtotime($start_time)];
  52. } //结束时间不为空时
  53. elseif ($end_time != '') {
  54. $where['reg_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  55. } else {
  56. /*
  57. $start_time = $end_time = date('Y-m-d', time());
  58. $where['reg_time'] = [
  59. ['>=', strtotime($start_time)],
  60. ['<=', strtotime($end_time . ' 23:59:59')],
  61. ];
  62. */
  63. }
  64. //渠道
  65. if ($channel_id != '') {
  66. $where['channel_id'] = $channel_id;
  67. }
  68. //游戏
  69. if ($gameid != '') {
  70. $where['gameid'] = $gameid;
  71. }
  72. //用户名
  73. if ($username != '') {
  74. $where['m.mg_username'] = $username;
  75. }
  76. //聚合渠道ID/用户名
  77. if ($complex_username != '') {
  78. $where['m.complex_username'] = $complex_username;
  79. }
  80. // 游戏上线状态
  81. $cooperation_status = $this->request->param('cooperation_status', 0, 'intval'); // 游戏上线状态
  82. if(!empty($cooperation_status)){
  83. $game_cooperation_status = $cooperation_status==1?1:2;
  84. $gameIds = Model('Game')->getNormalByIds($game_cooperation_status);
  85. $where['m.gameid'] = ['in', $gameIds];
  86. }
  87. $this->start_time = $start_time;
  88. $this->end_time = $end_time;
  89. $this->where = $where;
  90. }
  91. /**
  92. * 注册用户列表
  93. */
  94. public function index()
  95. {
  96. $this->getWhere();
  97. $param = input('get.');
  98. $list = $this->membersModel->table('nw_complex_members')->alias('m')
  99. ->join('nw_complex_channel c', 'm.channel_id=c.id', 'left')
  100. ->field('m.id,m.mg_username,m.complex_username,m.gameid,m.channel_id,m.total_pay_amount,m.reg_time,m.login_time,c.name channel_name')
  101. ->where($this->where)->order('m.reg_time desc')
  102. ->paginate(25, false, array('query' => $param));
  103. $data = $list->toArray()['data'];
  104. if ( ! empty($data) ) {
  105. foreach ($data as $v) {
  106. $gameIdArr[] = $v['gameid'];
  107. }
  108. $gameNameArr = model('Common/Game')->whereIn('id', $gameIdArr)->column('name', 'id');
  109. foreach ($data as $key => $item) {
  110. $data[ $key ]['game_name'] = isset($gameNameArr[ $item['gameid'] ]) ? $gameNameArr[ $item['gameid'] ] : '--';
  111. }
  112. }
  113. $channel_list = (new ComplexChannelModel())->getAllByCondition('id,name',['flag'=>4],'name asc');
  114. $gameList = model('Common/Game')->getAllByCondition('id,name',[],'','self');
  115. $selfGameList = array();
  116. foreach ($gameList as $game) {
  117. $selfGameList[ $game['id']] = $game;
  118. }
  119. //$gameList = array_column($gameList, 'name', 'id');
  120. $this->assign('list', $data);
  121. $this->assign('start_time', $this->start_time);
  122. $this->assign('end_time', $this->end_time);
  123. $this->assign('total', $list->total());
  124. $this->assign('page', $list->render());
  125. $this->assign('channel_list', $channel_list);
  126. $this->assign('game_list', $selfGameList);
  127. return $this->fetch();
  128. }
  129. /**
  130. * 报表下载
  131. *
  132. */
  133. public function download()
  134. {
  135. if (request()->isAjax()) {
  136. $auth = new \app\common\logic\Auth();
  137. $is_show = input('is_show');
  138. if($is_show>(int)$auth->check(session('ADMIN_ID'), 'admin/complex_member/memberDowmExcel'))
  139. {
  140. $this->error('您没有访问权限!');
  141. }
  142. $this->getWhere();
  143. $sql = $this->membersModel->table('nw_complex_members m,nw_complex_channel c')
  144. ->field('m.id,m.mg_username,m.complex_username,m.gameid,m.total_pay_amount,m.channel_id,m.reg_time,m.login_time,c.name channel_name')
  145. ->where('m.channel_id=c.id')
  146. ->where($this->where)->order('m.reg_time desc')
  147. ->fetchSql(true)->select();
  148. if ((new MakeReportGo())->addTask('admin.complexMemberList',$sql,session_id(),['is_show'=>boolval($is_show)])){
  149. //高级报表下载
  150. if($is_show==1){
  151. $this->insertLog($this->current_node,'注册用户高级报表下载',58);
  152. }
  153. $this->success('报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  154. }
  155. else{
  156. $this->error('报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  157. }
  158. }
  159. else{
  160. $this->error('非法请求');
  161. }
  162. }
  163. /**
  164. * 登录用户列表
  165. */
  166. public function loginList()
  167. {
  168. $list = Db::table('nw_complex_loginlog l,nw_complex_members m,nw_complex_channel c,cy_game g')
  169. ->field("l.ip,l.channel_id as channel_id,l.userid,l.login_time,m.mg_username,c.name as channel_name,g.name game_name,g.origin_name")
  170. ->where('l.userid = m.id and l.channel_id=c.id and l.gameid=g.id')
  171. ->where($this->_getLoginListCondition())
  172. ->order('l.id desc')
  173. ->paginate(20, false, ['query' => input('get.')]);
  174. $data = $list->toArray()['data'];
  175. $gameList = model('Common/Game')->getAllByCondition('id,name',[],'','self');
  176. $selfGameList = array();
  177. foreach ($gameList as $game) {
  178. $selfGameList[ $game['id']] = $game;
  179. }
  180. //$gameList = array_column($gameList, 'name', 'id');
  181. $this->assign('list', $data);
  182. $this->assign('total', $list->total());
  183. $this->assign('page', $list->render());
  184. $this->assign('game_list', $selfGameList);
  185. $this->assign('start_time', $this->login_start_time);
  186. $this->assign('end_time', $this->login_end_time);
  187. return $this->fetch('login_list');
  188. }
  189. /**
  190. * 登录用户 报表下载
  191. */
  192. public function loginListDownload()
  193. {
  194. if (request()->isAjax()) {
  195. $sql = Db::table('nw_complex_loginlog l,nw_complex_members m,nw_complex_channel c,cy_game g')
  196. ->field("l.ip,l.channel_id as channel_id,l.userid,l.login_time,m.mg_username,c.name as channel_name,g.name game_name,g.origin_name")
  197. ->where('l.userid = m.id and l.channel_id=c.id and l.gameid=g.id')
  198. ->where($this->_getLoginListCondition())
  199. ->order('l.id desc')
  200. ->fetchSql(true)
  201. ->select();
  202. if((new MakeReportGo())->addTask('admin.complexMemberLoginList', $sql, session_id())){
  203. $this->success('报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  204. }
  205. $this->error('报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  206. } else {
  207. $this->error('非法请求');
  208. }
  209. }
  210. /**
  211. * 登录用户 条件查询
  212. * @return array
  213. */
  214. protected function _getLoginListCondition()
  215. {
  216. $where = [];
  217. $start_time = input('request.start_time', '', 'trim');
  218. $end_time = input('request.end_time', '', 'trim');
  219. $channel_name = input('request.channel_name', '', 'trim');
  220. $username = input('request.mg_username', '', 'trim');
  221. $gameid = input('request.gameid', '', 'intval');
  222. //开始时间和结束时间不为空时
  223. if ($start_time != '' && $end_time != '') {
  224. $where['l.login_time'] = [
  225. ['>=', strtotime($start_time)],
  226. ['<=', strtotime($end_time . ' 23:59:59')],
  227. ];
  228. } //开始时间不为空时
  229. elseif ($start_time != '') {
  230. $where['l.login_time'] = ['>=', strtotime($start_time)];
  231. } //结束时间不为空时
  232. elseif ($end_time != '') {
  233. $where['l.login_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  234. } else {
  235. $start_time = date('Y-m-d', time());
  236. $end_time = $start_time;
  237. $where['l.login_time'] = [
  238. ['>=', strtotime($start_time)],
  239. ['<=', strtotime($end_time . ' 23:59:59')],
  240. ];
  241. }
  242. //渠道名称
  243. if ($channel_name != '') {
  244. $where['c.name'] = ['like', '%' . $channel_name . '%'];
  245. }
  246. //用户名
  247. if ($username != '') {
  248. $where['m.mg_username'] = $username;
  249. }
  250. //游戏名称
  251. if (!empty($gameid)) {
  252. $where['l.gameid'] = $gameid;
  253. }
  254. $this->login_start_time = $start_time;
  255. $this->login_end_time = $end_time;
  256. // 游戏上线状态
  257. $cooperation_status = $this->request->param('cooperation_status', 0, 'intval'); // 游戏上线状态
  258. if(!empty($cooperation_status)){
  259. $game_cooperation_status = $cooperation_status==1?1:2;
  260. $gameIds = Model('Game')->getNormalByIds($game_cooperation_status);
  261. $where['l.gameid'] = ['in', $gameIds];
  262. }
  263. return $where;
  264. }
  265. /**
  266. * 充值记录
  267. *
  268. */
  269. public function payList()
  270. {
  271. $condition = $this->_beforeGetPayParams();
  272. //充值完成总金额
  273. $totalAmount = 0;
  274. $totalData = [
  275. 'total_amount' => 0,
  276. 'total_pay_amount' => 0,
  277. ];
  278. $payModel = new ComplexPay;
  279. $cp_status = $this->request->param('cp_status');
  280. $where = [];
  281. if (is_numeric($cp_status)) {
  282. $where['y.status'] = (int)$cp_status;
  283. }
  284. $list = $payModel->alias('p')
  285. ->join('nw_complex_channel c', 'p.channel_id=c.id')
  286. ->join('cy_paycpinfo y', 'p.orderid=y.orderid')
  287. ->field('p.orderid,p.sub_orderid,p.gameid,p.username,p.userid,p.amount,p.status,p.create_time,p.attach,c.name channel_name,y.status as cp_status,p.roleid,p.rolename,p.pay_amount')
  288. ->where($where)
  289. ->where($condition)
  290. ->order('p.id desc')
  291. ->paginate(15, false, ['query' => input('get.')]);
  292. if ($list->total()>0) {
  293. $data = $list->toArray()['data'];
  294. $userIdArr = $gameIdArr = [];
  295. foreach ($data as $v) {
  296. $userIdArr[] = $v['userid'];
  297. $gameIdArr[] = $v['gameid'];
  298. }
  299. $gameNameArr = model('Common/Game')->whereIn('id', $gameIdArr)->column('name', 'id');
  300. $regTimeArr = $this->membersModel->whereIn('id', array_unique($userIdArr))->column('reg_time', 'id');
  301. foreach ($list as $key => $item) {
  302. $default = '--';
  303. $list[ $key ]['game_name'] = isset($gameNameArr[ $item['gameid'] ]) ? $gameNameArr[ $item['gameid'] ] : $default;
  304. $list[ $key ]['reg_time'] = isset($regTimeArr[ $item['userid'] ]) ? date("Y-m-d H:i:s", $regTimeArr[ $item['userid'] ]) : $default;
  305. }
  306. // 充值完成总金额 + 充值实付完成总金额
  307. $totalData = $payModel->alias('p')->field('sum(amount) as total_amount, sum(pay_amount) as total_pay_amount')->where($condition)->where('p.status=1')->find();
  308. }
  309. $channel_list = (new ComplexChannelModel())->getAllByCondition('id,name',['flag'=>4],'name asc');
  310. // 获取公司列表
  311. $companyList = (new CompanyGameBind())->field('id,company_name,company_alias')->order('id desc')->select();
  312. $companyArr = [];
  313. foreach ($companyList as $company) {
  314. $companyArr[] = [
  315. 'id' => $company['id'],
  316. 'name' => $company['company_name'] . ' (' . $company['company_alias'] . ')'
  317. ];
  318. }
  319. $this->assign('list', $list);
  320. $this->assign('total', $list->total());
  321. $this->assign('page', $list->render());
  322. $gameList = model('Common/Game')->getAllByCondition('id,name',[],'','self');
  323. $selfGameList = array();
  324. foreach ($gameList as $game) {
  325. $selfGameList[ $game['id']] = $game;
  326. }
  327. $this->assign('game_list', $selfGameList);
  328. $this->assign('channel_list', $channel_list); //渠道列表
  329. $this->assign('company_list', $companyArr); //公司列表
  330. $this->assign('totalData', $totalData);
  331. $this->assign('pay_start_time', $this->pay_start_time); //开始时间
  332. $this->assign('pay_end_time', $this->pay_end_time); //结束时间
  333. return $this->fetch('pay_list');
  334. }
  335. /**
  336. * 补单通知CP
  337. *
  338. */
  339. public function reCallback()
  340. {
  341. if($this->request->isPost()){
  342. $orderid = input('post.orderid','','trim');
  343. if(empty($orderid)){
  344. $this->error('订单号不能为空');
  345. }
  346. $payCpInfoModel = model('Common/PayCpinfo');
  347. $where['orderid'] = $orderid;
  348. $paycpInfo = $payCpInfoModel->field('id,fcallbackurl,params,payflag')->where($where)->find();
  349. if(empty($paycpInfo)){
  350. $this->error('订单通知回调记录不存在');
  351. }
  352. elseif($paycpInfo['payflag']!=1){
  353. $this->error('订单未完成支付或支付失败');
  354. }
  355. $guzzle = new Client();
  356. //将字符串转换成数组
  357. parse_str($paycpInfo['params'], $param);
  358. try {
  359. $response = $guzzle->request('post', $paycpInfo['fcallbackurl'], ['form_params' => $param, 'timeout' => 30]);
  360. log_message($paycpInfo['fcallbackurl'] . '?' . http_build_query($param) . ' ' . $response->getBody(), 'log', LOG_PATH . 'reCallback/');
  361. //通知成功
  362. if (0 == strcasecmp($response->getBody(), 'success')) {
  363. $payCpInfoModel->save(['status' => 1, 'update_time' => NOW_TIMESTAMP], ['id' => $paycpInfo['id']]);
  364. $this->success('订单通知成功');
  365. }
  366. else{
  367. $this->error('订单通知已发送给对方,但对方未返回[success]信息');
  368. }
  369. } catch (RequestException $e) {
  370. $this->error("订单通知失败: " . $e->getMessage());
  371. }
  372. }
  373. return $this->fetch('re_callback');
  374. }
  375. /**
  376. * 充值记录 报表下载
  377. */
  378. public function payListDownload()
  379. {
  380. if (request()->isAjax()) {
  381. $payModel = new ComplexPay;
  382. // $sql = $payModel->table('nw_complex_pay p,nw_complex_channel c,cy_game g,nw_complex_members m')
  383. // ->field('orderid,p.sub_orderid,p.gameid,p.username,amount,p.status,p.create_time,attach,c.name channel_name,g.name game_name,g.origin_name,m.reg_time')
  384. // ->where('p.channel_id=c.id and p.gameid=g.id and p.userid=m.id')
  385. // ->where($this->_beforeGetPayParams())
  386. // ->order('p.id desc')
  387. // ->fetchSql(true)
  388. // ->select();
  389. $condition = $this->_beforeGetPayParams();
  390. $cp_status = $this->request->param('cp_status');
  391. $where = [];
  392. if (is_numeric($cp_status)) {
  393. $where['y.status'] = (int)$cp_status;
  394. }
  395. $sql = $payModel->alias('p')
  396. ->join('nw_complex_channel c', 'p.channel_id=c.id')
  397. ->join('cy_paycpinfo y', 'p.orderid=y.orderid')
  398. ->join('nw_complex_members ncm', 'p.userid=ncm.id')
  399. ->field('p.orderid,p.sub_orderid,p.gameid,p.username,p.userid,p.amount,p.status,p.create_time,p.attach,c.name channel_name,y.status as cp_status,p.roleid,p.rolename,ncm.reg_time,p.pay_amount')
  400. ->where($where)
  401. ->where($condition)
  402. ->order('p.id desc')
  403. ->fetchSql(true)
  404. ->select();
  405. if ( (new MakeReportGo())->addTask('admin.complexPayList', $sql, session_id())){
  406. $this->success('报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  407. }
  408. $this->error('报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  409. } else {
  410. $this->error('非法请求');
  411. }
  412. }
  413. /**
  414. * 前置方法(参数获取)
  415. *
  416. * @return $condition array 查询条件
  417. */
  418. protected function _beforeGetPayParams()
  419. {
  420. $start = $this->request->param('start');
  421. $end = $this->request->param('end');
  422. $channel_id = $this->request->param('channel_id', 0, 'intval');
  423. $gameid = $this->request->param('gameid', 0, 'intval');
  424. $status = $this->request->param('status');
  425. $orderid = $this->request->param('orderid', '', 'trim');
  426. $sub_orderid = $this->request->param('sub_orderid', '', 'trim');
  427. $username = $this->request->param('username', '', 'trim');
  428. $rolename = $this->request->param('rolename', '', 'trim');
  429. $roleid = $this->request->param('roleid', '', 'trim');
  430. $company_id = $this->request->param('company_id', 0, 'intval');
  431. $condition = [];
  432. // 如果选择了公司,则使用公司绑定的游戏ID列表
  433. if (!empty($company_id)) {
  434. $companyGameBind = new CompanyGameBind();
  435. $companyInfo = $companyGameBind->where(['id' => $company_id])->find();
  436. if (!empty($companyInfo) && !empty($companyInfo['game_ids'])) {
  437. $gameIds = explode(',', $companyInfo['game_ids']);
  438. $gameIds = array_filter($gameIds);
  439. if (!empty($gameIds)) {
  440. if (count($gameIds) > 1) {
  441. $condition['p.gameid'] = ['in', $gameIds];
  442. } else {
  443. $condition['p.gameid'] = $gameIds[0];
  444. }
  445. } else {
  446. // 如果公司没有绑定游戏,设置一个不存在的ID,使查询结果为空
  447. $condition['p.gameid'] = -1;
  448. }
  449. } else {
  450. // 如果公司不存在或没有绑定游戏,设置一个不存在的ID
  451. $condition['p.gameid'] = -1;
  452. }
  453. } elseif (!empty($gameid)) {
  454. // 如果没有选择公司,则使用原来的游戏ID搜索逻辑
  455. $condition['p.gameid'] = $gameid;
  456. }
  457. if (!empty($orderid)) {
  458. $condition['p.orderid'] = $orderid;
  459. }
  460. if (!empty($sub_orderid)) {
  461. $condition['p.sub_orderid'] = $sub_orderid;
  462. }
  463. if (is_numeric($status)) {
  464. $condition['p.status'] = (int)$status;
  465. }
  466. if (!empty($username)) {
  467. $condition['p.username'] = $username;
  468. }
  469. if (!empty($channel_id)) {
  470. $condition['p.channel_id'] = $channel_id;
  471. }
  472. //开始时间和结束时间不为空时
  473. if ($start != '' && $end != '') {
  474. $condition['p.create_time'] = [
  475. ['>=', strtotime($start)],
  476. ['<=', strtotime($end . ' 23:59:59')],
  477. ];
  478. } //开始时间不为空时
  479. elseif ($start != '') {
  480. $condition['p.create_time'] = ['>=', strtotime($start)];
  481. } //结束时间不为空时
  482. elseif ($end != '') {
  483. $condition['p.create_time'] = ['<=', strtotime($end . ' 23:59:59')];
  484. } else {
  485. $start = date('Y-m-d', strtotime('-30 days'));
  486. $end = date('Y-m-d', time());
  487. $condition['p.create_time'] = [
  488. ['>=', strtotime($start)],
  489. ['<=', strtotime($end . ' 23:59:59')],
  490. ];
  491. }
  492. $this->pay_start_time = $start;
  493. $this->pay_end_time = $end;
  494. if ($rolename != '') {
  495. $condition['p.rolename'] = ['like', "%".$rolename."%"];
  496. }
  497. if ($roleid != '') {
  498. $condition['p.roleid'] = $roleid;
  499. }
  500. // 游戏上线状态
  501. $cooperation_status = $this->request->param('cooperation_status', 0, 'intval'); // 游戏上线状态
  502. if(!empty($cooperation_status)){
  503. $game_cooperation_status = $cooperation_status==1?1:2;
  504. $gameIds = Model('Game')->getNormalByIds($game_cooperation_status);
  505. $condition['p.gameid'] = ['in', $gameIds];
  506. }
  507. return $condition;
  508. }
  509. /**
  510. * ajax 获取cp订单号
  511. */
  512. public function showAttach(){
  513. $order_id = input('post.orderid', '', 'filterAndTrimInput'); // 订单号
  514. $this->_showAttach($order_id);
  515. }
  516. /**
  517. * 查看订单的attach信息
  518. * @param $orderid string 订单号
  519. */
  520. private function _showAttach($orderid) {
  521. $runnable = true;
  522. $msg = '';
  523. $status = false;
  524. if(empty($orderid)) {
  525. $runnable = false;
  526. $msg = '参数错误';
  527. }
  528. if($runnable) {
  529. $result = model('ComplexPay')
  530. ->where(['orderid'=>$orderid])
  531. ->field('attach')
  532. ->find();
  533. if(empty($result)) {
  534. $runnable = false;
  535. $msg = '订单不存在';
  536. }
  537. }
  538. // 查看attach
  539. if($runnable) {
  540. $status = 1;
  541. $result['attach'] = 'attach='.$result['attach'];
  542. parse_str($result['attach'],$arr);
  543. $msg = $arr['attach']; // 变量由上一个函数解析出来的
  544. }
  545. echo json_encode(['code'=>$status,'msg'=>$msg]);
  546. }
  547. /*
  548. * 聚合用户充值列表
  549. */
  550. public function rechargeList()
  551. {
  552. $condition = $this->_rechargePay();
  553. $payModel = new ComplexPay;
  554. $amountSort = strtolower($this->request->param('amount_sort', 'desc'));
  555. $amountSort = in_array($amountSort, ['asc', 'desc']) ? $amountSort : 'desc';
  556. $query = $payModel->alias('p')
  557. ->field('p.userid AS userid,SUM(p.pay_amount) AS amount,COUNT(1) AS frequency,MAX(p.create_time) AS last_pay_time')
  558. ->where($condition)
  559. ->where('p.status', 1)
  560. ->group('p.userid')
  561. ->order('amount ' . $amountSort);
  562. $list = $query->paginate(25, false, ['query' => input('get.')]);
  563. $listData = $list->getCollection()->toArray();
  564. if (!empty($listData)) {
  565. $userIdArr = array_column($listData, 'userid');
  566. $latestPays = $payModel->alias('latest')
  567. ->field('latest.userid,latest.pay_amount,latest.username')
  568. ->where($condition)
  569. ->where('latest.status', 1)
  570. ->whereIn('latest.userid', $userIdArr)
  571. ->order('latest.create_time desc')
  572. ->select();
  573. if ($latestPays instanceof \think\Collection) {
  574. $latestPays = $latestPays->toArray();
  575. } elseif (is_object($latestPays) && method_exists($latestPays, 'toArray')) {
  576. $latestPays = $latestPays->toArray();
  577. } elseif (empty($latestPays)) {
  578. $latestPays = [];
  579. }
  580. $latestMap = [];
  581. foreach ($latestPays as $record) {
  582. $uid = $record['userid'];
  583. if (!isset($latestMap[$uid])) {
  584. $latestMap[$uid] = [
  585. 'amount' => $record['pay_amount'],
  586. 'username' => $record['username'],
  587. ];
  588. }
  589. }
  590. foreach ($listData as $key => $item) {
  591. $uid = $item['userid'];
  592. $listData[$key]['ramount'] = isset($latestMap[$uid]) ? $latestMap[$uid]['amount'] : 0;
  593. $listData[$key]['username'] = isset($latestMap[$uid]) ? $latestMap[$uid]['username'] : '';
  594. }
  595. }
  596. $this->assign('list', $listData);
  597. $this->assign('total', $list->total());
  598. $this->assign('page', $list->render());
  599. $this->assign('amount_sort', $amountSort);
  600. $gameList = model('Common/Game')->getAllByCondition('id,name',[],'','self');
  601. $selfGameList = array();
  602. foreach ($gameList as $game) {
  603. $selfGameList[ $game['id']] = $game;
  604. }
  605. $this->assign('game_list', $selfGameList);
  606. $rawSelectedGameIds = $this->request->param('gameids', $this->request->param('game_ids', ''));
  607. $selectedGameIdArr = $this->normalizeGameIdsParam($rawSelectedGameIds);
  608. $this->assign('selected_game_id_arr', $selectedGameIdArr);
  609. $this->assign('selected_game_ids', implode(',', $selectedGameIdArr));
  610. return $this->fetch('recharge_list');
  611. }
  612. /*
  613. * 聚合用户充值查询
  614. */
  615. protected function _rechargePay()
  616. {
  617. $start = $this->request->param('start');
  618. $end = $this->request->param('end');
  619. $username = $this->request->param('username','','trim');
  620. $gameid = $this->request->param('gameid',0,'trim');
  621. $gameidsParam = $this->request->param('gameids', $this->request->param('game_ids', ''));
  622. $condition = [];
  623. $gameIdList = $this->normalizeGameIdsParam($gameidsParam);
  624. if (!empty($gameIdList)) {
  625. $condition['gameid'] = ['in', $gameIdList];
  626. } elseif (!empty($gameid)){
  627. $condition['gameid'] = $gameid;
  628. }
  629. if (!empty($username)){
  630. $condition['username'] = $username;
  631. }
  632. //开始时间和结束时间不为空时
  633. if ($start != '' && $end != '') {
  634. $condition['create_time'] = [
  635. ['>=', strtotime($start)],
  636. ['<=', strtotime($end . ' 23:59:59')],
  637. ];
  638. } //开始时间不为空时
  639. elseif ($start != '') {
  640. $condition['create_time'] = ['>=', strtotime($start)];
  641. } //结束时间不为空时
  642. elseif ($end != '') {
  643. $condition['create_time'] = ['<=', strtotime($end . ' 23:59:59')];
  644. } else {
  645. /*
  646. $start = date('Y-m-d', time());
  647. $end = date('Y-m-d', time());
  648. $condition['create_time'] = [
  649. ['>=', strtotime($start)],
  650. ['<=', strtotime($end . ' 23:59:59')],
  651. ];
  652. */
  653. }
  654. return $condition;
  655. }
  656. /**
  657. * 解析多选游戏参数
  658. * @param mixed $param
  659. * @return array
  660. */
  661. protected function normalizeGameIdsParam($param)
  662. {
  663. if (is_array($param)) {
  664. $ids = $param;
  665. } else {
  666. $param = trim((string)$param);
  667. if ($param === '') {
  668. return [];
  669. }
  670. $ids = explode(',', $param);
  671. }
  672. $ids = array_map('intval', $ids);
  673. $ids = array_filter($ids, function ($id) {
  674. return $id > 0;
  675. });
  676. return array_values(array_unique($ids));
  677. }
  678. /**
  679. * 查看聚合渠道ID/账号
  680. */
  681. public function showUsername()
  682. {
  683. $userid = input('userid');
  684. if(empty($userid)){
  685. $this->error('用户ID不能为空');
  686. }
  687. $username= model('ComplexMembers')->where(['id'=>$userid])->value('complex_username');
  688. if(!empty($username)){
  689. $this->insertLog($this->current_node,'查看账号:'.$username,57);
  690. $this->result($username,1);
  691. }
  692. else{
  693. $this->error('用户信息不存在');
  694. }
  695. }
  696. /**
  697. * 渠道玩家活跃记录
  698. */
  699. public function playerActive()
  700. {
  701. $filters = $this->getPlayerActiveFilters();
  702. $where = $this->getPlayerActiveCondition($filters);
  703. $list = $this->getPlayerActiveQuery($where, $filters)
  704. ->order('a.active_hour desc,a.id desc')
  705. ->paginate(20, false, ['query' => input('get.')]);
  706. $gameList = model('Common/Game')->getAllByCondition('id,name', [], '', 'self');
  707. $complexList = (new ComplexChannelModel())
  708. ->getAllByCondition('id,name', [], 'name asc');
  709. $serverList = $this->getPlayerActiveServerOptions(
  710. $filters['game_id'],
  711. $filters['complex_id']
  712. );
  713. $this->hydratePlayerActiveList($list, $gameList, $complexList);
  714. $this->assign('list', $list);
  715. $this->assign('total', $list->total());
  716. $this->assign('page', $list->render());
  717. $this->assign('filters', $filters);
  718. $this->assign('gameList', $gameList);
  719. $this->assign('complexList', $complexList);
  720. $this->assign('serverList', $serverList);
  721. return $this->fetch('player_active');
  722. }
  723. /**
  724. * 根据渠道和游戏获取渠道玩家活跃页的区服选项。
  725. */
  726. public function getPlayerActiveServerList()
  727. {
  728. $gameId = input('get.game_id', 0, 'intval');
  729. $complexId = input('get.complex_id', 0, 'intval');
  730. $this->success('', '', $this->getPlayerActiveServerOptions($gameId, $complexId));
  731. }
  732. /**
  733. * 查询指定渠道、游戏的区服,任一条件未选择时不返回数据。
  734. */
  735. private function getPlayerActiveServerOptions($gameId, $complexId)
  736. {
  737. $gameId = intval($gameId);
  738. $complexId = intval($complexId);
  739. if ($gameId <= 0 || $complexId <= 0) {
  740. return [];
  741. }
  742. return Db::table('nw_complex_server')
  743. ->cache('complexPlayerActive:serverList:' . $complexId . ':' . $gameId, 60)
  744. ->where(['game_id' => $gameId, 'complex_id' => $complexId])
  745. ->field('id,game_id,server_id,complex_id,server_name as name')
  746. ->order('id desc')
  747. ->select();
  748. }
  749. /**
  750. * 渠道玩家活跃记录导出。
  751. */
  752. public function playerActiveDownload()
  753. {
  754. if (!request()->isAjax()) {
  755. $this->error('非法请求');
  756. }
  757. $filters = $this->getPlayerActiveFilters();
  758. $where = $this->getPlayerActiveCondition($filters);
  759. $sql = $this->getPlayerActiveQuery($where, $filters, true)
  760. ->order('a.active_hour desc,a.id desc')
  761. ->fetchSql(true)
  762. ->select();
  763. if ((new MakeReportGo())->addTask('complexPlayerActiveList', $sql, session_id())) {
  764. $this->success('报表生成的任务已经提交,报表生成完成后会及时通知您,请耐心等待');
  765. }
  766. $this->error('报表生成任务提交失败,请稍后重试');
  767. }
  768. /**
  769. * 获取渠道玩家活跃查询参数
  770. */
  771. private function getPlayerActiveFilters()
  772. {
  773. $activeStartTime = input('request.active_start_time', '', 'trim');
  774. $activeEndTime = input('request.active_end_time', '', 'trim');
  775. $mgUsername = input('request.mg_username', '', 'trim');
  776. if ($mgUsername === '') {
  777. // 兼容修复前生成的查询链接。
  778. $mgUsername = input('request.complex_username', '', 'trim');
  779. }
  780. // 活跃表数据量会持续增长,无时间条件时默认查询当天。
  781. if ($activeStartTime === '' && $activeEndTime === '') {
  782. $activeStartTime = $activeEndTime = date('Y-m-d');
  783. }
  784. $filters = [
  785. 'mg_username' => $mgUsername,
  786. 'game_id' => input('request.game_id', 0, 'intval'),
  787. 'complex_id' => input('request.complex_id', 0, 'intval'),
  788. 'server_id' => input('request.server_id', '', 'trim'),
  789. 'role_name' => input('request.role_name', '', 'trim'),
  790. 'role_id' => input('request.role_id', '', 'trim'),
  791. 'create_start_time' => input('request.create_start_time', '', 'trim'),
  792. 'create_end_time' => input('request.create_end_time', '', 'trim'),
  793. 'active_start_time' => $activeStartTime,
  794. 'active_end_time' => $activeEndTime,
  795. 'register_start_time' => input('request.register_start_time', '', 'trim'),
  796. 'register_end_time' => input('request.register_end_time', '', 'trim'),
  797. ];
  798. $this->validatePlayerActiveTimeRanges($filters);
  799. return $filters;
  800. }
  801. /**
  802. * 校验渠道玩家活跃查询中的时间范围。
  803. */
  804. private function validatePlayerActiveTimeRanges(array $filters)
  805. {
  806. $ranges = [
  807. '角色创建时间' => ['create_start_time', 'create_end_time'],
  808. '账号注册时间' => ['register_start_time', 'register_end_time'],
  809. '活跃时间' => ['active_start_time', 'active_end_time'],
  810. ];
  811. foreach ($ranges as $label => $fields) {
  812. $startTime = $filters[$fields[0]];
  813. $endTime = $filters[$fields[1]];
  814. if ($startTime !== '' && $endTime !== '' && strtotime($startTime) > strtotime($endTime)) {
  815. $this->error($label . ':开始时间 不能大于 结束时间');
  816. }
  817. }
  818. }
  819. /**
  820. * 组装渠道玩家活跃查询条件
  821. */
  822. private function getPlayerActiveCondition(array $filters)
  823. {
  824. $where = [];
  825. if ($filters['game_id'] > 0) {
  826. $where['a.game_id'] = $filters['game_id'];
  827. }
  828. if ($filters['complex_id'] > 0) {
  829. $where['a.complex_id'] = $filters['complex_id'];
  830. }
  831. if ($filters['server_id'] !== '') {
  832. $where['a.server_id'] = $filters['server_id'];
  833. }
  834. if ($filters['role_name'] !== '') {
  835. $where['a.role_name'] = ['like', '%' . $filters['role_name'] . '%'];
  836. }
  837. if ($filters['role_id'] !== '') {
  838. $where['a.role_id'] = $filters['role_id'];
  839. }
  840. $this->appendPlayerActiveTimeCondition(
  841. $where,
  842. 'a.active_hour',
  843. $filters['active_start_time'],
  844. $filters['active_end_time'],
  845. false
  846. );
  847. $this->appendPlayerAccountCondition($where, $filters);
  848. return $where;
  849. }
  850. /**
  851. * 玩家账号先查询成员表,再使用 member_id 查询活跃主表。
  852. */
  853. private function appendPlayerAccountCondition(array &$where, array $filters)
  854. {
  855. if ($filters['mg_username'] === '') {
  856. return;
  857. }
  858. $memberWhere = ['mg_username' => $filters['mg_username']];
  859. if ($filters['game_id'] > 0) {
  860. $memberWhere['gameid'] = $filters['game_id'];
  861. }
  862. if ($filters['complex_id'] > 0) {
  863. $memberWhere['channel_id'] = $filters['complex_id'];
  864. }
  865. $this->appendPlayerActiveTimeCondition(
  866. $memberWhere,
  867. 'reg_time',
  868. $filters['register_start_time'],
  869. $filters['register_end_time']
  870. );
  871. $memberIds = Db::table('nw_complex_members')
  872. ->where($memberWhere)
  873. ->column('id');
  874. $memberIds = array_values(array_unique(array_map('intval', $memberIds)));
  875. if (empty($memberIds)) {
  876. $where['a.member_id'] = -1;
  877. } elseif (count($memberIds) === 1) {
  878. $where['a.member_id'] = $memberIds[0];
  879. } else {
  880. $where['a.member_id'] = ['in', $memberIds];
  881. }
  882. }
  883. /**
  884. * 添加时间范围条件。角色和账号时间为时间戳,活跃时间为 DATETIME。
  885. */
  886. private function appendPlayerActiveTimeCondition(
  887. array &$where,
  888. $field,
  889. $startTime,
  890. $endTime,
  891. $unixTime = true
  892. ) {
  893. if ($startTime !== '' && $endTime !== '') {
  894. $start = $unixTime ? strtotime($startTime) : $startTime . ' 00:00:00';
  895. $end = $unixTime ? strtotime($endTime . ' 23:59:59') : $endTime . ' 23:59:59';
  896. $where[$field] = [['>=', $start], ['<=', $end]];
  897. } elseif ($startTime !== '') {
  898. $where[$field] = ['>=', $unixTime ? strtotime($startTime) : $startTime . ' 00:00:00'];
  899. } elseif ($endTime !== '') {
  900. $where[$field] = ['<=', $unixTime ? strtotime($endTime . ' 23:59:59') : $endTime . ' 23:59:59'];
  901. }
  902. }
  903. /**
  904. * 渠道玩家活跃查询。角色创建时间取同一角色的首次创建时间。
  905. */
  906. private function getPlayerActiveQuery(array $where, array $filters, $forExport = false)
  907. {
  908. $query = Db::table('nw_complex_player_active_log')->alias('a')
  909. ->where($where);
  910. if ($forExport) {
  911. // a.server_id 存的是 nw_complex_server.id(主键)。
  912. // 角色创建时间与列表 hydrate 一致:按 member+complex+game+role_id 取首次创建时间,
  913. // 不强制同区服(避免角色只在其他区服有记录时导出为空);用分组子查询避免多区服打成多行。
  914. $roleLatestSql = Db::table('nw_complex_role')
  915. ->field('member_id,complex_id,game_id,role_id,MIN(create_time) as create_time')
  916. ->group('member_id,complex_id,game_id,role_id')
  917. ->buildSql();
  918. $query
  919. ->join('nw_complex_members member', 'member.id=a.member_id', 'left')
  920. ->join('cy_game game', 'game.id=a.game_id', 'left')
  921. ->join('nw_complex_channel complex', 'complex.id=a.complex_id', 'left')
  922. ->join(
  923. 'nw_complex_server server',
  924. 'server.id=a.server_id',
  925. 'left'
  926. )
  927. ->join(
  928. [$roleLatestSql => 'role'],
  929. 'role.member_id=a.member_id AND role.complex_id=a.complex_id AND role.game_id=a.game_id AND role.role_id=a.role_id',
  930. 'left'
  931. )
  932. ->field('member.complex_username,game.name as game_name,complex.name as complex_name,COALESCE(server.server_name,a.server_id) as server_name,a.role_name,a.active_hour,role.create_time as role_create_time,member.reg_time');
  933. } else {
  934. $query->field('a.*');
  935. }
  936. // 玩家账号已在分页前查询成员ID,此时注册时间也已经一并筛选。
  937. if ($filters['mg_username'] === '') {
  938. $query = $this->appendMemberRegisterTimeCondition(
  939. $query,
  940. $filters['register_start_time'],
  941. $filters['register_end_time']
  942. );
  943. }
  944. return $this->appendRoleCreateTimeCondition(
  945. $query,
  946. $filters['create_start_time'],
  947. $filters['create_end_time']
  948. );
  949. }
  950. /**
  951. * 只有使用注册时间筛选时,才按主键关联成员表进行过滤。
  952. */
  953. private function appendMemberRegisterTimeCondition($query, $startTime, $endTime)
  954. {
  955. if ($startTime === '' && $endTime === '') {
  956. return $query;
  957. }
  958. return $query->whereExists(function ($memberQuery) use ($startTime, $endTime) {
  959. $memberQuery->table('nw_complex_members')
  960. ->alias('member_filter')
  961. ->field('1')
  962. ->whereExp('member_filter.id', '= a.member_id');
  963. if ($startTime !== '') {
  964. $memberQuery->where(
  965. 'member_filter.reg_time',
  966. '>=',
  967. intval(strtotime($startTime))
  968. );
  969. }
  970. if ($endTime !== '') {
  971. $memberQuery->where(
  972. 'member_filter.reg_time',
  973. '<=',
  974. intval(strtotime($endTime . ' 23:59:59'))
  975. );
  976. }
  977. });
  978. }
  979. /**
  980. * 按用户、渠道、游戏、角色筛选角色创建时间。
  981. */
  982. private function appendRoleCreateTimeCondition($query, $startTime, $endTime)
  983. {
  984. if ($startTime === '' && $endTime === '') {
  985. return $query;
  986. }
  987. return $query->whereExists(function ($roleQuery) use ($startTime, $endTime) {
  988. $roleQuery->table('nw_complex_role')
  989. ->alias('role_filter')
  990. ->field('1')
  991. ->whereExp('role_filter.member_id', '= a.member_id')
  992. ->whereExp('role_filter.complex_id', '= a.complex_id')
  993. ->whereExp('role_filter.game_id', '= a.game_id')
  994. ->whereExp('role_filter.role_id', '= a.role_id');
  995. if ($startTime !== '') {
  996. $roleQuery->where(
  997. 'role_filter.create_time',
  998. '>=',
  999. intval(strtotime($startTime))
  1000. );
  1001. }
  1002. if ($endTime !== '') {
  1003. $roleQuery->where(
  1004. 'role_filter.create_time',
  1005. '<=',
  1006. intval(strtotime($endTime . ' 23:59:59'))
  1007. );
  1008. }
  1009. });
  1010. }
  1011. /**
  1012. * 分页完成后,仅批量查询当前页所需的关联数据并合并到列表。
  1013. */
  1014. private function hydratePlayerActiveList($list, $gameList, $complexList)
  1015. {
  1016. $items = $list->getCollection();
  1017. if ($items->isEmpty()) {
  1018. return;
  1019. }
  1020. $memberIds = [];
  1021. $complexIds = [];
  1022. $gameIds = [];
  1023. $serverIds = [];
  1024. $roleIds = [];
  1025. foreach ($items as $item) {
  1026. $memberIds[] = intval($item['member_id']);
  1027. $complexIds[] = intval($item['complex_id']);
  1028. $gameIds[] = intval($item['game_id']);
  1029. $serverIds[] = $item['server_id'];
  1030. $roleIds[] = (string)$item['role_id'];
  1031. }
  1032. $memberIds = array_values(array_unique($memberIds));
  1033. $complexIds = array_values(array_unique($complexIds));
  1034. $gameIds = array_values(array_unique($gameIds));
  1035. $serverIds = array_values(array_unique($serverIds));
  1036. $roleIds = array_values(array_unique($roleIds));
  1037. $memberList = Db::table('nw_complex_members')
  1038. ->where('id', 'in', $memberIds)
  1039. ->field('id,mg_username,complex_username,reg_time')
  1040. ->select();
  1041. // dump($memberIds, $memberList);
  1042. $memberById = array_column($memberList, null, 'id');
  1043. // $memberById = [];
  1044. // foreach ($memberList as $member) {
  1045. // $memberById[$member['id']] = $member;
  1046. // }
  1047. $currentServerList = Db::table('nw_complex_server')
  1048. ->where('id', 'in', $serverIds)
  1049. ->field('id,server_id,server_name')
  1050. ->select();
  1051. $serverById = [];
  1052. foreach ($currentServerList as $server) {
  1053. $serverById[$server['id']] = $server;
  1054. }
  1055. $gameById = array_column($gameList, 'name', 'id');
  1056. $complexById = array_column($complexList, 'name', 'id');
  1057. $roleCreateByKey = $this->getPlayerActiveRoleCreateMap(
  1058. $memberIds,
  1059. $complexIds,
  1060. $gameIds,
  1061. $roleIds
  1062. );
  1063. foreach ($items as $key => $item) {
  1064. $member = $memberById[$item['member_id']] ?? [];
  1065. $item['mg_username'] = $member['mg_username'] ?? '';
  1066. $item['reg_time'] = $member['reg_time'] ?? 0;
  1067. $item['game_name'] = $gameById[$item['game_id']] ?? '';
  1068. $item['complex_name'] = $complexById[$item['complex_id']] ?? '';
  1069. $server = $serverById[$item['server_id']] ?? [];
  1070. $item['complex_server_id'] = $server['server_id'] ?? '';
  1071. $item['server_name'] = $server['server_name'] ?? '';
  1072. $item['role_create_time'] = $roleCreateByKey[$this->buildPlayerActiveRoleKey($item)] ?? 0;
  1073. $items->offsetSet($key, $item);
  1074. }
  1075. }
  1076. /**
  1077. * 根据当前页用户、渠道、游戏、角色批量查询角色创建时间。
  1078. */
  1079. private function getPlayerActiveRoleCreateMap(
  1080. array $memberIds,
  1081. array $complexIds,
  1082. array $gameIds,
  1083. array $roleIds
  1084. ) {
  1085. if (empty($memberIds) || empty($complexIds) || empty($gameIds) || empty($roleIds)) {
  1086. return [];
  1087. }
  1088. // 与导出一致:同角色多区服时取首次创建时间。
  1089. $roleList = Db::table('nw_complex_role')
  1090. ->where('member_id', 'in', $memberIds)
  1091. ->where('complex_id', 'in', $complexIds)
  1092. ->where('game_id', 'in', $gameIds)
  1093. ->where('role_id', 'in', $roleIds)
  1094. ->field('member_id,complex_id,game_id,role_id,MIN(create_time) as create_time')
  1095. ->group('member_id,complex_id,game_id,role_id')
  1096. ->select();
  1097. $roleCreateByKey = [];
  1098. foreach ($roleList as $role) {
  1099. $roleKey = $this->buildPlayerActiveRoleKey($role);
  1100. $roleCreateByKey[$roleKey] = $role['create_time'];
  1101. }
  1102. return $roleCreateByKey;
  1103. }
  1104. private function buildPlayerActiveRoleKey(array $item)
  1105. {
  1106. return json_encode([
  1107. intval($item['member_id']),
  1108. intval($item['complex_id']),
  1109. intval($item['game_id']),
  1110. (string)$item['role_id'],
  1111. ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  1112. }
  1113. }