package com.zt.plat.module.qms.resource.material.dal.mapper;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
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.config.dal.dataobject.ConfigWarehouseLocationDO;
import com.zt.plat.module.qms.core.aspect.annotation.QmsPermission;
import com.zt.plat.module.qms.enums.QmsPermissionConstant;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundDetailRespVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.*;
import org.apache.ibatis.annotations.Mapper;

/**
 * 出库明细 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface MaterialInventoryOutboundDetailMapper extends BaseMapperX<MaterialInventoryOutboundDetailDO> {

    default PageResult<MaterialInventoryOutboundDetailDO> selectPage(MaterialInventoryOutboundDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MaterialInventoryOutboundDetailDO>()
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getOutboundId, reqVO.getOutboundId())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getInfomationId, reqVO.getInfomationId())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MaterialInventoryOutboundDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialInventoryOutboundDetailDO::getId));
    }

    @QmsPermission(moduleDataRoleCodes = QmsPermissionConstant.ADMIN_ROLE + "," + QmsPermissionConstant.MATERIAL_ADMIN_ROLE)
    default PageResult<MaterialInventoryOutboundDetailRespVO> selectPageWithMaterialInfo(MaterialInventoryOutboundDetailPageReqVO reqVO){
        MPJLambdaWrapperX<MaterialInventoryOutboundDetailDO> wrapperX = new MPJLambdaWrapperX<MaterialInventoryOutboundDetailDO>()
                .selectAll(MaterialInventoryOutboundDetailDO.class)
                .selectAs(MaterialInfomationDO::getCode, MaterialInventoryOutboundDetailRespVO::getInfomationCode)
                .selectAs(MaterialInfomationDO::getManagerUserId, MaterialInventoryOutboundDetailRespVO::getManagerUserId)
                .selectAs(MaterialInfomationDO::getManagerUserName, MaterialInventoryOutboundDetailRespVO::getManagerUserName)
                .selectAs(MaterialProductDO::getName, MaterialInventoryOutboundDetailRespVO::getInfomationName)
                .selectAs(MaterialInventoryOutboundDO::getBusinessType, MaterialInventoryOutboundDetailRespVO::getBusinessType)
                .selectAs(ConfigWarehouseLocationDO::getName, MaterialInventoryOutboundDetailRespVO::getLocationName)
                .selectAs(ConfigWarehouseLocationDO::getCode, MaterialInventoryOutboundDetailRespVO::getLocationCode)
                .leftJoin(MaterialInfomationDO.class, MaterialInfomationDO::getId, MaterialInventoryOutboundDetailDO::getInfomationId)
                .leftJoin(MaterialProductDO.class, MaterialProductDO::getId, MaterialInfomationDO::getProductId)
                .leftJoin(MaterialInventoryOutboundDO.class, MaterialInventoryOutboundDO::getId, MaterialInventoryOutboundDetailDO::getOutboundId)
                .leftJoin(ConfigWarehouseLocationDO.class, ConfigWarehouseLocationDO::getId, MaterialInfomationDO::getLocationId)
                .eqIfPresent(MaterialProductDO::getId, reqVO.getProductId())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getOutboundId, reqVO.getOutboundId())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getInfomationId, reqVO.getInfomationId())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(MaterialInventoryOutboundDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MaterialInventoryOutboundDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialInventoryOutboundDetailDO::getId);
        return selectJoinPage(reqVO, MaterialInventoryOutboundDetailRespVO.class, wrapperX);
    }
}