package com.hys.app.mapper.erp;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.mybatisplus.base.BaseDO;
import com.hys.app.framework.database.mybatisplus.base.BaseMapperX;
import com.hys.app.model.erp.dos.StockTransferDO;
import com.hys.app.model.erp.dto.StockTransferQueryParams;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dto.StockTransferStatisticsParam;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 库存调拨的Mapper
 *
 * @author 张崧
 * @since 2023-12-12 11:59:06
 */
public interface StockTransferMapper extends BaseMapperX<StockTransferDO> {

    default WebPage<StockTransferDO> selectPage(StockTransferQueryParams params) {
        return lambdaQuery()
                .likeIfPresent(StockTransferDO::getSn, params.getSn())
                .eqIfPresent(StockTransferDO::getOutDeptId, params.getOutDeptId())
                .eqIfPresent(StockTransferDO::getOutWarehouseId, params.getOutWarehouseId())
                .eqIfPresent(StockTransferDO::getOutHandledByName, params.getOutHandledBy())
                .eqIfPresent(StockTransferDO::getInDeptId, params.getInDeptId())
                .eqIfPresent(StockTransferDO::getInWarehouseId, params.getInWarehouseId())
                .eqIfPresent(StockTransferDO::getInHandledByName, params.getInHandledBy())
                .eqIfPresent(StockTransferDO::getCompanyId, AdminUserContext.getCompanyId())
                .eqIfPresent(StockTransferDO::getStatus, params.getStatus())
                .geIfPresent(BaseDO::getCreateTime, params.getStartTime())
                .leIfPresent(BaseDO::getCreateTime, params.getEndTime())
                .andX(params.getDeptIdList() != null, deptWrapper ->
                        deptWrapper.in(StockTransferDO::getInDeptId, params.getDeptIdList()).or().in(StockTransferDO::getOutDeptId, params.getDeptIdList()))
                .orderByDesc(StockTransferDO::getTransferTime)
                .page(params);
    }
}

