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

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.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.HttpUtil;
import com.qmth.teachcloud.report.business.entity.TBCloudExam;
import com.qmth.teachcloud.report.business.service.NewCallApiService;
import com.qmth.teachcloud.report.business.service.TBCloudExamService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * @Description: 云阅卷调用服务api
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/12/15
 */
@Service
public class NewCallApiServiceImpl implements NewCallApiService {
    private final static Logger log = LoggerFactory.getLogger(NewCallApiServiceImpl.class);

    @Resource
    DictionaryConfig dictionaryConfig;

    @Resource
    TBCloudExamService tbCloudExamService;

    /**
     * 根据考试id或考试编码调用云阅卷学生成绩接口
     *
     * @param examId
     * @param examCode
     * @return
     */
    @Override
    public List<Map> callStudentScore(Long examId, String examCode) throws IOException {
        String url = dictionaryConfig.yunMarkDomain().getUrl() + dictionaryConfig.yunMarkDomain().getStudentScoreApi();
        Map<String, Object> params = new HashMap<>();
        if (Objects.nonNull(examId)) {
            params.put("examId", examId);
        } else if (Objects.nonNull(examCode)) {
            params.put("examCode", examCode);
        }
        TBCloudExam tbCloudExam = tbCloudExamService.getById(examId);
        if (Objects.isNull(tbCloudExam)) {
            throw ExceptionResultEnum.ERROR.exception("找不到该考试");
        }
        int pageNumber = 1;
        int pageSize = 100;
        List<Map> students = null;
        for (; ; ) {
            params.put("pageNumber", pageNumber);
            params.put("pageSize", pageSize);
            Long timestamp = System.currentTimeMillis();
            String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.yunMarkDomain().getStudentScoreApi(), timestamp, tbCloudExam.getAccessKey(), tbCloudExam.getAccessSecret());
            String result = HttpUtil.post(url, params, accessToken, timestamp);
            result = StringEscapeUtils.unescapeHtml4(result);
            if (Objects.nonNull(result)) {
                if (result.contains("HTTP")) {
                    throw ExceptionResultEnum.ERROR.exception("云阅卷鉴权失败");
                }
                List<Map> tempList = JSONObject.parseArray(JSONObject.toJSON(result).toString(), Map.class);
                if (Objects.nonNull(tempList) && tempList.size() > 0) {
                    if (Objects.isNull(students)) {
                        students = new ArrayList<>();
                    }
                    students.addAll(tempList);
                    pageNumber++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return students;
    }
}
