package com.brilliance.isc.common.counter.service.impl;


import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.counter.service.impl.SegmentCounter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component("springTransaction")
public class CounterImplWithSpringTransaction implements CounterService {

    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Resource
    private SegmentCounter segmentCounter;


    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public long dbCounter(String nam)
    {
        return segmentCounter.idGen(nam);
    }

    @Override
    public String dbCounterFormat(String seqname) {
        long counter = dbCounter(seqname);
        return String.format("%016d", counter);
    }

    @Override
    public String dbCounterFormat(String seqname, int length) {
        long counter = dbCounter(seqname);
        String format = "%0" + length + "d";
        return String.format(format, counter);
    }
}
