CpDataLogic.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * DayPayLogic.php UTF-8
  4. *
  5. *
  6. * @date : 2017/12/18 11:42
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : liguanglong <lgl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\data;
  13. use huo\model\common\CommonModel;
  14. use huo\model\data\DayAgentGameCountryModel;
  15. use huo\model\data\DayAgentGameCountryVipModel;
  16. use huo\model\game\GameModel;
  17. use huo\logic\user\UserLogic;
  18. class CpDataLogic extends CommonModel {
  19. protected $_id;
  20. protected $_agent_id;
  21. protected $_app_id;
  22. protected $_cp_id;
  23. public function __construct($data = []) {
  24. parent::__construct($data);
  25. List($this->_id, $this->_cp_id, $this->_agent_id, $this->_app_id)
  26. = (new UserLogic())->getRoleLevelID(session('ADMIN_ID'));
  27. }
  28. /**
  29. * cp对账单
  30. * @param null $date
  31. * @param null $cp_id
  32. *
  33. * @return array
  34. * @throws \think\exception\DbException
  35. */
  36. public function getBalanceIndex($yearMonth = null, $cp_id = null){
  37. $_model = new DayAgentGameCountryVipModel();
  38. $_fields = <<<EOF
  39. id, app_id, date, sum(vip_day_cnt) vip_day_cnt, sum(vip_day_money) vip_day_money,
  40. sum(vip_down_cnt) vip_down_cnt, sum(vip_reg_cnt) vip_reg_cnt, sum(vip_sum_money) vip_sum_money,
  41. sum(vip_user_cnt) vip_user_cnt, sum(vip_free_cnt) vip_free_cnt
  42. EOF;
  43. $_model = $_model->field($_fields);
  44. if($yearMonth){
  45. $_model = $_model->where('left(date, 7) = "'.$yearMonth.'"' );
  46. }
  47. if($cp_id){
  48. $_game_model = new GameModel();
  49. $_app_id = $_game_model->where('cp_id', $cp_id)->column('id');
  50. $_model = $_model->whereIn('app_id', $_app_id);
  51. }
  52. $_model = $_model->whereIn('app_id', $this->_app_id)
  53. ->whereIn('agent_id', $this->_agent_id)
  54. ->with('game')
  55. ->group('app_id')
  56. ->paginate(10);
  57. $_page = $_model->render();
  58. $_data = $_model->toArray()['data'];
  59. $this->getBalanceScope($_data);
  60. $this->handleBalanceData($_data);
  61. return [$_data, $_page];
  62. }
  63. /**
  64. * 封装
  65. * @param $data
  66. */
  67. private function handleBalanceData(&$data){
  68. if(count($data)){
  69. foreach ($data as &$val){
  70. /*游戏名*/
  71. $val['game_name'] = null;
  72. /*下载占比*/
  73. $val['vip_down_cnt_percent'] =
  74. number_format($val['vip_down_cnt'] / $val['vip_day_cnt'], 2) * 100 .'%';
  75. /*vip订阅数*/
  76. $val['vip_sub_cnt'] = $val['vip_user_cnt'] + $val['vip_free_cnt'];
  77. if(!empty($val['game'])){
  78. $val['game_name'] = $val['game']['name'];
  79. unset($val['game']);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * cp附加数据
  86. * @param $data
  87. *
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. private function getBalanceScope(&$data){
  93. if(!empty($data)){
  94. $_model = new DayAgentGameCountryModel();
  95. $_fields = <<<EOF
  96. id, app_id, date, sum(user_cnt) user_cnt
  97. EOF;
  98. $_model = $_model->field($_fields);
  99. $_model = $_model->where('left(date, 7) = "'.substr($data[0]['date'], 0, 7).'"')
  100. ->whereIn('app_id', array_column($data, 'app_id'))
  101. ->group('app_id')
  102. ->select()
  103. ->toArray();
  104. foreach ($data as &$val){
  105. /*活跃玩家数*/
  106. $val['scope_user_cnt'] = 0;
  107. if(count($_model)){
  108. foreach ($_model as $vale){
  109. if($vale['date'] == $val['date'] && $vale['app_id'] == $val['app_id']){
  110. $val['scope_user_cnt'] = $vale['user_cnt'];
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }