Blame view

src/main/java/com/xkl/service/ReportService.java 11.8 KB
zhaoyue authored
1 2
package com.xkl.service;
zhaoyue authored
3
import com.alibaba.fastjson.JSONArray;
zhaoyue authored
4
import com.alibaba.fastjson.JSONObject;
5
import com.xkl.config.Constants;
zhaoyue authored
6
import com.xkl.config.ResultStatus;
7
import com.xkl.domain.*;
zhaoyue authored
8 9
import com.xkl.model.ReportIdModel;
import com.xkl.model.ResultModel;
zhaoyue authored
10
import com.xkl.repository.*;
zhaoyue authored
11
import com.xkl.security.SecurityTool;
zhaoyue authored
12
import lombok.extern.apachecommons.CommonsLog;
13
import org.springframework.beans.factory.annotation.Autowired;
zhaoyue authored
14 15 16
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
zhaoyue authored
17 18 19
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
20
import java.util.*;
zhaoyue authored
21 22 23 24 25

/**
 * Created by zhao yue on 2016/11/13.
 */
@Service
zhaoyue authored
26
@CommonsLog
27 28 29
public class ReportService implements IReportService {
    @Autowired
    private ReportMetaItemsRepository reportMetaItemsRepository;
zhaoyue authored
30
31 32 33 34 35 36
    @Autowired
    private ReportRepository reportRepository;

    @Autowired
    private ReportDetailRepository reportDetailRepository;
zhaoyue authored
37
    @Autowired
zhaoyue authored
38 39 40
    private ReportResultRepository reportResultRepository;

    @Autowired
zhaoyue authored
41 42
    private UserRepository userRepository;
zhaoyue authored
43 44 45
    @Autowired
    private IScoreService scoreService;
zhaoyue authored
46 47 48 49 50 51 52 53
    // 存储报告相关md5,防止重复上传已存在报告,防止重复上传错误报告。
    private RedisTemplate<String, String> redis;

    @Autowired
    public void setRedis(RedisTemplate redis) {
        this.redis = redis;
    }
zhaoyue authored
54
    private static Map<Integer, XklAmpReportMetaItemsEntity> rpMetaItemMap = new HashMap<Integer, XklAmpReportMetaItemsEntity>();
55
zhaoyue authored
56
    /*
zhaoyue authored
57 58
     * 存储报告
     */
zhaoyue authored
59
    public ResponseEntity<ResultModel> save(XklAdminEntity admin, String json_report) {
zhaoyue authored
60 61
        // 验证存在性
        String reportMd5 = SecurityTool.encode("MD5", json_report);
zhaoyue authored
62
        // 验证是无对应的会员,rediskey
zhaoyue authored
63
        String reportWithNoUser = reportMd5 + "Member";
zhaoyue authored
64
        // 验证报告格式有问题,rediskey
zhaoyue authored
65 66 67 68
        String reportWrongFormat = reportMd5 + "Format";
        /*
         * 如果已经处理过的报告,不再进行处理。
         */
zhaoyue authored
69
        XklAmpReportEntity report = reportRepository.findByMd5AndStatus(reportMd5, Constants.STATUS_OK);
zhaoyue authored
70 71
        if (report != null && report.getStatus() > 0) {
            // 返回,报告已存在。
zhaoyue authored
72
            return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_EXISTED_ERROR, new ReportIdModel(report.getId())), HttpStatus.OK);
zhaoyue authored
73 74
        } else if (redis.hasKey(reportWithNoUser)) {
            // 返回,报告对应会员不存在。
zhaoyue authored
75
            return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_USER_ERROR), HttpStatus.OK);
zhaoyue authored
76 77
        } else if (redis.hasKey(reportWrongFormat)) {
            // 返回,报告格式有问题。
zhaoyue authored
78
            return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_FORMAT_ERROR), HttpStatus.OK);
zhaoyue authored
79 80 81 82 83 84 85 86 87 88 89
        }
        /*
         * 解析报告数据
         */
        ReportData reportData = parseReport(json_report, reportMd5);
        /*
         * 检验报告格式
         */
        if (reportData == null) {
            redis.boundValueOps(reportWrongFormat).set("");
            // 返回,报告格式有问题。
zhaoyue authored
90
            return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_FORMAT_ERROR), HttpStatus.OK);
zhaoyue authored
91 92 93 94
        }
        /*
         * 检验会员存在性
         */
zhaoyue authored
95
        User user = userRepository.findByLoginAccountAndStatus(reportData.getAmpReport().getAccountStr(), Constants.STATUS_OK2);
zhaoyue authored
96 97 98
        if (user == null) {
            redis.boundValueOps(reportWithNoUser).set("");
            // 返回,报告对应会员不存在。
zhaoyue authored
99
            return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_USER_ERROR), HttpStatus.OK);
zhaoyue authored
100 101 102 103 104
        }

        /*
         * 存储报告
         */
zhaoyue authored
105
        long reportId = save2DB(reportData, admin, user);
