package cn.iocoder.yudao.module.system.dal.mysql.biz;


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.RecordsPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicalRecordsDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;


/**
 * 医疗记录主 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface MedicalRecordsMapper extends BaseMapperX<MedicalRecordsDO> {

    default PageResult<MedicalRecordsDO> selectPage(RecordsPageReqVO reqVO) {
        LambdaQueryWrapper<MedicalRecordsDO> medicalRecordsDOLambdaQueryWrapper = new LambdaQueryWrapperX<MedicalRecordsDO>()
                .eqIfPresent(MedicalRecordsDO::getElderId, reqVO.getElderId())
                .betweenIfPresent(MedicalRecordsDO::getDiscoveryTime, reqVO.getDiscoveryTime())
                .eqIfPresent(MedicalRecordsDO::getDiscoverer, reqVO.getDiscoverer())
                .eqIfPresent(MedicalRecordsDO::getConditionDesc, reqVO.getConditionDesc())
                .eqIfPresent(MedicalRecordsDO::getHandlingNote, reqVO.getHandlingNote())
                .eqIfPresent(MedicalRecordsDO::getTenantId, reqVO.getTenantId())
                .likeIfPresent(MedicalRecordsDO::getElderName, reqVO.getElderName());
        if(reqVO.getIsCommunication() == 1){
            medicalRecordsDOLambdaQueryWrapper = medicalRecordsDOLambdaQueryWrapper.isNotNull(MedicalRecordsDO::getCommunicateFamilyMember)
                    .isNotNull(MedicalRecordsDO::getCommunicateType)
                    .ne(MedicalRecordsDO::getCommunicateFamilyMember,"")
                    .ne(MedicalRecordsDO::getCommunicateType,"");
        }else if(reqVO.getIsCommunication() == 2){
            medicalRecordsDOLambdaQueryWrapper = medicalRecordsDOLambdaQueryWrapper.and(wrapper ->
                    wrapper.isNull(MedicalRecordsDO::getCommunicateFamilyMember)
                            .or()
                            .eq(MedicalRecordsDO::getCommunicateFamilyMember, "")
                            .or()
                            .isNull(MedicalRecordsDO::getCommunicateType)
                            .or()
                            .eq(MedicalRecordsDO::getCommunicateType, "")
            );
        }
        return selectPage(reqVO, medicalRecordsDOLambdaQueryWrapper
                .orderByDesc(MedicalRecordsDO::getDiscoveryTime, MedicalRecordsDO::getUpdateTime, MedicalRecordsDO::getCreateTime));
    }

}