Journal.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $sql = 'select * from mars_act_order where type = 3 and product_id = '.$info['id'].' and status = 2 and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
  56. $order = Dever::db('act/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_num'] > 0) {
  67. $buy_num += $v['buy_num'];
  68. } elseif ($v['buy_id'] && $v['buy_id'] > 0) {
  69. $buy = Dever::db('journal/buy_num')->one($v['buy_id']);
  70. if ($buy['num']) {
  71. $buy_num += $buy['num'];
  72. }
  73. }
  74. if ($v['cash']) {
  75. $buy_cash += $v['cash'];
  76. }
  77. }
  78. }
  79. }
  80. $this->update($info['cate_id'], $info['id'], $day, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash);
  81. }
  82. public function update($cate_id, $info_id, $day, $order_num, $order_yes_num, $order_no_num, $buy_num, $buy_cash)
  83. {
  84. $update = array();
  85. $update['day_string'] = $day;
  86. $update['day_int'] = Dever::maketime($day);
  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['cate_id'] = $cate_id;
  93. $update['journal_id'] = $info_id;
  94. $update['cdate'] = time();
  95. $where = array();
  96. $where['day_int'] = $update['day_int'];
  97. $where['journal_id'] = $update['journal_id'];
  98. $info = Dever::db('stat/journal')->one($where);
  99. if (!$info) {
  100. $id = Dever::db('stat/journal')->insert($update);
  101. } else {
  102. $id = $info['id'];
  103. $update['where_id'] = $id;
  104. Dever::db('stat/journal')->update($update);
  105. }
  106. $stat = Dever::db('stat/journal')->getNumByJournal(array('journal_id' => $where['journal_id']));
  107. if ($stat && $stat['buy_num']) {
  108. Dever::config('base')->after = 1;
  109. $set['num_ding'] = $stat['buy_num'];
  110. $set['where_id'] = $where['journal_id'];
  111. Dever::db('journal/info')->update($set);
  112. Dever::load('act/lib/num')->setCounter($set['where_id'], $set['num_ding']);
  113. }
  114. }
  115. }