package com.qmth.teachcloud.common.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.service.CallApiOrgCenterService;
import com.qmth.teachcloud.common.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 机构用户中心接口调取服务实现类
 * @Author: CaoZixuan
 * @Date: 2021-03-26
 */
@Service
public class CallApiOrgCenterServiceImpl implements CallApiOrgCenterService {
    @Resource
    private DictionaryConfig dictionaryConfig;

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

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Map> callOrgInfo() throws IOException {
        List<Map> orgList = null;
        if ((Objects.isNull(dictionaryConfig.sysDomain().getAccessKey()) || Objects.equals(dictionaryConfig.sysDomain().getAccessKey(), ""))
                && (Objects.isNull(dictionaryConfig.sysDomain().getAccessSecret()) || Objects.equals(dictionaryConfig.sysDomain().getAccessSecret(), ""))) {
            return orgList;
        }
        String url = dictionaryConfig.orgCenterDomain().getUrl() + dictionaryConfig.orgCenterDomain().getOrgQueryApi();

        Map<String, Object> params = new HashMap<>();
        int pageNumber = 1;
        int pageSize = 100;

        while (true) {
            params.put("pageNumber", pageNumber);
            params.put("pageSize", pageSize);
            long timestamp = System.currentTimeMillis();
            String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.orgCenterDomain().getOrgQueryApi(), timestamp, dictionaryConfig.sysDomain().getAccessKey(), dictionaryConfig.sysDomain().getAccessSecret());
            String result = HttpUtil.post(url, params, accessToken, timestamp);
            if (Objects.nonNull(result)) {
                if (result.contains("HTTP Status 401 – 未经授权的")) {
                    throw ExceptionResultEnum.ERROR.exception("机构用户中心鉴权失败");
                }
                List<Map> tempList = JSONObject.parseArray(result, Map.class);
                if (Objects.nonNull(tempList) && tempList.size() > 0) {
                    if (Objects.isNull(orgList)) {
                        orgList = new ArrayList<>();
                    }
                    orgList.addAll(tempList);
                    pageNumber++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return orgList;
    }
}
