package com.diagbot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.*;
import com.diagbot.entity.QcCasesRelevanceEntry;
import com.diagbot.facade.BehospitalInfoFacade;
import com.diagbot.facade.MedExamineInfoFacade;
import com.diagbot.facade.QcresultInfoFacade;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 住院病历信息 前端控制器
 * </p>
 *
 * @author zhoutg
 * @time: 2024-04-13
 */
@RestController
@RequestMapping("/qc/behospitalInfo")
@Api(value = "病历API", tags = { "病历API" })
@SuppressWarnings("unchecked")
public class BehospitalInfoController {
    @Autowired
    BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private QcresultInfoFacade qcresultInfoFacade;
    @Autowired
    MedExamineInfoFacade medExamineInfoFacade;

    @ApiOperation(value = "病历分页列表[by:zhoutg]",
            notes = "behospitalCode：病人住院序号<br>" +
                    "name：病人姓名<br>" +
                    "fileCode：档案号<br>" +
                    "statisticsType: 统计维度 1-本月，2-本年<br>" +
                    "behosDateStart：入院时间开始时间<br>" +
                    "behosDateEnd：入院时间结束时间<br>" +
                    "leaveHosDateStart：出院时间开始时间<br>" +
                    "leaveHosDateEnd：出院时间结束时间<br>" +
                    "doctorName：医生姓名<br>" +
                    "doctorCode：医生工号<br>" +
                    "level：等级<br>" +
                    "deptId：科室id<br>")
    @PostMapping("/page")
    @SysLogger("page")
    public RespDTO<IPage<BehospitalInfoDTO>> page(@RequestBody BehospitalPageVO behospitalPageVO) {
        IPage<BehospitalInfoDTO> data = behospitalInfoFacade.pageFac(behospitalPageVO);
        return RespDTO.onSuc(data);
    }

    @ApiOperation(value = "根据病历id获取详情[by:zhoutg]",
            notes = "")
    @PostMapping("/getByBehospitalCode")
    @SysLogger("getByBehospitalCode")
    public RespDTO<Map<String, Object>> getByBehospitalCode(@RequestBody GetDetailVO getDetailVO) {
        Map<String, Object> data = behospitalInfoFacade.getByBehospitalCode(getDetailVO);
        return RespDTO.onSuc(data);
    }

    @ApiOperation(value = "评分[by:zhoutg]",
            notes = "")
    @PostMapping("/analyze")
    @SysLogger("analyze")
    @Transactional
    public RespDTO<AnalyzeDTO> analyze(@RequestBody AnalyzeVO analyzeVO) {
        return RespDTO.onSuc(behospitalInfoFacade.analyze(analyzeVO));
    }

    @ApiOperation(value = "评分-运行质控[by:dsYun]",
            notes = "    病历id\n" +
                    "    String behospitalCode;\n" +
                    "    医院ID\n" +
                    "    Long hospitalId;\n" +
                    "    归档字段\n" +
                    "    String isPlacefile +\n" +
                    "    0：返回所有信息,1:只返回缺陷数量\n" +
                    "    String isDefect;")
    @PostMapping("/analyze_run")
    @SysLogger("analyze_run")
    @Transactional
    public RespDTO<AnalyzeRunDTO> analyzeRun(@Valid @RequestBody AnalyzeRunVO analyzeRunVO) {
        return RespDTO.onSuc(behospitalInfoFacade.analyzeRun(analyzeRunVO));
    }

    @ApiOperation(value = "定时任务评分[by:zhoutg]",
            notes = "")
    @PostMapping("/analyze_rpc")
    @SysLogger("analyze_rpc")
    @Transactional
    @ApiIgnore
    public RespDTO<AnalyzeDTO> analyze_rpc(@RequestBody AnalyzeVO analyzeVO) {
        return RespDTO.onSuc(behospitalInfoFacade.analyze(analyzeVO));
    }


    @ApiOperation(value = "评分-出院患者接口[by:dsYun]",
            notes = "")
    @PostMapping("/analyze_api")
    @SysLogger("analyze_api")
    @Transactional
    //    @ApiIgnore
    public RespDTO<AnalyzeApiDTO> analyzeApi(@RequestBody AnalyzeRunVO analyzeRunVO) {
        return RespDTO.onSuc(behospitalInfoFacade.analyzeApi(analyzeRunVO));
    }

    @ApiOperation(value = "根据病历号查看全部缺陷详情[by:dsYun]",
            notes = "")
    @PostMapping("/getCaseDetail")
    @SysLogger("getCaseDetail")
    public RespDTO<AnalyzeCaseDetailDTO> getCaseDetail(@RequestBody BehospitalcodeVO behospitalcodeVO) {
        AnalyzeCaseDetailDTO data = behospitalInfoFacade.getCaseDetail(behospitalcodeVO);
        return RespDTO.onSuc(data);
    }

