Cron.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Ad\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. public function run($data)
  7. {
  8. $source = $data[0];
  9. $desc = $data[1];
  10. $up = $data[2];
  11. if ($desc) {
  12. $update = array();
  13. foreach ($desc as $k => $v) {
  14. if (!isset($v['param']['ad_data_id'])) {
  15. continue;
  16. }
  17. $key = 'key_' . $up['time'] . '_' . $v['param']['ad_data_id'];
  18. if (!isset($update[$key])) {
  19. $update[$key] = array();
  20. $update[$key]['time'] = $up['time'];
  21. $update[$key]['data_id'] = $v['param']['ad_data_id'];
  22. if (isset($v['param']['ad_info_id'])) {
  23. $update[$key]['info_id'] = $v['param']['ad_info_id'];
  24. }
  25. if (isset($v['param']['ad_page_id'])) {
  26. $update[$key]['page_id'] = $v['param']['ad_page_id'];
  27. }
  28. $update[$key]['year'] = $up['year'];
  29. $update[$key]['month'] = $up['month'];
  30. $update[$key]['day'] = $up['day'];
  31. $update[$key]['hour'] = $up['hour'];
  32. $update[$key]['pv_click'] = 0;
  33. $update[$key]['uv_click'] = 0;
  34. $update[$key]['pv_view'] = 0;
  35. $update[$key]['uv_view'] = 0;
  36. }
  37. if ($v['param']['project'] == 'ad_click') {
  38. $update[$key]['pv_click']++;
  39. $update[$key]['uv_click']++;
  40. } else {
  41. $update[$key]['pv_view']++;
  42. $update[$key]['uv_view']++;
  43. }
  44. }
  45. if ($update) {
  46. foreach ($update as $k => $v) {
  47. $this->update($v);
  48. }
  49. }
  50. }
  51. }
  52. private function update($data)
  53. {
  54. $where = array();
  55. $where['data_id'] = $data['data_id'];
  56. $where['time'] = $data['time'];
  57. $info = Dever::db('ad/stat')->one($where);
  58. if (!$info) {
  59. Dever::db('ad/stat')->insert($data);
  60. } else {
  61. $data['where_id'] = $info['id'];
  62. if ($info['pv_click'] >= $data['pv_click']) {
  63. unset($data['pv_click']);
  64. }
  65. if ($info['uv_click'] >= $data['uv_click']) {
  66. unset($data['uv_click']);
  67. }
  68. if ($info['pv_view'] >= $data['pv_view']) {
  69. unset($data['pv_view']);
  70. }
  71. if ($info['uv_view'] >= $data['uv_view']) {
  72. unset($data['uv_view']);
  73. }
  74. Dever::db('ad/stat')->update($data);
  75. }
  76. }
  77. }