package thyyxxk.webserver.controller.medicalinsurance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.manage.dismissrules.MedinsDismissRules;
import thyyxxk.webserver.entity.medicalinsurance.manage.dismissrules.RuleElement;
import thyyxxk.webserver.service.medicalinsurance.DismissRulesService;
import thyyxxk.webserver.utils.ResultVoUtil;

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

@RestController
@RequestMapping("/dismissRules")
public class DismissRulesController {
    private final DismissRulesService service;

    @Autowired
    public DismissRulesController(DismissRulesService service) {
        this.service = service;
    }

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

    @GetMapping("/getBaseDict")
    public ResultVo<Map<String, List<RuleElement>>> getBaseDict() {
        return ResultVoUtil.success(service.getBaseDict());
    }

    @PostMapping("/saveEditResult")
    public ResultVo<String> saveEditResult(@RequestBody MedinsDismissRules request) {
        int result = service.saveEditResult(request);
        if (result == 1) {
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "操作失败，请稍后再试。");
    }

    @GetMapping("/deleteRule")
    public ResultVo<String> deleteRule(@RequestParam("id") String id) {
        int result = service.deleteRule(id);
        if (result == 1) {
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "操作失败，请稍后再试。");
    }
}
