Source.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Stat\Lib;
  3. use Dever;
  4. class Source
  5. {
  6. const NUM = 2;
  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();
  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. foreach ($journal as $k => $v) {
  42. $this->total($sv, $v, $where, $log['pv'], $log['uv'], $log['user_num'], $log['user_yes_num']);
  43. }
  44. }
  45. }
  46. }
  47. return true;
  48. }
  49. private function total($source, $info, $where, $pv, $uv, $user_num, $user_yes_num)
  50. {
  51. $day = date('Y-m-d', $where['start']);
  52. $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'];
  53. $order = Dever::db('journal/order')->fetchAll($sql);
  54. $order_num = $order_yes_num = $order_no_num = $buy_num = $buy_cash = 0;
  55. if ($order) {
  56. foreach ($order as $k => $v) {
  57. $order_num++;
  58. if ($v['status'] == 1) {
  59. # 待支付
  60. $order_no_num++;
  61. } elseif ($v['status'] == 2) {
  62. # 已支付
  63. $order_yes_num++;
  64. if ($v['buy_id']) {
  65. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  66. if ($buy['num']) {
  67. $buy_num += $buy['num'];
  68. }
  69. }
  70. if ($v['cash']) {
  71. $buy_cash += $v['cash'];
  72. }
  73. }
  74. }
  75. }
  76. $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);
  77. }
  78. 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)
  79. {
  80. $update = array();
  81. $update['day_string'] = $day;
  82. $update['day_int'] = Dever::maketime($day);
  83. $update['pv'] = $pv;
  84. $update['uv'] = $uv;
  85. $update['user_num'] = $user_num;
  86. $update['user_yes_num'] = $user_yes_num;
  87. $update['order_num'] = $order_num;
  88. $update['order_yes_num'] = $order_yes_num;
  89. $update['order_no_num'] = $order_no_num;
  90. $update['buy_num'] = $buy_num;
  91. $update['buy_cash'] = $buy_cash;
  92. $update['source_id'] = $source_id;
  93. $update['cate_id'] = $cate_id;
  94. $update['journal_id'] = $info_id;
  95. $update['cdate'] = time();
  96. $where = array();
  97. $where['day_int'] = $update['day_int'];
  98. $where['source_id'] = $update['source_id'];
  99. $where['journal_id'] = $update['journal_id'];
  100. $info = Dever::db('stat/source')->one($where);
  101. if (!$info) {
  102. $id = Dever::db('stat/source')->insert($update);
  103. } else {
  104. $id = $info['id'];
  105. $update['where_id'] = $id;
  106. Dever::db('stat/source')->update($update);
  107. }
  108. }
  109. }