package com.zt.plat.module.qms.business.bus.dal.mapper;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessBaseSampleDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.BaseSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigBaseSampleDO;
import com.zt.plat.module.qms.common.dic.dal.dataobject.DictionaryBusinessDO;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 委检登记样品明细 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BusinessSampleEntrustDetailMapper extends BaseMapperX<BusinessSampleEntrustDetailDO> {

	default List<BusinessSampleEntrustDetailExtendRespVO> selectList(BusinessSampleEntrustDetailReqVO reqVO) {
		return selectJoinList(BusinessSampleEntrustDetailExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustDetailDO>()
				.leftJoin(BusinessSampleEntrustRegistrationDO.class, BusinessSampleEntrustRegistrationDO::getId, BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId)
				.leftJoin(BaseSampleDO.class, BaseSampleDO::getId, BusinessSampleEntrustDetailDO::getBaseSampleId)
				.leftJoin(BusinessBaseSampleDO.class, BusinessBaseSampleDO::getId, BusinessSampleEntrustDetailDO::getBusinessBaseSampleId)
				.leftJoin(ConfigBaseSampleDO.class, ConfigBaseSampleDO::getId, BusinessBaseSampleDO::getConfigBaseSampleId)
				.selectAll(BusinessSampleEntrustDetailDO.class)
				.selectAs(BaseSampleDO::getName, BusinessSampleEntrustDetailExtendRespVO::getBaseSampleName)
				.selectAs(BusinessSampleEntrustRegistrationDO::getEntrustNumber, BusinessSampleEntrustDetailExtendRespVO::getEntrustNumber)
				.selectAs(BusinessSampleEntrustRegistrationDO::getEntrustUnit, BusinessSampleEntrustDetailExtendRespVO::getEntrustUnit)
				.selectAs(BusinessSampleEntrustRegistrationDO::getExternalInfomation, BusinessSampleEntrustDetailExtendRespVO::getExternalInfomation)
				.selectAs(BusinessBaseSampleDO::getConfigBaseSampleId, BusinessSampleEntrustDetailExtendRespVO::getConfigBaseSampleId)
				.selectAs(ConfigBaseSampleDO::getPrintTemplate, BusinessSampleEntrustDetailExtendRespVO::getPrintTemplate)
                .eqIfPresent(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, reqVO.getBusinessSampleEntrustRegistrationId())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, reqVO.getBusinessBaseSampleId())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getBaseSampleId, reqVO.getBaseSampleId())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSampleTypeDictionaryBusinessId, reqVO.getSampleTypeDictionaryBusinessId())
                .likeIfPresent(BusinessSampleEntrustDetailDO::getSampleName, reqVO.getSampleName())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSampleCode, reqVO.getSampleCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustBatchCode, reqVO.getEntrustBatchCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustGroupCode, reqVO.getEntrustGroupCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustDualFlag, reqVO.getEntrustDualFlag())
                .likeIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleName, reqVO.getEntrustSampleName())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleCode, reqVO.getEntrustSampleCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleQuantity, reqVO.getEntrustSampleQuantity())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleQuantityUnit, reqVO.getEntrustSampleQuantityUnit())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getAssayLevel, reqVO.getAssayLevel())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSort, reqVO.getSort())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getAssayProject, reqVO.getAssayProject())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getForecastResult, reqVO.getForecastResult())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getIsWeighing, reqVO.getIsWeighing())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSampleFlowInfomation, reqVO.getSampleFlowInfomation())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessSampleEntrustDetailDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessSampleEntrustDetailDO::getId));
	}

    default PageResult<BusinessSampleEntrustDetailExtendRespVO> selectPage(BusinessSampleEntrustDetailPageReqVO reqVO) {
        return selectJoinPage(reqVO, BusinessSampleEntrustDetailExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustDetailDO>()
				.leftJoin(BaseSampleDO.class, BaseSampleDO::getId, BusinessSampleEntrustDetailDO::getBaseSampleId)
				.leftJoin(BusinessBaseSampleDO.class, BusinessBaseSampleDO::getId, BusinessSampleEntrustDetailDO::getBusinessBaseSampleId)
				.leftJoin(ConfigBaseSampleDO.class, ConfigBaseSampleDO::getId, BusinessBaseSampleDO::getConfigBaseSampleId)
				.selectAll(BusinessSampleEntrustDetailDO.class)
				.selectAs(BaseSampleDO::getName, BusinessSampleEntrustDetailExtendRespVO::getBaseSampleName)
				.selectAs(BusinessBaseSampleDO::getConfigBaseSampleId, BusinessSampleEntrustDetailExtendRespVO::getConfigBaseSampleId)
				.selectAs(ConfigBaseSampleDO::getPrintTemplate, BusinessSampleEntrustDetailExtendRespVO::getPrintTemplate)
                .eqIfPresent(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, reqVO.getBusinessSampleEntrustRegistrationId())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, reqVO.getBusinessBaseSampleId())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getBaseSampleId, reqVO.getBaseSampleId())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSampleTypeDictionaryBusinessId, reqVO.getSampleTypeDictionaryBusinessId())
                .likeIfPresent(BusinessSampleEntrustDetailDO::getSampleName, reqVO.getSampleName())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSampleCode, reqVO.getSampleCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustBatchCode, reqVO.getEntrustBatchCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustGroupCode, reqVO.getEntrustGroupCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustDualFlag, reqVO.getEntrustDualFlag())
                .likeIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleName, reqVO.getEntrustSampleName())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleCode, reqVO.getEntrustSampleCode())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleQuantity, reqVO.getEntrustSampleQuantity())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getEntrustSampleQuantityUnit, reqVO.getEntrustSampleQuantityUnit())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getAssayLevel, reqVO.getAssayLevel())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSort, reqVO.getSort())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getAssayProject, reqVO.getAssayProject())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getForecastResult, reqVO.getForecastResult())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getIsWeighing, reqVO.getIsWeighing())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSampleFlowInfomation, reqVO.getSampleFlowInfomation())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessSampleEntrustDetailDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessSampleEntrustDetailDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessSampleEntrustDetailDO::getId));
    }
    
    default List<BusinessSampleEntrustDetailDO> selectByBusinessSampleEntrustRegistrationId(Long businessSampleEntrustRegistrationId) {
    	return selectList(new LambdaQueryWrapperX<BusinessSampleEntrustDetailDO>().eq(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, businessSampleEntrustRegistrationId));
    }
    
    default List<BusinessSampleEntrustDetailExtendRespVO> selectExtendByBusinessSampleEntrustRegistrationId(Long businessSampleEntrustRegistrationId) {
    	return selectJoinList(BusinessSampleEntrustDetailExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustDetailDO>()
//				.leftJoin(BaseSampleDO.class, BaseSampleDO::getId, BusinessSampleEntrustDetailDO::getBaseSampleId)
				.leftJoin(DictionaryBusinessDO.class, DictionaryBusinessDO::getId, BusinessSampleEntrustDetailDO::getSampleTypeDictionaryBusinessId)
				.selectAll(BusinessSampleEntrustDetailDO.class)
				.selectAs(DictionaryBusinessDO::getKey, BusinessSampleEntrustDetailExtendRespVO::getSampleTypeDictionaryBusinessKey)
    			.eq(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, businessSampleEntrustRegistrationId));
    }
    
    default List<BusinessSampleEntrustDetailDO> selectByBusinessSampleEntrustRegistrationIds(List<Long> businessSampleEntrustRegistrationIds) {
    	return selectList(new LambdaQueryWrapperX<BusinessSampleEntrustDetailDO>().in(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, businessSampleEntrustRegistrationIds));
    }
    
    default BusinessSampleEntrustDetailDO selectByBusinessBaseSampleId(Long businessBaseSampleId) {
    	return selectOne(new LambdaQueryWrapperX<BusinessSampleEntrustDetailDO>().eq(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, businessBaseSampleId));
    }
    
    default List<BusinessSampleEntrustDetailDO> selectByBusinessBaseSampleIds(List<Long> businessBaseSampleIds) {
    	return selectList(new LambdaQueryWrapperX<BusinessSampleEntrustDetailDO>().in(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, businessBaseSampleIds));
    }
    
    /**
     * 查询当前批次的第一条数据
     * @param businessBaseSampleId 任意一条的主样id
     * @param baseSampleDictionaryBusinessKey 样品大类类型key
     * @return
     */
    default BusinessSampleEntrustDetailDO selectCurrBatchFirstByBusinessBaseSampleIdAndBaseSampleDictionaryBusinessKey(Long businessBaseSampleId, String baseSampleDictionaryBusinessKey) {
    	BusinessSampleEntrustDetailDO businessSampleEntrustDetailDO = selectByBusinessBaseSampleId(businessBaseSampleId);
    	//查询当前委托的第一条
    	return selectJoinOne(BusinessSampleEntrustDetailDO.class, new MPJLambdaWrapperX<BusinessSampleEntrustDetailDO>()
    			.leftJoin(BaseSampleDO.class, BaseSampleDO::getId, BusinessSampleEntrustDetailDO::getBaseSampleId)
    			.eq(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, businessSampleEntrustDetailDO.getBusinessSampleEntrustRegistrationId())
    			.eq(BaseSampleDO::getDictionaryBusinessKey, baseSampleDictionaryBusinessKey)
    			.orderByAsc(BusinessSampleEntrustDetailDO::getSort)
    			.last(" limit 1 "));
    	
    }

    /**
     * 物理批量删除删除
     * @param idList
     * @return
     */
    int physicalDeleteByIds(@Param("idList") List<Long> idList);

}