package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.boot.core.cache.service.CacheService;
import com.qmth.boot.core.solar.config.SolarProperties;
import com.qmth.boot.core.solar.model.AppInfo;
import com.qmth.boot.core.solar.model.OrgInfo;
import com.qmth.boot.core.solar.service.SolarService;
import com.qmth.jkserver.constant.SpringContextHolder;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dto.result.AppInfoResult;
import com.qmth.jkserver.enums.AuthEnum;
import com.qmth.jkserver.model.BasicSchool;
import com.qmth.jkserver.model.TSAuth;
import com.qmth.jkserver.service.AuthInfoService;
import com.qmth.jkserver.service.BasicSchoolService;
import com.qmth.jkserver.service.CommonCacheService;
import com.qmth.jkserver.service.TSAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * @Description: 授权信息service
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2022/5/17
 */
@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class AuthInfoServiceImpl implements AuthInfoService {
    private final static Logger log = LoggerFactory.getLogger(AuthInfoServiceImpl.class);

    @Resource
    SolarProperties solarProperties;

    @Resource
    SolarService solarService;

    @Resource
    TSAuthService tsAuthService;

    @Resource
    CommonCacheService commonCacheService;

    @Resource
    BasicSchoolService basicSchoolService;

    @Resource
    AuthInfoService authInfoService;

    /**
     * 授权信息初始化
     *
     * @return
     */
    @Override
    public AppInfo appInfoInit() {
        AppInfo appInfo = null;
        try {
            appInfo = solarService.getAppInfo();
            if (Objects.nonNull(appInfo) && Objects.nonNull(solarProperties)) {
                if (Objects.nonNull(solarProperties.getAccessKey())
                        && !Objects.equals(solarProperties.getAccessKey().trim(), "")
                        && Objects.nonNull(solarProperties.getAccessSecret())
                        && !Objects.equals(solarProperties.getAccessSecret().trim(), "")) {//在线激活
//                    this.authError("不支持在线激活，请联系系统管理员！");
                    authInfoService.saveAuthInfo(appInfo, AuthEnum.ON_LINE, null);
//                    log.error("不支持在线激活，请联系系统管理员！");
//                    licenseForDb(AuthEnum.OFF_LINE);
                } else if (Objects.nonNull(solarProperties.getLicense())
                        && !Objects.equals(solarProperties.getLicense().trim(), "")) {//离线激活
//                    this.authError("不支持离线激活，请联系系统管理员！");
//                    log.error("不支持离线激活，请联系系统管理员！");
//                    licenseForDb(AuthEnum.OFF_LINE);
                    authInfoService.saveAuthInfo(appInfo, AuthEnum.OFF_LINE, null);
                }
            } else {
                licenseForDb(AuthEnum.OFF_LINE);
            }
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        }
        return appInfo;
    }

    /**
     * 授权信息初始化
     *
     * @param appInfo
     * @param accessKey
     * @param accessSecret
     * @return
     * @throws Exception
     */
    @Override
    public AppInfo appInfoInit(AppInfo appInfo, String accessKey, String accessSecret) throws Exception {
        authInfoService.saveAuthInfo(appInfo, AuthEnum.ON_LINE, null);
        return appInfo;
    }

    /**
     * app是否过期
     *
     * @param code
     * @return
     */
    @Override
    public boolean appHasExpired(String code) {
        BasicSchool basicSchool = commonCacheService.schoolCache(code);
        if ((Objects.nonNull(basicSchool) && Objects.nonNull(basicSchool.getEnable()) && !basicSchool.getEnable())
                || (Objects.isNull(solarService.getAppInfo()) || solarService.getAppInfo().getControl().hasExpired())
                || (Objects.isNull(solarService.getAppControl()) || solarService.getAppControl().hasExpired())) {
//            throw JkServerException.AUTH_INFO_ERROR;
            return false;
        } else {
            return true;
        }
    }

    /**
     * 离线激活
     *
     * @param licenseData
     */
    @Override
    public void updateLicense(byte[] licenseData) throws Exception {
        AppInfo appInfo = solarService.update(licenseData);
        if (Objects.isNull(appInfo)) {
            throw new JkServerException("激活失败");
        } else {
            authInfoService.saveAuthInfo(appInfo, AuthEnum.OFF_LINE, licenseData);
        }
    }

    /**
     * 查询授权信息
     *
     * @return
     */
    @Override
    public AppInfoResult selectAuthInfo() throws Exception {
        Long expireTime = null;
        AppInfo appInfo = solarService.getAppInfo();
        AuthEnum authEnum = AuthEnum.NON_ACTIVATED;
        String accessKey = null, accessSecret = null;
        if (Objects.isNull(appInfo)) {//说明未激活，从数据库取最后激活记录
            TSAuth tsAuth = licenseForDb(AuthEnum.OFF_LINE);
            if (Objects.nonNull(tsAuth)) {
                return new AppInfoResult(tsAuth.getExpireTime(), tsAuth.getAccessKey(), tsAuth.getAccessSecret(), tsAuth.getType());
            } else {
                return new AppInfoResult(expireTime, accessKey, accessSecret, authEnum);
            }
        } else {
            accessKey = solarProperties.getAccessKey();
            accessSecret = solarProperties.getAccessSecret();
            if (Objects.nonNull(appInfo) && Objects.nonNull(appInfo.getControl())) {
                expireTime = Objects.nonNull(appInfo.getControl().getExpireTime()) ? appInfo.getControl().getExpireTime() : -1;
            }
            if (Objects.nonNull(accessKey) && Objects.nonNull(accessSecret)) {
                authEnum = AuthEnum.ON_LINE;
            } else if (Objects.nonNull(expireTime)) {
                authEnum = AuthEnum.OFF_LINE;
            }
            return new AppInfoResult(expireTime, accessKey, accessSecret, authEnum);
        }
    }

    /**
     * 保存鉴权信息
     *
     * @param appInfo
     * @param authEnum
     * @param file
     * @throws Exception
     */
    @Override
    @Transactional
    public void saveAuthInfo(AppInfo appInfo, AuthEnum authEnum, byte[] file) throws Exception {
        List<OrgInfo> orgInfoList = solarService.getOrgList();
        List<TSAuth> tsAuthList = null;
        Map<String, String> orgCodesMap = null;
        Set<BasicSchool> basicSchoolSet = null;
        if (!CollectionUtils.isEmpty(orgInfoList)) {
            tsAuthList = new ArrayList<>();
            basicSchoolSet = new HashSet<>();
            orgCodesMap = new HashMap<>();
        }

        Long expireTime = null;
        if (Objects.nonNull(appInfo) && Objects.nonNull(appInfo.getControl())) {
            expireTime = Objects.nonNull(appInfo.getControl().getExpireTime()) ? appInfo.getControl().getExpireTime() : -1;
        }
        for (OrgInfo o : orgInfoList) {
            orgCodesMap.put(o.getCode(), o.getCode());
            if (authEnum == AuthEnum.OFF_LINE) {
                if (Objects.isNull(file)) {
                    tsAuthList.add(new TSAuth(o.getId(), solarProperties.getLicense(), authEnum, expireTime));
                } else {
                    tsAuthList.add(new TSAuth(o.getId(), file, authEnum, expireTime));
                }
            } else {
                tsAuthList.add(new TSAuth(o.getId(), solarProperties.getAccessKey(), solarProperties.getAccessSecret(), authEnum, expireTime));
            }
            QueryWrapper<BasicSchool> basicSchoolQueryWrapper = new QueryWrapper<>();
            basicSchoolQueryWrapper.lambda().eq(BasicSchool::getCode, o.getCode());
            BasicSchool basicSchool = basicSchoolService.getOne(basicSchoolQueryWrapper);
            if (Objects.isNull(basicSchool)) {//不存在则创建学校
                basicSchool = new BasicSchool(o.getId(), o.getCode(), o.getName(), o.getAccessKey(), o.getAccessSecret());
                if (Objects.nonNull(o.getLogo()) && (!o.getLogo().startsWith("https:") || !o.getLogo().startsWith("http"))) {
                    basicSchool.setLogo("data:image/png;base64," + o.getLogo());
                } else {
                    basicSchool.setLogo(o.getLogoUrl());
                }
                basicSchoolSet.add(basicSchool);
            } else {
                basicSchool.setAccessKey(o.getAccessKey());
                basicSchool.setAccessSecret(o.getAccessSecret());
                basicSchool.setLogo(Objects.nonNull(o.getLogoUrl()) ? o.getLogoUrl() : basicSchool.getLogo());
                basicSchoolSet.add(basicSchool);
            }
        }

        if (!CollectionUtils.isEmpty(tsAuthList) && !CollectionUtils.isEmpty(orgCodesMap)) {
            tsAuthService.remove(new QueryWrapper<TSAuth>().lambda().ge(TSAuth::getId, 0L));
            tsAuthService.saveOrUpdateBatch(tsAuthList);

            if (!CollectionUtils.isEmpty(basicSchoolSet)) {
                basicSchoolService.saveOrUpdateBatch(basicSchoolSet);
                commonCacheService.removeSchoolIdCache();
                commonCacheService.removeSchoolCodeCache();
            }

            AuthInfoService authInfoService = SpringContextHolder.getBean(AuthInfoService.class);
            authInfoService.updateSchoolEnable(orgCodesMap);
        }
    }

//    /**
//     * 激活失败
//     *
//     * @param message
//     */
//    protected void authError(String message) {
//        List<OrgInfo> orgInfoList = solarService.getOrgList();
//        if (!CollectionUtils.isEmpty(orgInfoList)) {
//            Map<Long, Long> orgIdsMap = orgInfoList.stream().collect(Collectors.toMap(m -> m.getId(), m -> m.getId()));
//            this.updateSchoolEnable(orgIdsMap);
//        }
//        throw ExceptionResultEnum.AUTH_INFO_ERROR.exception(message);
//    }

    /**
     * 更新学校启用/禁用
     *
     * @param orgCodesMap
     */
    @Override
    public void updateSchoolEnable(Map<String, String> orgCodesMap) {
        List<BasicSchool> basicSchoolList = basicSchoolService.list();
        if (!CollectionUtils.isEmpty(basicSchoolList)) {
            for (BasicSchool b : basicSchoolList) {
                if (orgCodesMap.containsKey(b.getCode())) {
                    b.setEnable(true);
                } else {
                    b.setEnable(false);
                }
            }
            basicSchoolService.saveOrUpdateBatch(basicSchoolList);
            commonCacheService.removeSchoolIdCache();
            commonCacheService.removeSchoolCodeCache();
        }
    }

    /**
     * 强制从db更新激活信息
     *
     * @param authEnum
     * @throws Exception
     */
    public TSAuth licenseForDb(AuthEnum authEnum) throws Exception {
        TSAuth tsAuth = tsAuthService.lastAuthInfo(authEnum);
        if (Objects.nonNull(tsAuth)) {
            AppInfo appInfo = solarService.update(tsAuth.getFile());
            authInfoService.saveAuthInfo(appInfo, authEnum, tsAuth.getFile());
        } else {
            tsAuth = tsAuthService.lastAuthInfo(AuthEnum.ON_LINE);
            if (Objects.nonNull(tsAuth)) {
                AppInfo appInfo = solarService.update(tsAuth.getAccessKey(), tsAuth.getAccessSecret());
                authInfoService.saveAuthInfo(appInfo, AuthEnum.ON_LINE, null);
            }
        }
        return tsAuth;
    }
}