    @ApiOperation(value = "忽略质控条目[by:dsYun]",
            notes = "")
    @PostMapping("/loseSightOfCase")
    @SysLogger("loseSightOfCase")
    @Transactional
    public RespDTO<AnalyzeDTO> loseSightOfCase(@RequestBody QcresultVO qcresultVO) {
        qcresultVO.setType(5);
        return RespDTO.onSuc(qcresultInfoFacade.changeQcResult(qcresultVO));
    }

    @ApiOperation(value = "新增质控条目[by:zhoutg]",
            notes = "")
    @PostMapping("/addCase")
    @SysLogger("addCase")
    @Transactional
    public RespDTO<AnalyzeDTO> addCase(@RequestBody QcresultVO qcresultVO) {
        qcresultVO.setType(1);
        return RespDTO.onSuc(qcresultInfoFacade.changeQcResult(qcresultVO));
    }

    @ApiOperation(value = "删除质控条目[by:zhoutg]",
            notes = "behospitalCode：病历号，必填<br>" +
                    "id：明细id，必填<br>")
    @PostMapping("/delCase")
    @SysLogger("delCase")
    @Transactional
    public RespDTO<AnalyzeDTO> delCase(@RequestBody QcresultVO qcresultVO) {
        qcresultVO.setType(2);
        return RespDTO.onSuc(qcresultInfoFacade.changeQcResult(qcresultVO));
    }

    @ApiOperation(value = "逻辑删除质控条目[by:songxl]",
            notes = "behospitalCode：病历号，必填<br>" +
                    "id：明细id，必填<br>" +
                    "delStatus：逻辑删除接口（1-还原和0-删除）标志 必填<br>")
    @PostMapping("/logicDelCase")
    @SysLogger("logicDelCase")
    @Transactional
    public RespDTO<AnalyzeDTO> logicDelCase(@RequestBody QcresultVO qcresultVO) {
        qcresultVO.setType(4);
        return RespDTO.onSuc(qcresultInfoFacade.changeQcResult(qcresultVO));
    }

    @ApiOperation(value = "修改质控条目[by:zhoutg]",
            notes = "")
    @PostMapping("/updCase")
    @SysLogger("updCase")
    @Transactional
    public RespDTO<AnalyzeDTO> updCase(@RequestBody QcresultVO qcresultVO) {
        qcresultVO.setType(3);
        return RespDTO.onSuc(qcresultInfoFacade.changeQcResult(qcresultVO));
    }

    @ApiOperation(value = "查看全部驳回患者[by:dsYun]",
            notes = "查看全部驳回患者")
    @PostMapping("/getRejectPatient")
    @SysLogger("getRejectPatient")
    @Transactional
    public RespDTO<List<RejectPatientDTO>> getRejectPatient(@RequestBody BehospitalcodeVO behospitalcodeVO) {
        return RespDTO.onSuc(medExamineInfoFacade.getRejectPatient(behospitalcodeVO));
    }

    @ApiOperation(value = "对病历进行评分【****************批量评分*************】[by:zhoutg]",
            notes = "参数内容清空：【所有医院】【未评分】病历数据<br>" +
                    "<br>" +
                    "hosptialIdList：医院列表id，1：长兴，3：台州，2：邵逸夫<br>" +
                    "useCrfCache: 是否使用crf缓存（true：使用crf缓存，false：不使用）<br>" +
                    "【个性化需求】——默认即可<br>" +
                    "filterFlag： 筛选病历数据：0：未评分的病历【推荐使用】；1：全部病历【谨慎使用】<br>" +
                    "individuation：【1：近3天不含当天的病历强制评分更新】<br>" +
                    "type：【1：出院时间比当前时间早3天前的病历（例如：长兴病历）】)<br>" +
                    "behospitalCodeList：指定病历号<br>")
    @PostMapping("/execule")
    @SysLogger("execule")
    public RespDTO<Boolean> execule(@RequestBody TaskVO taskVO) {
        behospitalInfoFacade.execute(taskVO, "");
        return RespDTO.onSuc(true);
    }


    @ApiOperation(value = "导出病历例子[by:gaodm]",
            notes = "")
    @PostMapping("/exportExcel")
    @SysLogger("exportExcel")
    public void exportExcel(HttpServletResponse response) {
        behospitalInfoFacade.exportExcelFac(response);
    }

    @ApiOperation(value = "质控结果导出[by:zhaops]",
            notes = "")
    @PostMapping("/exportQcresult")
    @SysLogger("exportQcresult")
    public void exportQcresult(HttpServletResponse response, @RequestBody ExportQcresultVO exportQcresultVO) {
        behospitalInfoFacade.exportQcresult(response, exportQcresultVO);
    }


}