zhaoyue authored
106 107 108 109 110
        if (reportId > 0) {
            // 返回,报告存储成功,报告id
            return new ResponseEntity<>(ResultModel.ok(new ReportIdModel(reportId)), HttpStatus.OK);
        } else {
            // 返回,服务器存储问题。
zhaoyue authored
111
            return new ResponseEntity<>(ResultModel.error(ResultStatus.DB_ERROR), HttpStatus.OK);
zhaoyue authored
112 113 114 115 116 117
        }
    }

    /*
     * 操作员删除报告,只能删除该操作员自己创建的报告。
     */
zhaoyue authored
118
    public ResponseEntity<ResultModel> delete(XklAdminEntity admin, long report_id) {
zhaoyue authored
119
        //  1. 得到report,验证报告存在性
zhaoyue authored
120
        XklAmpReportEntity report = reportRepository.findById(report_id);
zhaoyue authored
121
        if (report == null || report.getStatus() == 0) {
zhaoyue authored
122
            // 报告不存在,返回
zhaoyue authored
123
            return new ResponseEntity<>(ResultModel.error(ResultStatus.REPORT_INVALID_ERROR), HttpStatus.OK);
zhaoyue authored
124 125 126
        }

        // 2. 验证admin
zhaoyue authored
127
        if (report.getCreateBy() != admin.getId()) {
zhaoyue authored
128
            // 非此admin创建,不能删除,返回
zhaoyue authored
129
            return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_ADMIN_RPDEL_ERROR), HttpStatus.OK);
zhaoyue authored
130 131 132
        }
        // 3. 删除report和detail,返回ok
        reportRepository.delete(report);
zhaoyue authored
133
        List<XklAmpReportDetailEntity> detailList = reportDetailRepository.findByReportId(report.getId());
zhaoyue authored
134
        reportDetailRepository.delete(detailList);
zhaoyue authored
135 136
        List<XklAmpReportResultEntity> rpResList = reportResultRepository.findByReportId(report.getId());
        reportResultRepository.delete(rpResList);
zhaoyue authored
137 138 139 140
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }

    /*
141 142 143 144 145 146
     验证md5
     获取report
     获取detail
     评判detail
     验证member
     获取admin
zhaoyue authored
147
     */
zhaoyue authored
148 149
    // 需要程喆增加 title,account,machine_num字段 String; 修改set字段为int,0男,1女。125项目,改为float类型。
    //
zhaoyue authored
150
    private ReportData parseReport(String reportJson, String md5) {
zhaoyue authored
151
        ReportData reportData = new ReportData();
zhaoyue authored
152 153
        XklAmpReportEntity ampReport = new XklAmpReportEntity();
        List<XklAmpReportDetailEntity> detailList = new ArrayList<>();
zhaoyue authored
154
        List<XklAmpReportResultEntity> basicResList = new ArrayList<>();
155
        int sex;
zhaoyue authored
156
        int age;
157
zhaoyue authored
158
        /*
159
         * 2. 获取report基础信息
zhaoyue authored
160 161
         */
        try {
zhaoyue authored
162
            log.info("INFO, got json:" + reportJson);
zhaoyue authored
163
            JSONObject rpJson = JSONObject.parseObject(reportJson);
164
            sex = rpJson.getInteger("sex").intValue();
zhaoyue authored
165 166 167 168 169 170 171
            age = rpJson.getInteger("age").intValue();
            if (sex != Constants.FEMALE && sex != Constants.MALE) {
                return null;
            }
            if (age < 0 || age > 127) {
                return null;
            }
zhaoyue authored
172 173
            ampReport.setReport(rpJson.getString("name"),
                    rpJson.getString("title"),///                    "AMP快速无创身心健康评估报告",
zhaoyue authored
174
                    new Timestamp(rpJson.getLong("report_date")),
zhaoyue authored
175
                    new Timestamp(System.currentTimeMillis()),
zhaoyue authored
176
                    rpJson.getString("account").trim(),///
zhaoyue authored
177 178
                    rpJson.getInteger("sex").byteValue(),///
                    rpJson.getInteger("age").byteValue(),
zhaoyue authored
179 180 181
                    rpJson.getInteger("weight").intValue(),
                    rpJson.getInteger("pulse").intValue(),
                    rpJson.getInteger("respiratory_rate").intValue(),
zhaoyue authored
182 183 184 185 186 187 188
                    rpJson.getDouble("atmospheric_pressure").doubleValue(),
                    rpJson.getDouble("LCA").doubleValue(),
                    rpJson.getDouble("RCA").doubleValue(),
                    rpJson.getDouble("LAC").doubleValue(),
                    rpJson.getDouble("RAC").doubleValue(),
                    rpJson.getDouble("ABD").doubleValue(),
                    rpJson.getDouble("total").doubleValue(),
zhaoyue authored
189
                    rpJson.getInteger("stable").intValue(),
zhaoyue authored
190 191 192 193 194 195 196
                    md5, rpJson.getString("machine_num"),
                    rpJson.getString("basic_result"),
                    rpJson.getString("T0"),
                    rpJson.getString("T1"),
                    rpJson.getString("T2"),
                    rpJson.getString("T3"),
                    rpJson.getString("T4")
zhaoyue authored
197
            );
zhaoyue authored
198
            /*
199
             * 3. 获取detail信息,id,float类型
zhaoyue authored
200 201 202
             */
            JSONObject rpDetails = rpJson.getJSONObject("detail");
            for (int item_id = 1; item_id <= 125; item_id++) {
203
                float val = rpDetails.getFloat(String.valueOf(item_id)).floatValue();
zhaoyue authored
204
                XklAmpReportDetailEntity detail = new XklAmpReportDetailEntity();
205 206
                detail.setItemValue(val);
                detail.setItemId(item_id);
zhaoyue authored
207 208
                detailList.add(detail);
            }
zhaoyue authored
209 210 211 212 213 214 215 216 217 218 219
            /*
             * 4. 获取诊断结论编号
             */
            JSONArray basicResIdsArr = rpJson.getJSONArray("basic_result_ids");
            if (basicResIdsArr != null && basicResIdsArr.size() > 0) {
                for (int resId = 0; resId < basicResIdsArr.size(); resId++) {
                    XklAmpReportResultEntity rpRes = new XklAmpReportResultEntity();
                    rpRes.setResultItemId(basicResIdsArr.getInteger(resId));
                    basicResList.add(rpRes);
                }
            }
zhaoyue authored
220
        } catch (Exception e) {
zhaoyue authored
221
            log.info("ERROR, JSON FROMAT ERROR!!!"+e);
zhaoyue authored
222 223
            return null;
        }
224
        markItemStatus(sex, detailList);
zhaoyue authored
225 226
        reportData.setAmpReport(ampReport);
        reportData.setRpDetailList(detailList);
zhaoyue authored
227
        reportData.setBasicResList(basicResList);
zhaoyue authored
228 229
        return reportData;
    }
