Journal.php 3.6 KB

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