Commit da5a7834ac9842eca0a2ef28ecece97f2e79be41

Authored by dongqin
1 parent 8f44bf38d5

产后访视统计率

Showing 2 changed files with 127 additions and 3 deletions

platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/MatDeliverFacade.java View file @ da5a783
... ... @@ -41,6 +41,10 @@
41 41 import org.springframework.beans.factory.annotation.Qualifier;
42 42 import org.springframework.data.domain.Sort;
43 43 import org.springframework.data.mongodb.core.MongoTemplate;
  44 +import org.springframework.data.mongodb.core.aggregation.Aggregation;
  45 +import org.springframework.data.mongodb.core.aggregation.AggregationResults;
  46 +import org.springframework.data.mongodb.core.aggregation.GroupOperation;
  47 +import org.springframework.data.mongodb.core.aggregation.MatchOperation;
44 48 import org.springframework.data.mongodb.core.query.Criteria;
45 49 import org.springframework.data.mongodb.core.query.Query;
46 50 import org.springframework.data.mongodb.core.query.Update;
... ... @@ -49,6 +53,9 @@
49 53  
50 54 import javax.servlet.http.HttpServletResponse;
51 55 import java.io.IOException;
  56 +import java.math.BigDecimal;
  57 +import java.math.BigInteger;
  58 +import java.net.InetSocketAddress;
52 59 import java.sql.Connection;
53 60 import java.sql.PreparedStatement;
54 61 import java.sql.ResultSet;
... ... @@ -1885,7 +1892,7 @@
1885 1892 matDeliverQuery.setContactReason(matdeliverFollowRequest.getContactReason());
1886 1893 }
1887 1894  
1888   - matDeliverQuery.setVisitResult(matdeliverFollowRequest.getVisitResult());
  1895 +
1889 1896 matDeliverQuery.setVisitAffirm(matdeliverFollowRequest.getVisitAffirm());
1890 1897 matDeliverQuery.setVisitStatus(matdeliverFollowRequest.getVisitStatus());
1891 1898 matDeliverQuery.setMakeType(matdeliverFollowRequest.getMakeType());
... ... @@ -1929,6 +1936,33 @@
1929 1936 }
1930 1937  
1931 1938  
  1939 + Criteria criteria = matDeliverQuery.convertToQuery().getCriteria();
  1940 + criteria.and("visitResult").exists(true).in(1, 2);
  1941 + MatchOperation match = Aggregation.match(criteria);
  1942 + GroupOperation group = Aggregation.group("visitResult").count().as("count");
  1943 + Aggregation aggregation = Aggregation.newAggregation(match, group);
  1944 + AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, MaternalDeliverModel.class, Map.class);
  1945 + List<Map> mappedResults = aggregate.getMappedResults();
  1946 + Integer okCount = 0, errorCount = 0, totalCount = 0;
  1947 + if (CollectionUtils.isNotEmpty(mappedResults)) {
  1948 + for (Map map : mappedResults) {
  1949 + Integer visitResult = Integer.parseInt(map.get("_id").toString());
  1950 + Integer ok = 1;
  1951 + if (ok == visitResult) {
  1952 + okCount += Integer.parseInt(map.get("count").toString());
  1953 + }
  1954 + if (ok != visitResult) {
  1955 + errorCount += Integer.parseInt(map.get("count").toString());
  1956 + }
  1957 + }
  1958 + }
  1959 + totalCount = okCount + errorCount;
  1960 + double okPercentage = 0, errorPercentage = 0;
  1961 + if (totalCount > 0) {
  1962 + okPercentage = new BigDecimal(okCount.toString()).divide(new BigDecimal(totalCount.toString()), 1, BigDecimal.ROUND_HALF_UP).doubleValue() * 100;
  1963 + errorPercentage = 100 - okPercentage;
  1964 + }
  1965 + matDeliverQuery.setVisitResult(matdeliverFollowRequest.getVisitResult());
1932 1966 if (StringUtils.isNotEmpty(matdeliverFollowRequest.getNeed())) {//是否分页
1933 1967 matDeliverQuery.setNeed("need");
1934 1968 matDeliverQuery.setLimit(matdeliverFollowRequest.getLimit());
... ... @@ -2143,7 +2177,11 @@
2143 2177 matdeliverFollowListResults.add(matdeliverFollowListResult);
2144 2178 }
2145 2179 listResponse.setPageInfo(matDeliverQuery.getPageInfo());
2146   - listResponse.setObject(matdeliverFollowListResults);
  2180 + HashMap<String, Object> map = new HashMap<>();
  2181 + map.put("matdeliverFollowListResults", matdeliverFollowListResults);
  2182 + map.put("okPercentage", okPercentage);
  2183 + map.put("errorPercentage", errorPercentage);
  2184 + listResponse.setObject(map);
2147 2185 return listResponse;
2148 2186 }
2149 2187  
platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/MatdeliverFollowFacade.java View file @ da5a783
... ... @@ -10,6 +10,7 @@
10 10 import com.lyms.platform.common.result.BaseObjectResponse;
11 11 import com.lyms.platform.common.result.BaseResponse;
12 12 import com.lyms.platform.common.utils.DateUtil;
  13 +import com.lyms.platform.common.utils.ExcelUtil;
