Journal.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Stat\Lib;
  3. use Dever;
  4. class Journal
  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. $info_where['buy'] = 1;
  34. $hot = Dever::input('hot');
  35. if ($hot) {
  36. $info_where['hot'] = $hot;
  37. }
  38. $journal = Dever::db('journal/info')->state($info_where);
  39. if ($journal) {
  40. for ($i = 0; $i < $num; $i++) {
  41. $where['start'] = strtotime("$start -$i day");
  42. $where['end'] = $where['start'] + 86399;
  43. foreach ($journal as $k => $v) {
  44. $this->total($v, $where);
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. private function total($info, $where)
  51. {
  52. # 这里就是为了做下sql的实现方式,其实用dever自带的功能完全可以实现。
  53. $day = date('Y-m-d', $where['start']);
  54. $order_num = $order_yes_num = $order_no_num = $buy_num = $buy_cash = 0;
  55. /*
  56. $sql = 'select * from wonderful_journal_order where type in(1,3,4) and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
  57. $order = Dever::db('journal/order')->fetchAll($sql);
  58. if ($order) {
  59. foreach ($order as $k => $v) {
  60. $order_num++;
  61. if ($v['status'] == 1) {
  62. # 待支付
  63. $order_no_num++;
  64. } elseif ($v['status'] == 2) {
  65. # 已支付
  66. $order_yes_num++;
  67. if ($v['buy_id'] && $v['buy_id'] > 0) {
  68. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  69. if ($buy['num']) {
  70. $buy_num += $buy['num'];
  71. }
  72. } elseif ($v['buy_num'] > 0) {
  73. $buy_num += $v['buy_num'];
  74. }
  75. if ($v['cash']) {
  76. $buy_cash += $v['cash'];
  77. }
  78. }
  79. }
  80. }
  81. */
  82. $sql = 'select * from wonderful_act_order where type = 3 and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
  83. $order = Dever::db('act/order')->fetchAll($sql);
  84. if ($order) {
  85. foreach ($order as $k => $v) {
  86. $order_num++;
  87. if ($v['status'] == 1) {
  88. # 待支付
  89. $order_no_num++;
  90. } elseif ($v['status'] == 2) {
  91. # 已支付
  92. $order_yes_num++;
  93. if ($v['buy_id'] && $v['buy_id'] > 0) {
  94. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  95. if ($buy['num']) {
  96. $buy_num += $buy['num'];
  97. }
  98. } elseif ($v['buy_num'] > 0) {
  99. $buy_num += $v['buy_num'];
  100. }
  101. if ($v['cash']) {
  102. $buy_cash += $v['cash'];
  103. }
  104. }
  105. }
  106. }
  107. $this->update($info['cate_id'], $info['id'], $day, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash);
  108. }
  109. public function update($cate_id, $info_id, $day, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash)
  110. {
  111. $update = array();
  112. $update['day_string'] = $day;
  113. $update['day_int'] = Dever::maketime($day);
  114. $update['order_num'] = $order_num;
  115. $update['order_yes_num'] = $order_yes_num;
  116. $update['order_no_num'] = $order_no_num;
  117. $update['buy_num'] = $buy_num;
  118. $update['buy_cash'] = $buy_cash;
  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['journal_id'] = $update['journal_id'];
  125. $info = Dever::db('stat/journal')->one($where);
  126. if (!$info) {
  127. $id = Dever::db('stat/journal')->insert($update);
  128. } else {
  129. $id = $info['id'];
  130. $update['where_id'] = $id;
  131. Dever::db('stat/journal')->update($update);
  132. }
  133. $stat = Dever::db('stat/journal')->getNumByJournal(array('journal_id' => $where['journal_id']));
  134. if ($stat && $stat['buy_num']) {
  135. $set['num_ding'] = $stat['buy_num'];
  136. $set['where_id'] = $where['journal_id'];
  137. Dever::db('journal/info')->update($set);
  138. }
  139. }
  140. }