lhh 1 tuần trước cách đây
mục cha
commit
4763ef2b0d

+ 186 - 76
svr/svr-internet-hospital/src/main/java/com/yihu/jw/hospital/endpoint/consult/DoctorConsultStatisticsEndpoint.java

@ -42,6 +42,8 @@ import com.yihu.jw.wlyy.service.WlyyBusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -52,10 +54,10 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;
@ -121,7 +123,7 @@ public class DoctorConsultStatisticsEndpoint extends EnvelopRestEndpoint {
	private String demoFlag;
	
	@GetMapping (value = "/statisticsData")
	public Envelop statisticsData(
	public ObjEnvelop statisticsData(
			@ApiParam(name = "mediaType", value = "图文1,视频2")
			@RequestParam(value = "mediaType",required = true) Integer mediaType,
			@ApiParam(name = "aggField", value = "聚合方式 org dept doctor")
@ -130,60 +132,66 @@ public class DoctorConsultStatisticsEndpoint extends EnvelopRestEndpoint {
			@RequestParam(value = "startTime",required = true) String startTime,
			@ApiParam(name = "endTime", value = "结束时间")
			@RequestParam(value = "endTime",required = true) String endTime,
			@ApiParam(name = "relationId", value = "机构id | 科室id")
			@RequestParam(value = "relationId",required = false) String relationId,
			@ApiParam(name = "orgId", value = "检查机构id")
			@RequestParam(value = "orgId",required = false) String orgId,
			@ApiParam(name = "deptId", value = "科室id")
			@RequestParam(value = "deptId",required = false) String deptId,
			@ApiParam(name = "pageNum", value = "pageNum")
			@RequestParam(value = "pageNum",required = true) Integer pageNum,
			@ApiParam(name = "pageSize", value = "pageSize")
			@RequestParam(value = "pageSize",required = true) Integer pageSize) {
		try {
			String selectSql = "SELECT\n" +
					"o.code AS orgCode," +
					"o.name AS orgName, " +
					"COUNT(*) AS 'consultNum', " +
					"COUNT(CASE WHEN op.status >= 1 THEN 1 ELSE NULL END) AS 'haveReceiveNum', " +
					"COUNT(CASE WHEN op.status <= 0 THEN 1 ELSE NULL END) AS 'noReceiveNum', " +
					"COUNT(CASE WHEN op.status = -1 THEN 1 ELSE NULL END) AS 'cancelNum' ";
			String selectSql = "SELECT " +
					"op.scbgjt_org_code scbgjtOrgCode," +
					"op.scbgjt_org_name scbgjtOrgName, " +
					"COUNT(*) AS consultNum, " +
					"COUNT(CASE WHEN op.status >= 1 THEN 1 ELSE NULL END) AS haveReceiveNum, " +
					"COUNT(CASE WHEN op.status <= 0 THEN 1 ELSE NULL END) AS noReceiveNum, " +
					"COUNT(CASE WHEN op.status = -1 THEN 1 ELSE NULL END) AS cancelNum ";
			String fromPart = " FROM base_org o " +
					" LEFT JOIN dict_hospital_dept dept ON o.code=dept.org_code " +
					" LEFT JOIN base_doctor_hospital dh ON dh.dept_code=dept.code " +
					" LEFT JOIN wlyy_outpatient op ON dh.doctor_code = op.doctor" ;
			String fromPart = " FROM " +
					" base_doctor_hospital dh " +
					" LEFT JOIN wlyy_outpatient op ON dh.doctor_code = op.doctor ";
			String filterSql = " WHERE " +
					" op.scbgjt IS NOT NULL AND op.create_time BETWEEN TO_DATE('"+startTime+"', 'YYYY-MM-DD')" +
					" AND TO_DATE('"+endTime+"', 'YYYY-MM-DD') ";
					" AND TO_DATE('"+endTime+"', 'YYYY-MM-DD') AND op.scbgjt_org_code IS NOT NULL ";
			String groupSql = "GROUP BY " +
					" o.code, o.name ";
					" op.scbgjt_org_code, op.scbgjt_org_name ";
			if("org".equals(aggField)) {
				selectSql += fromPart;
			} else if("dept".equals(aggField)) {
				selectSql += ",dept.code AS deptCode,dept.name AS deptName ";
				selectSql += ",dh.dept_code AS deptCode,dh.dept_name AS deptName ";
				selectSql += fromPart;
				if(StringUtils.isBlank(relationId)) {
				if(StringUtils.isBlank(orgId)) {
					throw new ServiceException("参数未传");
				}
				filterSql += " AND o.code = '" + relationId + "' ";
				groupSql += " ,dept.code, dept.name ";
				filterSql += " AND op.scbgjt_org_code = '" + orgId + "' ";
				groupSql += " ,dh.dept_code, dh.dept_name ";
			} else if("doctor".equals(aggField)) {
				selectSql += ",dept.code AS deptCode,dept.name AS deptName,op.doctor,op.doctor_name doctorName ";
				selectSql += ",dh.dept_code AS deptCode," +
						"dh.dept_name AS deptName," +
						"op.doctor," +
						"op.doctor_name AS doctorName," +
						"dh.doctor_duty_name AS doctorDutyName ";
				selectSql += fromPart;
				if(StringUtils.isBlank(relationId)) {
				if(StringUtils.isBlank(orgId) || StringUtils.isBlank(deptId)) {
					throw new ServiceException("参数未传");
				}
				filterSql += " AND dept.code='" + relationId + "' ";
				groupSql += ",dept.code, dept.name,op.doctor,op.`doctor_name` ";
			}
			if("1".equals(mediaType)) {
				filterSql += " AND (op.type = 1 AND op.outpatient_type IN (1, 3)) ";
				filterSql += " AND op.scbgjt_org_code ='"+orgId+"' AND dh.dept_code='" + deptId + "' ";
				groupSql += ",dh.dept_code,dh.dept_name,op.doctor,op.doctor_name,dh.doctor_duty_name ";
			}
			if("2".equals(mediaType)) {
				filterSql += " AND (op.type = 2 AND op.outpatient_type IN (1, 3)) ";
			if(mediaType != null) {
				if(mediaType == 1) {
					filterSql += " AND (op.type = 1 AND op.outpatient_type IN (1, 3)) ";
				}
				if(mediaType == 2) {
					filterSql += " AND (op.type = 2 AND op.outpatient_type IN (1, 3)) ";
				}
			}
			selectSql += filterSql;
@ -191,26 +199,35 @@ public class DoctorConsultStatisticsEndpoint extends EnvelopRestEndpoint {
			String countSql = " SELECT COUNT(*) FROM ( " + selectSql+" ) t ";
			selectSql = selectSql + " limit " + (pageNum-1)*pageSize +","+ pageSize;
			int startRow = (pageNum - 1) * pageSize;
			int endRow = pageNum * pageSize;
			String pageSql =
					"SELECT * FROM (" +
							"  SELECT a.*, ROWNUM rn FROM (" + selectSql + ") a WHERE ROWNUM <= " + endRow +
							") WHERE rn > " + startRow;
//			List<Map<String, Object>> list = jdbcTemplate.queryForList(selectSql);
//			Integer total = jdbcTemplate.queryForObject(countSql, Integer.class);
//			selectSql = selectSql + " limit " + (pageNum-1)*pageSize +","+ pageSize;
			System.out.println(selectSql);
			System.out.println(countSql);
			List<Map<String, Object>> list = jdbcTemplate.queryForList(selectSql);
			Integer total = jdbcTemplate.queryForObject(countSql, Integer.class);
//			System.out.println(pageSql);
//			System.out.println("---");
//			System.out.println(countSql);
			JSONObject result = new JSONObject();
//			result.put("total",total);
//			result.put("list",list);
			result.put("total",total);
			result.put("list",list);
			return success("请求成功",result);
		}catch (Exception e){
			return failedListEnvelopException(e);
			return failedObjEnvelopException(e);
		}
	}
	@GetMapping (value = "/statisticsGraph")
	public Envelop statisticsGraph(
	public ObjEnvelop statisticsGraph(
			@ApiParam(name = "mediaType", value = "图文1,视频2")
			@RequestParam(value = "mediaType",required = true) Integer mediaType,
			@ApiParam(name = "aggField", value = "聚合方式 org dept doctor")
@ -219,54 +236,69 @@ public class DoctorConsultStatisticsEndpoint extends EnvelopRestEndpoint {
			@RequestParam(value = "startTime",required = true) String startTime,
			@ApiParam(name = "endTime", value = "结束时间")
			@RequestParam(value = "endTime",required = true) String endTime,
			@ApiParam(name = "relationId", value = "机构id | 科室id")
			@RequestParam(value = "relationId",required = false) String relationId) {
			@ApiParam(name = "orgId", value = "检查机构id")
			@RequestParam(value = "orgId",required = false) String orgId,
			@ApiParam(name = "deptId", value = "科室id")
			@RequestParam(value = "deptId",required = false) String deptId,
			@ApiParam(name = "doctorId", value = "医生的id")
			@RequestParam(value = "doctorId",required = false) String doctorId) {
		try {
			String selectSql = "SELECT " +
					"DATE(op.create_time) AS 'statDate',dh.org_code,dh.org_name, " +
					"COUNT(*) 'consultNum', " +
					"COUNT(IF(`op`.`status` >= 1,1,null)) 'haveReceiveNum', " +
					"COUNT(IF(`op`.`status` <= 0,1,null)) 'noReceiveNum', " +
					"COUNT(IF(`op`.`status` = -1,1,null)) 'cancelNum' ";
					"TRUNC(op.create_time) AS statDate," +
					"op.scbgjt_org_code scbgjtOrgCode," +
					"op.scbgjt_org_name scbgjtOrgName, " +
					"COUNT(*) AS consultNum, " +
					"COUNT(CASE WHEN op.status >= 1 THEN 1 ELSE NULL END) AS haveReceiveNum, " +
					"COUNT(CASE WHEN op.status <= 0 THEN 1 ELSE NULL END) AS noReceiveNum, " +
					"COUNT(CASE WHEN op.status = -1 THEN 1 ELSE NULL END) AS cancelNum ";
			String fromPart = " FROM " +
					" base_doctor_hospital dh " +
					" LEFT JOIN wlyy_outpatient op ON dh.doctor_code = op.doctor ";
			String filterSql = " WHERE " +
					" op.scbgjt IS NOT NULL AND op.create_time BETWEEN '"+startTime+"' AND '"+endTime+"' ";
					" op.scbgjt IS NOT NULL AND op.create_time BETWEEN TO_DATE('"+startTime+"', 'YYYY-MM-DD')" +
					" AND TO_DATE('"+endTime+"', 'YYYY-MM-DD') AND op.scbgjt_org_code IS NOT NULL ";
			String groupSql = "GROUP BY DATE(op.create_time)," +
					" dh.org_code,dh.org_name";
			String groupSql = "GROUP BY " +
					" TRUNC(op.create_time),op.scbgjt_org_code, op.scbgjt_org_name ";
			if("org".equals(aggField)) {
				selectSql += " FROM " +
						" base_doctor_hospital dh LEFT JOIN wlyy_outpatient op ON dh.doctor_code=op.doctor ";
				selectSql += fromPart;
				if(StringUtils.isBlank(orgId)) {
					throw new ServiceException("参数未传");
				}
				filterSql += " AND op.scbgjt_org_code='"+orgId+"' ";
			} else if("dept".equals(aggField)) {
				String groupField = ",dh.dept_code,dh.dept_name ";
				selectSql += groupField;
				selectSql += " FROM " +
						" base_doctor_hospital dh LEFT JOIN wlyy_outpatient op ON dh.doctor_code=op.doctor ";
				if(StringUtils.isBlank(relationId)) {
				selectSql += ",dh.dept_code AS deptCode,dh.dept_name AS deptName ";
				selectSql += fromPart;
				if(StringUtils.isBlank(orgId) || StringUtils.isBlank(deptId)) {
					throw new ServiceException("参数未传");
				}
				filterSql += " AND dh.org_code = '" + relationId + "' ";
				groupSql += groupField;
				filterSql += " AND op.scbgjt_org_code ='"+orgId+"' AND dh.dept_code='" + deptId + "' ";
				groupSql += " ,dh.dept_code, dh.dept_name ";
			} else if("doctor".equals(aggField)) {
				String groupField = ",dh.dept_code,dh.dept_name,op.doctor,op.`doctor_name` ";
				selectSql += groupField;
				selectSql += " FROM " +
						" base_doctor_hospital dh LEFT JOIN wlyy_outpatient op ON dh.doctor_code=op.doctor ";
				if(StringUtils.isBlank(relationId)) {
				selectSql += ",dh.dept_code AS deptCode," +
						"dh.dept_name AS deptName," +
						"op.doctor," +
						"op.doctor_name AS doctorName," +
						"dh.doctor_duty_name AS doctorDutyName ";
				selectSql += fromPart;
				if(StringUtils.isBlank(orgId) || StringUtils.isBlank(deptId) || StringUtils.isBlank(doctorId)) {
					throw new ServiceException("参数未传");
				}
				filterSql += " AND dh.dept_code='" + relationId + "' ";
				groupSql += groupField;
			}
			if("1".equals(mediaType)) {
				filterSql += " (((`op`.`outpatient_type` = 3) AND (`op`.`type` = 1)) OR ((`op`.`outpatient_type` = 1) AND (`op`.`type` = 1))) ";
				filterSql += " AND op.scbgjt_org_code ='"+orgId+"' AND dh.dept_code='" + deptId + "' AND op.doctor='"+doctorId+"' ";
				groupSql += ",dh.dept_code,dh.dept_name,op.doctor,op.doctor_name,dh.doctor_duty_name ";
			}
			if("2".equals(mediaType)) {
				filterSql += " (((`op`.`outpatient_type` = 1) AND (`op`.`type` = 2)) OR ((`op`.`outpatient_type` = 3) AND (`op`.`type` = 2))) ";
			if(mediaType != null) {
				if(mediaType == 1) {
					filterSql += " AND (op.type = 1 AND op.outpatient_type IN (1, 3)) ";
				}
				if(mediaType == 2) {
					filterSql += " AND (op.type = 2 AND op.outpatient_type IN (1, 3)) ";
				}
			}
			selectSql += filterSql;
@ -274,10 +306,88 @@ public class DoctorConsultStatisticsEndpoint extends EnvelopRestEndpoint {
			List<Map<String, Object>> list = jdbcTemplate.queryForList(selectSql);
			return success("请求成功",list);
			JSONObject result = new JSONObject();
			result.put("list",list);
			return success("请求成功",result);
		}catch (Exception e){
			return failedObjEnvelopException(e);
		}
	}
	@GetMapping (value = "/statisticsExportData")
	public Envelop statisticsExportData(
			@ApiParam(name = "mediaType", value = "图文1,视频2")
			@RequestParam(value = "mediaType",required = true) Integer mediaType,
			@ApiParam(name = "aggField", value = "聚合方式 org dept doctor")
			@RequestParam(value = "aggField",required = true) String aggField,
			@ApiParam(name = "startTime", value = "开始时间")
			@RequestParam(value = "startTime",required = true) String startTime,
			@ApiParam(name = "endTime", value = "结束时间")
			@RequestParam(value = "endTime",required = true) String endTime,
			@ApiParam(name = "orgId", value = "检查机构id")
			@RequestParam(value = "orgId",required = false) String orgId,
			@ApiParam(name = "deptId", value = "科室id")
			@RequestParam(value = "deptId",required = false) String deptId,
			HttpServletResponse response) {
		try {
			ObjEnvelop envelop = this.statisticsData(mediaType, aggField, startTime, endTime, orgId, deptId, 1, 20000);
			Object list = ((JSONObject) (envelop.getObj())).get("list");
			response.setContentType("octets/stream");
			response.setHeader("Content-Disposition", "attachment; filename=" + new String("searchRecordWrite.xls"));
			OutputStream os = response.getOutputStream();
			prescriptionService.searchRecordWrite(os, (List) list);
			return success("操作成功");
		}catch (Exception e){
			return failedListEnvelopException(e);
		}
	}
	public void searchRecordWrite(OutputStream os, List<Map<String, Object>> list) throws Exception{
		WritableWorkbook wwb = jxl.Workbook.createWorkbook(os);
		try {
			WritableSheet ws;
			ws = wwb.createSheet("sheet", 1);
			String[] header = {"检查机构","咨询机构科室","医生","职称","阅读量","预约量","咨询量","未接诊","已接诊","已取消"};
			int k = 0;
			for (String h : header) {
				addCell(ws, 0, k, h);//表名,行,列,header
				k++;
			}
			int i = 1;
			for (Map<String, Object> m : list) {
				addCell(ws, i, 0, Objects.toString(m.get("scbgjtOrgName"),""));
				addCell(ws, i, 1, Objects.toString(m.get("deptName"),""));
				addCell(ws, i, 2, Objects.toString(m.get("doctorName"),""));
				addCell(ws, i, 3, Objects.toString(m.get("doctorDutyName"),""));
				addCell(ws, i, 4, "");
				addCell(ws, i, 5, "");
				addCell(ws, i, 6, Objects.toString(m.get("consultNum"),""));
				addCell(ws, i, 7, Objects.toString(m.get("haveReceiveNum"),""));
				addCell(ws, i, 8, Objects.toString(m.get("noReceiveNum"),""));
				addCell(ws, i, 9, Objects.toString(m.get("cancelNum"),""));
				i++;
			}
			wwb.write();
			wwb.close();
		} catch (IOException e) {
			e.printStackTrace();
			if (wwb != null) wwb.close();
			throw e;
		}
	}
	//添加单元格内容
	public void addCell(WritableSheet ws, int row, int column, String data) throws WriteException {
		Label label = new Label(column, row, data);
		WritableCellFormat cellFormat = new WritableCellFormat();
		cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
		cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
		label.setCellFormat(cellFormat);
		ws.addCell(label);
	}
}