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

import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.common.sequence.SequenceService;
import net.northking.cctp.testbug.db.entity.BugNo;
import net.northking.cctp.testbug.db.service.BugNoService;
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.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Service;

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

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

  @Autowired
  private BugNoService bugNoService;

  @Autowired
  private RedisLockRegistry redisLockRegistry;
  @Autowired
  private SequenceService sequenceService;

  @Override
  public Long genBugNo(BugNo target) {
    String suffix = "test-bug-no";
    String after  = "manualTest";
    String projectId = StringUtils.isEmpty(target.getProjectId())?"tenant":target.getProjectId();
    String key = after+":"+NKSecurityContext.getTenantId() + ":"+ suffix +":"+ projectId + ":" + target.getNoType();
    return sequenceService.nextValue(key);
  }

  @Override
  public Long genPubBugNo(BugNo target,String tenantId) {
    String suffix = "test-bug-no";
    String after  = "manualTest";
    String projectId = StringUtils.isEmpty(target.getProjectId())?"tenant":target.getProjectId();
    String key = after+":"+tenantId + ":"+ suffix +":"+ projectId + ":" + target.getNoType();
    return sequenceService.nextValue(key);
  }


}
