Counts.php 11 KB

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