package com.erp.warehouse.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.erp.warehouse.dto.StockDTO;
import com.erp.warehouse.dto.StockLogDTO;
import com.erp.warehouse.dto.StockOpDTO.*;
import com.erp.warehouse.entity.Stock;
import com.erp.warehouse.entity.StockLog;

/**
 * ERP本地库存服务接口
 */
public interface StockService {

    // ==================== 库存查询 ====================

    /**
     * 分页查询库存列表
     */
    IPage<StockDTO> listStocks(String skuCode, String skuName, Long warehouseId, Boolean lowStock, int page, int limit);

    /**
     * 查询单SKU在指定仓库的库存
     */
    StockDTO getStock(String skuCode, Long warehouseId);

    /**
     * 分页查询库存流水
     */
    IPage<StockLogDTO> listLogs(String skuCode, Long warehouseId, String changeType,
                                 Long orderId, String startDate, String endDate, int page, int limit);

    // ==================== 库存操作 ====================

    /**
     * 手动入库
     */
    Stock inbound(String skuCode, Long warehouseId, Integer quantity, String relatedNo, String remark, Long operatorId);

    /**
     * 手动出库
     */
    Stock outbound(String skuCode, Long warehouseId, Integer quantity, String relatedNo, String remark, Long operatorId);

    /**
     * 残次品入库
     */
    Stock defectiveInbound(String skuCode, Long warehouseId, Integer quantity, String relatedNo, String remark, Long orderId);

    /**
     * 盘点调整库存
     */
    Stock adjust(String skuCode, Long warehouseId, Integer quantity, Integer defectiveQuantity, String remark, Long operatorId);

    /**
     * 更新预警阈值
     */
    Stock updateThreshold(String skuCode, Long warehouseId, Integer warningThreshold);

    // ==================== 订单联动 ====================

    /**
     * 锁定库存（审核订单时）
     */
    Stock lock(String skuCode, Long warehouseId, Integer quantity, Long orderId);

    /**
     * 解锁库存（驳回订单时）
     */
    Stock unlock(String skuCode, Long warehouseId, Integer quantity, Long orderId);

    /**
     * 预扣库存（两阶段第一阶段）
     */
    ReserveResult reserve(String skuCode, Long warehouseId, Integer quantity, Long orderId, String reserveToken);

    /**
     * 确认预扣出库（两阶段第二阶段）
     */
    Stock confirmReserve(String reserveToken, Long orderId, String trackingNo);

    /**
     * 取消预扣（两阶段第二阶段）
     */
    Stock cancelReserve(String reserveToken, Long orderId);

    /**
     * 发货出库
     */
    Stock ship(String skuCode, Long warehouseId, Integer quantity, Long orderId, String trackingNo);

    /**
     * 预扣结果
     */
    record ReserveResult(Stock stock, String reserveToken) {}
}
