package thyyxxk.webserver.controller.inpatient.casefrontsheet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.casefrontsheet.*;
import thyyxxk.webserver.entity.casefrontsheet.pojo.FrontsheetQualityVerification;
import thyyxxk.webserver.entity.casefrontsheet.pojo.GroupLeaderVerification;
import thyyxxk.webserver.entity.casefrontsheet.request.*;
import thyyxxk.webserver.entity.casefrontsheet.request.OpCaseFrontsheet;
import thyyxxk.webserver.entity.casefrontsheet.response.AuditCount;
import thyyxxk.webserver.entity.casefrontsheet.response.BaOpLog;
import thyyxxk.webserver.entity.casefrontsheet.response.DismissCount;
import thyyxxk.webserver.entity.casefrontsheet.response.LendOutSheet;
import thyyxxk.webserver.entity.casefrontsheet.state.State;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.service.inpatient.casefrontsheet.CaseFrontSheetAnalyzeService;
import thyyxxk.webserver.service.inpatient.casefrontsheet.CaseFrontSheetMainService;
import thyyxxk.webserver.service.inpatient.casefrontsheet.FrontsheetAuditService;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;
import java.util.Map;

/**
 * @author dj
 */
@RestController
@RequestMapping("/caseFrontSheet")
public class CaseFrontSheetController {
    private final CaseFrontSheetMainService service;
    private final CaseFrontSheetAnalyzeService analyzeService;
    private final FrontsheetAuditService auditService;

    @Autowired
    public CaseFrontSheetController(CaseFrontSheetMainService service, CaseFrontSheetAnalyzeService analyzeService, FrontsheetAuditService auditService) {
        this.service = service;
        this.analyzeService = analyzeService;
        this.auditService = auditService;
    }

    @GetMapping("/getAllDictionary")
    @PassToken
    public ResultVo<Map<String, List<CodeName>>> getAllDictionary() {
        return service.getAllDictionary();
    }

    @GetMapping("/resetDictionary")
    @PassToken
    public ResultVo<String> resetDictionary() {
        return service.resetDictionary();
    }

    @GetMapping("/getUserWards")
    public ResultVo<List<CodeName>> getUserWards() {
        return service.getUserWards();
    }

    @GetMapping("/getAllWards")
    public ResultVo<List<CodeName>> getWards() {
        return service.getAllWards();
    }

    @GetMapping("/getPatientOverview")
    public ResultVo<List<SheetOverview>> getPatientOverview(String ward) {
        return service.getPatientOverview(ward);
    }

    @PostMapping("/outPatients")
    public ResultVo<List<SheetOverview>> getOutPatients(@RequestBody DismissedSheetInquiry param) {
        return service.getOutPatient(param);
    }

    @PostMapping("/basOutPatients")
    public ResultVo<List<SheetOverview>> getOutPatientsForBas(@RequestBody DismissedSheetInquiry param) {
        return service.basOutPatients(param);
    }

    @PostMapping("/getSheetInfo")
    public ResultVo<CaseFrontsheetMain> getPatientInfo(@RequestBody SheetOverview overview) {
        CaseFrontsheetMain response = service.getPatientInfo(overview);
        if (null == response) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(response);
    }

    @GetMapping("/getYbDiags")
    public ResultVo<List<YiBaoDisdiag>> getYbDiags(@RequestParam("bah") String bah,
                                                   @RequestParam("times") Integer times) {
        return service.getYbDiags(bah, times);
    }

    @PostMapping("/saveYbDiags")
    public ResultVo<String> saveYbDiags(@RequestBody SaveYbDiag param) {
        return service.saveYbDiags(param);
    }

    @PostMapping("/unArchiveBa")
    public ResultVo<String> unArchiveBa(@RequestBody OpCaseFrontsheet param) {
        param.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return service.unArchiveSheet(param);
    }

    @PostMapping("/printVerify")
    public ResultVo<Map<String, List<CodeName>>> printVerification(@RequestBody OpCaseFrontsheet param) {
        param.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return service.printVerification(param);
    }

    @PostMapping("/saveVerify")
    public ResultVo<List<CodeName>> saveVerify(@RequestBody OpCaseFrontsheet param) {
        param.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return service.saveVerify(param);
    }

