package com.erp.tenant.repository.order;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.tenant.entity.order.OrderItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 订单明细数据访问
 */
@Mapper
public interface OrderItemRepository extends BaseMapper<OrderItem> {

    @Select("SELECT * FROM order_items WHERE order_id = #{orderId} AND deleted_at IS NULL")
    List<OrderItem> findByOrderId(@Param("orderId") Long orderId);

    @Select("SELECT * FROM order_items WHERE tenant_id = #{tenantId} AND deleted_at IS NULL")
    List<OrderItem> findAllByTenantId(@Param("tenantId") Long tenantId);

    @Update("UPDATE order_items SET erp_sku_id = #{erpSkuId}, erp_sku = #{erpSkuCode}, " +
            "match_status = 1, updated_at = NOW() " +
            "WHERE platform_sku = #{platformSku} AND match_status = 0 AND deleted_at IS NULL")
    int batchUpdateMatchByPlatformSku(@Param("platformSku") String platformSku,
                                      @Param("erpSkuId") Long erpSkuId,
                                      @Param("erpSkuCode") String erpSkuCode);
}
