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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.merchantsettlement.entity.MerchantSettlementPayout;
import com.jeesharp.business.modules.finance.merchantsettlement.mapper.MerchantSettlementPayoutMapper;
import com.jeesharp.business.modules.finance.merchantsettlement.service.MerchantSettlementPayoutService;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 商户结算打款记录领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-29
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MerchantSettlementPayoutServiceImpl implements MerchantSettlementPayoutService {

    private final MerchantSettlementPayoutMapper merchantSettlementPayoutMapper;

    @Override
    @Transactional(readOnly = false)
    public MerchantSettlementPayout createPayout(MerchantSettlementPayout payout) {
        if (payout == null) {
            throw new BizErrorException("打款记录不能为空");
        }
        if (StrUtil.isNotBlank(payout.getId())) {
            throw new BizErrorException("新增打款记录不应预置主键");
        }
        payout.preInsert();
        merchantSettlementPayoutMapper.insert(payout);
        return payout;
    }

    @Override
    public List<MerchantSettlementPayout> listBySettlementBillId(String settlementBillId) {
        if (StrUtil.isBlank(settlementBillId)) {
            return Collections.emptyList();
        }
        return merchantSettlementPayoutMapper.listBySettlementBillId(settlementBillId.trim());
    }
}
