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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.merchantsettlement.entity.MerchantSettlementBillLine;
import com.jeesharp.business.modules.finance.merchantsettlement.mapper.MerchantSettlementBillLineMapper;
import com.jeesharp.business.modules.finance.merchantsettlement.service.MerchantSettlementBillLineService;
import com.jeesharp.business.modules.finance.merchantsettlement.vo.MerchantSettlementBillLineQueryVO;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商户结算单构成明细领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-29
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MerchantSettlementBillLineServiceImpl
        extends CrudService<MerchantSettlementBillLineMapper, MerchantSettlementBillLine, MerchantSettlementBillLineQueryVO>
        implements MerchantSettlementBillLineService {

    @Override
    @Transactional(readOnly = false)
    public void batchCreateLines(List<MerchantSettlementBillLine> lines) {
        if (lines == null || lines.isEmpty()) {
            return;
        }
        for (MerchantSettlementBillLine line : lines) {
            if (line == null) {
                throw new BizErrorException("构成明细不能为空");
            }
            if (StrUtil.isNotBlank(line.getId())) {
                throw new BizErrorException("新增构成明细不应预置主键");
            }
            line.preInsert();
        }
        getBaseMapper().batchInsert(lines);
    }

    @Override
    public long countBySettlementBillId(String settlementBillId) {
        if (StrUtil.isBlank(settlementBillId)) {
            return 0L;
        }
        return getBaseMapper().countBySettlementBillId(settlementBillId.trim());
    }

    @Override
    public Page<MerchantSettlementBillLine> findPage(Page<MerchantSettlementBillLine> page,
            MerchantSettlementBillLineQueryVO query) {
        return super.findPage(page, query);
    }
}
