/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
 *
 */
package net.northking.cctp.scriptcase.api.component.service;


import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.common.sequence.SequenceService;
import net.northking.cctp.scriptcase.constants.ScriptConstant;
import net.northking.cctp.scriptcase.db.service.AtuBusinessNoService;

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;


/**
 * 业务编号生成表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

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

    @Autowired
    private AtuBusinessNoService atuBusinessNoService;
    @Autowired
    private SequenceService sequenceService;

    @Override
    public List<Integer> genBusinessNo(String project, String noType, long count) {
        List<Integer> result = new ArrayList<>();
        String suffix = ScriptConstant.SCRIPT_CASE_NO;
        String after = ScriptConstant.SCRIPT_CASE;
        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;
    }
}
