package com.qmth.distributed.print;

import com.alibaba.fastjson.JSON;
import com.qmth.teachcloud.common.bean.marking.CardJpgResult;
import com.qmth.teachcloud.common.bean.marking.scanAnswerCard.AnswerCard;
import com.qmth.teachcloud.common.bean.marking.scanAnswerCard.AnswerCardPage;
import com.qmth.teachcloud.common.util.HttpUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 从卡格式获取图片
 */
public class AnswerCardPageImageTest {

    public static void main(String[] args) {
        List<CardJpgResult> cardJpgResultList = new ArrayList<>();
        String url = "https://oss-file.qmth.com.cn/teachcloud-dps-dev-private/card/506180312611422208/Z137211/adapt/11.json";
        try {
            String jsonContent = HttpUtil.get(url, null, null, null);
            if (StringUtils.isNotBlank(jsonContent)) {
                AnswerCard answerCard = JSON.parseObject(jsonContent, AnswerCard.class);
                if (answerCard != null && CollectionUtils.isNotEmpty(answerCard.getPages())) {
                    int pageIndex = 1;
                    for (AnswerCardPage page : answerCard.getPages()) {
                        if (page.getExchange() != null && StringUtils.isNotBlank(page.getExchange().getPageImage())) {
                            String pageImage = page.getExchange().getPageImage().replaceAll("\\r\\n", "");
                            cardJpgResultList.add(new CardJpgResult("page" + pageIndex, pageIndex, "data:image/png;base64," + pageImage));
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
