package com.cool.store.dao.wallet;

import com.cool.store.entity.wallet.WalletTradeDO;
import com.cool.store.mapper.wallet.WalletTradeMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * <p>
 * 钱包交易单DAO
 * </p>
 *
 * @author wangff
 * @since 2026/2/4
 */
@Repository
@RequiredArgsConstructor
public class WalletTradeDAO {
    private final WalletTradeMapper walletTradeMapper;

    public void insertSelective(WalletTradeDO walletTradeDO) {
        walletTradeMapper.insertSelective(walletTradeDO);
    }

    public void insertBatch(List<WalletTradeDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        walletTradeMapper.insertBatch(list);
    }

    public WalletTradeDO getByPayNo(String payNo) {
        Example example = new Example(WalletTradeDO.class);
        example.createCriteria().andEqualTo("payNo", payNo);
        return walletTradeMapper.selectOneByExample(example);
    }

    public void updateByPrimaryKeySelective(WalletTradeDO walletTradeDO) {
        walletTradeMapper.updateByPrimaryKeySelective(walletTradeDO);
    }

    public boolean updateByPayNo(WalletTradeDO record) {
        if (record == null || record.getPayNo() == null) {
            return false;
        }
        return walletTradeMapper.updateByPayNo(record) > 0;
    }

    public boolean updateBatchByPayNo(List<WalletTradeDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return walletTradeMapper.updateBatchByPayNo(list) > 0;
    }

    /**
     * 查询支付中交易单批次号
     * @param module 业务模块
     * @param type 交易类型
     * @return 批次号列表
     */
    public List<String> getPayingOrderBatchCode(String module, Integer type) {
        return walletTradeMapper.getPayingOrderBatchCode(module, type);
    }

    /**
     * 查询支付中的 单次支付的数据 非批量
     * @param module
     * @param type
     * @return
     */
    public List<WalletTradeDO> getPayingOrder(String module, Integer type) {
        return walletTradeMapper.getPayingOrder(module, type);
    }


    public List<WalletTradeDO> transferTradeList(String module, Integer type) {
        return walletTradeMapper.transferTradeList(module, type);
    }

    /**
     * 根据批次号更新支付状态
     * @param batchCode 批次号
     * @param payStatus 支付状态
     */
    public void updatePayStatusByBatchCode(String batchCode, Integer payStatus) {
        Example example = new Example(WalletTradeDO.class);
        example.createCriteria().andEqualTo("batchCode", batchCode);
        WalletTradeDO walletTradeDO = new WalletTradeDO();
        walletTradeDO.setPayStatus(payStatus);
        walletTradeMapper.updateByExampleSelective(walletTradeDO, example);
    }
}
