package thyyxxk.webserver.controller.outpatient.template;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.outpatient.template.TcInquiry;
import thyyxxk.webserver.utils.ResultVoUtil;


@Slf4j
@RestController
@RequestMapping("/template")
public class TemplateController {
    @Value("${thmz-api-url}")
    private String thmzApiUrl;
    private final RestTemplate template;

    @Autowired
    public TemplateController(RestTemplate template) {
        this.template = template;
    }

    @PostMapping("/queryTcDepartment")
    public ResultVo<JSONArray> queryTcDepartment() {
        String url = thmzApiUrl + "/queryTcDiscuntDept";
        ResultVo response = template.getForObject(url, ResultVo.class);
        if (null == response) {
            return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
        }
        if (response.getCode() == 0) {
            response.setCode(200);
        }
        return response;
    }

    @PostMapping("/queryAllDepartment")
    public ResultVo<JSONArray> queryAllDepartment() {
        String url = thmzApiUrl + "/queryAllDept";
        ResultVo response = template.getForObject(url, ResultVo.class);
        if (null == response) {
            return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
        }
        if (response.getCode() == 0) {
            response.setCode(200);
        }
        return response;
    }

    @PostMapping("/queryTcByDepartment")
    public ResultVo queryTcByDepartment(@RequestBody TcInquiry inquiry) {
        String url = thmzApiUrl + "/queryTcByDeptCode";
        ResultVo response = template.postForObject(url, inquiry, ResultVo.class);
        if (null == response) {
            return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
        }
        if (response.getCode() == 0) {
            response.setCode(200);
        }
        return response;
    }

    @PostMapping("/queryTcByTemplateId")
    public ResultVo queryTcByTemplateId(@RequestBody TcInquiry inquiry) {
        String url = thmzApiUrl + "/queryTcByTemplateId";
        ResultVo response = template.postForObject(url, inquiry, ResultVo.class);
        if (null == response) {
            return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
        }
        if (response.getCode() == 0) {
            response.setCode(200);
        }
        return response;
    }
}