13 14 import com.lyms.platform.operate.web.request.MatDeliverFollowAddRequest;
14 15 import com.lyms.platform.operate.web.request.MatdeliverFollowRequest;
15 16 import com.lyms.platform.operate.web.result.MatdeliverFollowListResult;
16 17  
17 18  
... ... @@ -26,13 +27,22 @@
26 27 import com.lyms.platform.query.MatDeliverFollowQuery;
27 28 import com.lyms.platform.query.PersonModelQuery;
28 29 import com.lyms.platform.query.TrackDownRecordQuery;
  30 +import jxl.Workbook;
  31 +import jxl.format.Alignment;
  32 +import jxl.format.Colour;
  33 +import jxl.format.UnderlineStyle;
  34 +import jxl.write.*;
  35 +import jxl.write.biff.RowsExceededException;
29 36 import org.apache.commons.collections.MapUtils;
30 37 import org.apache.commons.lang.StringUtils;
31 38 import org.apache.log4j.Logger;
  39 +import org.apache.poi.ss.util.CellRangeAddress;
32 40 import org.springframework.beans.factory.annotation.Autowired;
33 41 import org.springframework.stereotype.Component;
34 42  
35 43 import javax.servlet.http.HttpServletResponse;
  44 +import java.io.IOException;
  45 +import java.io.OutputStream;
36 46 import java.util.*;
37 47  
38 48 /**
... ... @@ -549,7 +559,9 @@
549 559  
550 560 List<Map<String, Object>> results = new ArrayList<>();
551 561 if (object != null) {
552   - List<MatdeliverFollowListResult> matdeliverFollowListResults = (List<MatdeliverFollowListResult>) object;
  562 + Map m = (Map) object;
  563 + Object res = m.get("matdeliverFollowListResults");
  564 + List<MatdeliverFollowListResult> matdeliverFollowListResults = (List<MatdeliverFollowListResult>) res;
553 565  
554 566 for (int i = 0; i < matdeliverFollowListResults.size(); i++) {
555 567 MatdeliverFollowListResult mf = matdeliverFollowListResults.get(i);
556 568  
... ... @@ -575,7 +587,81 @@
575 587 results.add(result);
576 588 }
577 589 }
  590 + OutputStream out = null;
  591 + try {
  592 + out = resp.getOutputStream();
  593 + resp.setContentType("application/octet-stream");
  594 + resp.setCharacterEncoding("UTF-8");
  595 + resp.setHeader("Content-Disposition", "attachment;fileName=data.xls");
  596 + toExcel(out, results, cnames, object);
  597 + } catch (IOException e) {
  598 + e.printStackTrace();
  599 + }
578 600 ResponseUtil.responseExcel(cnames, results, resp);
  601 + }
  602 +
  603 + public OutputStream toExcel(OutputStream out, List<Map<String, Object>> data, Map<String, String> columName, Object object) {
  604 + WritableWorkbook wwb;
  605 + try {
  606 + wwb = Workbook.createWorkbook(out);
  607 + WritableSheet ws = wwb.createSheet("sheet", 0); // 创建一个工作表
  608 + /**
  609 + * 设置单元格样式
  610 + */
  611 + WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
  612 + WritableCellFormat wcf = new WritableCellFormat(wf);
  613 + ws.setRowView(0, 300); // 设置指定行高
  614 + // 设置列宽
  615 + for (int j = 0, columLen = columName.size(); j < columLen; j++) {
  616 + ws.setColumnView(j, 15);
  617 + }
  618 + String okPercentage = "0", errorPercentage = "0";
  619 + if (object != null) {
  620 + Map m = (Map) object;
  621 + okPercentage = m.get("okPercentage").toString();
  622 + errorPercentage = m.get("errorPercentage").toString();
  623 + }
  624 + String string = "本机构的有效访视率为: %s %% 失败率为: %s %%";
  625 + String format = String.format(string, okPercentage, errorPercentage);
  626 +
  627 + ws.mergeCells(0, 0, 12, 0);
  628 + Label label = new Label(0, 0, format, wcf);
  629 + WritableCellFormat cellFormat = new WritableCellFormat();
  630 + cellFormat.setAlignment(Alignment.CENTRE);
  631 + label.setCellFormat(cellFormat);
  632 + ws.addCell(label);
  633 + // 填充数据的内容
  634 + Map<String, Object> map;
  635 + for (int i = 1, len = data.size(); i < len; i++) {
  636 + map = data.get(i);
  637 + Iterator<String> ite = columName.keySet().iterator();
  638 + int j = 0;
  639 + String keyORvalue = "";
  640 + String keyName = "";
  641 + while (ite.hasNext()) {
  642 + keyName = ite.next();
  643 + if (i > 1) { // 类容数据
  644 + keyORvalue = map.get(keyName) == null ? "" : map.get(keyName).toString();
  645 + } else { // 第一行列名
  646 + keyORvalue = map.get(keyName) == null ? "" : map.get(keyName).toString();
  647 + keyName = columName.get(keyName);
  648 + ws.addCell(new Label(j, 1, keyName, wcf));
  649 + }
  650 + ws.addCell(new Label(j, 1 + i, keyORvalue));
  651 + j++;
  652 + }
  653 + }
  654 +
  655 + wwb.write();
  656 + wwb.close();
  657 + } catch (IOException e) {
  658 + e.printStackTrace();
  659 + } catch (RowsExceededException e) {
  660 + e.printStackTrace();
  661 + } catch (WriteException e) {
  662 + e.printStackTrace();
  663 + }
  664 + return out;
579 665 }
580 666 }