Source.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Stat\Lib;
  3. use Dever;
  4. class Source
  5. {
  6. const NUM = 7;
  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. $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'];
  54. $order = Dever::db('journal/order')->fetchAll($sql);
  55. $order_num = $order_yes_num = $order_no_num = $buy_num = $buy_cash = 0;
  56. if ($order) {
  57. foreach ($order as $k => $v) {
  58. $order_num++;
  59. if ($v['status'] == 1) {
  60. # 待支付
  61. $order_no_num++;
  62. } elseif ($v['status'] == 2) {
  63. # 已支付
  64. $order_yes_num++;
  65. if ($v['buy_id']) {
  66. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  67. if ($buy['num']) {
  68. $buy_num += $buy['num'];
  69. }
  70. }
  71. if ($v['cash']) {
  72. $buy_cash += $v['cash'];
  73. }
  74. }
  75. }
  76. }
  77. $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'];
  78. $order = Dever::db('act/order')->fetchAll($sql);
  79. if ($order) {
  80. foreach ($order as $k => $v) {
  81. $order_num++;
  82. if ($v['status'] == 1) {
  83. # 待支付
  84. $order_no_num++;
  85. } elseif ($v['status'] == 2) {
  86. # 已支付
  87. $order_yes_num++;
  88. if ($v['buy_id']) {
  89. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  90. if ($buy['num']) {
  91. $buy_num += $buy['num'];
  92. }
  93. }
  94. if ($v['cash']) {
  95. $buy_cash += $v['cash'];
  96. }
  97. }
  98. }
  99. }
  100. $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);
  101. }
  102. 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)
  103. {
  104. $update = array();
  105. $update['day_string'] = $day;
  106. $update['day_int'] = Dever::maketime($day);
  107. $update['pv'] = $pv;
  108. $update['uv'] = $uv;
  109. $update['user_num'] = $user_num;
  110. $update['user_yes_num'] = $user_yes_num;
  111. $update['order_num'] = $order_num;
  112. $update['order_yes_num'] = $order_yes_num;
  113. $update['order_no_num'] = $order_no_num;
  114. $update['buy_num'] = $buy_num;
  115. $update['buy_cash'] = $buy_cash;
  116. $update['source_id'] = $source_id;
  117. $update['cate_id'] = $cate_id;
  118. $update['journal_id'] = $info_id;
  119. $update['cdate'] = time();
  120. $where = array();
  121. $where['day_int'] = $update['day_int'];
  122. $where['source_id'] = $update['source_id'];
  123. $where['journal_id'] = $update['journal_id'];
  124. $info = Dever::db('stat/source')->one($where);
  125. if (!$info) {
  126. $id = Dever::db('stat/source')->insert($update);
  127. } else {
  128. $id = $info['id'];
  129. $update['where_id'] = $id;
  130. Dever::db('stat/source')->update($update);
  131. }
  132. }
  133. }