BaseDataBiServies.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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('month')
  116. ->order('month', '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. var_dump(9);
  139. if($j > $w) {
  140. $days = $this->_getMonthDayData($where);
  141. } else {
  142. $days = $this->_getWeekDayData($where);
  143. }
  144. var_dump(1);
  145. // 获取本月每日检查数量
  146. $months = $this->_fillMonthDayData($days);
  147. var_dump(2);
  148. // 获取本周每日检查数量
  149. $weeks = $this->_fillWeekDayData($days);
  150. var_dump(3);
  151. // 获取今日检查数量
  152. $day = $this->_fillDayCount($days);
  153. var_dump(4);
  154. return [
  155. 'months' => $months,
  156. 'weeks' => $weeks,
  157. 'day' => $day,
  158. ];
  159. }
  160. /**
  161. * 格式化本日检查数量,没有返回的统一赋值为0
  162. */
  163. public function _fillDayCount($days) {
  164. foreach($days as $day) {
  165. if($day['day'] === date('Ymd')) {
  166. return $day['count'];
  167. }
  168. }
  169. return 0;
  170. }
  171. /**
  172. * 填充本月每日检查数量,补齐日期信息从01到今日,没有返回的统一赋值为0
  173. */
  174. public function _fillMonthDayData($days) {
  175. $months = array();
  176. $firstDay = intval(date('Ym01'));
  177. $lastDay = intval(date('Ymd'));
  178. for ($day = $firstDay; $day <= intval($lastDay); $day++) {
  179. $val = 0;
  180. foreach ($days as $dayData) {
  181. if ($dayData['DAY'] == $day) {
  182. $val = $dayData['COUNT'];
  183. break;
  184. }
  185. }
  186. $months[] = [
  187. 'label' => $day,
  188. 'value' => $val,
  189. ];
  190. }
  191. return $months;
  192. }
  193. /**
  194. * 填充本周每日检查数量,补齐周几信息,如周一 周二,到今日;没有返回的统一赋值为0
  195. */
  196. public function _fillWeekDayData($days) {
  197. $w = date('w');
  198. $weeks = [];
  199. $i = 0;
  200. while($w - $i >= 1) {
  201. array_unshift($weeks, [
  202. 'label' => date('Ymd', strtotime('-'.$i.' day')),
  203. 'value' => 0,
  204. ]);
  205. $i++;
  206. }
  207. foreach ($weeks as &$weekData) {
  208. foreach ($days as $dayData) {
  209. if ($dayData['day'] == $weekData['label']) {
  210. $weekData['value'] = $dayData['count'];
  211. break;
  212. }
  213. }
  214. }
  215. return $weeks;
  216. }
  217. /**
  218. * 单日新增 本月每日上传报告的患者数量,日历图
  219. */
  220. public function _getMonthDayData($where = false) {
  221. $days = $this->baseModel
  222. ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m%d") AS day')
  223. ->where($where)
  224. ->whereMonth($this->timeFieldName)
  225. ->group('day')
  226. ->order('day', 'asc')
  227. ->buildSql();
  228. var_dump($days);
  229. return $days;
  230. }
  231. /**
  232. * 本周每日上传报告的患者数量,柱状图(月度数据不足7天时使用)
  233. */
  234. public function getWeekCount($where = false) {
  235. $res = $this->baseModel
  236. ->field('count('.$this->countField.') AS count')
  237. ->where($where)
  238. ->whereWeek($this->timeFieldName)
  239. ->select()
  240. ->toArray();
  241. return $res[0]['count'];
  242. }
  243. /**
  244. * 单日新增 本月每日上传报告的患者数量,日历图
  245. */
  246. public function getMonthCount($where = false) {
  247. $res = $this->baseModel
  248. ->field('count('.$this->countField.') AS count')
  249. ->where($where)
  250. ->whereMonth($this->timeFieldName)
  251. ->select()
  252. ->toArray();
  253. return $res[0]['count'];
  254. }
  255. /**
  256. * 本周每日上传报告的患者数量,柱状图(月度数据不足7天时使用)
  257. */
  258. public function _getWeekDayData($where = false) {
  259. $days = $this->baseModel
  260. ->field('count('.$this->countField.') AS count, date_format('.$this->timeFieldName.', "%Y%m%d") AS day')
  261. ->where($where)
  262. ->whereWeek($this->timeFieldName)
  263. ->group('day')
  264. ->order('day', 'asc')
  265. ->select()
  266. ->toArray();
  267. return $days;
  268. }
  269. }