package com.gic.haoban.manage.service.service.content.adaptor;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.enterprise.api.constant.EnterpriseServiceEnum;
import com.gic.enterprise.api.dto.enterprise.EnterpriseUsingStatusDTO;
import com.gic.enterprise.api.service.EnterpriseUseForbidService;
import com.gic.redis.data.util.RedisUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/4/2 10:05 PM
 * @Description
 * @Version
 **/
@Slf4j
@Component
public class MaterialEnterpriseAdaptor {

    @Getter
    @AllArgsConstructor
    public enum MaterialLevel {
        LOW(1, "低版本"),
        HIGH(2, "高版本");
        private Integer code;
        private String desc;
    }

    private static final Long cache_hour_sec = 5 * 60L;

    @Autowired
    EnterpriseUseForbidService enterpriseUseForbidService;

    /**
     * @see MaterialLevel
     * @param highLevel 1 低版本; 2高版本
     * @return
     */
    public List<String> queryHasRightEnterpriseIds(Integer highLevel) {

        ServiceResponse<List<EnterpriseUsingStatusDTO>> serviceResponse = enterpriseUseForbidService.getUseEnterpriseByCode(EnterpriseServiceEnum.CONTENT.getRightMenuCode(), null);
        if (!serviceResponse.isSuccess()) {
            log.info("查询企业信息异常 {}", JSON.toJSONString(serviceResponse));
            return Collections.emptyList();
        }
        log.info("有权限的企业 {}", JSON.toJSONString(serviceResponse));
        return serviceResponse.getResult()
                .stream()
                .filter(item -> item.getVersion() != null && item.getVersion() >= highLevel)
                .filter(item -> Boolean.TRUE.equals(item.getVaild()))
                .map(EnterpriseUsingStatusDTO::getEnterpriseId)
                .collect(Collectors.toList());
    }

    /**
     * 校验企业是否有内容权限
     * @param enterpriseId
     * @return
     */
    public boolean checkEnterpriseHasContentRight(String enterpriseId) {
        String key = "haoban_enterprise_content:" + enterpriseId;
        String cache = RedisUtil.getCache(key, String.class);
        if (StringUtils.isBlank(cache)) {
            cache = "0";
            ServiceResponse<List<EnterpriseUsingStatusDTO>> tempServiceResponse = enterpriseUseForbidService.getUseEnterpriseByCode(EnterpriseServiceEnum.CONTENT.getRightMenuCode(), enterpriseId);
            if (!tempServiceResponse.isSuccess() || CollectionUtils.isEmpty(tempServiceResponse.getResult())) {
                log.info("企业 {} 没有内容权限 {}", enterpriseId, JSON.toJSONString(tempServiceResponse));
                RedisUtil.setCache(key, cache, cache_hour_sec, TimeUnit.SECONDS);
                return false;
            }
            EnterpriseUsingStatusDTO enterpriseUsingStatusDTO = tempServiceResponse.getResult().get(0);
            if (!Boolean.TRUE.equals(enterpriseUsingStatusDTO.getVaild())) {
                log.info("企业 {} 内容权限已失效 {}", enterpriseId, JSON.toJSONString(tempServiceResponse));
                RedisUtil.setCache(key, cache, cache_hour_sec, TimeUnit.SECONDS);
                return false;
            }
            cache = "1";
            RedisUtil.setCache(key, cache, cache_hour_sec, TimeUnit.SECONDS);
        }
        return StringUtils.equals(cache, "1");
    }
}
