DataShareController.java 7.51 KB
package com.xkl.controller;

import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.LogAnnotation;
import com.xkl.authorization.annotation.Sign;
import com.xkl.config.Constants;
import com.xkl.domain.*;
import com.xkl.model.ReportDetailModel;
import com.xkl.model.ReportModel;
import com.xkl.model.ResultModel;
import com.xkl.repository.*;
import com.xkl.security.AntiXSS;
import com.xkl.service.IScoreService;
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.ArrayList;
import java.util.List;

import static com.xkl.config.ResultStatus.*;

/**
 * Created by win7 on 2016/11/20.
 */
@RestController
@RequestMapping("/dataShare")
public class DataShareController {
    @Autowired
    private XklAmpReportRespository xklAmpReportRespository;
    @Autowired
    private XklAmpReportDetailRespository xklAmpReportDetailRespository;
    @Autowired
    private XklAmpReportHealthScoreRespository xklAmpReportHealthScoreRespository;
    @Autowired
    private XklApiKeyRespository xklApiKeyRespository;
    @Autowired
    private XklCompanyRespository xklCompanyRespository;
    @Autowired
    private IScoreService scoreService;

    @LogAnnotation
    @AntiXSS
    @Sign
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "体检报告列表共享接口")
    public ResponseEntity<ResultModel> getReportList(HttpServletRequest request,
                                                     @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(SIGN_ERROR), HttpStatus.OK);
        XklApiKeyEntity xklApiKey = xklApiKeyRespository.findOne((long)type);
        List<XklAmpReportEntity> xklAmpReportResult = new ArrayList<>();
        if(xklApiKey.getActiveStatus()!=1){
            return new ResponseEntity<>(ResultModel.error(SIGN_NO_ACTIVE), HttpStatus.OK);
        }

        long companyId = xklApiKey.getCompanyId();
        XklCompanyEntity xklCompany = xklCompanyRespository.findOne(companyId);
        if(xklCompany!= null){
            int level = xklCompany.getLevel();//level:1-5
            if(level == 1){//level-1对应所有的
                xklAmpReportResult = xklAmpReportRespository.findByStatus(1);
            }else{
                int zeroLen = 8-(level-1)*2;
                long companyCode = xklCompany.getCompanyCode();
                long prefix = companyCode/((long)Math.pow(10,zeroLen));
                List<XklCompanyEntity> childCompanyList = xklCompanyRespository.findByCompanyCodeStartingWith(String.valueOf(prefix));
                if(childCompanyList!=null && childCompanyList.size()!=0){
                    List<Long> companyIdList = new ArrayList<>();
                    for(XklCompanyEntity company: childCompanyList){
                        companyIdList.add(company.getId());
                    }
                    xklAmpReportResult =  xklAmpReportRespository.findByCompanyIdIn(companyIdList);
                }
            }
        }else{
            return new ResponseEntity<>(ResultModel.error(SIGN_ERROR), HttpStatus.OK);
        }

        return new ResponseEntity<>(ResultModel.ok(xklAmpReportResult), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Sign
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "体检报告详情共享接口")
    public ResponseEntity<ResultModel> getReportDetail(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(SIGN_ERROR), HttpStatus.OK);
        XklAmpReportEntity report = xklAmpReportRespository.findOne(report_id);

        XklApiKeyEntity xklApiKey = xklApiKeyRespository.findOne((long)type);
        if(xklApiKey.getActiveStatus()!=1){
            return new ResponseEntity<>(ResultModel.error(SIGN_NO_ACTIVE), HttpStatus.OK);
        }

        long companyId = xklApiKey.getCompanyId();
        XklCompanyEntity company = xklCompanyRespository.findOne(companyId);
        if(company!=null){
            int level = company.getLevel();
            int zeroLen = 8-(level-1)*2;
            if(level!=1){
                long reportCompanyId = report.getCompanyId();
                XklCompanyEntity reportCompany = xklCompanyRespository.findOne(reportCompanyId);
                if(reportCompany!=null){
                    long reportParentCompanyCode = reportCompany.getCompanyCode() - reportCompany.getCompanyCode()%((long)Math.pow(10,zeroLen));
                    if(reportParentCompanyCode != company.getCompanyCode()){//该报告必须属于密钥所属的公司或区域
                        return new ResponseEntity<>(ResultModel.error(COMPANY_ERROR), HttpStatus.OK);
                    }
                }else{
                    return new ResponseEntity<>(ResultModel.error(COMPANY_ERROR), HttpStatus.OK);
                }
            }
        }else{
            return new ResponseEntity<>(ResultModel.error(COMPANY_ERROR), HttpStatus.OK);
        }

        List<ReportDetailModel> reportDetailModelList = new ArrayList<>();
        if (report != null) {
            if (report.getScore() - 0 < Constants.SMALL_DOUBLE || report.getScore() == null) {//首次调用接口,score为0
                scoreService.getScore(report_id);
            }
            List<XklAmpReportDetailEntity> reportDetailEntityList = xklAmpReportDetailRespository.findAllByReportId(report_id);
            for (XklAmpReportDetailEntity detail : reportDetailEntityList) {
                int itemId = detail.getItemId();
                ReportDetailModel reportDetail = new ReportDetailModel();
                reportDetail.setItemId(itemId);
                reportDetail.setItemValue(detail.getItemValue());
                reportDetail.setStatus(detail.getStatus());

                XklAmpReportMetaItemsEntity metaItems = Constants.itemMetaMap.get(itemId);
                if (metaItems != null) {
                    reportDetail.setTitle(metaItems.getTitle());
                    if (report.getSex() == Constants.MALE) {
                        reportDetail.setNormalRange(metaItems.getStandardLowMale() + " - " + metaItems.getStandardHighMale());
                    } else {
                        reportDetail.setNormalRange(metaItems.getStandardLowFemale() + " - " + metaItems.getStandardHighFemale());
                    }
                    reportDetail.setType(metaItems.getType());
                }

                XklAmpReportHealthScoreEntity scoreEntity = xklAmpReportHealthScoreRespository.findByReportIdAndType(report_id, metaItems.getType());
                if (scoreEntity != null)
                    reportDetail.setTypeHealthScore(scoreEntity.getTypeHealthScore());
                reportDetailModelList.add(reportDetail);
            }
        }
        ReportModel reportModel = new ReportModel(report, reportDetailModelList);
        return new ResponseEntity<>(ResultModel.ok(reportModel), HttpStatus.OK);
    }

}