Statistics_model.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Statistics_model extends Base_model {
  5. protected $table = 'statistics';
  6. public function __construct() {
  7. parent::__construct();
  8. }
  9. public function get_data($ids, $date, $type){
  10. if (empty($ids)) return array();
  11. if (is_array($ids)) {
  12. $where = 'lampid in ('.implode(',', $ids).')';
  13. }else{
  14. $where = 'lampid = '.$ids;
  15. }
  16. if ($type == 'power') {
  17. $field = 't1.dayGeneration,t1.dayConsumption,t1.totalGeneration,t1.totalConsumption,t1.monthGeneration,t1.monthConsumption,t1.yearGeneration,t1.yearConsumption,t1.updatetime';
  18. }elseif ($type == 'current') {
  19. $field = 't1.dayCharMaxCurr,t1.dayDischarMaxCurr,t1.monthCharMaxCurr,t1.monthDischarMaxCurr,t1.yearCharMaxCurr,t1.yearDischarMaxCurr,t1.updatetime';
  20. }elseif ($type == 'temper') {
  21. $field = 't1.dayMinTemper,t1.dayMaxTemper,t1.monthMinTemper,t1.monthMaxTemper,t1.yearMinTemper,t1.yearMaxTemper,t1.updatetime';
  22. }elseif ($type == 'capacity') {
  23. $field = 't1.dayCharMaxPower,t1.dayDischarMaxPower,t1.monthCharMaxPower,t1.monthDischarMaxPower,t1.yearCharMaxPower,t1.yearDischarMaxPower,t1.updatetime';
  24. }else{
  25. $field = 't1.dayMinVoltage,t1.dayMaxVoltage,t1.monthMinVoltage,t1.monthMaxVoltage,t1.yearMinVoltage,t1.yearMaxVoltage,t1.updatetime';
  26. }
  27. $field .= ',t1.lampid';
  28. $sql = 'SELECT '.$field.' from new_statistics AS t1 JOIN (SELECT lampid,max(updatetime) as maxTime from new_statistics WHERE updatetime <= "'.$date.'" AND '.$where.' group by lampid) AS t2 on t1.lampid = t2.lampid AND t1.updatetime = t2.maxTime';
  29. $query = $this->db->query($sql);
  30. $temp = $query->result_array();
  31. $res = [];
  32. foreach ($temp as $v) {
  33. $res[$v['lampid']] = $v;
  34. }
  35. return $res;
  36. }
  37. }