package thyyxxk.webserver.controller.inpatient;

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.inpatient.chargelist.BriefPatInfo;
import thyyxxk.webserver.entity.inpatient.chargelist.PatOverview;
import thyyxxk.webserver.service.inpatient.ChargeListService;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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

@RestController
@RequestMapping("/chargeList")
public class ChargeListController {
    private final ChargeListService service;

    @Autowired
    public ChargeListController(ChargeListService service) {
        this.service = service;
    }

    @GetMapping("/selectPatOverviews")
    public ResultVo<List<PatOverview>> selectPatOverviews(@RequestParam("patNo") String patNo) {
        if (StringUtil.isBlank(patNo)) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "住院号不能为空！");
        }
        List<PatOverview> resultList = service.selectPatOverviews(patNo);
        if (ListUtil.isBlank(resultList)) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(resultList);
    }

    @PostMapping("/selectBriefPatInfo")
    @PassToken
    public ResultVo<BriefPatInfo> selectBriefPatInfo(@RequestBody PatOverview overview) {
        BriefPatInfo result = service.selectBriefPatInfo(overview);
        if (null == result) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有查询到患者【" + overview.getPatNo() + "】在我院的住院记录。");
        }
        return ResultVoUtil.success(result);
    }

    @PostMapping("/selectChargeList")
    @PassToken
    public ResultVo<Map<String, Object>> selectChargeList(@RequestBody PatOverview overview) {
        Map<String, Object> resultMap = service.selectChargeList(overview);
        if (null == resultMap || resultMap.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有查询到患者的有效费用。");
        }
        return ResultVoUtil.success(resultMap);
    }

    @PostMapping("/executeSelfpayUpload")
    public ResultVo<String> executeSelfpayUpload(@RequestBody BriefPatInfo patInfo) {
        return service.executeSelfpayUpload(patInfo);
    }
}