    @PostMapping("/frontsheetQualityCheck")
    public ResultVo<String> frontsheetQualityCheck(@RequestBody CaseFrontsheetMain sheet) {
        return service.frontsheetQualityCheck(sheet);
    }

    @PostMapping("/advanceSearch")
    public ResultVo<List<SheetOverview>> advanceSearch(@RequestBody AdvanceSearch param) {
        return service.advanceSearch(param);
    }

    @GetMapping("/fetchSsfz")
    public ResultVo<String> fetchSsfz(@RequestParam("code") String code,
                                      @RequestParam("bah") String bah,
                                      @RequestParam("times") Integer times) {
        return service.fetchSsfz(code, bah, times);
    }

    @PostMapping("/analyzeDismissCount")
    public ResultVo<List<DismissCount>> analyzeDismissCount(@RequestBody SheetAnalyzeParams param) {
        return analyzeService.analyzeDismissCount(param);
    }

    @GetMapping("/increaseDiagWeight")
    public void increaseDiagWeight(@RequestParam("code") String code) {
        service.increaseDiagWeight(code);
    }

    @PostMapping("/analyzeQualityControl")
    public ResultVo<List<SheetQuality>> analyzeQualityControl(@RequestBody SheetAnalyzeParams params) {
        return analyzeService.analyzeQualityControl(params);
    }

    @GetMapping("/selectSiDiagByBaDiag")
    public ResultVo<CodeName> selectSiDiagByBaDiag(@RequestParam("code") String code) {
        return service.selectSiDiagByBaDiag(code);
    }

    @GetMapping("/isMedinsSetl")
    public ResultVo<String> isMedinsSetl(@RequestParam("patNo") String patNo, @RequestParam("times") Integer times) {
        return service.isMedinsSetl(patNo, times);
    }

    @PassToken
    @PostMapping("/sheetVerification")
    public ResultVo<Map<String, List<CodeName>>> sheetVerification(@RequestBody SheetOverview view) {
        return service.sheetVerification(view);
    }

    @PostMapping("/markDuplicate")
    public ResultVo<String> markDuplicate(@RequestBody MarkStateRequest request) {
        String response = service.markDuplicate(request);
        if (response.equals("OK")) {
            return ResultVoUtil.success(response);
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, response);
    }

    @PostMapping("/markClosedown")
    public ResultVo<String> markClosedown(@RequestBody MarkStateRequest request) {
        String response = service.markClosedown(request);
        if (response.equals("OK")) {
            return ResultVoUtil.success(response);
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, response);
    }

    @GetMapping("/getMarkStateDetail")
    public ResultVo<MarkStateRequest> getMarkStateDetail(@RequestParam("bah") String bah,
                                                         @RequestParam("times") int times,
                                                         @RequestParam("type") String type) {
        MarkStateRequest request = service.getMarkStateDetail(bah, times, type);
        if (null == request) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(request);
    }

    @GetMapping("/signedByDateCount")
    public ResultVo<Map<String, List<SheetOverview>>> signedByDateCount(@RequestParam("signDate") String signDate) {
        return ResultVoUtil.success(service.signedByDateCount(signDate));
    }

    @GetMapping("/autoSign")
    @PassToken
    public void autoSign() {
        List<SheetOverview> list = service.getSheetsForAutoSign();
        for (SheetOverview o : list) {
            o.setInOutFlag(State.InHospital.NO);
            CaseFrontsheetMain mainSheet = service.getPatientInfo(o);
            if (null == mainSheet) {
                continue;
            }
            String prtres = service.printVerifyForAutoSign(mainSheet);
            if (!prtres.equals("OK")) {
                continue;
            }
            mainSheet.setSignDate(o.getSignDateOrigin());
            mainSheet.setLateFlag(o.getLateFlag());
            mainSheet.setSignStaff(o.getSignStaff());
            mainSheet.setFileStatus(State.FileStatus.SIGNED);
            mainSheet.setUnsignDate(null);
            service.autoSign(mainSheet);
        }
    }

