package cn.palmte.work.service;

import cn.palmte.work.model.NumSeq;
import cn.palmte.work.model.NumSeqRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

/**
 * @author Yuanping Zhang
 * @date 2022/08/08
 */
@Service
public class NumSeqService {
    private static final Logger logger = LoggerFactory.getLogger(NumSeqService.class);

    @Autowired
    private NumSeqRepository numSeqRepository;

    @Transactional(rollbackOn = Exception.class)
    public String generate() {
        String prefix = "P";
        NumSeq one = numSeqRepository.findNum();
        int number = one.getNumber();
        String counterString = String.format("%05d", number++);
        numSeqRepository.updateNum(number);
        return prefix + counterString;
    }
}
