Counts.php 11 KB

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