Seller.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Stat\Lib;
  3. use Dever;
  4. class Seller
  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. $seller = Dever::db('code/seller')->state();
  34. $journal = Dever::db('journal/info')->state(array('buy' => 1));
  35. if ($seller && $journal) {
  36. foreach ($seller as $sk => $sv) {
  37. for ($i = 0; $i < $num; $i++) {
  38. $where['start'] = strtotime("$start -$i day");
  39. $where['end'] = $where['start'] + 86399;
  40. foreach ($journal as $k => $v) {
  41. $this->total($sv, $v, $where);
  42. }
  43. }
  44. }
  45. }
  46. return true;
  47. }
  48. private function total($seller, $info, $where)
  49. {
  50. $day = date('Y-m-d', $where['start']);
  51. $sql = 'select * from wonderful_code_info where seller_id = '.$seller['id'].' and product_id = '.$info['id'].' and state = 1 and cdate >= '.$where['start'].' and cdate <= '.$where['end'];
  52. $code = Dever::db('code/info')->fetchAll($sql);
  53. $code_num = $code_yes_num = $code_no_num = $code_drop_num = 0;
  54. if ($code) {
  55. foreach ($code as $k => $v) {
  56. $code_num++;
  57. if ($v['type'] <= 2) {
  58. # 未使用
  59. $code_no_num++;
  60. } elseif ($v['type'] == 3) {
  61. # 已使用
  62. $code_yes_num++;
  63. } elseif ($v['type'] == 4) {
  64. # 已作废
  65. $code_drop_num++;
  66. }
  67. }
  68. }
  69. $this->update($seller['id'], $info['cate_id'], $info['id'], $day, $code_num, $code_yes_num, $code_no_num, $code_drop_num);
  70. }
  71. public function update($seller_id, $cate_id, $info_id, $day, $code_num, $code_yes_num, $code_no_num, $code_drop_num)
  72. {
  73. $update = array();
  74. $update['day_string'] = $day;
  75. $update['day_int'] = Dever::maketime($day);
  76. $update['code_num'] = $code_num;
  77. $update['code_yes_num'] = $code_yes_num;
  78. $update['code_no_num'] = $code_no_num;
  79. $update['code_drop_num'] = $code_drop_num;
  80. $update['seller_id'] = $seller_id;
  81. $update['cate_id'] = $cate_id;
  82. $update['journal_id'] = $info_id;
  83. $update['cdate'] = time();
  84. $where = array();
  85. $where['day_int'] = $update['day_int'];
  86. $where['seller_id'] = $update['seller_id'];
  87. $where['journal_id'] = $update['journal_id'];
  88. $info = Dever::db('stat/seller')->one($where);
  89. if (!$info) {
  90. $id = Dever::db('stat/seller')->insert($update);
  91. } else {
  92. $id = $info['id'];
  93. $update['where_id'] = $id;
  94. Dever::db('stat/seller')->update($update);
  95. }
  96. }
  97. }