Counts.php 9.0 KB

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