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.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleDispatchDetailExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleDispatchDetailPageReqVO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleDispatchDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationParDO;
import com.zt.plat.module.qms.enums.QmsWarehouseLocationConstant;
import org.apache.ibatis.annotations.Mapper;

/**
 * 样品调拨明细 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BusinessSampleDispatchDetailMapper extends BaseMapperX<BusinessSampleDispatchDetailDO> {

    default PageResult<BusinessSampleDispatchDetailExtendRespVO> selectPage(BusinessSampleDispatchDetailPageReqVO reqVO) {
        MPJLambdaWrapperX<BusinessSampleDispatchDetailDO> wrapper = new MPJLambdaWrapperX<>();
        //子样表
        wrapper.leftJoin(BusinessSubSampleDO.class , BusinessSubSampleDO::getId, BusinessSampleDispatchDetailDO::getBusinessSubSampleId);
        //库位
        wrapper.leftJoin(ConfigWarehouseLocationDO.class, ConfigWarehouseLocationDO::getId, BusinessSubSampleDO::getConfigWarehouseLocationInfomationId, w->{
            w.eq(ConfigWarehouseLocationDO::getWarehouseType, QmsWarehouseLocationConstant.WAREHOUSE_TYPE_SAMPLE);
        });
        //仓库
        wrapper.leftJoin(ConfigWarehouseLocationParDO.class, ConfigWarehouseLocationParDO::getId, ConfigWarehouseLocationDO::getParentId);
        wrapper.selectAll(BusinessSampleDispatchDetailDO.class)
                .selectAs(BusinessSubSampleDO::getSampleCode, BusinessSampleDispatchDetailExtendRespVO::getSampleCode)
                .selectAs(BusinessSubSampleDO::getSampleName, BusinessSampleDispatchDetailExtendRespVO::getSampleName)
                .selectAs(BusinessSubSampleDO::getSampleReturnCode, BusinessSampleDispatchDetailExtendRespVO::getSampleReturnCode)
                .selectAs(BusinessSubSampleDO::getReturnStatus, BusinessSampleDispatchDetailExtendRespVO::getSampleReturnStatus)
                .selectAs(BusinessSubSampleDO::getReturnTime, BusinessSampleDispatchDetailExtendRespVO::getSampleReturnTime)
                .selectAs(BusinessSubSampleDO::getDispatchStatus, BusinessSampleDispatchDetailExtendRespVO::getDispatchStatus)
                .selectAs(ConfigWarehouseLocationDO::getCode, BusinessSampleDispatchDetailExtendRespVO::getWarehouseLocationCode)
                .selectAs(ConfigWarehouseLocationParDO::getName, BusinessSampleDispatchDetailExtendRespVO::getWarehouseName)
                //查询条件
                .inIfPresent(BusinessSampleDispatchDetailDO::getId, reqVO.getIdList())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getParentId, reqVO.getParentId())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .eqIfPresent(BusinessSubSampleDO::getSampleReturnCode, reqVO.getSampleReturnCode())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getBorrowUser, reqVO.getBorrowUser())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getBorrowUserId, reqVO.getBorrowUserId())
                .betweenIfPresent(BusinessSampleDispatchDetailDO::getBorrowTime, reqVO.getBorrowTime())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getBorrowStatus, reqVO.getBorrowStatus())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getGivebackUser, reqVO.getGivebackUser())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getGivebackUserId, reqVO.getGivebackUserId())
                .betweenIfPresent(BusinessSampleDispatchDetailDO::getGivebackTime, reqVO.getGivebackTime())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getGivebackStatus, reqVO.getGivebackStatus())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getUseStatus, reqVO.getUseStatus())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(BusinessSampleDispatchDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(BusinessSampleDispatchDetailDO::getCreateTime, reqVO.getCreateTime())
                //排序
                .orderByDesc(BusinessSampleDispatchDetailDO::getId);

        return selectJoinPage(reqVO, BusinessSampleDispatchDetailExtendRespVO.class, wrapper);

    }

}