ReportController.java 15.6 KB
package com.xkl.controller;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentUser;
import com.xkl.authorization.annotation.LogAnnotation;
import com.xkl.authorization.annotation.Sign;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.*;
import com.xkl.model.*;
import com.xkl.repository.XklAmpReportDetailRespository;
import com.xkl.repository.XklAmpReportHealthScoreRespository;
import com.xkl.repository.XklAmpReportRespository;
import com.xkl.security.AntiXSS;
import com.xkl.service.IScoreService;
import com.xkl.tools.HtmlTools;
import com.xkl.tools.UtilTools;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by win7 on 2016/11/20.
 */
@CommonsLog
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private XklAmpReportRespository xklAmpReportRespository;
    @Autowired
    private XklAmpReportDetailRespository xklAmpReportDetailRespository;
    @Autowired
    private XklAmpReportHealthScoreRespository xklAmpReportHealthScoreRespository;

    @Autowired
    private IScoreService scoreService;

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "体检报告列表查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> getReportList(HttpServletRequest request, @CurrentUser User user,
                                                     @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        long member_id = user.getMemberId();
        List<XklAmpReportEntity> xklAmpReportEntity = xklAmpReportRespository.findByMemberIdAndStatus(member_id, 1);
        return new ResponseEntity<>(ResultModel.ok(xklAmpReportEntity), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "体检报告详情查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> getReportDetail(HttpServletRequest request, @CurrentUser User user, @RequestParam long report_id,
                                                       @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        XklAmpReportEntity report = xklAmpReportRespository.findOne(report_id);
        Map<Integer,ReportTypeModel> reportTypeMap = new HashMap<>();
        if (report != null) {
            if (report.getScore() == null || report.getScore() - 0 < Constants.SMALL_DOUBLE) {//首次调用接口,score为0
                scoreService.getScore(report_id);
            }
            List<XklAmpReportDetailEntity> reportDetailEntityList = xklAmpReportDetailRespository.findAllByReportId(report_id);
            for (XklAmpReportDetailEntity detail : reportDetailEntityList) {

                int itemId = detail.getItemId();
                XklAmpReportMetaItemsEntity metaItems = Constants.itemMetaMap.get(itemId);
                if (metaItems != null) {
                    int item_type = metaItems.getType();
                    boolean testFlag = false;//是否是测试项目,测试项目要存两份
                    if(Constants.testItemSet.contains(itemId)){
                        testFlag = true;
                    }

                    ReportDetailModel reportDetail = new ReportDetailModel();
                    reportDetail.setItemId(itemId);
                    reportDetail.setItemValue(detail.getItemValue());
                    reportDetail.setStatus(detail.getStatus());
                    reportDetail.setTitle(metaItems.getTitle());
                    if(Constants.itemIdSpSet.contains(itemId)){//特殊项
                        reportDetail.setNormalRange(Constants.itemRangeSpString);
                    }else if (report.getSex() == Constants.MALE) {//男
                        reportDetail.setNormalRange(metaItems.getStandardLowMale() + " - " + metaItems.getStandardHighMale());
                    }else {//女
                        reportDetail.setNormalRange(metaItems.getStandardLowFemale() + " - " + metaItems.getStandardHighFemale());
                    }
                    //正常的一份
                    if(!reportTypeMap.containsKey(item_type)){
                        ReportTypeModel reportTypeModel = new ReportTypeModel();
                        reportTypeModel.setType(item_type);
                        XklAmpReportHealthScoreEntity scoreEntity = xklAmpReportHealthScoreRespository.findByReportIdAndType(report_id, item_type);
                        if (scoreEntity != null) {
                            reportTypeModel.setTypeHealthScore(scoreEntity.getTypeHealthScore());
                        }
                        List<ReportDetailModel> reportDetailModelList = new ArrayList<>();
                        reportDetailModelList.add(reportDetail);
                        reportTypeModel.setReportDetailModelList(reportDetailModelList);
                        reportTypeMap.put(item_type,reportTypeModel);
                    }else{
                        ReportTypeModel reportTypeModel = reportTypeMap.get(item_type);
                        List<ReportDetailModel> reportDetailModelList = reportTypeModel.getReportDetailModelList();
                        reportDetailModelList.add(reportDetail);
                        reportTypeModel.setReportDetailModelList(reportDetailModelList);
                        reportTypeMap.put(item_type,reportTypeModel);
                    }
                    //测试项目另存一份
                    if(testFlag){
                        if(!reportTypeMap.containsKey(Constants.testItemType)){
                            //test项目一份
                            ReportTypeModel reportTypeModel_test = new ReportTypeModel();
                            reportTypeModel_test.setTypeHealthScore(0);//测试项目没有打分
                            reportTypeModel_test.setType(Constants.testItemType);
                            List<ReportDetailModel> reportDetailModelList_test = new ArrayList<>();
                            reportDetailModelList_test.add(reportDetail);
                            reportTypeModel_test.setReportDetailModelList(reportDetailModelList_test);
                            reportTypeMap.put(Constants.testItemType,reportTypeModel_test);
                        }else{
                            ReportTypeModel reportTypeModel_test = reportTypeMap.get(Constants.testItemType);
                            List<ReportDetailModel> reportDetailModelList_test = reportTypeModel_test.getReportDetailModelList();
                            reportDetailModelList_test.add(reportDetail);
                            reportTypeModel_test.setReportDetailModelList(reportDetailModelList_test);
                            reportTypeMap.put(Constants.testItemType,reportTypeModel_test);
                        }
                    }
                }
            }
        }
        ReportModel reportModel = new ReportModel(report, reportTypeMap);
        return new ResponseEntity<>(ResultModel.ok(reportModel), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Sign
    @RequestMapping(value = "/score", method = RequestMethod.GET)
    @ApiOperation(value = "健康评分接口")
    public ResponseEntity<ResultModel> getReportScore(HttpServletRequest request, @RequestParam long report_id,
                                                      @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
        double score = 0;
        XklAmpReportEntity report = xklAmpReportRespository.findOne(report_id);
        if (report != null) {
            if (report.getScore() - 0 < Constants.SMALL_DOUBLE || report.getScore() == null) {//首次调用接口,score为0
                //TODO:可以在上传时直接打分?
                //单独测试需要删除xkl_amp_report_health_scroe表中数据
                score = scoreService.getScore(report_id);
            } else {
                score = report.getScore();
            }
        }
        return new ResponseEntity<>(ResultModel.ok(score), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/itemInfo", method = RequestMethod.GET)
    @ApiOperation(value = "指标解释查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> getReportItemInfo(HttpServletRequest request, @CurrentUser User user, @RequestParam int itemId, @RequestParam int status, @RequestParam boolean isPureHtml,
                                                         @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        XklAmpReportMetaItemsEntity metaItems = Constants.itemMetaMap.get(itemId);
        String result = "";
        if (metaItems != null) {
            //0, normal,1, lower,2, higher
            if (status == Constants.NORMAL) {
                result = metaItems.getExplainNormal();
            } else if (status == Constants.LOWER) {
                result = metaItems.getExplainLow();
            } else if (status == Constants.HIGHER) {
                result = metaItems.getExplainHigh();
            } else {
                result = "没有此status相关信息";
            }
        } else {
            result = "没有此itemId相关信息";
        }
        if (isPureHtml) {
            result = HtmlTools.stripHtml(result);
        }
        return new ResponseEntity<>(ResultModel.ok(result), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/itemGraph", method = RequestMethod.GET)
    @ApiOperation(value = "指标曲线查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> getReportItemGraph(HttpServletRequest request, @CurrentUser User user, @RequestParam int itemId, @RequestParam String stime, @RequestParam String etime,
                                                          @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        long member_id = user.getMemberId();
        List<XklAmpReportEntity> xklAmpReportEntity = xklAmpReportRespository.findByMemberIdAndStatus(member_id, 1);
        List<ReportItemGraphModel> reportItemGraphModelList = new ArrayList<>();

        if (xklAmpReportEntity != null && xklAmpReportEntity.size() > 0) {
            for (XklAmpReportEntity report : xklAmpReportEntity) {
                long reportTime = report.getUptime().getTime();
                long stimeLong = UtilTools.getLongTime(stime);
                long etimeLong = UtilTools.getLongTime(etime);
                long reportId = report.getId();
                if (reportTime >= stimeLong && reportTime <= etimeLong) {//在时间范围内
                    XklAmpReportDetailEntity reportDetail = xklAmpReportDetailRespository.findByReportIdAndItemId(reportId, itemId);
                    if (reportDetail != null) {
                        ReportItemGraphModel reportItemGraphModel = new ReportItemGraphModel(reportTime, reportDetail.getItemValue());
                        reportItemGraphModelList.add(reportItemGraphModel);
                    }
                }
            }
        }
        return new ResponseEntity<>(ResultModel.ok(reportItemGraphModelList), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/compare", method = RequestMethod.POST)
    @ApiOperation(value = "微信端体检报告分年龄比较接口")
    public ResponseEntity<ResultModel> openIdCompare(HttpServletRequest request, @CurrentUser User user, @RequestParam Long report_id,
                                                     @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
        List<CompareModel> result = new ArrayList<>();

        XklAmpReportEntity report = xklAmpReportRespository.findOne(report_id);
        double score = 0;
        if (report != null) {
            if (report.getScore() - 0 < Constants.SMALL_DOUBLE || report.getScore() == null) {//首次调用接口,score为0
                score = scoreService.getScore(report_id);
            } else {
                score = report.getScore();
            }
            int age = report.getAge();
            String ageId = String.valueOf((age - age % 5) / 5);
            for (Map.Entry<Integer, XklAmpReportCategoryEntity> entry : Constants.weightedScoreMap.entrySet()) {
                //List<XklAmpReportHealthScoreEntity> scoreEntity = xklAmpReportHealthScoreRespository.findByReportIdAndType(report_id);
                int typeId = entry.getKey();
                XklAmpReportCategoryEntity value = entry.getValue();
                String key = ageId + "-" + typeId;
                double aveScore = Constants.aveScoreMap.get(key);
                double typeScore = 0;
                XklAmpReportHealthScoreEntity scoreEntity = xklAmpReportHealthScoreRespository.findByReportIdAndType(report_id, typeId);
                if (scoreEntity != null) {
                    typeScore = scoreEntity.getTypeHealthScore();
                } else {
                    if (typeId == 1) {//第一项为测试项目
                        typeScore = score;
                    }
                }
                CompareModel cm = new CompareModel(typeId, value.getName(), typeScore, aveScore);
                result.add(cm);
            }
        }
        return new ResponseEntity<>(ResultModel.ok(result), HttpStatus.OK);
    }

}