package thyyxxk.webserver.service.zygl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.dao.his.zygl.ZyConfigMapper;
import thyyxxk.webserver.entity.zygl.ZyConfig;
import thyyxxk.webserver.utils.AssertUtil;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2025/5/8
 */
@Service
@Slf4j
public class ZyConfigService {

    @Resource
    private ZyConfigMapper zyConfigMapper;

    public int getInpatientNo()  {
        ZyConfig zyConfig = zyConfigMapper.selectZyConfig();
        if (zyConfig == null) {
            throw new BizException("住院序列号生成器不存在，请先设置！");
        }
        Integer old=zyConfig.getInpatientNo();
        zyConfig.setInpatientNo(zyConfig.getInpatientNo() + 1);
        int num = zyConfigMapper.updateInpatientNo(old,zyConfig.getInpatientNo());
        if (num == 0) {
            throw new BizException("获取住院号失败，请重试!");
        }
        return zyConfig.getInpatientNo();
    }

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

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

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

    /**
     * @description: 获取住院删除号
     * @author: lihong
     * @date: 2023/11/16 10:14
     * @param: classCode
     * @return: java.lang.String
     */
    public String getDelNo() {
        ZyConfig zyConfig = zyConfigMapper.selectZyConfig();
        AssertUtil.isnotBlank(zyConfig,"删除号编码生成器不存在，请先设置！");
        AssertUtil.isnotBlank(zyConfig.getDelNo(),"删除号编码生成器不存在，请先设置！");
        Integer newDelNo = zyConfig.getDelNo() + 1;
        int num = zyConfigMapper.updateDelNo(zyConfig.getDelNo(), newDelNo);
        if(num <= 0 ){
            throw new BizException("获取删除号编码生成器失败,请重试");
        }
        return newDelNo.toString();
    }
}