230 231 232 233

    /*
     * 存储报告
     */
zhaoyue authored
234 235
    private long save2DB(ReportData report, XklAdminEntity admin, User user) {
        report.getAmpReport().setCreateBy(admin.getId());
zhaoyue authored
236 237
        report.getAmpReport().setCompanyId((long) admin.getCoid());
        report.getAmpReport().setMemberId((long) user.getMemberId());
zhaoyue authored
238 239
        XklAmpReportEntity ampReport = reportRepository.save(report.getAmpReport());
        for (XklAmpReportDetailEntity detail : report.getRpDetailList()) {
240 241
            detail.setReportId(ampReport.getId());
        }
zhaoyue authored
242
        for (XklAmpReportResultEntity rpRes : report.getBasicResList()) {
zhaoyue authored
243 244
            rpRes.setReportId(ampReport.getId());
        }
245
        reportDetailRepository.save(report.getRpDetailList());
zhaoyue authored
246
        reportResultRepository.save(report.getBasicResList());
zhaoyue authored
247
        scoreService.getScore(ampReport.getId());
248 249 250 251 252 253
        return ampReport.getId();
    }

    /*
     * 判断detail是正常,高于标准或低于标准。
     */
zhaoyue authored
254
    private void markItemStatus(int sex, List<XklAmpReportDetailEntity> detailList) {
255 256 257
        // load ReportMetaItems into memory.
        synchronized (this) {
            if (rpMetaItemMap.size() == 0) {
zhaoyue authored
258
                Iterator<XklAmpReportMetaItemsEntity> rpMetaIter = reportMetaItemsRepository.findAll().iterator();
259
                while (rpMetaIter.hasNext()) {
zhaoyue authored
260 261
                    XklAmpReportMetaItemsEntity rpMetaItem = rpMetaIter.next();
                    rpMetaItemMap.put(rpMetaItem.getItemId(), rpMetaItem);
262 263 264 265
                }
            }
        }
        // mark status
zhaoyue authored
266 267 268
        for (XklAmpReportDetailEntity detail : detailList) {
            double lowSt;
            double highSt;
269 270
            // get standard
            if (sex == Constants.MALE) { // male
zhaoyue authored
271 272
                lowSt = rpMetaItemMap.get(detail.getItemId()).getStandardLowMale();
                highSt = rpMetaItemMap.get(detail.getItemId()).getStandardHighMale();
273
            } else { // female
zhaoyue authored
274 275
                lowSt = rpMetaItemMap.get(detail.getItemId()).getStandardLowFemale();
                highSt = rpMetaItemMap.get(detail.getItemId()).getStandardHighFemale();
276 277 278 279 280 281 282 283 284 285 286 287
            }
            int status;
            if (detail.getItemValue() < lowSt) {
                status = Constants.LOWER;
            } else if (detail.getItemValue() > highSt) {
                status = Constants.HIGHER;
            } else {
                status = Constants.NORMAL;
            }
            detail.setStatus(status);
        }
    }
zhaoyue authored
288
}