BiService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <?php
  2. namespace app\index\service;
  3. use app\index\model\Bi;
  4. use app\admin\model\exams\ExamsModel;
  5. use app\admin\model\remote\Remote;
  6. // use app\admin\model\report\Report;
  7. use app\admin\model\patient\PatientInfoModel;
  8. use think\Cache;
  9. use think\Db;
  10. class BiService
  11. {
  12. protected $model = null;
  13. protected $examsModel = null;
  14. public function __construct()
  15. {
  16. $this->model = new Bi();
  17. $this->examsModel = new ExamsModel();
  18. $this->remoteModel = new Remote();
  19. // $this->reportModel = new Report();
  20. $this->patientModel = new PatientInfoModel();
  21. }
  22. // 获取最新20条检查数据
  23. public function getExams($id) {
  24. $info = Cache::get($id.'exams');
  25. if(empty($info))
  26. {
  27. $data = $this->examsModel
  28. ->field('name, exam_project, exam_class, exam_datetime, createdAt')
  29. ->where('institution_id', $id)
  30. ->order('createdAt', 'desc')
  31. ->limit(20)
  32. ->select();
  33. Cache::set($id.'exams',$data,600);
  34. }else{
  35. return $info;
  36. }
  37. return $data;
  38. }
  39. // 获取七日内检查数据
  40. public function getStudy($id) {
  41. $info = Cache::get($id.'study');
  42. if(empty($info))
  43. {
  44. $group = $this->examsModel
  45. ->field('date_format(createdAt, "%Y.%m.%d") as day, count(*) as count')
  46. ->where('institution_id', $id)
  47. ->where('createdAt', '>', $this->_getWeekTime())
  48. // ->where('createdAt', '<', $this->_getYesterdayTime())
  49. ->group('day')
  50. ->select();
  51. Cache::set($id.'study',$group,600);
  52. }else{
  53. $group = $info;
  54. }
  55. return $this->_formatWeekData($group);
  56. }
  57. // 获取七日内远程诊断数据
  58. public function getRemote($id) {
  59. $info = Cache::get($id.'remote');
  60. if(empty($info))
  61. {
  62. $group = $this->remoteModel
  63. ->field('date_format(createdAt, "%Y.%m.%d") as day, count(*) as count')
  64. ->where('local_institution_id', $id)
  65. ->where('createdAt', '>', $this->_getWeekTime())
  66. // ->where('createdAt', '<', $this->_getYesterdayTime())
  67. ->group('day')
  68. ->select();
  69. Cache::set($id.'remote',$group,600);
  70. }else{
  71. $group = $info;
  72. }
  73. return $this->_formatWeekData($group);
  74. }
  75. // 获取七日内阳性数据(直查)
  76. // public function getPositive($id) {
  77. // $group = $this->examsModel
  78. // ->field('date_format(createdAt, "%Y.%m.%d") as day, count(*) as count')
  79. // ->where('institution_id', $id)
  80. // ->where('report_result', 2)
  81. // ->where('createdAt', '>', $this->_getWeekTime())
  82. // ->group('day')
  83. // ->select();
  84. // return $this->_formatWeekData($group);
  85. // }
  86. // 获取七日内阳性数据(连表)
  87. public function getPositive($id) {
  88. $info = Cache::get($id.'positive');
  89. if(empty($info))
  90. {
  91. $group = $this->examsModel
  92. ->alias('e')
  93. ->join(['report'=>'r'],'r.exam_id=e.id')
  94. ->field('date_format(e.createdAt, "%Y.%m.%d") as day, count(*) as count')
  95. ->where('e.institution_id', $id)
  96. ->where('r.report_result', 2)
  97. ->where('r.type', 1)
  98. ->where('e.createdAt', '>', $this->_getWeekTime())
  99. // ->where('e.createdAt', '<', $this->_getYesterdayTime())
  100. ->group('day')
  101. ->select();
  102. Cache::set($id.'positive',$group,600);
  103. }else{
  104. $group = $info;
  105. }
  106. return $this->_formatWeekData($group);
  107. }
  108. // 科室检查统计
  109. public function getDepartment($id)
  110. {
  111. $info = Cache::get($id.'getDepartment');
  112. if(empty($info))
  113. {
  114. $today = $this->_getDepartment($id, $this->_getTodayTime());
  115. $month = $this->_getDepartment($id, $this->_getMonthTime());
  116. $all = $this->_getDepartment($id);
  117. Cache::set($id.'getDepartment',[
  118. "today" => $today,
  119. "month" => $month,
  120. "all" => $all
  121. ],600);
  122. return [
  123. "today" => $today,
  124. "month" => $month,
  125. "all" => $all
  126. ];
  127. }else{
  128. return $info;
  129. }
  130. }
  131. // 科室检查统计
  132. public function getDepartmentToday($id)
  133. {
  134. $info = Cache::get($id.'getDepartmentToday');
  135. if(empty($info))
  136. {
  137. $today = $this->_getDepartment($id, $this->_getTodayTime());
  138. Cache::set($id.'getDepartmentToday',[
  139. "today" => $today
  140. ],600);
  141. return [
  142. "today" => $today
  143. ];
  144. }else{
  145. return $info;
  146. }
  147. }
  148. // 科室检查统计
  149. public function getModality($id)
  150. {
  151. $info = Cache::get($id.'getModality');
  152. if(empty($info))
  153. {
  154. $today = $this->_getModality($id, $this->_getTodayTime());
  155. $month = $this->_getModality($id, $this->_getMonthTime());
  156. $all = $this->_getModality($id);
  157. Cache::set($id.'getModality',[
  158. "today" => $today,
  159. "month" => $month,
  160. "all" => $all
  161. ],600);
  162. return [
  163. "today" => $today,
  164. "month" => $month,
  165. "all" => $all
  166. ];
  167. }else{
  168. return $info;
  169. }
  170. }
  171. // 科室检查统计
  172. public function getModalityToday($id)
  173. {
  174. $info = Cache::get($id.'getModalityToday');
  175. if(empty($info))
  176. {
  177. $today = $this->_getModality($id, $this->_getTodayTime());
  178. Cache::set($id.'getModalityToday',[
  179. "today" => $today
  180. ],60);
  181. return [
  182. "today" => $today
  183. ];
  184. }else{
  185. return $info;
  186. }
  187. }
  188. // 年龄统计
  189. public function getAge($id)
  190. {
  191. $info = Cache::get($id.'getAge');
  192. if(empty($info))
  193. {
  194. $group = $this->examsModel
  195. ->field('CASE WHEN age + 0 > 60 THEN "60岁以上" WHEN age + 0 > 40 THEN "40-60岁" WHEN age + 0 > 20 THEN "20-40岁" ELSE "0-20岁" END as age_range, count(*) as count')
  196. ->where('institution_id', $id)
  197. ->group('age_range')
  198. ->select();
  199. Cache::set($id.'getAge',$group,600);
  200. }else{
  201. $group = $info;
  202. }
  203. return $group;
  204. }
  205. // 年龄统计
  206. // todo
  207. public function getAgeToday($id)
  208. {
  209. $info = Cache::get($id.'getAgeToday');
  210. if(empty($info))
  211. {
  212. $group = $this->examsModel
  213. ->field('CASE WHEN age + 0 > 60 THEN "60岁以上" WHEN age + 0 > 40 THEN "40-60岁" WHEN age + 0 > 20 THEN "20-40岁" ELSE "0-20岁" END as age_range, count(*) as count')
  214. ->where('institution_id', $id)
  215. ->group('age_range')
  216. ->select();
  217. Cache::set($id.'getAgeToday',$group,60);
  218. }else{
  219. $group = $info;
  220. }
  221. return $group;
  222. }
  223. // 主页数据
  224. public function getMain($id)
  225. {
  226. $info = Cache::get($id.'getMain');
  227. if(empty($info)) {
  228. $allPositive = $this->_getPositiveCount($id);
  229. $allRemote = $this->_getRemoteCount($id);
  230. $allStudy = $this->_getStudyCount($id);
  231. $todayPositive = $this->_getPositiveCount($id, $this->_getTodayTime());
  232. $todayRemote = $this->_getRemoteCount($id, $this->_getTodayTime());
  233. $todayStudy = $this->_getStudyCount($id, $this->_getTodayTime());
  234. $allPositiveRate = $allPositive == 0 ? 0: $allPositive * 100 / $allStudy;
  235. $todayPositiveRate = $todayPositive == 0 ? 0: ($todayPositive * 100 / $todayStudy) ."%";
  236. Cache::set($id.'getMain',[
  237. "allPositive" => $allPositive,
  238. "allRemote" => $allRemote,
  239. "allStudy" => $allStudy,
  240. "todayPositive" => $todayPositive,
  241. "todayRemote" => $todayRemote,
  242. "todayStudy" => $todayStudy,
  243. "allPositiveRate" => $allPositiveRate,
  244. "todayPositiveRate" => $todayPositiveRate
  245. ],600);
  246. return [
  247. "allPositive" => $allPositive,
  248. "allRemote" => $allRemote,
  249. "allStudy" => $allStudy,
  250. "todayPositive" => $todayPositive,
  251. "todayRemote" => $todayRemote,
  252. "todayStudy" => $todayStudy,
  253. "allPositiveRate" => $allPositiveRate,
  254. "todayPositiveRate" => $todayPositiveRate
  255. ];
  256. }else{
  257. return $info;
  258. }
  259. }
  260. // 主页数据
  261. public function getMainToday($id)
  262. {
  263. $info = Cache::get($id.'getMainToday');
  264. if(empty($info)) {
  265. $todayPositive = $this->_getPositiveCount($id, $this->_getTodayTime());
  266. $todayRemote = $this->_getRemoteCount($id, $this->_getTodayTime());
  267. $todayStudy = $this->_getStudyCount($id, $this->_getTodayTime());
  268. $todayPositiveRate = $todayPositive == 0 ? 0: ($todayPositive * 100 / $todayStudy) ."%";
  269. Cache::set($id.'getMainToday',[
  270. "todayPositive" => $todayPositive,
  271. "todayRemote" => $todayRemote,
  272. "todayStudy" => $todayStudy,
  273. "todayPositiveRate" => $todayPositiveRate
  274. ],60);
  275. return [
  276. "todayPositive" => $todayPositive,
  277. "todayRemote" => $todayRemote,
  278. "todayStudy" => $todayStudy,
  279. "todayPositiveRate" => $todayPositiveRate
  280. ];
  281. }else{
  282. return $info;
  283. }
  284. }
  285. // 运营数据
  286. public function getOperate($id)
  287. {
  288. $info = Cache::get($id.'getOperate');
  289. if(empty($info))
  290. {
  291. $today = $this->_getOperate($id, $this->_getTodayTime());
  292. $month = $this->_getOperate($id, $this->_getMonthTime());
  293. $all = $this->_getOperate($id);
  294. Cache::set($id.'getOperate',[
  295. "today" => $today,
  296. "month" => $month,
  297. "all" => $all
  298. ],600);
  299. return [
  300. "today" => $today,
  301. "month" => $month,
  302. "all" => $all
  303. ];
  304. }else{
  305. return $info;
  306. }
  307. }
  308. // 运营数据
  309. public function getOperateToday($id)
  310. {
  311. $info = Cache::get($id.'getOperateToday');
  312. if(empty($info))
  313. {
  314. $today = $this->_getOperate($id, $this->_getTodayTime());
  315. Cache::set($id.'getOperateToday',[
  316. "today" => $today
  317. ],60);
  318. return [
  319. "today" => $today
  320. ];
  321. }else{
  322. return $info;
  323. }
  324. }
  325. // 部位数据
  326. public function getArea($id)
  327. {
  328. $info = Cache::get($id.'getArea');
  329. if(empty($info))
  330. {
  331. $today = $this->_getArea($id, $this->_getTodayTime());
  332. $month = $this->_getArea($id, $this->_getMonthTime());
  333. $all = $this->_getArea($id);
  334. Cache::set($id.'getArea',[
  335. "today" => $today,
  336. "month" => $month,
  337. "all" => $all
  338. ],600);
  339. return [
  340. "today" => $today,
  341. "month" => $month,
  342. "all" => $all
  343. ];
  344. }else{
  345. return $info;
  346. }
  347. }
  348. // 部位数据
  349. public function getAreaToday($id)
  350. {
  351. $info = Cache::get($id.'getAreaToday');
  352. if(empty($info))
  353. {
  354. $today = $this->_getArea($id, $this->_getTodayTime());
  355. Cache::set($id.'getAreaToday',[
  356. "today" => $today
  357. ],60);
  358. return [
  359. "today" => $today
  360. ];
  361. }else{
  362. return $info;
  363. }
  364. }
  365. // 患者来源
  366. public function getSource($id)
  367. {
  368. $info = Cache::get($id.'getSource');
  369. if(empty($info))
  370. {
  371. $today = $this->_getSource($id, $this->_getTodayTime());
  372. $month = $this->_getSource($id, $this->_getMonthTime());
  373. $all = $this->_getSource($id);
  374. Cache::set($id.'getSource',[
  375. "today" => $today,
  376. "month" => $month,
  377. "all" => $all
  378. ],600);
  379. return [
  380. "today" => $today,
  381. "month" => $month,
  382. "all" => $all
  383. ];
  384. }else{
  385. return $info;
  386. }
  387. }
  388. // 患者来源
  389. public function getSourceToday($id)
  390. {
  391. $info = Cache::get($id.'getSourceToday');
  392. if(empty($info))
  393. {
  394. $today = $this->_getSource($id, $this->_getTodayTime());
  395. Cache::set($id.'getSourceToday',[
  396. "today" => $today
  397. ],60);
  398. return [
  399. "today" => $today
  400. ];
  401. }else{
  402. return $info;
  403. }
  404. }
  405. // TODO 科室数据
  406. public function getDoctor($id)
  407. {
  408. // TODO 新建表数据
  409. return [
  410. ["level" => "执业医师", "count" => 501],
  411. ["level" => "执业助理医师", "count" => 121],
  412. ["level" => "注册护士", "count" => 1524],
  413. ["level" => "其他", "count" => 326]
  414. ];
  415. }
  416. // TODO 设备数据
  417. public function getDevice($id)
  418. {
  419. // TODO 新建表数据
  420. $info = Cache::get($id.'getDevice');
  421. if(empty($info))
  422. {
  423. $today = $this->_getDevice($id, $this->_getTodayTime());
  424. $month = $this->_getDevice($id, $this->_getMonthTime());
  425. $all = $this->_getDevice($id);
  426. $data = [
  427. "today" => $today,
  428. "month" => $month,
  429. "all" => $all
  430. ];
  431. Cache::set($id.'getDevice',$data,600);
  432. return $data;
  433. }
  434. return $info;
  435. }
  436. // TODO 设备数据
  437. public function getDeviceList($id)
  438. {
  439. // TODO 新建表数据
  440. $info = Cache::get($id.'getDeviceList');
  441. if(empty($info))
  442. {
  443. $data = $this->_getDeviceList($id, $this->_getTodayTime());
  444. Cache::set($id.'getDeviceList',$data,600);
  445. return $data;
  446. }
  447. return $info;
  448. }
  449. public function getDeviceToday($id)
  450. {
  451. // TODO 新建表数据
  452. $info = Cache::get($id.'getDeviceToday');
  453. if(empty($info))
  454. {
  455. $today = $this->_getDevice($id, $this->_getTodayTime());
  456. $data = [
  457. "today" => $today
  458. ];
  459. Cache::set($id.'getDeviceToday',$data,60);
  460. return $data;
  461. }
  462. return $info;
  463. }
  464. // TODO 设备数据
  465. public function getExamProject($device, $id)
  466. {
  467. // TODO 新建表数据
  468. $info = Cache::get($id.'getExamProject'.$device);
  469. if(empty($info))
  470. {
  471. $today = $this->_getExamProject($device, $id, $this->_getTodayTime());
  472. $month = $this->_getExamProject($device, $id, $this->_getMonthTime());
  473. $all = $this->_getExamProject($device, $id);
  474. $data = [
  475. "today" => $today,
  476. "month" => $month,
  477. "all" => $all
  478. ];
  479. Cache::set($id.'getExamProject'.$device,$data,600);
  480. return $data;
  481. }
  482. return $info;
  483. }
  484. // TODO 设备数据
  485. public function getExamProjectToday($device, $id)
  486. {
  487. // TODO 新建表数据
  488. $info = Cache::get($id.'getExamProjectToday'.$device);
  489. if(empty($info))
  490. {
  491. $today = $this->_getExamProject($device, $id, $this->_getTodayTime());
  492. $data = [
  493. "today" => $today
  494. ];
  495. Cache::set($id.'getExamProjectToday'.$device,$data,60);
  496. return $data;
  497. }
  498. return $info;
  499. }
  500. // 检查设备
  501. public function _getDevice($id, $time = false)
  502. {
  503. $model = $this->examsModel;
  504. if($time) {
  505. $model->where('createdAt', '>', $time);
  506. }
  507. $group = $model->field('device_name as name, count(*) as count')
  508. ->where('institution_id', $id)
  509. ->whereNotNull('device_name')
  510. ->group('device_name')
  511. ->order('count', 'desc')
  512. ->limit(5)
  513. ->select();
  514. return $group;
  515. }
  516. // 检查设备
  517. public function _getDeviceList($id, $time = false)
  518. {
  519. $model = $this->examsModel;
  520. $group = $model->field('device_name as name')
  521. ->where('institution_id', $id)
  522. ->whereNotNull('device_name')
  523. ->group('device_name')
  524. ->select();
  525. return $group;
  526. }
  527. // 检查方法
  528. public function _getExamProject($device, $id, $time = false)
  529. {
  530. $model = $this->examsModel;
  531. if($time) {
  532. $model->where('createdAt', '>', $time);
  533. }
  534. if($device !== 'all') {
  535. $model->where('device_name', $device);
  536. }
  537. $group = $model->field('exam_project as name, count(*) as count')
  538. ->where('institution_id', $id)
  539. ->whereNotNull('exam_project')
  540. ->group('exam_project')
  541. ->order('count', 'desc')
  542. ->limit(5)
  543. ->select();
  544. return $group;
  545. }
  546. // 检查部位
  547. public function _getArea($id, $time = false)
  548. {
  549. $model = $this->examsModel;
  550. if($time) {
  551. $model->where('createdAt', '>', $time);
  552. }
  553. $group = $model->field('body_part as name, count(*) as count')
  554. ->where('institution_id', $id)
  555. ->whereNotNull('body_part')
  556. ->group('body_part')
  557. ->order('count', 'desc')
  558. ->limit(5)
  559. ->select();
  560. return $group;
  561. }
  562. // 运营数据
  563. public function _getOperate($id, $time = false) {
  564. $patient = $this->_getPatientCount($id, $time);
  565. $study = $this->_getStudyCount($id, $time);
  566. $remote = $this->_getRemoteCount($id, $time);
  567. $positive = $this->_getPositiveCount($id, $time);
  568. return [
  569. "patient" => $patient,
  570. "study" => $study,
  571. "remote" => $remote,
  572. "positive" => $positive
  573. ];
  574. }
  575. // 患者来源
  576. public function _getSource($id, $time = false)
  577. {
  578. $model = $this->examsModel;
  579. if($time) {
  580. $model->where('createdAt', '>', $time);
  581. }
  582. $group = $model->field('CASE WHEN patient_source = 1 THEN "急诊" WHEN patient_source = 2 THEN "住院" WHEN patient_source = 3 THEN "门诊" WHEN patient_source = 4 THEN "体检" ELSE "其他" END as source, count(*) as count')
  583. ->where('institution_id', $id)
  584. ->group('source')
  585. ->select();
  586. return $group;
  587. }
  588. // 患者数量
  589. public function _getPatientCount($id, $time = false)
  590. {
  591. $model = $this->patientModel;
  592. if($time) {
  593. $model->where('createdAt', '>', $time);
  594. }
  595. $count = $model->where('institution_id', $id)->count();
  596. return $count;
  597. }
  598. // 检查数量
  599. public function _getStudyCount($id, $time = false)
  600. {
  601. $model = $this->examsModel;
  602. if($time) {
  603. $model->where('createdAt', '>', $time);
  604. }
  605. $count = $model->where('institution_id', $id)->count();
  606. return $count;
  607. }
  608. // 远程诊断数量
  609. public function _getRemoteCount($id, $time = false)
  610. {
  611. $model = $this->remoteModel;
  612. if($time) {
  613. $model->where('createdAt', '>', $time);
  614. }
  615. $count = $model->where('local_institution_id', $id)->count();
  616. return $count;
  617. }
  618. // 阳性数量
  619. public function _getPositiveCount($id, $time = false)
  620. {
  621. $model = $this->examsModel
  622. ->alias('e')
  623. ->join(['report'=>'r'],'r.exam_id=e.id')
  624. ->field('date_format(e.createdAt, "%Y.%m.%d") as day, count(*) as count')
  625. ->where('e.institution_id', $id)
  626. ->where('r.report_result', 2)
  627. ->where('r.type', 1);
  628. if($time) {
  629. $model->where('e.createdAt', '>', $time);
  630. }
  631. $count = $model->count();
  632. return $count;
  633. }
  634. // 科室检查统计
  635. public function _getDepartment($id, $time = false)
  636. {
  637. $model = $this->examsModel;
  638. if($time) {
  639. $model->where('createdAt', '>', $time);
  640. }
  641. $group = $model->field('application_department as name, count(*) as count')
  642. ->where('institution_id', $id)
  643. ->whereNotNull('application_department')
  644. // ->where('createdAt', '>', $time)
  645. ->group('application_department')
  646. ->order('count', 'desc')
  647. ->limit(5)
  648. ->select();
  649. return $group;
  650. }
  651. // 科室检查统计
  652. public function _getModality($id, $time = false)
  653. {
  654. $model = $this->examsModel;
  655. if($time) {
  656. $model->where('createdAt', '>', $time);
  657. }
  658. $group = $model->field('exam_class as name, count(*) as count')
  659. ->where('institution_id', $id)
  660. ->whereNotNull('exam_class')
  661. // ->where('createdAt', '>', $time)
  662. ->group('exam_class')
  663. ->order('count', 'desc')
  664. ->limit(5)
  665. ->select();
  666. return $group;
  667. }
  668. // 今天
  669. public function _getTodayTime()
  670. {
  671. return date('Y-m-d 00:00:00');
  672. }
  673. // 七天
  674. public function _getYesterdayTime()
  675. {
  676. $time = strtotime('-1 days');
  677. return date('Y-m-d 00:00:00',$time);
  678. }
  679. // 七天
  680. public function _getWeekTime()
  681. {
  682. $time = strtotime('-6 days');
  683. return date('Y-m-d 00:00:00',$time);
  684. }
  685. // 本月1日
  686. public function _getMonthTime()
  687. {
  688. return date('Y-m-01 00:00:00');
  689. }
  690. // 获取时间字符串key值
  691. public function _getKeyFromToday($day)
  692. {
  693. $time = strtotime($day.' days');
  694. return date('Y.m.d', $time);
  695. }
  696. // 格式化7天数据
  697. // 7天数据量较少,没有封装生成数据方法,直接嵌套循环,如果数据量较大,考虑减少嵌套
  698. public function _formatWeekData($data)
  699. {
  700. $res = [
  701. ["day" => $this->_getKeyFromToday(-6), "count" => 0],
  702. ["day" => $this->_getKeyFromToday(-5), "count" => 0],
  703. ["day" => $this->_getKeyFromToday(-4), "count" => 0],
  704. ["day" => $this->_getKeyFromToday(-3), "count" => 0],
  705. ["day" => $this->_getKeyFromToday(-2), "count" => 0],
  706. ["day" => $this->_getKeyFromToday(-1), "count" => 0],
  707. ["day" => $this->_getKeyFromToday(-0), "count" => 0],
  708. ];
  709. foreach($res as $key => $val1 ) {
  710. foreach($data as $val2) {
  711. if($val1['day'] == $val2['day']) {
  712. $res[$key]['count'] = $val2['count'];
  713. }
  714. }
  715. }
  716. return $res;
  717. }
  718. public function downExcel($institution)
  719. {
  720. set_time_limit(0);
  721. import('phpexcel.PHPExcel', EXTEND_PATH);
  722. $objPHPExcel = new \PHPExcel();
  723. $allData = Db::table('exams')->where('institution_id','43600001')->group('device_name')->cache('43600001',3600)->column('device_name,count(*) as c');
  724. $device = array_keys($allData);
  725. $month = date('Y-m-01 00:00:00');
  726. $monthData = Db::table('exams')->where('institution_id','43600001')->where("createdAt > '$month'")->group('device_name')->cache('43600001_month',3600)->column('device_name,count(*) as c');
  727. $today = date('Y-m-d 00:00:00');
  728. $todayData = Db::table('exams')->where('institution_id','43600001')->where("createdAt > '$today'")->group('device_name')->cache('43600001_today',3600)->column('device_name,count(*) as c');
  729. $objPHPExcel->getActiveSheet()->setTitle('设备检查人次统计');
  730. $objPHPExcel->getActiveSheet()->setCellValue('A1', '设备名称');
  731. $objPHPExcel->getActiveSheet()->setCellValue('B1', '检查人次(今日)');
  732. $objPHPExcel->getActiveSheet()->setCellValue('C1', '检查人次(本月)');
  733. $objPHPExcel->getActiveSheet()->setCellValue('D1', '检查人次(总计)');
  734. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  735. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
  736. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
  737. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
  738. $i = 2;
  739. foreach ($allData as $k=>$v)
  740. {
  741. if(empty($k))
  742. {
  743. continue;
  744. }
  745. $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $k);
  746. $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $todayData[$k] ?? 0);
  747. $objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $monthData[$k] ?? 0);
  748. $objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $v);
  749. $i++;
  750. }
  751. $allBodyData = Db::table('exams')->where('institution_id','43600001')->group('exam_project')->cache('43600001_body',3600)->column('exam_project,count(*) as c');
  752. $monthBodyData = Db::table('exams')->where('institution_id','43600001')->where("createdAt > '$month'")->group('exam_project')->cache('43600001_month_body',3600)->column('exam_project,count(*) as c');
  753. $todayBodyData = Db::table('exams')->where('institution_id','43600001')->where("createdAt > '$today'")->group('exam_project')->cache('43600001_today_body',3600)->column('exam_project,count(*) as c');
  754. // 添加Worksheet2
  755. $sheet = 1;
  756. $objPHPExcel->createSheet();
  757. $objPHPExcel->setActiveSheetIndex($sheet);
  758. $objPHPExcel->getActiveSheet()->setTitle('检查部位统计');
  759. $objPHPExcel->getActiveSheet()->setCellValue('A1', '设备名称');
  760. $objPHPExcel->getActiveSheet()->setCellValue('B1', '今日');
  761. $objPHPExcel->getActiveSheet()->setCellValue('C1', '本月');
  762. $objPHPExcel->getActiveSheet()->setCellValue('D1', '总计');
  763. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(80);
  764. $i = 2;
  765. foreach ($allBodyData as $k=>$v)
  766. {
  767. if(empty($k))
  768. {
  769. continue;
  770. }
  771. $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $k);
  772. $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $todayBodyData[$k] ?? 0);
  773. $objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $monthBodyData[$k] ?? 0);
  774. $objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $v);
  775. $i++;
  776. }
  777. foreach ($device as $v)
  778. {
  779. if(empty($v))
  780. {
  781. continue;
  782. }
  783. $allDeviceData = Db::table('exams')->where('institution_id','43600001')->where('device_name',$v)->group('exam_project')->where('exam_project != ""')->cache('43600001_device_'.$v,3600)->column('exam_project,count(*) as c');
  784. if(empty($allDeviceData))
  785. {
  786. continue;
  787. }
  788. $monthDeviceData = Db::table('exams')->where('institution_id','43600001')->where('device_name',$v)->where("createdAt > '$month'")->group('exam_project')->cache('43600001_month_device_'.$v,3600)->column('exam_project,count(*) as c');
  789. $todayDeviceData = Db::table('exams')->where('institution_id','43600001')->where('device_name',$v)->where("createdAt > '$today'")->group('exam_project')->cache('43600001_today_device_'.$v,3600)->column('exam_project,count(*) as c');
  790. $objPHPExcel->createSheet();
  791. $sheet++;
  792. $objPHPExcel->setActiveSheetIndex($sheet);
  793. try {
  794. $objPHPExcel->getActiveSheet()->setTitle($v.'检查统计');
  795. }catch (\PHPExcel_Exception $e)
  796. {
  797. preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]+/u","$v",$arr);
  798. $title = (join('',$arr[0]));
  799. $objPHPExcel->getActiveSheet()->setTitle($title);
  800. }
  801. $objPHPExcel->getActiveSheet()->setCellValue('A1', '设备名称');
  802. $objPHPExcel->getActiveSheet()->setCellValue('B1', '今日');
  803. $objPHPExcel->getActiveSheet()->setCellValue('C1', '本月');
  804. $objPHPExcel->getActiveSheet()->setCellValue('D1', '总计');
  805. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(80);
  806. $i = 2;
  807. foreach ($allDeviceData as $k=>$value)
  808. {
  809. if(empty($k))
  810. {
  811. continue;
  812. }
  813. $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $k);
  814. $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $todayDeviceData[$k] ?? 0);
  815. $objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $monthDeviceData[$k] ?? 0);
  816. $objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $value);
  817. $i++;
  818. }
  819. }
  820. header('Content-Type: application/vnd.ms-excel');
  821. header('Content-Disposition: attachment;filename="' . time() . '.xls');
  822. header('Cache-Control: max-age=1');
  823. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  824. $objWriter->save('php://output');
  825. }
  826. }