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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.zy.APatientMi;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.mapper.his.zy.APatientMiMapper;
import cn.hnthyy.thmz.service.his.zy.APatientMiService;
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.util.List;
@Service
public class APatientMiServiceImpl implements APatientMiService {
    @SuppressWarnings("all")
    @Autowired
    private APatientMiMapper aPatientMiMapper;
    @Autowired
    private ZyConfigService zyConfigService;
    @Override
    public List<String> queryInPatientNoByMzNo(String mzNo) {
        if(StringUtils.isBlank(mzNo)){
            return null;
        }
        return aPatientMiMapper.selectInPatientNoByMzNo(mzNo);
    }

    @Override
    public APatientMi queryPatientMiByInPatientNo(String inpatientNo) {
        return aPatientMiMapper.selectPatientMiByInPatientNo(inpatientNo);
    }

    @Override
    public APatientMi queryAPatientByMzNo(String mzNo) {
        return aPatientMiMapper.selectAPatientByMzNo(mzNo);
    }

    @Override
    public int saveMzPatientMi(APatientMi aPatientMi) throws MzException {
        if(StringUtils.isBlank(aPatientMi.getInpatientNo())){
            //住院号为空，需要自动生成住院号
            if(YesNoEnum.NO.code.equals(aPatientMi.getInpatientNoType())){
                //自动的住院号
                int inpatientNo=  zyConfigService.getInpatientNo();
                aPatientMi.setInpatientNo(String.valueOf(inpatientNo));
            }else  if(YesNoEnum.NO.code.equals(aPatientMi.getInpatientNoType())){
                //家床住院号
                String jcNo=  zyConfigService.getJcNo();
                aPatientMi.setInpatientNo(jcNo);
            }
        }
        aPatientMi.setOutpatientNo(aPatientMi.getInpatientNo());
        return aPatientMiMapper.insertMzPatientMi(aPatientMi);
    }

    @Override
    public int modifyMzPatientMi(APatientMi aPatientMi) {
        return aPatientMiMapper.updateMzPatientMiByMzno(aPatientMi);
    }
}
