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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.mz.MzVisitTable;
import cn.hnthyy.thmz.entity.his.mz.MzfzPatientOrder;
import cn.hnthyy.thmz.entity.his.mz.MzyReqrec;
import cn.hnthyy.thmz.entity.thmz.Clinic;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.ClinicStatusEnum;
import cn.hnthyy.thmz.enums.MzfzStatusEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.mapper.his.mz.MzPatientMiMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzfzPatientOrderMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzyReqrecMapper;
import cn.hnthyy.thmz.mapper.thmz.ClinicMapper;
import cn.hnthyy.thmz.service.his.mz.MzVisitTableService;
import cn.hnthyy.thmz.service.thmz.ClinicService;
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;

import java.util.Date;
import java.util.List;

@Service
public class ClinicServiceImpl implements ClinicService {
    @SuppressWarnings("all")
    @Autowired
    private ClinicMapper clinicMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzfzPatientOrderMapper mzfzPatientOrderMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzyReqrecMapper mzyReqrecMapper;
    @Autowired
    private MzVisitTableService mzVisitTableService;

    @Override
    public int saveClinic(Clinic clinic) {
        return clinicMapper.insertClinic(clinic);
    }

    @Override
    public int batchSaveClinic(List<Clinic> clinicList) {
        return clinicMapper.batchInsertClinic(clinicList);
    }

    @Override
    public Clinic queryById(Long id) {
        return clinicMapper.selectById(id);
    }

    @Override
    public List<Clinic> queryClinicWithPage(Clinic clinic) {
        return clinicMapper.selectClinicWithPage(clinic);
    }

    @Override
    public Integer queryCountClinic(Clinic clinic) {
        return clinicMapper.selectCountClinic(clinic);
    }

    @Override
    public int modifyClinic(Clinic clinic) {
        return clinicMapper.updateClinic(clinic);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int inClinic(String patientId, String serialNo, User tokenUser) throws MzException {
        MzfzPatientOrder mzfzPatientOrder = mzfzPatientOrderMapper.selectBySerialNo(serialNo);
        if (mzfzPatientOrder == null) {
            throw new MzException("当前接诊患者的分诊记录不存在");
        }
        //此时已经时接诊中，无需改变状态
        if(ClinicStatusEnum.IN_CLINIC.code.equals(mzfzPatientOrder.getSlowFlag())){
            return 0;
        }
        MzVisitTable mzVisitTable = mzVisitTableService.queryByPatientIdAndTimes(mzfzPatientOrder.getPatientId(), mzfzPatientOrder.getTimes());
        if (mzVisitTable == null) {
            throw new MzException("当前接诊患者的就诊记录不存在");
        }
        MzyReqrec updateMzyReqrec = new MzyReqrec();
        updateMzyReqrec.setPatientId(patientId);
        updateMzyReqrec.setTimes(mzfzPatientOrder.getTimes());
        updateMzyReqrec.setVisitedMark(YesNoEnum.YES.code);
        updateMzyReqrec.setVisitDate(new Date());
        updateMzyReqrec.setVisitDept(mzfzPatientOrder.getDeptCode());
        updateMzyReqrec.setVisitDoctor(tokenUser.getUserIdCode());
        mzyReqrecMapper.updateMzyReqrec(updateMzyReqrec);
        Clinic clinic = new Clinic();
        clinic.setPatientId(patientId);
        clinic.setTimes(mzfzPatientOrder.getTimes());
        clinic.setClinicStatus(ClinicStatusEnum.REGISTRATION_TO_CLINIC.code);
        clinicMapper.updateClinicByPatientIdAndTimes(clinic);
        MzfzPatientOrder updateMzfzPatientOrder = new MzfzPatientOrder();
        updateMzfzPatientOrder.setSerialNo(Integer.valueOf(serialNo));
        updateMzfzPatientOrder.setStatusFlag(MzfzStatusEnum.IN_CLINIC.code);
        return mzfzPatientOrderMapper.updateMzfzPatientOrder(updateMzfzPatientOrder);
    }
}
