package com.qmth.teachcloud.report.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qmth.boot.tools.signature.SignatureEntity;
import com.qmth.boot.tools.signature.SignatureType;
import com.qmth.teachcloud.common.config.DictionaryConfig;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicSchool;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.util.HttpUtil;
import com.qmth.teachcloud.common.util.JacksonUtil;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.report.business.bean.dto.printOpen.*;
import com.qmth.teachcloud.report.business.service.CallPrintOpenApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 请求分布式印刷服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-06-08
 */
@Service
public class CallPrintOpenApiServiceImpl implements CallPrintOpenApiService {
    @Resource
    CommonCacheService commonCacheService;

    @Resource
    DictionaryConfig dictionaryConfig;

    private final static Logger log = LoggerFactory.getLogger(CallPrintOpenApiServiceImpl.class);


    @Override
    public PaperConfig callPaperConfig(Long schoolId, Long thirdExamId, String gradeCourseCode) throws IOException {
        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
        log.info("请求分布式印刷试卷配置");
        JSONObject printOpenParams = new JSONObject();
        printOpenParams.put("thirdExamId", thirdExamId);
        printOpenParams.put("gradeCourseCode", gradeCourseCode);
        long timestamp = System.currentTimeMillis();

        String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.printOpenDomain().getPaperConfig(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
        String callResult = HttpUtil.postJson(dictionaryConfig.printOpenDomain().getHostUrl() + dictionaryConfig.printOpenDomain().getPaperConfig(), JacksonUtil.parseJson(printOpenParams), accessToken, timestamp);
        PaperConfig paperConfig = new PaperConfig();
        if (Objects.nonNull(callResult)) {
            log.info("callResult:{}", JacksonUtil.parseJson(callResult));
            Result result = JSON.parseObject(callResult, Result.class);
            Object data = result.getData();
            paperConfig = JSON.parseObject(JSON.toJSONString(data), PaperConfig.class);
        }
        return paperConfig;
    }

    @Override
    public List<PaperDimension> callPaperDimension(Long schoolId, Long thirdExamId, String gradeCourseCode) throws IOException {
        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
        log.info("请求分布式印刷维度查询");
        JSONObject printOpenParams = new JSONObject();
        printOpenParams.put("thirdExamId", thirdExamId);
        printOpenParams.put("gradeCourseCode", gradeCourseCode);
        long timestamp = System.currentTimeMillis();

        String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.printOpenDomain().getPaperDimension(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
        String callResult = HttpUtil.postJson(dictionaryConfig.printOpenDomain().getHostUrl() + dictionaryConfig.printOpenDomain().getPaperDimension(), JacksonUtil.parseJson(printOpenParams), accessToken, timestamp);
        List<PaperDimension> paperDimensionList = new ArrayList<>();
        if (Objects.nonNull(callResult)) {
            log.info("callResult:{}", JacksonUtil.parseJson(callResult));
            Result result = JSON.parseObject(callResult, Result.class);
            Object data = result.getData();
            paperDimensionList = JSON.parseArray(JSON.toJSONString(data), PaperDimension.class);
        }
        return paperDimensionList;
    }

    @Override
    public List<PaperStructure> callPaperStruct(Long schoolId, Long thirdExamId, String gradeCourseCode) throws IOException {
        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
        log.info("请求分布式印刷试卷结构查询");
        JSONObject printOpenParams = new JSONObject();
        printOpenParams.put("thirdExamId", thirdExamId);
        printOpenParams.put("gradeCourseCode", gradeCourseCode);
        long timestamp = System.currentTimeMillis();

        String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.printOpenDomain().getPaperStructure(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
        String callResult = HttpUtil.postJson(dictionaryConfig.printOpenDomain().getHostUrl() + dictionaryConfig.printOpenDomain().getPaperStructure(), JacksonUtil.parseJson(printOpenParams), accessToken, timestamp);
        List<PaperStructure> paperStructureList = new ArrayList<>();
        if (Objects.nonNull(callResult)) {
            log.info("callResult:{}", JacksonUtil.parseJson(callResult));
            Result result = JSON.parseObject(callResult, Result.class);
            Object data = result.getData();
            paperStructureList = JSON.parseArray(JSON.toJSONString(data), PaperStructure.class);
        }
        return paperStructureList;
    }

    @Override
    public List<PaperEvaluation> callPaperModule(Long schoolId, Long thirdExamId, String gradeCourseCode) throws IOException {
        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
        log.info("请求分布式印刷试卷模块评价查询");
        JSONObject printOpenParams = new JSONObject();
        printOpenParams.put("thirdExamId", thirdExamId);
        printOpenParams.put("gradeCourseCode", gradeCourseCode);
        long timestamp = System.currentTimeMillis();

        String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.printOpenDomain().getPaperEvaluation(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
        String callResult = HttpUtil.postJson(dictionaryConfig.printOpenDomain().getHostUrl() + dictionaryConfig.printOpenDomain().getPaperEvaluation(), JacksonUtil.parseJson(printOpenParams), accessToken, timestamp);
        List<PaperEvaluation> paperEvaluationList = new ArrayList<>();
        if (Objects.nonNull(callResult)) {
            log.info("callResult:{}", JacksonUtil.parseJson(callResult));
            Result result = JSON.parseObject(callResult, Result.class);
            Object data = result.getData();
            paperEvaluationList = JSON.parseArray(JSON.toJSONString(data), PaperEvaluation.class);
        }
        return paperEvaluationList;
    }

    @Override
    public List<ExamStudentScore> callExamStudentScore(Long schoolId, Long thirdExamId, String gradeCourseCode) throws IOException {
        BasicSchool basicSchool = commonCacheService.schoolCache(schoolId);
        log.info("请求分布式印刷考生成绩查询");
        JSONObject printOpenParams = new JSONObject();
        printOpenParams.put("thirdExamId", thirdExamId);
        printOpenParams.put("gradeCourseCode", gradeCourseCode);
        printOpenParams.put("pageSize", 100);
        List<ExamStudentScore> examStudentScoreList = new ArrayList<>();
        List<ExamStudentScore> examStudentScoreCell = new ArrayList<>();
        int pageNumber = 1;
        do {
            printOpenParams.put("pageNumber", pageNumber);
            long timestamp = System.currentTimeMillis();
            String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.printOpenDomain().getExamStudentScore(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
            String callResult = HttpUtil.postJson(dictionaryConfig.printOpenDomain().getHostUrl() + dictionaryConfig.printOpenDomain().getExamStudentScore(), JacksonUtil.parseJson(printOpenParams), accessToken, timestamp);
            if (Objects.nonNull(callResult)) {
                log.info("callResult:{}", JacksonUtil.parseJson(callResult));
                Result result = JSON.parseObject(callResult, Result.class);
                Object data = result.getData();
                examStudentScoreCell = JSON.parseArray(JSON.toJSONString(data), ExamStudentScore.class);
                examStudentScoreList.addAll(examStudentScoreCell);
            }
            pageNumber++;
        } while (examStudentScoreCell.size() > 0);
        return examStudentScoreList;
    }


}
