Counts.php 11 KB

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