Source.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Stat\Lib;
  3. use Dever;
  4. class Source
  5. {
  6. const NUM = 3;
  7. public function data_api()
  8. {
  9. }
  10. /**
  11. * 获取数据-可以放到cron里
  12. *
  13. * @return mixed
  14. */
  15. public function data()
  16. {
  17. # 获取最近self::NUM天的数据
  18. $num = Dever::input('num', self::NUM);
  19. $day = Dever::input('day');
  20. if ($day) {
  21. $day = Dever::maketime($day);
  22. } else {
  23. $day = time();
  24. }
  25. $start = date('Y-m-d 00:00:00', $day);
  26. $option = array
  27. (
  28. 'state' => 1,
  29. 'start' => array('yes-cdate', '>'),
  30. 'end' => array('yes-cdate', '<'),
  31. );
  32. $where['option'] = $option;
  33. $source = Dever::db('source/info')->state(array('type' => 2));
  34. $journal = Dever::db('journal/info')->state(array('buy' => 1));
  35. if ($source && $journal) {
  36. foreach ($source as $sk => $sv) {
  37. for ($i = 0; $i < $num; $i++) {
  38. $where['start'] = strtotime("$start -$i day");
  39. $where['end'] = $where['start'] + 86399;
  40. //$log = Dever::load('source/lib/core')->get(date('Y_m_d', $where['start']), 'list', $sv['id'], $where['start'], $where['end']);
  41. $log['pv'] = $log['uv'] = $log['user_num'] = $log['user_yes_num'] = 0;
  42. foreach ($journal as $k => $v) {
  43. $this->total($sv, $v, $where, $log['pv'], $log['uv'], $log['user_num'], $log['user_yes_num']);
  44. }
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. private function total($source, $info, $where, $pv, $uv, $user_num, $user_yes_num)
  51. {
  52. $day = date('Y-m-d', $where['start']);
  53. $order_num = $order_yes_num = $order_no_num = $buy_num = $buy_cash = 0;
  54. /*
  55. $sql = 'select * from wonderful_journal_order where type in(1,3) and source = '.$source['id'].' and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
  56. $order = Dever::db('journal/order')->fetchAll($sql);
  57. if ($order) {
  58. foreach ($order as $k => $v) {
  59. $order_num++;
  60. if ($v['status'] == 1) {
  61. # 待支付
  62. $order_no_num++;
  63. } elseif ($v['status'] == 2) {
  64. # 已支付
  65. $order_yes_num++;
  66. if ($v['buy_id']) {
  67. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  68. if ($buy['num']) {
  69. $buy_num += $buy['num'];
  70. }
  71. }
  72. if ($v['cash']) {
  73. $buy_cash += $v['cash'];
  74. }
  75. }
  76. }
  77. }
  78. */
  79. $sql = 'select * from wonderful_act_order where type = 3 and source = '.$source['id'].' and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
  80. $order = Dever::db('act/order')->fetchAll($sql);
  81. if ($order) {
  82. foreach ($order as $k => $v) {
  83. $order_num++;
  84. if ($v['status'] == 1) {
  85. # 待支付
  86. $order_no_num++;
  87. } elseif ($v['status'] == 2) {
  88. # 已支付
  89. $order_yes_num++;
  90. if ($v['buy_id']) {
  91. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  92. if ($buy['num']) {
  93. $buy_num += $buy['num'];
  94. }
  95. }
  96. if ($v['cash']) {
  97. $buy_cash += $v['cash'];
  98. }
  99. }
  100. }
  101. }
  102. $this->update($source['id'], $info['cate_id'], $info['id'], $day, $pv, $uv, $user_num, $user_yes_num, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash);
  103. }
  104. public function update($source_id, $cate_id, $info_id, $day, $pv, $uv, $user_num, $user_yes_num, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash)
  105. {
  106. $update = array();
  107. $update['day_string'] = $day;
  108. $update['day_int'] = Dever::maketime($day);
  109. $update['pv'] = $pv;
  110. $update['uv'] = $uv;
  111. $update['user_num'] = $user_num;
  112. $update['user_yes_num'] = $user_yes_num;
  113. $update['order_num'] = $order_num;
  114. $update['order_yes_num'] = $order_yes_num;
  115. $update['order_no_num'] = $order_no_num;
  116. $update['buy_num'] = $buy_num;
  117. $update['buy_cash'] = $buy_cash;
  118. $update['source_id'] = $source_id;
  119. $update['cate_id'] = $cate_id;
  120. $update['journal_id'] = $info_id;
  121. $update['cdate'] = time();
  122. $where = array();
  123. $where['day_int'] = $update['day_int'];
  124. $where['source_id'] = $update['source_id'];
  125. $where['journal_id'] = $update['journal_id'];
  126. $info = Dever::db('stat/source')->one($where);
  127. if (!$info) {
  128. $id = Dever::db('stat/source')->insert($update);
  129. } else {
  130. $id = $info['id'];
  131. $update['where_id'] = $id;
  132. Dever::db('stat/source')->update($update);
  133. }
  134. }
  135. }