Counts.php 8.7 KB

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