package net.northking.cctp.dataPool.api.dataSets.service;

import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.common.sequence.SequenceService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@ConditionalOnMissingBean(name = "TreeIndexNoApiServiceImpl")
public class TreeIndexNoApiServiceImpl implements TreeIndexNoApiService{
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(TreeIndexNoApiServiceImpl.class);

    @Autowired
    private SequenceService sequenceService;

    @Override
    public List<Integer> genIndexNo(String project, String noType, long count) {
        List<Integer> result = new ArrayList<>();
        String suffix = "data-pool-no";
        String after = "dataPool";
        String projectId = StringUtils.isEmpty(project) ? "tenant" : project;
        String key = after + ":" + NKSecurityContext.getTenantId() + ":" + suffix + ":" + projectId + ":" + noType;
        long value = sequenceService.skipValue(key, (int) count);
        for (long i = value; i > value - count; i--) {
            result.add(0, Integer.valueOf(i+""));
        }
        return result;
    }
}
