_getCommonWhere($params, $this->timeFieldName); } protected function _getCommonWhere($params, $timeName) { $where = false; $ORGNAME = $params['ORGNAME'] ?? false; $start = $params['start'] ?? false; $end = $params['end'] ?? false; if($ORGNAME) { $_where = ['ORGNAME', '=', $ORGNAME]; $where = [$_where]; } if($start) { $_where = [$timeName, '>=', $start]; if($where) { $where[] = $_where; } else { $where = [$_where]; } } if($end) { $_where = [$timeName, '<=', $end]; if($where) { $where[] = $_where; } else { $where = [$_where]; } } return $where; } /** * 获取机构数据情况 */ public function getOrg($where = false) { $orgs = $this->_getOrg($where); $orgs = $this->_fillOrg($orgs); return $orgs; } public function _fillOrg($orgs) { foreach($orgs as &$org) { $org['label'] = $org['ORGNAME']; $org['value'] = $org['count']; unset($org['ORGNAME']); unset($org['count']); } return $orgs; } public function _getOrg($where = false) { $orgs = $this->baseModel ->field('count('.$this->countField.') AS count, ORGNAME') ->where($where) ->group('ORGNAME') ->order('count', 'desc') ->select() ->toArray(); return $orgs; } /** * 获取近6月数据情况,不足统一复制0 */ public function getMonth($where = false) { $months = $this->_getMonth($where); $months = $this->_fillMonth($months); return $months; } public function _fillMonth($_months) { $months = []; $i = 0; while($i < 6) { array_unshift($months, [ 'label' => date('Ym', strtotime('-'.$i.' month')), 'value' => 0, ]); $i++; } foreach ($months as &$monthData) { foreach ($_months as $_month) { if ($_month['month'] == $monthData['label']) { $monthData['value'] = $_month['count']; break; } } } return $months; } public function _getMonth($where = false) { $months = $this->baseModel ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m") AS month') ->where($where) ->whereTime($this->timeFieldName, '>=', date('Y-m-01 00:00:00', strtotime('-5 months'))) ->group(date_format($this->timeFieldName, "%Y%m")) ->order(date_format($this->timeFieldName, "%Y%m"), 'asc') ->select() ->toArray(); return $months; } /** * 获取所有数据条数 */ public function getAllCount($where = false) { $res = $this->baseModel ->field('count('.$this->countField.') AS count') ->where($where) ->select() ->toArray(); return $res[0]['count']; } public function getDayData($where = false) { // 获取今天是本月第几日 $j = date('j'); // 获取今天是本周第几日 $w = date('w'); $days = false; if($j > $w) { $days = $this->_getMonthDayData($where); } else { $days = $this->_getWeekDayData($where); } // 获取本月每日检查数量 $months = $this->_fillMonthDayData($days); // 获取本周每日检查数量 $weeks = $this->_fillWeekDayData($days); // 获取今日检查数量 $day = $this->_fillDayCount($days); return [ 'months' => $months, 'weeks' => $weeks, 'day' => $day, ]; } /** * 格式化本日检查数量,没有返回的统一赋值为0 */ public function _fillDayCount($days) { foreach($days as $day) { if($day['day'] === date('Ymd')) { return $day['count']; } } return 0; } /** * 填充本月每日检查数量,补齐日期信息从01到今日,没有返回的统一赋值为0 */ public function _fillMonthDayData($days) { $months = array(); $firstDay = intval(date('Ym01')); $lastDay = intval(date('Ymd')); for ($day = $firstDay; $day <= intval($lastDay); $day++) { $val = 0; foreach ($days as $dayData) { if ($dayData['DAY'] == $day) { $val = $dayData['COUNT']; break; } } $months[] = [ 'label' => $day, 'value' => $val, ]; } return $months; } /** * 填充本周每日检查数量,补齐周几信息,如周一 周二,到今日;没有返回的统一赋值为0 */ public function _fillWeekDayData($days) { $w = date('w'); $weeks = []; $i = 0; while($w - $i >= 1) { array_unshift($weeks, [ 'label' => date('Ymd', strtotime('-'.$i.' day')), 'value' => 0, ]); $i++; } foreach ($weeks as &$weekData) { foreach ($days as $dayData) { if ($dayData['day'] == $weekData['label']) { $weekData['value'] = $dayData['count']; break; } } } return $weeks; } /** * 单日新增 本月每日上传报告的患者数量,日历图 */ public function _getMonthDayData($where = false) { $days = $this->baseModel ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m%d") AS day') ->where($where) ->whereMonth($this->timeFieldName) ->group(date_format($this->timeFieldName, "%Y%m%d")) ->order(date_format($this->timeFieldName, "%Y%m%d"), 'asc') ->buildSql(); var_dump($days);die; return $days; } /** * 本周每日上传报告的患者数量,柱状图(月度数据不足7天时使用) */ public function getWeekCount($where = false) { $res = $this->baseModel ->field('count('.$this->countField.') AS count') ->where($where) ->whereWeek($this->timeFieldName) ->select() ->toArray(); return $res[0]['count']; } /** * 单日新增 本月每日上传报告的患者数量,日历图 */ public function getMonthCount($where = false) { $res = $this->baseModel ->field('count('.$this->countField.') AS count') ->where($where) ->whereMonth($this->timeFieldName) ->select() ->toArray(); return $res[0]['count']; } /** * 本周每日上传报告的患者数量,柱状图(月度数据不足7天时使用) */ public function _getWeekDayData($where = false) { $days = $this->baseModel ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m%d") AS day') ->where($where) ->whereWeek($this->timeFieldName) ->group(date_format($this->timeFieldName, "%Y%m%d")) ->order(date_format($this->timeFieldName, "%Y%m%d"), 'asc') ->select() ->toArray(); return $days; } }