Counts.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\manage\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Session;
  6. use think\Config;
  7. use think\Cookie;
  8. use think\Request;
  9. use app\common\library\SysLogs;
  10. use app\common\library\UUIDs;
  11. /**
  12. *
  13. */
  14. class Counts extends Base {
  15. public function index(){
  16. return $this->fetch();
  17. }
  18. public function datas(){
  19. $admin = Session::get('session_manager');
  20. $institution_id = $admin['institution_id'];
  21. $request = Request::instance();
  22. $params = $request->param();
  23. $where = array();
  24. foreach ($params as $k=>$v) {
  25. switch($k){
  26. case 'upload1':
  27. if($params['upload1'] == null){
  28. continue;
  29. }
  30. $where[] = ' s.createdAt > \''.$params['upload1'].'\' ';
  31. break;
  32. case 'upload2':
  33. if($params['upload2'] == null){
  34. continue;
  35. }
  36. $where[] = ' s.createdAt < \''.$params['upload2'].'\' ';
  37. break;
  38. case 'exam1':
  39. if($params['exam1'] == null){
  40. continue;
  41. }
  42. $exam1 = str_replace('-', '', $params['exam1']);
  43. $where[] = ' s.studydate > \''.$exam1.'\' ';
  44. break;
  45. case 'exam2':
  46. if($params['exam2'] == null){
  47. continue;
  48. }
  49. $exam2 = str_replace('-', '', $params['exam2']);
  50. $where[] = ' s.studydate < \''.$exam2.'\' ';
  51. break;
  52. case 'exam_class':
  53. if($params['exam_class'] == null){
  54. continue;
  55. }
  56. $where[] = 's.modality=\''.$params['exam_class'].'\' ';
  57. break;
  58. }
  59. }
  60. if(count($where) <= 1){
  61. $where1 = implode('',$where);
  62. $search = $where1;
  63. }else{
  64. $where1 = implode(' and ',$where);
  65. $search = $where1;
  66. }
  67. $page = empty($_GET["page"]) ? 1 : $_GET["page"];
  68. $pagesize = empty($_GET["rows"]) ? 1 : $_GET["rows"];
  69. if (empty($page) || $page < 1) {
  70. $page = 1;
  71. }
  72. if (empty($pagesize) || $pagesize < 1) {
  73. $pagesize = 30;
  74. }
  75. $start = $pagesize*($page-1);
  76. if($page != 1){
  77. $start = $start+1;
  78. }
  79. $finish = $pagesize*$page;
  80. $limit = "limit $start,$finish";
  81. /*$count = DB::table('studies')
  82. ->alias('s')
  83. ->join(['patient_infos'=>'p'],'p.id=s.patient_id')
  84. ->join(['user_bind'=>'u'],'u.patient_id=s.patient_id','left')
  85. ->where('s.institution_id='.$institution_id)
  86. ->where($search)
  87. ->count();
  88. $list = DB::table('studies')
  89. ->alias('s')
  90. ->join(['patient_infos'=>'p'],'p.id=s.patient_id')
  91. ->join(['user_bind'=>'u'],'u.patient_id=s.patient_id','left')
  92. ->where('s.institution_id='.$institution_id)
  93. ->where($search)
  94. ->page($page, $pagesize)
  95. ->field('p.name,p.age,p.sex,s.studydate,s.createdAt,s.modality,u.exam_id')
  96. ->order('s.createdAt desc')
  97. ->select();*/
  98. $count = "SELECT count(*) FROM `studies` `s` INNER JOIN `patient_infos` `p` ON `p`.`id`=`s`.`patient_id` LEFT JOIN ( SELECT `patient_id`, max(`createAt`) FROM `user_bind` group by `patient_id`) AS u ON `u`.`patient_id`=`s`.`patient_id` WHERE ( s.institution_id=$institution_id ) ORDER BY `s`.`createdAt`";
  99. $list = "SELECT `p`.`name`,`p`.`age`,`p`.`sex`,`s`.`studydate`,`s`.`createdAt`,`s`.`modality`,`u`.`patient_id` FROM `studies` `s` INNER JOIN `patient_infos` `p` ON `p`.`id`=`s`.`patient_id` LEFT JOIN ( SELECT `patient_id`, max(`createAt`) FROM `user_bind` group by `patient_id`) AS u ON `u`.`patient_id`=`s`.`patient_id` WHERE ( s.institution_id=$institution_id ) ORDER BY `s`.`createdAt` $limit";
  100. $data["total"] = $count;
  101. $data["rows"] = $list;
  102. echo json_encode($data);
  103. }
  104. public function out() {
  105. $admin = Session::get('session_manager');
  106. $institution_id = $admin['institution_id'];
  107. $request = Request::instance();
  108. $params = $request->param();
  109. $where = array();
  110. foreach ($params as $k=>$v) {
  111. switch($k){
  112. case 'upload1':
  113. if($params['upload1'] == null){
  114. continue;
  115. }
  116. $where[] = ' s.createdAt > \''.$params['upload1'].'\' ';
  117. break;
  118. case 'upload2':
  119. if($params['upload2'] == null){
  120. continue;
  121. }
  122. $where[] = ' s.createdAt < \''.$params['upload2'].'\' ';
  123. break;
  124. case 'exam1':
  125. if($params['exam1'] == null){
  126. continue;
  127. }
  128. $exam1 = str_replace('-', '', $params['exam1']);
  129. $where[] = ' s.studydate > \''.$exam1.'\' ';
  130. break;
  131. case 'exam2':
  132. if($params['exam2'] == null){
  133. continue;
  134. }
  135. $exam2 = str_replace('-', '', $params['exam2']);
  136. $where[] = ' s.studydate < \''.$exam2.'\' ';
  137. break;
  138. case 'exam_class':
  139. if($params['exam_class'] == null){
  140. continue;
  141. }
  142. $where[] = 's.modality=\''.$params['exam_class'].'\' ';
  143. break;
  144. }
  145. }
  146. if(count($where) <= 1){
  147. $where1 = implode('',$where);
  148. $search = $where1;
  149. }else{
  150. $where1 = implode(' and ',$where);
  151. $search = $where1;
  152. }
  153. $data = DB::table('studies')
  154. ->alias('s')
  155. ->join(['patient_infos'=>'p'],'p.id=s.patient_id')
  156. ->join(['user_bind'=>'u'],'u.patient_id=s.patient_id','left')
  157. ->where('s.institution_id='.$institution_id)
  158. ->where($search)
  159. ->field('p.name,p.age,p.sex,s.studydate,s.createdAt,s.modality,u.exam_id')
  160. ->order('s.createdAt desc')
  161. ->select();
  162. //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
  163. /*import("Org.Util.PHPExcel");
  164. import("Org.Util.PHPExcel.Writer.Excel5");
  165. import("Org.Util.PHPExcel.IOFactory.php");*/
  166. foreach($data as $k=>$v){
  167. if($v['sex'] == 'F'){
  168. $data[$k]['sex'] = '女';
  169. }elseif($v['sex'] == 'M'){
  170. $data[$k]['sex'] = '男';
  171. }elseif($v['sex'] == '男'){
  172. $data[$k]['sex'] = '男';
  173. }elseif($v['sex'] == '女'){
  174. $data[$k]['sex'] = '女';
  175. }else{
  176. $data[$k]['sex'] = '未知';
  177. }
  178. if($v['exam_id'] == null){
  179. $data[$k]['exam_id'] = '未绑定';
  180. }else{
  181. $data[$k]['exam_id'] = '已绑定';
  182. }
  183. }
  184. $filename = time()."数据统计表";
  185. $headArr = array("患者姓名","患者年龄", "患者性别","检查时间", "上传时间", "患者类型", "是否绑定微信");
  186. $this->getExcel($filename, $headArr, $data);
  187. }
  188. public function getExcel($fileName, $headArr, $data) {
  189. //对数据进行检验
  190. if (empty($data) || !is_array($data)) {
  191. die("data must be a array");
  192. }
  193. //检查文件名
  194. if (empty($fileName)) {
  195. exit;
  196. }
  197. $date = date("Y_m_d", time());
  198. $fileName .= "_{$date}.xls";
  199. //创建PHPExcel对象,注意,不能少了\
  200. $objPHPExcel = new \PHPExcel();
  201. $objProps = $objPHPExcel->getProperties();
  202. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
  203. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
  204. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
  205. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  206. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
  207. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
  208. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
  209. //设置表头
  210. $key = ord("A");
  211. foreach ($headArr as $v) {
  212. $colum = chr($key);
  213. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  214. $key += 1;
  215. }
  216. $column = 2;
  217. $objActSheet = $objPHPExcel->getActiveSheet();
  218. foreach ($data as $key => $rows) { //行写入
  219. $span = ord("A");
  220. foreach ($rows as $keyName => $value) {// 列写入
  221. $j = chr($span);
  222. $objActSheet->setCellValue($j . $column, $value);
  223. $span++;
  224. }
  225. $column++;
  226. }
  227. $fileName = iconv("utf-8", "gb2312", $fileName);
  228. //重命名表
  229. // $objPHPExcel->getActiveSheet()->setTitle('test');
  230. //设置活动单指数到第一个表,所以Excel打开这是第一个表
  231. $objPHPExcel->setActiveSheetIndex(0);
  232. header('Content-Type: application/vnd.ms-excel');
  233. header("Content-Disposition: attachment;filename=\"$fileName\"");
  234. header('Cache-Control: max-age=0');
  235. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  236. $objWriter->save('php://output'); //文件通过浏览器下载
  237. exit;
  238. }
  239. }