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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.mz.MzYshConfig;
import cn.hnthyy.thmz.mapper.his.mz.MzYshConfigMapper;
import cn.hnthyy.thmz.service.his.mz.MzYshConfigService;
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 MzYshConfigServiceImpl implements MzYshConfigService {
    @SuppressWarnings("all")
    @Autowired
    private MzYshConfigMapper mzYshConfigMapper;

    /**
     * 医技申请单号
     * @return
     * @throws MzException
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int getReqNo() throws MzException {
        mzYshConfigMapper.updateForBlock();
        MzYshConfig mzYshConfig = mzYshConfigMapper.selectMzYshConfig();
        if (mzYshConfig == null) {
            throw new MzException("序列号生成器不存在，请先设置！");
        }
        mzYshConfig.setReqNo(mzYshConfig.getReqNo() + 1);
        MzYshConfig updateMzYshConfig = new MzYshConfig();
        updateMzYshConfig.setReqNo(mzYshConfig.getReqNo());
        int num = mzYshConfigMapper.updateMzYshConfig(updateMzYshConfig);
        if (num == 0) {
            throw new MzException("获取医技申请单号失败，请重试!");
        }
        return mzYshConfig.getReqNo();
    }

    /**
     * 医技号
     * @return
     * @throws MzException
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int getReqPageNo() throws MzException {
        mzYshConfigMapper.updateForBlock();
        MzYshConfig mzYshConfig = mzYshConfigMapper.selectMzYshConfig();
        if (mzYshConfig == null) {
            throw new MzException("申请单号生成器不存在，请先设置！");
        }
        mzYshConfig.setReqPageNo(mzYshConfig.getReqPageNo() + 1);
        MzYshConfig updateMzYshConfig = new MzYshConfig();
        updateMzYshConfig.setReqPageNo(mzYshConfig.getReqPageNo());
        int num = mzYshConfigMapper.updateMzYshConfig(updateMzYshConfig);
        if (num == 0) {
            throw new MzException("获取医技申请单号失败，请重试!");
        }
        return mzYshConfig.getReqPageNo();
    }

    /**
     * 获取病历号
     * @return
     * @throws MzException
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int getBlNo() throws MzException {
        mzYshConfigMapper.updateForBlock();
        MzYshConfig mzYshConfig = mzYshConfigMapper.selectMzYshConfig();
        if (mzYshConfig == null) {
            throw new MzException("病历单号生成器不存在，请先设置！");
        }
        mzYshConfig.setBlNo(mzYshConfig.getBlNo() + 1);
        MzYshConfig updateMzYshConfig = new MzYshConfig();
        updateMzYshConfig.setBlNo(mzYshConfig.getBlNo());
        int num = mzYshConfigMapper.updateMzYshConfig(updateMzYshConfig);
        if (num == 0) {
            throw new MzException("获取病历单号失败，请重试!");
        }
        return mzYshConfig.getBlNo();
    }
}
