package thyyxxk.webserver.controller.dictionary;

import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.HisWjwMatchEntity;
import thyyxxk.webserver.service.dictionary.HisWjwMatchService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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

@RestController
@RequestMapping("/hisWjwMatch")
public class HisWjwMatchController {
    private final HisWjwMatchService service;

    public HisWjwMatchController(HisWjwMatchService service) {
        this.service = service;
    }

    @GetMapping("/selectMatchableDataByLabel")
    public ResultVo<Map<String, List<HisWjwMatchEntity>>> selectMatchableDataByLabel(@RequestParam("label") String label) {
        if (StringUtil.isBlank(label)) {
            return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "label参数不能为空。");
        }
        Map<String, List<HisWjwMatchEntity>> result = service.selectMatchableDataByLabel(label);
        if (null == result) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(result);
    }

    @GetMapping("/fetchSimilarities")
    public ResultVo<List<HisWjwMatchEntity>> fetchSimilarities(@RequestParam("label") String label,
                                                               @RequestParam("name") String name,
                                                               @RequestParam("key") String key) {
        return ResultVoUtil.success(service.fetchSimilarities(label, name, key));
    }

    @PostMapping("/executeMatchAction")
    public ResultVo<String> executeMatchAction(@RequestBody HisWjwMatchEntity entity) {
        if (StringUtil.isBlank(entity.getCode())) {
            return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "HIS编码不能为空！");
        }
        if (StringUtil.isBlank(entity.getLabel())) {
            return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "匹配数据类别不能为空！");
        }
        switch (entity.getLabel()) {
            case "anaesthesia":
                entity.setTable("zd_anaesthesia");
                break;
            case "department":
                entity.setTable("zd_unit_code");
                break;
            case "diagnose":
            case "si_diag":
                entity.setTable("zd_icd_code_new");
                break;
            case "surgery":
            case "surgery_chargeCode":
            default:
                entity.setTable("zd_icd9_cm3");
                break;
        }
        return ResultVoUtil.success(service.executeMatchAction(entity));
    }

}
