package thyyxxk.webserver.service.inpatient.charge;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.inpatient.charge.MisPosVo;

/**
 * @Description: 智能银行接口
 * @Author:lihong
 * @Date: 2024/9/14
 */
@Slf4j
@Service
public class TransactionService {
    @Value("${thmz-api-url}")
    private String thmzUrl;
    private final RestTemplate template;

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

    public ResultVo posGet(String url,String param){
        String realUrl = StrUtil.format("{}/{}?{}", thmzUrl, url,param);
        log.info("调用智能pos机,realUrl={}",realUrl);
        ResultVo response = template.getForObject(realUrl, ResultVo.class);
        dealResponse(response);
        return response;
    }

    private void dealResponse(ResultVo response) {
        if (response == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "调用智能pos机失败");
        }
        if (response.getCode() == -1) {
            response.setCode(ExceptionEnum.INTERNAL_SERVER_ERROR.getCode());
        } else {
            response.setCode(ExceptionEnum.SUCCESS_AND_EL_MESSAGE.getCode());
        }
    }
    public ResultVo posPost(MisPosVo misPosVo,String url){
        String realUrl = StrUtil.format("{}/{}", thmzUrl,url);
        log.info("调用智能pos机,realUrl={}",realUrl);
        ResultVo response = template.postForObject(realUrl, misPosVo, ResultVo.class);
        dealResponse(response);
        return response;
    }


}