    @GetMapping("/getSimpleSheetForLendOut")
    public ResultVo<List<SheetOverview>> getSimpleSheetForLendOut(@RequestParam("bah") String bah) {
        List<SheetOverview> list = service.getSimpleSheetForLendOut(bah);
        if (list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(list);
    }

    @PostMapping("/doLendOut")
    public ResultVo<String> doLendOut(@RequestBody LendOutRequest request) {
        String response = service.doLendOut(request);
        if (response.equals("OK")) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, response);
    }

    @GetMapping("/getLendOutSheets")
    public ResultVo<List<LendOutSheet>> getLendOutSheets() {
        return ResultVoUtil.success(service.getLendOutSheets());
    }

    @GetMapping("/returnLendOutSheet")
    public ResultVo<String> returnLendOutSheet(@RequestParam("id") Integer id) {
        String response = service.returnLendOutSheet(id);
        if (response.equals("OK")) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, response);
    }

    @GetMapping("/getLendOutDetail")
    public ResultVo<LendOutRequest> getLendOutDetail(@RequestParam("bah") String bah,
                                                     @RequestParam("times") int times) {
        LendOutRequest request = service.getLendOutDetail(bah, times);
        if (null == request) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(request);
    }

    @PostMapping("/submitQualityVerification")
    public ResultVo<String> submitQualityVerification(@RequestBody FrontsheetQualityVerification request) {
        String response = auditService.submitQualityVerification(request);
        if (response.equals("OK")) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, response);
    }

    @PostMapping("/fetchQualityVerifications")
    public ResultVo<List<FrontsheetQualityVerification>> fetchQualityVerifications(@RequestBody AuditInquiry inquiry) {
        return ResultVoUtil.success(auditService.fetchQualityVerifications(inquiry));
    }

    @PostMapping("/executeAudit")
    public ResultVo<String> executeAudit(@RequestBody FrontsheetQualityVerification request) {
        String result = auditService.executeAudit(request);
        if (result.equals("OK")) {
            return ResultVoUtil.success("操作成功。");
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result);
    }

    @PostMapping("/revokeApprovedAudit")
    public ResultVo<String> revokeApprovedAudit(@RequestBody FrontsheetQualityVerification request) {
        return ResultVoUtil.success(auditService.revokeApprovedAudit(request));
    }

    @PostMapping("/fetchAuditHistories")
    public ResultVo<Map<String, Object>> fetchAuditHistories(@RequestBody FrontsheetQualityVerification request) {
        return ResultVoUtil.success(auditService.fetchAuditHistories(request));
    }

    @PostMapping("/fetchAuditCount")
    public ResultVo<AuditCount> fetchAuditCount(@RequestBody FrontsheetQualityVerification request) {
        return ResultVoUtil.success(auditService.fetchAuditCount(request));
    }

    @GetMapping("/getAllSmallDept")
    public ResultVo<List<CodeName>> getAllSmallDept() {
        return ResultVoUtil.success(auditService.getAllSmallDept());
    }

    @PostMapping("/fetchExportData")
    public ResultVo<List<FrontsheetQualityVerification>> fetchExportData(@RequestBody AuditInquiry inquiry) {
        return ResultVoUtil.success(auditService.fetchExportData(inquiry));
    }

    @GetMapping("/getBaOpLogs")
    public ResultVo<List<BaOpLog>> getBaOpLogs(@RequestParam("bah") String bah,
                                               @RequestParam("times") Integer times) {
        List<BaOpLog> list = service.getBaOpLogs(bah, times);
        if (ListUtil.isBlank(list)) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(list);
    }

    @PostMapping("/fetchLeaderAudit")
    public ResultVo<List<GroupLeaderVerification>> fetchLeaderAudit(@RequestBody AuditInquiry inquiry) {
        return ResultVoUtil.success(auditService.fetchLeaderAudit(inquiry));
    }

    @PostMapping("/submitLeaderAudit")
    public ResultVo<String> submitLeaderAudit(@RequestBody GroupLeaderVerification request) {
        return ResultVoUtil.success(auditService.submitLeaderAudit(request));
    }

    @PostMapping("/fetchLeaderExportData")
    public ResultVo<List<GroupLeaderVerification>> fetchLeaderExportData(@RequestBody AuditInquiry inquiry) {
        return ResultVoUtil.success(auditService.fetchLeaderExportData(inquiry));
    }
}
