package com.jeesharp.business.modules.finance.merchantreceivable.mapper;

import com.jeesharp.business.modules.finance.merchantreceivable.entity.MerchantReceivable;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableQueryVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableSettlementQueryVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.MerchantReceivableSettlementSummaryVO;
import com.jeesharp.business.modules.finance.merchantreceivable.vo.PlatformCommissionPeriodSummaryVO;
import com.jeesharp.business.modules.finance.platformrevenue.vo.PlatformRevenueRollupAggregateVO;
import com.jeesharp.framework.mybatis.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 商户应收账流水 MyBatis Mapper。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Mapper
public interface MerchantReceivableMapper extends BaseMapper<MerchantReceivable, MerchantReceivableQueryVO> {

    /**
     * 按商户与账期汇总应收净额。
     */
    List<MerchantReceivableSettlementSummaryVO> summarizeByMerchant(
            @Param("query") MerchantReceivableSettlementQueryVO query);

    /**
     * 平台生成结算单：按商户与账期汇总，不应用 dataScope。
     */
    MerchantReceivableSettlementSummaryVO summarizeByMerchantForSettlement(
            @Param("merchantTenantId") String merchantTenantId,
            @Param("periodStart") java.time.LocalDateTime periodStart,
            @Param("periodEnd") java.time.LocalDateTime periodEnd);

    /**
     * 取商户最近一条应收流水的 tenant_code。
     */
    String selectTenantCodeByMerchantTenantId(@Param("merchantTenantId") String merchantTenantId);

    /**
     * 按日或按月汇总平台佣金与商户应得净额。
     */
    List<PlatformCommissionPeriodSummaryVO> summarizePlatformCommissionByPeriod(
            @Param("query") MerchantReceivableSettlementQueryVO query);

    /**
     * 平台收入日汇总：按业务时间窗聚合全平台代收流水（不走 dataScope）。
     */
    PlatformRevenueRollupAggregateVO summarizeForDailyRollup(
            @Param("windowStart") java.time.LocalDateTime windowStart,
            @Param("windowEnd") java.time.LocalDateTime windowEnd);

    /**
     * 生成结算单：账期内尚未被结算单占用的应收流水。
     */
    List<MerchantReceivable> listUnsettledForSettlement(
            @Param("merchantTenantId") String merchantTenantId,
            @Param("periodStart") java.time.LocalDateTime periodStart,
            @Param("periodEnd") java.time.LocalDateTime periodEnd);

    /**
     * 批量将应收流水绑定到结算单（仅未占用行）。
     */
    int batchBindSettlementBill(@Param("receivableIds") List<String> receivableIds,
            @Param("settlementBillId") String settlementBillId,
            @Param("settlementBillNo") String settlementBillNo);

    /**
     * 作废结算单时释放应收流水占用。
     */
    int clearSettlementBillByBillId(@Param("settlementBillId") String settlementBillId);
}
