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

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.controller.vo.BusinessSampleHandoverDetailPageReqVO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleHandoverDetailDO;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;

import java.util.List;

import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 样品交接明细 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BusinessSampleHandoverDetailMapper extends BaseMapperX<BusinessSampleHandoverDetailDO> {

    default PageResult<BusinessSampleHandoverDetailDO> selectPage(BusinessSampleHandoverDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BusinessSampleHandoverDetailDO>()
                .eqIfPresent(BusinessSampleHandoverDetailDO::getBusinessSampleHandoverId, reqVO.getBusinessSampleHandoverId())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .likeIfPresent(BusinessSampleHandoverDetailDO::getSampleName, reqVO.getSampleName())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getSampleCode, reqVO.getSampleCode())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getSampleWeight, reqVO.getSampleWeight())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getBalanceCode, reqVO.getBalanceCode())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getDictionaryBusinessId, reqVO.getDictionaryBusinessId())
                .likeIfPresent(BusinessSampleHandoverDetailDO::getDictionaryBusinessName, reqVO.getDictionaryBusinessName())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessSampleHandoverDetailDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessSampleHandoverDetailDO::getId));
    }
    
    default List<BusinessSampleHandoverDetailDO> selectByBusinessSampleHandoverId(Long businessSampleHandoverId) {
    	return selectList(new MPJLambdaWrapperX<BusinessSampleHandoverDetailDO>()
    			.eq(BusinessSampleHandoverDetailDO::getBusinessSampleHandoverId, businessSampleHandoverId)
    			.orderByAsc(BusinessSampleHandoverDetailDO::getSampleName)
                .orderByAsc("NVL(REGEXP_SUBSTR(SMP_CD, '^(.*-)'), SMP_CD)")
                .orderByAsc("CASE WHEN INSTR(SMP_CD, '-') = 0 THEN 0 WHEN REGEXP_LIKE(REGEXP_SUBSTR(SMP_CD, '[^-]+$'), '[^0-9]') THEN 9999999 ELSE CAST(REGEXP_SUBSTR(SMP_CD, '[^-]+$') AS BIGINT) END")
    			);
    }

    /*
    * 归库明细分页查询*/
    default PageResult<BusinessSampleHandoverDetailRespVO> takeOffPageList(BusinessSampleHandoverDetailPageReqVO reqVO) {
        return selectJoinPage(reqVO, BusinessSampleHandoverDetailRespVO.class, new MPJLambdaWrapperX<BusinessSampleHandoverDetailDO>()
                .leftJoin(BusinessSubSampleDO.class, BusinessSubSampleDO::getId, BusinessSampleHandoverDetailDO::getBusinessSubSampleId)
                .leftJoin(ConfigWarehouseLocationDO.class, ConfigWarehouseLocationDO::getId, BusinessSubSampleDO::getConfigWarehouseLocationInfomationId)
                .selectAll(BusinessSampleHandoverDetailDO.class)
                .selectAs(BusinessSubSampleDO::getSampleCode, BusinessSampleHandoverDetailRespVO::getOriginalSampleCode)
                .selectAs(BusinessSubSampleDO::getReturnTime, BusinessSampleHandoverDetailRespVO::getReturnTime)
                .selectAs(ConfigWarehouseLocationDO::getCode, BusinessSampleHandoverDetailRespVO::getWarehouseLocationCode)
                .eqIfPresent(BusinessSampleHandoverDetailDO::getBusinessSampleHandoverId, reqVO.getBusinessSampleHandoverId())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .likeIfPresent(BusinessSampleHandoverDetailDO::getSampleName, reqVO.getSampleName())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getSampleCode, reqVO.getSampleCode())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getSampleWeight, reqVO.getSampleWeight())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getBalanceCode, reqVO.getBalanceCode())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getDictionaryBusinessId, reqVO.getDictionaryBusinessId())
                .likeIfPresent(BusinessSampleHandoverDetailDO::getDictionaryBusinessName, reqVO.getDictionaryBusinessName())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessSampleHandoverDetailDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessSampleHandoverDetailDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessSampleHandoverDetailDO::getId));
    }

}