123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- declare (strict_types=1);
- namespace app\admin\servies\bi;
- abstract class BaseDataBiServies
- {
- /**
- * 时间筛选字段
- */
- protected $timeFieldName = 'create_time';
- /**
- * 统计主表模型
- */
- protected $baseModel;
- /**
- * 数据统计主字段
- */
- protected $countField = '*';
- /**
- * 报告统计字段
- */
- protected $repoertCountField = '*';
- /**
- * 获取通用where条件
- */
- protected function getCommonWhere($params) {
- return $this->_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;
- }
- }
|