package thyyxxk.webserver.controller.inpatient.casefrontsheet;

import org.springframework.beans.factory.annotation.Autowired;
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 thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.casefrontsheet.request.SheetOverview;
import thyyxxk.webserver.entity.casefrontsheet.tcmsheet.TcmFrontSheetMain;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.service.inpatient.casefrontsheet.tcm.TcmSheetMainService;
import thyyxxk.webserver.service.inpatient.casefrontsheet.tcm.TcmSheetVerifyService;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

@RestController
@RequestMapping("/tcmFrontSheet")
public class TcmSheetController {
    private final TcmSheetMainService mainService;
    private final TcmSheetVerifyService verifyService;

    @Autowired
    public TcmSheetController(TcmSheetMainService mainService, TcmSheetVerifyService verifyService) {
        this.mainService = mainService;
        this.verifyService = verifyService;
    }

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

    @PostMapping("/saveTcmSheet")
    public ResultVo<String> saveTcmSheet(@RequestBody TcmFrontSheetMain sheet) {
        return ResultVoUtil.success(mainService.saveTcmSheet(sheet));
    }

    @PostMapping("/verify")
    public ResultVo<List<CodeName>> verify(@RequestBody TcmFrontSheetMain sheet) {
        List<CodeName> array = verifyService.verify(sheet);
        if (null == array || array.isEmpty()) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "校验未通过。", array);
    }
}