package com.erp.product.sku.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.product.sku.entity.OrderItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 订单明细 Repository
 */
@Mapper
public interface OrderItemRepository extends BaseMapper<OrderItem> {
    
    /**
     * 根据平台SKU更新匹配状态
     */
    @Update("UPDATE order_items SET erp_sku_id = #{erpSkuId}, erp_sku = #{erpSku}, " +
            "match_status = 1 WHERE platform_sku = #{platformSku} AND match_status = 0")
    int updateMatchByPlatformSku(@Param("platformSku") String platformSku,
                                   @Param("erpSkuId") Long erpSkuId,
                                   @Param("erpSku") String erpSku);
    
    /**
     * 解除匹配状态
     */
    @Update("UPDATE order_items SET match_status = 0 " +
            "WHERE platform_sku = #{platformSku} AND erp_sku_id = #{erpSkuId}")
    int resetMatchStatus(@Param("platformSku") String platformSku,
                         @Param("erpSkuId") Long erpSkuId);
    
    /**
     * 查询订单的所有明细
     */
    List<OrderItem> findByOrderId(@Param("orderId") Long orderId);
}
