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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.zy.ZyConfig;
import cn.hnthyy.thmz.mapper.his.zy.ZyConfigMapper;
import cn.hnthyy.thmz.service.his.zy.ZyConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ZyConfigServiceImpl implements ZyConfigService {
    @SuppressWarnings("all")
    @Autowired
    private ZyConfigMapper zyConfigMapper;
    @Override
    public int getInpatientNo() throws MzException {
        zyConfigMapper.updateForBlock(new Date());
        ZyConfig zyConfig = zyConfigMapper.selectZyConfig();
        if (zyConfig == null) {
            throw new MzException("住院序列号生成器不存在，请先设置！");
        }
        zyConfig.setInpatientNo(zyConfig.getInpatientNo() + 1);
        int num = zyConfigMapper.updateZyConfig(zyConfig);
        if (num == 0) {
            throw new MzException("获取住院号失败，请重试!");
        }
        return zyConfig.getInpatientNo();
    }

    @Override
    public String getJcNo() throws MzException {
        zyConfigMapper.updateForBlock(new Date());
        ZyConfig zyConfig = zyConfigMapper.selectZyConfig();
        if (zyConfig == null) {
            throw new MzException("家床号生成器不存在，请先设置！");
        }
        zyConfig.setJcNo(zyConfig.getJcNo() + 1);
        int num = zyConfigMapper.updateZyConfig(zyConfig);
        if (num == 0) {
            throw new MzException("获取家床号失败，请重试!");
        }
        SimpleDateFormat smf = new SimpleDateFormat("yy");
        return smf.format(new Date())+zyConfig.getJcNo();
    }

    @Override
    public String getZySerialNo() throws MzException {
        zyConfigMapper.updateForBlock(new Date());
        ZyConfig zyConfig = zyConfigMapper.selectZyConfig();
        if (zyConfig == null) {
            throw new MzException("住院流水号生成器不存在，请先设置！");
        }
        zyConfig.setZySerialNo(zyConfig.getZySerialNo() + 1);
        int num = zyConfigMapper.updateZyConfig(zyConfig);
        if (num == 0) {
            throw new MzException("获取流水号失败，请重试!");
        }
        SimpleDateFormat smf = new SimpleDateFormat("yyMMdd");
        return smf.format(new Date())+zyConfig.getZySerialNo();
    }

    @Override
    public String getChargeCode(String classCode) throws MzException {
        zyConfigMapper.updateForBlock(new Date());
        ZyConfig zyConfig = zyConfigMapper.selectZyConfig();
        if (zyConfig == null) {
            throw new MzException("收费项目编码生成器不存在，请先设置！");
        }
        Integer chargeCode =zyConfig.getChargeCode();
        zyConfig.setChargeCode(zyConfig.getChargeCode() + 1);
        int num = zyConfigMapper.updateZyConfig(zyConfig);
        if (num == 0) {
            throw new MzException("获取收费项目编码失败，请重试!");
        }
        if(StringUtils.isBlank(classCode)){
            classCode="0";
        }
        return classCode+chargeCode.toString();
    }
}
