package thyyxxk.webserver.service.yj_req;

import cn.hutool.core.io.FileUtil;
import lombok.Data;
import org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.dao.his.examinations.InspectionsDao;
import thyyxxk.webserver.entity.yiji.IndexInquiry;
import thyyxxk.webserver.entity.yiji.YjReqIndex;

import java.util.List;

public interface YjReqInterface {

    List<YjReqIndex> queryYjReqIndex(IndexInquiry inquiry);

    static String uploadPdf(YjReqIndex yjReq, InspectionsDao dao) {
        RestTemplate restTemplate = new RestTemplate();
        JcbgSaveReq saveReq = new JcbgSaveReq();
        saveReq.setUid(yjReq.getUid());
        try {
            saveReq.setFile(yjReq.getFile().getBytes());
            FileUtil.writeBytes(saveReq.getFile(), yjReq.getArchiveDir());
        } catch (Exception e) {
            throw new BizException(e.getMessage());
        }
        String url = "https://staticweb.hnthyy.cn/wxserver/api/saveJcbg/save";
        String result = restTemplate.postForObject(url, saveReq, String.class);
        yjReq.setReportUrl(result);
        dao.insertNewCheckData(yjReq);
        dao.insertArchive(yjReq);
        return "OK";
    }

    @Data
    class JcbgSaveReq {
        private String uid;
        private byte[] file;
    }
}
