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

import cn.hnthyy.thmz.entity.his.mz.MzBlRecord;
import cn.hnthyy.thmz.entity.his.mz.MzVisitTable;
import cn.hnthyy.thmz.mapper.his.mz.MzBlRecordMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzVisitTableMapper;
import cn.hnthyy.thmz.service.his.mz.MzVisitTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MzVisitTableServiceImpl implements MzVisitTableService {
    @SuppressWarnings("all")
    @Autowired
    private MzVisitTableMapper mzVisitTableMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzBlRecordMapper mzBlRecordMapper;
    @Override
    public MzVisitTable queryByPatientIdAndTimes(String patientId, Integer times) {
        MzVisitTable mzVisitTable= mzVisitTableMapper.selectByPatientIdAndTimes(patientId,times,"mz_visit_table");
        if(mzVisitTable!=null){
            return mzVisitTable;
        }
        return mzVisitTableMapper.selectByPatientIdAndTimes(patientId,times,"mz_visit_table_b");
    }

    @Override
    public MzVisitTable queryLastMzVisitTableByPatientId(String patientId) {
        MzVisitTable mzVisitTable = mzVisitTableMapper.selectLastMzVisitTableByPatientId(patientId,"mz_visit_table");
        if(mzVisitTable==null){
            mzVisitTable=mzVisitTableMapper.selectLastMzVisitTableByPatientId(patientId,"mz_visit_table_b");
        }
        MzBlRecord mzBlRecord= mzBlRecordMapper.selectLastMzVisitTableByPatientId(patientId);
        Integer mzBlRecordTimes = 0;
        if(mzBlRecord!=null){
            mzBlRecordTimes=mzBlRecord.getTimes();
        }
        if(mzVisitTable!=null){
            if(mzBlRecordTimes>mzVisitTable.getTimes()){
                mzVisitTable.setTimes(mzBlRecordTimes);
            }
            return mzVisitTable;
        }
        mzVisitTable=new MzVisitTable();
        mzVisitTable.setPatientId(patientId);
        mzVisitTable.setTimes(mzBlRecordTimes);
        return mzVisitTable;

    }
}
