package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.entity.BehospitalInfo;
import com.diagbot.entity.MedNurse;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.mapper.MedNurseMapper;
import com.diagbot.service.impl.MedNurseServiceImpl;
import com.diagbot.vo.MedNurseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: cy
 * @time: 2024/9/23 11:39
 */
@Component
public class MedNurseFacade extends MedNurseServiceImpl {
    @Autowired
    BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    MedNurseMapper medNurseMapper;

    public  IPage<MedNurse> getMedNursePage(MedNurseInfoVO medNurseInfoVO){
        // 添加住院病历信息
       BehospitalInfo behospitalInfo = behospitalInfoFacade.getOne(new QueryWrapper<BehospitalInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("behospital_code", medNurseInfoVO.getBehospitalCode())
               .select("behospital_code","hospital_id","behospital_date","leave_hospital_date")
        );
        QueryWrapper<MedNurse> medNurseQueryWrapper = new QueryWrapper<>();
        medNurseQueryWrapper.eq("behospital_code",medNurseInfoVO.getBehospitalCode());
        medNurseQueryWrapper .eq("hospital_id",behospitalInfo.getHospitalId());
        medNurseQueryWrapper .eq("is_deleted", IsDeleteEnum.N.getKey());
        medNurseQueryWrapper .like("item_name", medNurseInfoVO.getItemName());
        medNurseQueryWrapper.orderByDesc( "measure_time");
        //测量时间
        medNurseQueryWrapper.ge( "measure_time", medNurseInfoVO.getDateStart());
        medNurseQueryWrapper.le( "measure_time", medNurseInfoVO.getDateEnd());
        //出入院时间
        medNurseQueryWrapper.ge( "measure_time", behospitalInfo.getBehospitalDate());
        if(behospitalInfo.getLeaveHospitalDate() != null){
            medNurseQueryWrapper.le( "measure_time",behospitalInfo.getLeaveHospitalDate());
        }
        Page<MedNurse> pageCase = new Page<MedNurse>(medNurseInfoVO.getCurrent(),medNurseInfoVO.getSize());
        IPage<MedNurse> medNurseIPage = medNurseMapper.selectPage(pageCase, medNurseQueryWrapper);
        medNurseIPage.getRecords().forEach(medNurse -> {
            medNurse.setItemValue(medNurse.getItemValue()+medNurse.getItemUnit());
        });
        return medNurseIPage;

    }
}
