package cn.iocoder.yudao.module.order.dal.mysql.orderLocation;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.framework.mybatis.core.mapper.AbstractMapper;
import cn.iocoder.yudao.module.order.dal.dataobject.orderLocation.OrderLocationDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.orderLocation.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 订单仓位 Mapper
 * @author 系统管理员
 */
@Mapper
public interface OrderLocationMapper extends AbstractMapper<OrderLocationDO> {
    @Override
    default PageResult<OrderLocationDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderLocationQueryVO) {
            OrderLocationQueryVO vo = (OrderLocationQueryVO)object;
            return selectPage(page, new LambdaQuery<OrderLocationDO>()
                .eqIfPresent(OrderLocationDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderLocationDO::getWareId, vo.getWareId())
                .eqIfPresent(OrderLocationDO::getAreaId, vo.getAreaId())
                .eqIfPresent(OrderLocationDO::getLocationId, vo.getLocationId())
                .eqIfPresent(OrderLocationDO::getOrderItemId, vo.getOrderItemId())
                .eqIfPresent(OrderLocationDO::getWarehouseInId, vo.getWarehouseInId())
                .eqIfPresent(OrderLocationDO::getAdminId, vo.getAdminId())
                .betweenIfPresent(OrderLocationDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderLocationDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderLocationDO> selectList(Object object) {
        if (object instanceof OrderLocationQueryVO) {
            OrderLocationQueryVO vo = (OrderLocationQueryVO)object;
            return selectList(new LambdaQuery<OrderLocationDO>()
                .eqIfPresent(OrderLocationDO::getOrderId, vo.getOrderId())
                .eqIfPresent(OrderLocationDO::getWareId, vo.getWareId())
                .eqIfPresent(OrderLocationDO::getAreaId, vo.getAreaId())
                .eqIfPresent(OrderLocationDO::getLocationId, vo.getLocationId())
                .eqIfPresent(OrderLocationDO::getOrderItemId, vo.getOrderItemId())
                .eqIfPresent(OrderLocationDO::getWarehouseInId, vo.getWarehouseInId())
                .eqIfPresent(OrderLocationDO::getAdminId, vo.getAdminId())
                .betweenIfPresent(OrderLocationDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderLocationDO::getId));
        }

        return null;
    }


    @Select("select loc.*, ware.name as warehouse_name, area.name as area_name, posi.code as location_name from ecw_order_location loc " +
            "left join ecw_warehouse_area ware on loc.ware_id = ware.id " +
            "left join ecw_warehouse_area area on loc.area_id = area.id " +
            "left join ecw_warehouse_area_position posi on loc.location_id = posi.id " +
            "where order_id = #{orderId} and loc.deleted=0")
    List<OrderLocationDO> selectListWithName(@Param("orderId") Long orderId);


    @Select("select loc.*, ware.name as warehouse_name, area.name as area_name, posi.code as location_name from ecw_order_location loc " +
            "left join ecw_warehouse_area ware on loc.ware_id = ware.id " +
            "left join ecw_warehouse_area area on loc.area_id = area.id " +
            "left join ecw_warehouse_area_position posi on loc.location_id = posi.id " +
            "where warehouse_in_id = #{warehouseInId} and loc.deleted=0")
    List<OrderLocationDO> selectListWithNameByWarehouseInId(@Param("warehouseInId") Long warehouseInId);


}
