AbsLogic.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. /**
  3. * AbsLogic.php UTF-8
  4. * huosdk_v8
  5. *
  6. * @date : 2018/7/31 23:05
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : guxiannong<gxn@huosdk.com>
  10. * @version : HUSDK 8.0
  11. */
  12. namespace huo\logic\abs;
  13. use huo\model\common\CommonModel;
  14. use think\Log;
  15. class AbsLogic extends CommonModel {
  16. public function getSubAgentList(
  17. $where, $is_today, $platform_id, $start, $end, $app_id, $agent_id, $plan_id, $landing_page_id, $son_agent_id, $page,
  18. $list_rows
  19. ) {
  20. }
  21. public function subAgent() {
  22. $this->genUsers();
  23. $this->_game();
  24. if (3 == $this->role_type) {
  25. $this->where = " pp.owner_id".$this->agentwhere;
  26. } else {
  27. // $this->where = " 1 ";
  28. $_agent_ids = implode(',', $this->buy_agents);
  29. $this->where = " pp.owner_id in (".$_agent_ids.") ";
  30. }
  31. $this->_getSubAgent();
  32. $this->display();
  33. }
  34. public function _getSubAgent() {
  35. $_is_today = I('is_today', '今日');
  36. if ('今日' == $_is_today) {
  37. $this->_initTodayData();
  38. $_day_model = M('tmp_day_promotion_game');
  39. $_table = 'c_tmp_day_promotion_game';
  40. } else {
  41. $_day_model = M('day_promotion_game');
  42. $_table = 'c_day_promotion_game';
  43. }
  44. $platform_id = I('platform_id');
  45. $start = I('start_time');
  46. $end = I('end_time');
  47. $app_id = I('app_id');
  48. $agent_id = I('agent_id/d', 0);
  49. $son_agent_id = I('son_agent_id/d', 0);
  50. $plan_id = I('plan_id/d', '');
  51. $material_id = I('material_id/d', '');
  52. $landing_page_id = I('landing_page_id/d', '');
  53. /* 导出参数 */
  54. $_csv_do = I('csv_do', 1);
  55. $_csv_page = I('csv_page', 1);
  56. $_csv_offset = I('csv_offset', 1000);
  57. $_csv_file = I('csv_file', '');
  58. $_explore_csv_class = new \Huosdk\Data\ExportCsv();
  59. $where = $this->where;
  60. $sum_where = [];
  61. if (isset($platform_id) && !empty($platform_id)) {
  62. $where .= " AND pp.platform_id = '".$platform_id."'";
  63. }
  64. if (isset($start) && !empty($start)) {
  65. $where .= " AND d.date >= '".$start."'";
  66. }
  67. if (isset($end) && !empty($end)) {
  68. $where .= " AND d.date <= '".$end."'";
  69. }
  70. if (isset($agent_id) && !empty($agent_id)) {
  71. $where .= " AND pp.owner_id=".$agent_id;
  72. }
  73. if (isset($son_agent_id) && !empty($son_agent_id)) {
  74. $where .= " AND pd.agent_id=".$son_agent_id;
  75. }
  76. if (isset($plan_id) && !empty($plan_id)) {
  77. $where .= " AND pd.promotion_plan_id=".$plan_id;
  78. }
  79. if (isset($material_id) && !empty($material_id)) {
  80. $landing_page_ids = M('game_landing_page')->where(['material_id' => $material_id])->getField('id', true);
  81. $idstr = implode(',', $landing_page_ids);
  82. $where .= " AND glp.id in ('".$idstr."') ";
  83. }
  84. if (isset($app_id) && !empty($app_id)) {
  85. $where .= " AND d.app_id=".$app_id;
  86. }
  87. if (isset($landing_page_id) && !empty($landing_page_id)) {
  88. $where .= " AND glp.id = '".$landing_page_id."'";
  89. }
  90. $count_sql
  91. = "SELECT count(*) as tp_count FROM (SELECT d.id FROM {$_table} d
  92. LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id and pd.app_id=d.app_id
  93. LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id
  94. LEFT JOIN ".C('DB_PREFIX')."game_landing_page glp ON glp.id=pd.landing_page_id
  95. WHERE {$where} GROUP BY d.date,d.app_id,pp.owner_id,pp.platform_id) taba";
  96. $count_info = M()->query($count_sql);
  97. $count = isset($count_info[0]['tp_count']) ? $count_info[0]['tp_count'] : 0;
  98. // $count = $_day_model
  99. // ->alias('d')
  100. // ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id")
  101. // ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id")
  102. // ->join("LEFT JOIN ".C('DB_PREFIX')."game_landing_page glp ON glp.id=pd.landing_page_id")
  103. // ->where($where)
  104. // ->count();
  105. if (2 == $_csv_do) {
  106. // $_fields = 'd.*,pd.promotion_plan_id,pd.landing_page_id,pp.platform_id,pp.owner_id';
  107. $_fields
  108. = "d.date,d.app_id,pp.platform_id,d.agent_id,sum(d.reg_cnt) reg_cnt,sum(d.reg_device) reg_device,sum(d.reg_pay_cnt) reg_pay_cnt,";
  109. $_fields .= "sum(d.sum_reg_money) sum_reg_money,sum(d.user_cnt) user_cnt,sum(d.pay_user_cnt) pay_user_cnt,";
  110. $_fields .= "sum(d.sum_money) sum_money,pp.owner_id,";
  111. $_fields .= 'sum(d.day2) day2,sum(d.day7) day7,sum(d.day15) day15';
  112. $items = $_day_model
  113. ->alias('d')
  114. ->field($_fields)
  115. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id and pd.app_id=d.app_id")
  116. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id")
  117. ->join("LEFT JOIN ".C('DB_PREFIX')."game_landing_page glp ON glp.id=pd.landing_page_id")
  118. ->where($where)
  119. ->order('d.id DESC')
  120. ->page($_csv_page, $_csv_offset)
  121. ->group('d.date,d.app_id,pp.owner_id,pp.platform_id')
  122. ->select();
  123. if (!empty($items) && is_array($items)) {
  124. $_title = '投放数据';
  125. $_expCellName = array('日期', '买量专员', '投放渠道', '游戏名称', '点击数量', '下载数量', '注册', '新增设备', '新增付费数', '新增付费率',
  126. '新增付费金额', '活跃', '付费人数', '付费金额', '活跃付费率', '活跃arpu', '付费arpu', 'ltv 1日', 'ltv 7日',
  127. 'ltv 15日', '留存 1日', '留存 7日', '留存 15日'
  128. );
  129. $_export_data = [];
  130. $agents = M('users')->where(
  131. array('id' => array('in', implode(',', array_unique(array_column($items, 'agent_id')))))
  132. )->getField("id,user_login agentname", true);
  133. $games = M('game')->where(
  134. array('id' => array('in', implode(',', array_unique(array_column($items, 'app_id')))))
  135. )->getField('id,name', true);
  136. $platforms = M('platform')->getField("id,name platform", true);
  137. foreach ($items as $k => $v) {
  138. $_where = " `date`='".$v['date']."' AND `agent_id`='".$v['agent_id']."' AND `app_id`='".$v['app_id']
  139. ."'";
  140. $_ltv = $this->getLtvData($_where);
  141. $_day_map = [
  142. 'agent_id' => $v['agent_id'],
  143. 'date' => $v['date'],
  144. 'app_id' => $v['app_id']
  145. ];
  146. //$_day_log = $this->getPromotionDayLog($_day_map);
  147. $_day_log = $this->getPromotionByowner($v['date'],$v['owner_id'],$v['app_id'],$v['platform_id']);
  148. $_v = [];
  149. $_v['date'] = $v['date'];
  150. $_v['agent_id'] = (isset($agents[$v['owner_id']]) ? $agents[$v['owner_id']] : '官包')."\t";
  151. $_v['platform'] = $platforms[$v['platform_id']];
  152. $_v['game'] = $games[$v['app_id']];
  153. $_v['check_cnt'] = $_day_log['distinct_visit_cnt'];
  154. $_v['down_cnt'] = $_day_log['distinct_down_cnt'];
  155. $_v['reg_cnt'] = $v['reg_cnt'];
  156. $_v['reg_device'] = $v['reg_device'];
  157. $_v['reg_pay_cnt'] = $v['reg_pay_cnt'];
  158. $_v['reg_pay_rate'] = (number_format($v['reg_pay_cnt'] / $v['reg_cnt'], 2, '.', '') * 100).'%';
  159. $_v['sum_reg_money'] = $v['sum_reg_money'];
  160. $_v['user_cnt'] = $v['user_cnt'];
  161. $_v['pay_user_cnt'] = $v['pay_user_cnt'];
  162. $_v['sum_money'] = $v['sum_money'];
  163. $_v['active_pay_rate'] = (number_format($v['pay_user_cnt'] / $v['user_cnt'], 2, '.', '') * 100).'%';
  164. $_v['active_arpu'] = number_format($v['sum_money'] / $v['user_cnt'], 2, '.', '');
  165. $_v['pay_arpu'] = number_format($v['sum_money'] / $v['pay_user_cnt'], 2, '.', '');
  166. $_v['ltv1'] = isset($_ltv[0]['day1']) ? $_ltv[0]['day1'] : 0;
  167. $_v['ltv7'] = isset($_ltv[0]['day7']) ? $_ltv[0]['day7'] : 0;
  168. $_v['ltv15'] = isset($_ltv[0]['day15']) ? $_ltv[0]['day15'] : 0;
  169. $_v['day2'] = $this->_showRegPoint($v['day2'], $v['reg_cnt']);
  170. $_v['day7'] = $this->_showRegPoint($v['day7'], $v['reg_cnt']);
  171. $_v['day15'] = $this->_showRegPoint($v['day15'], $v['reg_cnt']);
  172. $_export_data[] = $_v;
  173. }
  174. $_explore_csv_class->exportCsv(
  175. $_title, $_expCellName, $_export_data, $_csv_file, $count, $_csv_page, $_csv_offset
  176. );
  177. } else {
  178. $_arr['error'] = 0;
  179. $_arr['msg'] = '';
  180. $_arr['data'] = '';
  181. $this->ajaxReturn($_arr);
  182. }
  183. } else {
  184. $rows = isset($_POST['rows']) ? intval($_POST['rows']) : $this->abs_row;
  185. $page = $this->page($count, $rows);
  186. // $_fields = 'd.*,pd.promotion_plan_id,pd.landing_page_id,pp.platform_id,pp.owner_id';
  187. $_fields
  188. = "d.date,d.app_id,pp.platform_id,d.agent_id,sum(d.reg_cnt) reg_cnt,sum(d.reg_device) reg_device,sum(d.reg_pay_cnt) reg_pay_cnt,";
  189. $_fields .= "sum(d.sum_reg_money) sum_reg_money,sum(d.user_cnt) user_cnt,sum(d.pay_user_cnt) pay_user_cnt,";
  190. $_fields .= "sum(d.sum_money) sum_money,pp.owner_id,";
  191. $_fields .= 'sum(d.day2) day2,sum(d.day7) day7,sum(d.day15) day15';
  192. $items = $_day_model
  193. ->alias('d')
  194. ->field($_fields)
  195. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id and pd.app_id=d.app_id")
  196. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id")
  197. ->join("LEFT JOIN ".C('DB_PREFIX')."game_landing_page glp ON glp.id=pd.landing_page_id")
  198. ->where($where)
  199. ->order('d.id DESC')
  200. ->limit($page->firstRow.','.$page->listRows)
  201. ->group('d.date,d.app_id,pp.owner_id,pp.platform_id')
  202. ->select();
  203. $_check_cnt = $_down_cnt = 0;
  204. $_ltv_agent_ids = [];
  205. foreach ($items as $_k => $_v) {
  206. $_ltv_agent_ids[] = $_v['agent_id'];
  207. $_where = " `date`='".$_v['date']."' AND `agent_id`='".$_v['agent_id']."' AND `app_id`='".$_v['app_id']
  208. ."'";
  209. $_ltv = $this->getLtvData($_where);
  210. $items[$_k]['ltv'] = $_ltv[0];
  211. $_day_map = [
  212. 'agent_id' => $_v['agent_id'],
  213. 'date' => $_v['date'],
  214. 'app_id' => $_v['app_id']
  215. ];
  216. //$_day_log = $this->getPromotionDayLog($_day_map);
  217. $_day_log = $this->getPromotionByowner($_v['date'],$_v['owner_id'],$_v['app_id'],$_v['platform_id']);
  218. $items[$_k]['check_cnt'] = $_day_log['distinct_visit_cnt'];
  219. $items[$_k]['down_cnt'] = $_day_log['distinct_down_cnt'];
  220. $_check_cnt += $_day_log['distinct_visit_cnt'];
  221. $_down_cnt += $_day_log['distinct_down_cnt'];
  222. $items[$_k]['day2'] = $this->_showRegPoint($_v['day2'], $_v['reg_cnt']);
  223. $items[$_k]['day7'] = $this->_showRegPoint($_v['day7'], $_v['reg_cnt']);
  224. $items[$_k]['day15'] = $this->_showRegPoint($_v['day15'], $_v['reg_cnt']);
  225. }
  226. /* 汇总数据 */
  227. $_sum_field
  228. = "sum(d.reg_cnt) reg_cnt,sum(d.reg_device) reg_device,sum(d.reg_pay_cnt) reg_pay_cnt,
  229. sum(d.sum_reg_money) sum_reg_money,sum(d.user_cnt) user_cnt,sum(d.pay_user_cnt) pay_user_cnt,
  230. sum(d.sum_money) sum_money,sum(d.day2) day2,sum(d.day7) day7,sum(d.day15) day15,sum(d.check_cnt) check_cnt,
  231. sum(d.down_cnt) down_cnt,sum(reg_device) reg_device
  232. ";
  233. $_sum_items = $_day_model
  234. ->alias('d')
  235. ->field($_sum_field)
  236. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id and pd.app_id=d.app_id")
  237. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id")
  238. ->join("LEFT JOIN ".C('DB_PREFIX')."game_landing_page glp ON glp.id=pd.landing_page_id")
  239. ->where($where)
  240. ->select();
  241. $_prom_data = M("promotion_day")
  242. ->alias('d')
  243. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id and pd.app_id=d.app_id")
  244. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id")
  245. ->where($where)
  246. ->field("sum(`distinct_visit_cnt`) distinct_visit_cnt,sum(`distinct_down_cnt`) distinct_down_cnt")
  247. ->select();
  248. $_sum_items[0]['check_cnt'] = $_prom_data[0]['distinct_visit_cnt'];
  249. $_sum_items[0]['down_cnt'] = $_prom_data[0]['distinct_down_cnt'];
  250. //$_sum_items[0]['check_cnt'] = $_check_cnt;
  251. //$_sum_items[0]['down_cnt'] = $_down_cnt;
  252. if (3 == $this->role_type) {
  253. $_where = " agent_id".$this->agentwhere;
  254. } else {
  255. if (!empty($_ltv_agent_ids)) {
  256. $_agent_ids = implode(',', $_ltv_agent_ids);
  257. $_where = " agent_id in (".$_agent_ids.") ";
  258. } else {
  259. $_where = " 1 ";
  260. }
  261. }
  262. /*if ('今日' == $_is_today) {
  263. $_where .= " and `date`='".date('Y-m-d')."'";
  264. }*/
  265. if ('今日' == $_is_today) {
  266. $_where .= " and `date`='".date('Y-m-d')."'";
  267. $where .= " and d.`date`='".date('Y-m-d')."'";
  268. $_prom_data = M("promotion_day")
  269. ->alias('d')
  270. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_detail pd ON pd.agent_id=d.agent_id and pd.app_id=d.app_id")
  271. ->join("LEFT JOIN ".C('DB_PREFIX')."promotion_plan pp ON pp.id=pd.promotion_plan_id")
  272. ->where($where)
  273. ->field("sum(`distinct_visit_cnt`) distinct_visit_cnt,sum(`distinct_down_cnt`) distinct_down_cnt")
  274. ->select();
  275. $_sum_items[0]['check_cnt'] = !empty($_prom_data[0]['distinct_visit_cnt']) ? $_prom_data[0]['distinct_visit_cnt'] : 0;
  276. $_sum_items[0]['down_cnt'] = !empty($_prom_data[0]['distinct_down_cnt']) ? $_prom_data[0]['distinct_down_cnt'] : 0;
  277. }
  278. $_ltv = $this->getLtvData($_where);
  279. $_sum_items[0]['ltv'] = $_ltv[0];
  280. $_sum_items[0]['day2'] = $this->_showRegPoint($_sum_items[0]['day2'], $_sum_items[0]['reg_cnt']);
  281. $_sum_items[0]['day7'] = $this->_showRegPoint($_sum_items[0]['day7'], $_sum_items[0]['reg_cnt']);
  282. $_sum_items[0]['day15'] = $this->_showRegPoint($_sum_items[0]['day15'], $_sum_items[0]['reg_cnt']);
  283. $_export_button = $_explore_csv_class->exportButton('数据导出', 'input', 1000, 'btn btn-primary');
  284. $this->assign("export_button", $_export_button);
  285. $this->assign("pays", $items);
  286. $this->assign($_GET);
  287. $this->assign("sum_items", $_sum_items);
  288. $this->assign("Page", $page->show('Admin'));
  289. }
  290. }
  291. /**
  292. * 初始化实时数据
  293. */
  294. public function _initTodayData() {
  295. $this->today_date = date('Y-m-d');
  296. $this->today_start_time = strtotime($this->today_date);
  297. $this->today_end_time = time();
  298. $_agents = Db::name('user')->where(['role_id' => ['in', [24, 241]]])->column('id', 'id');
  299. if (!empty($_agents)) {
  300. $_agent_ids = implode(',', $_agents);
  301. $_sql
  302. = "
  303. TRUNCATE h_tmp_promotion_logindata;
  304. ALTER TABLE h_tmp_promotion_logindata DISABLE KEYS;
  305. INSERT INTO h_tmp_promotion_logindata
  306. (
  307. `mem_id`,
  308. `app_id`,
  309. `login_date`,
  310. `reg_date`,
  311. `login_cnt`,
  312. `agent_id`,
  313. `imei`,
  314. `imei_cnt`,
  315. `regdays`
  316. )
  317. SELECT
  318. `ll`.`mem_id` AS `mem_id`,
  319. `ll`.`app_id` AS `app_id`,
  320. DATE_FORMAT(FROM_UNIXTIME(`ll`.`login_time`),'%Y-%m-%d') AS `login_date`,
  321. DATE_FORMAT(FROM_UNIXTIME(IFNULL(`m`.`reg_time`,0)),'%Y-%m-%d') AS `reg_date`,
  322. COUNT(`ll`.`id`) AS `login_cnt`,
  323. IFNULL(`m`.`agent_id` , '0') AS `agent_id`,
  324. `ll`.`imei` AS `imei`,
  325. COUNT(DISTINCT(`ll`.`imei`)) AS `imei_cnt`,
  326. (TO_DAYS(FROM_UNIXTIME(`ll`.`login_time`)) - TO_DAYS(FROM_UNIXTIME(IFNULL(`m`.`reg_time`,0)))) AS `regdays`
  327. FROM
  328. (`c_login_log` `ll`)
  329. LEFT JOIN `c_members` m ON ll.mem_id = m.id
  330. WHERE
  331. (`ll`.`login_time` >= ".$this->today_start_time." AND `ll`.`login_time` < ".$this->today_end_time
  332. ." AND `ll`.`agent_id` in (".$_agent_ids.") AND (`ll`.`app_id` > 0)) AND (`ll`.`app_id` = `m`.`app_id`)
  333. GROUP BY `ll`.`mem_id` , `ll`.`app_id`;
  334. ALTER TABLE c_tmp_promotion_logindata ENABLE KEYS;
  335. TRUNCATE c_tmp_promotion_paydata;
  336. ALTER TABLE c_tmp_promotion_paydata DISABLE KEYS;
  337. INSERT INTO c_tmp_promotion_paydata
  338. (
  339. `mem_id`,
  340. `app_id`,
  341. `agent_id`,
  342. `login_date`,
  343. `reg_date`,
  344. `order_cnt`,
  345. `sum_money`,
  346. `imei`,
  347. `imei_cnt`,
  348. `regdays`
  349. )
  350. SELECT
  351. `p`.`mem_id` AS `mem_id`,
  352. `p`.`app_id` AS `app_id`,
  353. IFNULL(`m`.`agent_id` , '0') AS `agent_id`,
  354. DATE_FORMAT(FROM_UNIXTIME(`p`.`create_time`),'%Y-%m-%d') AS `login_date`,
  355. DATE_FORMAT(FROM_UNIXTIME(IFNULL(`m`.`reg_time`,0)),'%Y-%m-%d') AS `reg_date`,
  356. COUNT(`p`.`id`) AS `order_cnt`,
  357. SUM(`p`.`amount`) AS `sum_money`,
  358. `m`.`imei` AS `imei`,
  359. COUNT(DISTINCT(`m`.`imei`)) AS `imei_cnt`,
  360. (TO_DAYS(FROM_UNIXTIME(`p`.`create_time`)) - TO_DAYS(FROM_UNIXTIME(IFNULL(`m`.`reg_time`,0)))) AS `regdays`
  361. FROM
  362. `c_pay` p
  363. LEFT JOIN `c_members` m ON p.mem_id = m.id
  364. WHERE
  365. ((`p`.`status` = 2)
  366. AND `p`.`create_time` >= ".$this->today_start_time."
  367. AND `p`.`create_time` < ".$this->today_end_time."
  368. AND `m`.`agent_id` in (".$_agent_ids.")
  369. )
  370. GROUP BY `p`.`mem_id` , `p`.`app_id`;
  371. ALTER TABLE c_tmp_promotion_paydata ENABLE KEYS;
  372. TRUNCATE c_tmp_promotion_daypayuser;
  373. ALTER TABLE c_tmp_promotion_daypayuser DISABLE KEYS;
  374. INSERT INTO c_tmp_promotion_daypayuser
  375. (
  376. `login_date`,
  377. `reg_date`,
  378. `mem_id`,
  379. `app_id`,
  380. `agent_id`,
  381. `sum_money`,
  382. `order_cnt`,
  383. `login_cnt`,
  384. `imei`,
  385. `imei_cnt`,
  386. `regdays`
  387. )
  388. SELECT
  389. `a`.`login_date` AS `login_date`,
  390. IFNULL(`a`.`reg_date`, '1970-01-01') AS `reg_date`,
  391. `a`.`mem_id` AS `mem_id`,
  392. `a`.`app_id` AS `app_id`,
  393. IFNULL(`a`.`agent_id`, 0) AS `agent_id`,
  394. IFNULL(`b`.`sum_money`, 0) AS `sum_money`,
  395. IFNULL(`b`.`order_cnt`, 0) AS `order_cnt`,
  396. `a`.`login_cnt` AS `login_cnt`,
  397. `a`.`imei` AS `imei`,
  398. `a`.`imei_cnt` AS `imei_cnt`,
  399. `a`.`regdays` AS `regdays`
  400. FROM
  401. (`c_tmp_promotion_logindata` `a`
  402. LEFT JOIN `c_tmp_promotion_paydata` `b` ON (((`a`.`login_date` = `b`.`login_date`)
  403. AND (`a`.`mem_id` = `b`.`mem_id`)
  404. AND (`a`.`app_id` = `b`.`app_id`))))
  405. WHERE
  406. (`a`.`login_date` = '".$this->today_date."')
  407. UNION SELECT
  408. `b`.`login_date` AS `login_date`,
  409. IFNULL(`b`.`reg_date`, '1970-01-01') AS `reg_date`,
  410. `b`.`mem_id` AS `mem_id`,
  411. `b`.`app_id` AS `app_id`,
  412. IFNULL(`b`.`agent_id`, 0) AS `agent_id`,
  413. `b`.`sum_money` AS `sum_money`,
  414. `b`.`order_cnt` AS `order_cnt`,
  415. IFNULL(`a`.`login_cnt`, 0) AS `login_cnt`,
  416. IFNULL(`b`.`imei`, '000000000000000') AS `imei`,
  417. IFNULL(`b`.`imei_cnt`, 0) AS `imei_cnt`,
  418. IFNULL(`b`.`regdays`, 100000) AS `regdays`
  419. FROM
  420. (`c_tmp_promotion_paydata` `b`
  421. LEFT JOIN `c_tmp_promotion_logindata` `a` ON (((`a`.`login_date` = `b`.`login_date`)
  422. AND (`a`.`mem_id` = `b`.`mem_id`)
  423. AND (`a`.`app_id` = `b`.`app_id`))))
  424. WHERE
  425. `b`.`login_date` = '".$this->today_date."';
  426. ALTER TABLE c_tmp_promotion_daypayuser ENABLE KEYS;
  427. TRUNCATE c_tmp_promotion_dayagentgame;
  428. ALTER TABLE c_tmp_promotion_dayagentgame DISABLE KEYS;
  429. INSERT INTO c_tmp_promotion_dayagentgame
  430. (
  431. `date`,
  432. `reg_date`,
  433. `regdays`,
  434. `agent_id`,
  435. `app_id`,
  436. `user_cnt`,
  437. `sum_money`,
  438. `pay_user_cnt`,
  439. `order_cnt`,
  440. `reg_device`
  441. )
  442. SELECT
  443. '".$this->today_date."' AS `date`,
  444. `reg_date` AS `reg_date`,
  445. `regdays` AS `regdays`,
  446. `agent_id` AS `agent_id`,
  447. `app_id` AS `app_id`,
  448. COUNT(DISTINCT `mem_id`) AS `user_cnt`,
  449. SUM(`sum_money`) AS `sum_money`,
  450. COUNT(DISTINCT(CASE WHEN `sum_money`>0 THEN `mem_id` END)) AS `pay_user_cnt`,
  451. SUM(`order_cnt`) AS `order_cnt`,
  452. (SELECT COUNT(DISTINCT t1.`imei`) FROM `c_tmp_promotion_daypayuser` t1 WHERE t1.`regdays`='0' AND t1.`agent_id`=c_tmp_promotion_daypayuser.`agent_id` AND t1.`app_id`=c_tmp_promotion_daypayuser.`app_id` AND t1.`reg_date`=c_tmp_promotion_daypayuser.`reg_date`) AS imei_cnt
  453. FROM `c_tmp_promotion_daypayuser`
  454. GROUP BY `agent_id`,`app_id`,`reg_date`;
  455. ALTER TABLE c_tmp_promotion_dayagentgame ENABLE KEYS;
  456. TRUNCATE c_tmp_day_promotion;
  457. ALTER TABLE c_tmp_day_promotion DISABLE KEYS;
  458. REPLACE INTO `c_tmp_day_promotion`
  459. (
  460. `date`,
  461. `agent_id`,
  462. `user_cnt`,
  463. `sum_money`,
  464. `order_cnt`,
  465. `reg_order_cnt`,
  466. `pay_user_cnt`,
  467. `reg_pay_cnt`,
  468. `sum_reg_money`,
  469. `reg_cnt`,
  470. `reg_device`
  471. )
  472. (SELECT
  473. '".$this->today_date."' AS `date`,
  474. `agent_id`,
  475. COUNT(DISTINCT `mem_id`) AS `user_cnt`,
  476. SUM(`sum_money`) AS `sum_money`,
  477. SUM(`order_cnt`) AS `order_cnt`,
  478. SUM(CASE WHEN `regdays`=0 THEN `order_cnt` ELSE 0 END) AS `reg_order_cnt`,
  479. COUNT(DISTINCT(CASE WHEN `sum_money`>0 THEN `mem_id` END)) AS `pay_user_cnt`,
  480. COUNT(DISTINCT(CASE WHEN `regdays`=0 AND `sum_money`>0 THEN `mem_id` END)) AS `reg_pay_cnt`,
  481. SUM(CASE WHEN `regdays`=0 THEN `sum_money` ELSE 0 END) AS `sum_reg_money`,
  482. COUNT(DISTINCT(CASE WHEN `regdays`=0 THEN `mem_id` END)) AS `reg_cnt`,
  483. COUNT(DISTINCT(CASE WHEN `regdays`=0 THEN `imei` END)) AS imei_cnt
  484. FROM `c_tmp_promotion_daypayuser`
  485. GROUP BY agent_id)
  486. UNION
  487. (SELECT
  488. '".$this->today_date."' AS `date`,
  489. `agent_id`,
  490. 0 AS `user_cnt`,
  491. 0 AS `sum_money`,
  492. 0 AS `order_cnt`,
  493. 0 AS `reg_order_cnt`,
  494. 0 AS `pay_user_cnt`,
  495. 0 AS `reg_pay_cnt`,
  496. 0 AS `sum_reg_money`,
  497. 0 AS `reg_cnt`,
  498. 0 AS `imei_cnt`
  499. FROM
  500. `c_promotion_day` WHERE `agent_id` NOT IN (SELECT agent_id FROM c_tmp_promotion_daypayuser WHERE agent_id IS NOT NULL )
  501. AND
  502. `date` = '".$this->today_date."');
  503. ALTER TABLE c_tmp_day_promotion ENABLE KEYS;
  504. TRUNCATE c_tmp_day_promotion_game;
  505. ALTER TABLE c_tmp_day_promotion_game DISABLE KEYS;
  506. REPLACE INTO `c_tmp_day_promotion_game`
  507. (
  508. `date`,
  509. `agent_id`,
  510. `app_id`,
  511. `user_cnt`,
  512. `sum_money`,
  513. `order_cnt`,
  514. `reg_order_cnt`,
  515. `pay_user_cnt`,
  516. `reg_pay_cnt`,
  517. `sum_reg_money`,
  518. `reg_cnt`,
  519. `reg_device`
  520. )
  521. (SELECT
  522. `date`,
  523. `agent_id`,
  524. `app_id`,
  525. SUM(user_cnt) AS `user_cnt`,
  526. SUM(sum_money) AS `sum_money`,
  527. SUM(order_cnt) AS `order_cnt`,
  528. SUM(CASE WHEN `regdays`=0 THEN `order_cnt` ELSE 0 END) AS `reg_order_cnt`,
  529. SUM(pay_user_cnt) AS `pay_user_cnt`,
  530. SUM(CASE WHEN `regdays`=0 THEN `pay_user_cnt` ELSE 0 END) AS `reg_pay_cnt`,
  531. SUM(CASE WHEN `regdays`=0 THEN `sum_money` ELSE 0 END) AS `sum_reg_money`,
  532. SUM(CASE WHEN `regdays`=0 THEN `user_cnt` ELSE 0 END) AS `reg_cnt`,
  533. SUM(`reg_device`) AS `reg_device`
  534. FROM c_tmp_promotion_dayagentgame
  535. GROUP BY agent_id, app_id)
  536. UNION
  537. (SELECT
  538. '".$this->today_date."' AS `date`,
  539. `agent_id`,
  540. `app_id`,
  541. 0 AS `user_cnt`,
  542. 0 AS `sum_money`,
  543. 0 AS `order_cnt`,
  544. 0 AS `reg_order_cnt`,
  545. 0 AS `pay_user_cnt`,
  546. 0 AS `reg_pay_cnt`,
  547. 0 AS `sum_reg_money`,
  548. 0 AS `reg_cnt`,
  549. 0 AS `reg_device`
  550. FROM
  551. `c_promotion_day` WHERE `agent_id` NOT IN (SELECT agent_id FROM c_tmp_promotion_dayagentgame WHERE agent_id IS NOT NULL )
  552. AND
  553. `date` = '".$this->today_date."');
  554. ALTER TABLE c_tmp_day_promotion_game ENABLE KEYS;
  555. ";
  556. M()->execute($_sql);
  557. $this->doDayPromotionGame();
  558. $this->doDayPromotion();
  559. }
  560. }
  561. /**
  562. * 操作tmp_day_promotion_game留存数据
  563. */
  564. public function doDayPromotionGame() {
  565. $_fields = "reg_date,app_id,agent_id,regdays,COUNT(DISTINCT `mem_id`) AS `user_cnt`";
  566. $_map['regdays'] = [
  567. 'in' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 29, 59]
  568. ];
  569. $_list = M('tmp_promotion_daypayuser')->field($_fields)->where($_map)->group(
  570. 'app_id,agent_id,regdays'
  571. )->select();
  572. if (!empty($_list)) {
  573. foreach ($_list as $_k => $_v) {
  574. $_field = "";
  575. switch ($_v['regdays']) {
  576. CASE 1:
  577. $_field = "day2";
  578. break;
  579. CASE 2:
  580. $_field = "day3";
  581. break;
  582. CASE 3:
  583. $_field = "day4";
  584. break;
  585. CASE 4:
  586. $_field = "day5";
  587. break;
  588. CASE 5:
  589. $_field = "day6";
  590. break;
  591. CASE 7:
  592. $_field = "day8";
  593. break;
  594. CASE 8:
  595. $_field = "day9";
  596. break;
  597. CASE 9:
  598. $_field = "day10";
  599. break;
  600. CASE 10:
  601. $_field = "day11";
  602. break;
  603. CASE 11:
  604. $_field = "day12";
  605. break;
  606. CASE 12:
  607. $_field = "day13";
  608. break;
  609. CASE 13:
  610. $_field = "day14";
  611. break;
  612. CASE 14:
  613. $_field = "day15";
  614. break;
  615. CASE 20:
  616. $_field = "day21";
  617. break;
  618. CASE 29:
  619. $_field = "day30";
  620. break;
  621. CASE 59:
  622. $_field = "day60";
  623. break;
  624. }
  625. if (!empty($_field)) {
  626. $_map = [
  627. 'date' => $_v['reg_date'],
  628. 'agent_id' => $_v['agent_id'],
  629. 'app_id' => $_v['app_id']
  630. ];
  631. M('tmp_day_promotion_game')->where($_map)->setField($_field, $_v['user_cnt']);
  632. }
  633. }
  634. }
  635. }
  636. /**
  637. * 操作tmp_day_promotion表留存数据
  638. */
  639. public function doDayPromotion() {
  640. $_map['regdays'] = [
  641. 'in' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 29, 59]
  642. ];
  643. $_fields = "reg_date,agent_id,regdays,COUNT(DISTINCT `mem_id`) AS `user_cnt`";
  644. $_list = M('tmp_promotion_daypayuser')->field($_fields)->where($_map)->group('agent_id,regdays')
  645. ->select();
  646. if (!empty($_list)) {
  647. foreach ($_list as $_k => $_v) {
  648. $_field = "";
  649. switch ($_v['regdays']) {
  650. CASE 1:
  651. $_field = "day2";
  652. break;
  653. CASE 2:
  654. $_field = "day3";
  655. break;
  656. CASE 3:
  657. $_field = "day4";
  658. break;
  659. CASE 4:
  660. $_field = "day5";
  661. break;
  662. CASE 5:
  663. $_field = "day6";
  664. break;
  665. CASE 7:
  666. $_field = "day8";
  667. break;
  668. CASE 8:
  669. $_field = "day9";
  670. break;
  671. CASE 9:
  672. $_field = "day10";
  673. break;
  674. CASE 10:
  675. $_field = "day11";
  676. break;
  677. CASE 11:
  678. $_field = "day12";
  679. break;
  680. CASE 12:
  681. $_field = "day13";
  682. break;
  683. CASE 13:
  684. $_field = "day14";
  685. break;
  686. CASE 14:
  687. $_field = "day15";
  688. break;
  689. CASE 20:
  690. $_field = "day21";
  691. break;
  692. CASE 29:
  693. $_field = "day30";
  694. break;
  695. CASE 59:
  696. $_field = "day60";
  697. break;
  698. }
  699. if (!empty($_field)) {
  700. $_map = [
  701. 'date' => $_v['reg_date'],
  702. 'agent_id' => $_v['agent_id']
  703. ];
  704. M('tmp_day_promotion')->where($_map)->setField($_field, $_v['user_cnt']);
  705. }
  706. }
  707. }
  708. }
  709. function _showRegPoint($cnt, $reg) {
  710. if ($reg) {
  711. return $cnt.'('.number_format($cnt * 100 / $reg, 2, '.', '').'%)';
  712. } else {
  713. return $cnt.'(0.00%)';
  714. }
  715. }
  716. }