BaseDataBiServies.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\servies\bi;
  4. abstract class BaseDataBiServies
  5. {
  6. /**
  7. * 时间筛选字段
  8. */
  9. protected $timeFieldName = 'create_time';
  10. /**
  11. * 统计主表模型
  12. */
  13. protected $baseModel;
  14. /**
  15. * 数据统计主字段
  16. */
  17. protected $countField = '*';
  18. /**
  19. * 报告统计字段
  20. */
  21. protected $repoertCountField = '*';
  22. /**
  23. * 获取通用where条件
  24. */
  25. protected function getCommonWhere($params) {
  26. return $this->_getCommonWhere($params, $this->timeFieldName);
  27. }
  28. protected function _getCommonWhere($params, $timeName) {
  29. $where = false;
  30. $ORGNAME = $params['ORGNAME'] ?? false;
  31. $start = $params['start'] ?? false;
  32. $end = $params['end'] ?? false;
  33. if($ORGNAME) {
  34. $_where = ['ORGNAME', '=', $ORGNAME];
  35. $where = [$_where];
  36. }
  37. if($start) {
  38. $_where = [$timeName, '>=', $start];
  39. if($where) {
  40. $where[] = $_where;
  41. } else {
  42. $where = [$_where];
  43. }
  44. }
  45. if($end) {
  46. $_where = [$timeName, '<=', $end];
  47. if($where) {
  48. $where[] = $_where;
  49. } else {
  50. $where = [$_where];
  51. }
  52. }
  53. return $where;
  54. }
  55. /**
  56. * 获取机构数据情况
  57. */
  58. public function getOrg($where = false) {
  59. $orgs = $this->_getOrg($where);
  60. $orgs = $this->_fillOrg($orgs);
  61. return $orgs;
  62. }
  63. public function _fillOrg($orgs) {
  64. foreach($orgs as &$org) {
  65. $org['label'] = $org['ORGNAME'];
  66. $org['value'] = $org['count'];
  67. unset($org['ORGNAME']);
  68. unset($org['count']);
  69. }
  70. return $orgs;
  71. }
  72. public function _getOrg($where = false) {
  73. $orgs = $this->baseModel
  74. ->field('count('.$this->countField.') AS count, ORGNAME')
  75. ->where($where)
  76. ->group('ORGNAME')
  77. ->order('count', 'desc')
  78. ->select()
  79. ->toArray();
  80. return $orgs;
  81. }
  82. /**
  83. * 获取近6月数据情况,不足统一复制0
  84. */
  85. public function getMonth($where = false) {
  86. $months = $this->_getMonth($where);
  87. $months = $this->_fillMonth($months);
  88. return $months;
  89. }
  90. public function _fillMonth($_months) {
  91. $months = [];
  92. $i = 0;
  93. while($i < 6) {
  94. array_unshift($months, [
  95. 'label' => date('Ym', strtotime('-'.$i.' month')),
  96. 'value' => 0,
  97. ]);
  98. $i++;
  99. }
  100. foreach ($months as &$monthData) {
  101. foreach ($_months as $_month) {
  102. if ($_month['month'] == $monthData['label']) {
  103. $monthData['value'] = $_month['count'];
  104. break;
  105. }
  106. }
  107. }
  108. return $months;
  109. }
  110. public function _getMonth($where = false) {
  111. $months = $this->baseModel
  112. ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m") AS month')
  113. ->where($where)
  114. ->whereTime($this->timeFieldName, '>=', date('Y-m-01 00:00:00', strtotime('-5 months')))
  115. ->group(date_format($this->timeFieldName, "%Y%m"))
  116. ->order(date_format($this->timeFieldName, "%Y%m"), 'asc')
  117. ->select()
  118. ->toArray();
  119. return $months;
  120. }
  121. /**
  122. * 获取所有数据条数
  123. */
  124. public function getAllCount($where = false) {
  125. $res = $this->baseModel
  126. ->field('count('.$this->countField.') AS count')
  127. ->where($where)
  128. ->select()
  129. ->toArray();
  130. return $res[0]['count'];
  131. }
  132. public function getDayData($where = false) {
  133. // 获取今天是本月第几日
  134. $j = date('j');
  135. // 获取今天是本周第几日
  136. $w = date('w');
  137. $days = false;
  138. if($j > $w) {
  139. $days = $this->_getMonthDayData($where);
  140. } else {
  141. $days = $this->_getWeekDayData($where);
  142. }
  143. // 获取本月每日检查数量
  144. $months = $this->_fillMonthDayData($days);
  145. // 获取本周每日检查数量
  146. $weeks = $this->_fillWeekDayData($days);
  147. // 获取今日检查数量
  148. $day = $this->_fillDayCount($days);
  149. return [
  150. 'months' => $months,
  151. 'weeks' => $weeks,
  152. 'day' => $day,
  153. ];
  154. }
  155. /**
  156. * 格式化本日检查数量,没有返回的统一赋值为0
  157. */
  158. public function _fillDayCount($days) {
  159. foreach($days as $day) {
  160. if($day['day'] === date('Ymd')) {
  161. return $day['count'];
  162. }
  163. }
  164. return 0;
  165. }
  166. /**
  167. * 填充本月每日检查数量,补齐日期信息从01到今日,没有返回的统一赋值为0
  168. */
  169. public function _fillMonthDayData($days) {
  170. $months = array();
  171. $firstDay = intval(date('Ym01'));
  172. $lastDay = intval(date('Ymd'));
  173. for ($day = $firstDay; $day <= intval($lastDay); $day++) {
  174. $val = 0;
  175. foreach ($days as $dayData) {
  176. if ($dayData['DAY'] == $day) {
  177. $val = $dayData['COUNT'];
  178. break;
  179. }
  180. }
  181. $months[] = [
  182. 'label' => $day,
  183. 'value' => $val,
  184. ];
  185. }
  186. return $months;
  187. }
  188. /**
  189. * 填充本周每日检查数量,补齐周几信息,如周一 周二,到今日;没有返回的统一赋值为0
  190. */
  191. public function _fillWeekDayData($days) {
  192. $w = date('w');
  193. $weeks = [];
  194. $i = 0;
  195. while($w - $i >= 1) {
  196. array_unshift($weeks, [
  197. 'label' => date('Ymd', strtotime('-'.$i.' day')),
  198. 'value' => 0,
  199. ]);
  200. $i++;
  201. }
  202. foreach ($weeks as &$weekData) {
  203. foreach ($days as $dayData) {
  204. if ($dayData['day'] == $weekData['label']) {
  205. $weekData['value'] = $dayData['count'];
  206. break;
  207. }
  208. }
  209. }
  210. return $weeks;
  211. }
  212. /**
  213. * 单日新增 本月每日上传报告的患者数量,日历图
  214. */
  215. public function _getMonthDayData($where = false) {
  216. $days = $this->baseModel
  217. ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m%d") AS day')
  218. ->where($where)
  219. ->whereMonth($this->timeFieldName)
  220. ->group(date_format($this->timeFieldName, "%Y%m%d"))
  221. ->order(date_format($this->timeFieldName, "%Y%m%d"), 'asc')
  222. ->buildSql();
  223. var_dump($days);die;
  224. return $days;
  225. }
  226. /**
  227. * 本周每日上传报告的患者数量,柱状图(月度数据不足7天时使用)
  228. */
  229. public function getWeekCount($where = false) {
  230. $res = $this->baseModel
  231. ->field('count('.$this->countField.') AS count')
  232. ->where($where)
  233. ->whereWeek($this->timeFieldName)
  234. ->select()
  235. ->toArray();
  236. return $res[0]['count'];
  237. }
  238. /**
  239. * 单日新增 本月每日上传报告的患者数量,日历图
  240. */
  241. public function getMonthCount($where = false) {
  242. $res = $this->baseModel
  243. ->field('count('.$this->countField.') AS count')
  244. ->where($where)
  245. ->whereMonth($this->timeFieldName)
  246. ->select()
  247. ->toArray();
  248. return $res[0]['count'];
  249. }
  250. /**
  251. * 本周每日上传报告的患者数量,柱状图(月度数据不足7天时使用)
  252. */
  253. public function _getWeekDayData($where = false) {
  254. $days = $this->baseModel
  255. ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m%d") AS day')
  256. ->where($where)
  257. ->whereWeek($this->timeFieldName)
  258. ->group(date_format($this->timeFieldName, "%Y%m%d"))
  259. ->order(date_format($this->timeFieldName, "%Y%m%d"), 'asc')
  260. ->select()
  261. ->toArray();
  262. return $days;
  263. }
  264. }