package com.jeesharp.business.modules.finance.reconcile.service.impl;

import com.jeesharp.business.modules.finance.payorder.entity.PayOrder;
import com.jeesharp.business.modules.finance.payrefund.entity.PayRefundOrder;
import com.jeesharp.business.modules.finance.profitsharing.entity.ProfitSharingOrder;
import com.jeesharp.business.modules.finance.reconcile.mapper.FinanceReconcileMapper;
import com.jeesharp.business.modules.finance.reconcile.service.FinanceReconcileService;
import com.jeesharp.business.modules.finance.reconcile.vo.FinanceReconcileQueryVO;
import com.jeesharp.framework.mybatis.core.service.DataScopeWrapper;
import com.jeesharp.framework.pojo.query.PaginationVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 财务对账领域服务实现，委托 {@link FinanceReconcileMapper} 查询三类业务单据。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class FinanceReconcileServiceImpl implements FinanceReconcileService {

    private final FinanceReconcileMapper financeReconcileMapper;

    @Override
    public List<PayOrder> listPayOrders(FinanceReconcileQueryVO query) {
        if (query == null) {
            return Collections.emptyList();
        }
        applyDataScope(query, PayOrder.class);
        return financeReconcileMapper.listPayOrders(query);
    }

    @Override
    public List<ProfitSharingOrder> listSharingOrders(FinanceReconcileQueryVO query) {
        if (query == null) {
            return Collections.emptyList();
        }
        applyDataScope(query, ProfitSharingOrder.class);
        return financeReconcileMapper.listSharingOrders(query);
    }

    @Override
    public List<PayRefundOrder> listRefundOrders(FinanceReconcileQueryVO query) {
        if (query == null) {
            return Collections.emptyList();
        }
        applyDataScope(query, PayRefundOrder.class);
        return financeReconcileMapper.listRefundOrders(query);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private void applyDataScope(PaginationVO query, Class entityClass) {
        new DataScopeWrapper(entityClass, null).dataRuleFilter(query);
    }
}
