package cn.hnthyy.thmz.service.impl.his.mz;

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.mz.MzfzConfig;
import cn.hnthyy.thmz.mapper.his.mz.MzfzConfigMappper;
import cn.hnthyy.thmz.service.his.mz.MzfzConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MzfzConfigServiceImpl implements MzfzConfigService {
    @SuppressWarnings("all")
    @Autowired
    private MzfzConfigMappper mzfzConfigMappper;

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int getGhNo() throws MzException {
        mzfzConfigMappper.updateForBlock();
        MzfzConfig mzfzConfig = mzfzConfigMappper.selectMzfzConfig();
        if (mzfzConfig == null) {
            throw new MzException("序列号生成器不存在，请先设置！");
        }
        mzfzConfig.setGhNo(mzfzConfig.getGhNo() + 1);
        int num = mzfzConfigMappper.updateMzfzConfig(mzfzConfig);
        if (num == 0) {
            throw new MzException("获取序号失败，请重试!");
        }
        return mzfzConfig.getGhNo();
    }
}
