package com.erp.reconciliation.service;

import com.erp.reconciliation.dto.*;
import com.erp.reconciliation.entity.ReconciliationBill;
import com.erp.reconciliation.entity.ReconciliationDifference;
import com.erp.reconciliation.entity.ReconciliationTask;

import java.time.LocalDate;
import java.util.List;
import com.erp.common.core.model.PageResponse;
import java.util.Map;

/**
 * 对账服务接口
 * 
 * 功能覆盖：
 * 1. 对账CRUD - 对账单的创建、查询、更新
 * 2. 账单核对 - 平台账单与内部订单一致性核对
 * 3. 差异处理 - 发现、管理、处理对账差异
 * 4. 对账报表 - 统计报表生成
 * 
 * @author ERP Migration
 */
public interface ReconciliationService {

    // ==================== 对账账单管理（CRUD） ====================

    /**
     * 创建对账单
     * 对应PHP DataConsistencyService::checkFinanceConsistency
     */
    ReconciliationBill createBill(ReconciliationBill bill);

    /**
     * 根据ID获取对账单
     */
    ReconciliationBill getBillById(Long id);

    /**
     * 根据对账单号获取
     */
    ReconciliationBill getBillByBillNo(String billNo);

    /**
     * 分页查询对账单
     */
    PageResponse<ReconciliationBillDTO> queryBills(ReconciliationQueryRequest request);

    /**
     * 更新对账单
     */
    ReconciliationBill updateBill(ReconciliationBill bill);

    /**
     * 删除对账单
     */
    boolean deleteBill(Long id);

    /**
     * 确认对账单
     */
    boolean confirmBill(Long id, Long userId, String userName);

    // ==================== 账单核对 ====================

    /**
     * 执行对账核对
     * 核心对账逻辑：比对平台账单与内部订单
     */
    ReconciliationBill performReconciliation(Long billId);

    /**
     * 批量执行对账
     */
    List<ReconciliationBill> batchReconciliation(List<Long> billIds);

    /**
     * 按平台和周期创建对账单并执行核对
     */
    ReconciliationBill createAndReconcile(String platform, Long shopId, String periodType, LocalDate periodStart, LocalDate periodEnd);

    /**
     * 订单-金额一致性检查
     * 对应PHP DataConsistencyService::checkFinanceConsistency
     */
    List<Map<String, Object>> checkOrderAmountConsistency(LocalDate startDate, LocalDate endDate);

    /**
     * 订单-库存一致性检查
     * 对应PHP DataConsistencyService::checkOrderStockConsistency
     */
    List<Map<String, Object>> checkOrderStockConsistency();

    /**
     * 运单-物流轨迹一致性检查
     * 对应PHP DataConsistencyService::checkWaybillConsistency
     */
    List<Map<String, Object>> checkWaybillConsistency();

    /**
     * 执行全面一致性检查
     * 对应PHP DataConsistencyService::fullCheck
     */
    Map<String, Object> fullConsistencyCheck();

    // ==================== 差异处理 ====================

    /**
     * 获取对账差异列表
     */
    PageResponse<ReconciliationDifferenceDTO> queryDifferences(ReconciliationQueryRequest request);

    /**
     * 获取差异详情
     */
    ReconciliationDifference getDifferenceById(Long id);

    /**
     * 处理差异
     */
    boolean handleDifference(Long id, String handleMethod, String handleRemark, Long userId, String userName);

    /**
     * 批量处理差异
     */
    int batchHandleDifferences(List<Long> ids, String handleMethod, String handleRemark, Long userId, String userName);

    /**
     * 更新差异处理状态
     */
    boolean updateDifferenceStatus(Long id, String handleStatus);

    /**
     * 获取待处理差异统计
     */
    Map<String, Long> getPendingDifferenceStats();

    // ==================== 对账任务 ====================

    /**
     * 创建对账任务
     */
    ReconciliationTask createTask(ReconciliationTask task);

    /**
     * 获取任务进度
     */
    ReconciliationTask getTaskById(Long id);

    /**
     * 分页查询任务
     */
    PageResponse<ReconciliationTaskDTO> queryTasks(ReconciliationQueryRequest request);

    /**
     * 更新任务进度
     */
    boolean updateTaskProgress(Long taskId, Integer progress, String currentStep);

    /**
     * 取消任务
     */
    boolean cancelTask(Long taskId);

    // ==================== 统计报表 ====================

    /**
     * 获取对账统计数据
     */
    ReconciliationStatisticsDTO getStatistics(ReconciliationQueryRequest request);

    /**
     * 获取平台维度统计
     */
    ReconciliationStatisticsDTO.PlatformStatistics getPlatformStatistics(String platform, LocalDate startDate, LocalDate endDate);

    /**
     * 获取店铺维度统计
     */
    ReconciliationStatisticsDTO.ShopStatistics getShopStatistics(Long shopId, LocalDate startDate, LocalDate endDate);

    /**
     * 获取差异趋势
     */
    List<Map<String, Object>> getDifferenceTrend(LocalDate startDate, LocalDate endDate);

    // ==================== 定时对账 ====================

    /**
     * 执行每日定时对账
     */
    void executeDailyReconciliation();

    /**
     * 执行每周定时对账
     */
    void executeWeeklyReconciliation();

    /**
     * 执行每月定时对账
     */
    void executeMonthlyReconciliation();
}
