package com.cool.store.dao.fees;

import com.cool.store.entity.fees.WalletPayInfoDO;
import com.cool.store.enums.fees.AllocationPayStatusEnum;
import com.cool.store.mapper.fees.WalletPayInfoMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 钱包加盟缴费信息DAO
 * </p>
 *
 * @author wangff
 * @since 2026/1/30
 */
@Repository
@RequiredArgsConstructor
public class WalletPayInfoDAO {
    private final WalletPayInfoMapper walletPayInfoMapper;

    /**
     * 是否存在缴费信息
     * @param shopId 门店id
     * @return 是否存在
     */
    public boolean existPayInfo(Long shopId) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria().andEqualTo("shopId", shopId);
        return walletPayInfoMapper.selectCountByExample(example) > 0;
    }

    /**
     * 批量新增
     */
    public void insertBatch(List<WalletPayInfoDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        walletPayInfoMapper.insertBatch(list);
    }

    /**
     * 根据payNo编辑
     */
    public void updateByPayNo(WalletPayInfoDO walletPayInfoDO) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria().andEqualTo("payNo", walletPayInfoDO.getPayNo());
        walletPayInfoMapper.updateByExampleSelective(walletPayInfoDO, example);
    }

    /**
     * 根据门店id查询
     */
    public List<WalletPayInfoDO> getByShopId(Long shopId, String module, Integer payStatus) {
        Example example = new Example(WalletPayInfoDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("shopId", shopId)
                .andEqualTo("module", module);
        if (Objects.nonNull(payStatus)) {
            criteria.andEqualTo("payStatus", payStatus);
        }
        example.setOrderByClause("create_time DESC");
        return walletPayInfoMapper.selectByExample(example);
    }

    /**
     * 根据门店查询已支付
     */
    public List<WalletPayInfoDO> getPaidByShopId(Long shopId, String module) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria()
                .andEqualTo("shopId", shopId)
                .andEqualTo("module", module)
                .andEqualTo("payStatus", AllocationPayStatusEnum.PAID.getStatus());
        example.setOrderByClause("create_time ASC");
        return walletPayInfoMapper.selectByExample(example);
    }

    public WalletPayInfoDO getById(Long id) {
        return walletPayInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据付款单号查询
     */
    public WalletPayInfoDO getByPayNo(String payNo) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria().andEqualTo("payNo", payNo);
        return walletPayInfoMapper.selectOneByExample(example);
    }

    public List<WalletPayInfoDO> getByPayNoList(List<String> payNoList) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria().andIn("payNo", payNoList);
        return walletPayInfoMapper.selectByExample(example);
    }

    public void updateByPrimaryKeySelective(WalletPayInfoDO walletPayInfoDO) {
        walletPayInfoMapper.updateByPrimaryKeySelective(walletPayInfoDO);
    }

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

    /**
     * 根据付款单号修改认领状态
     * @param payNo 付款单号
     * @param claimStatus 认领状态 0未认领1已认领
     * @return 是否成功
     */
    public void updateClaimStatusByPayNo(String payNo, Integer claimStatus) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria().andEqualTo("payNo", payNo);
        WalletPayInfoDO walletPayInfoDO = new WalletPayInfoDO();
        walletPayInfoDO.setClaimStatus(claimStatus);
        walletPayInfoMapper.updateByExampleSelective(walletPayInfoDO, example);
    }

    /**
     * 根据批次编号查询
     * @param batchCode 批次编号
     * @return 列表
     */
    public List<WalletPayInfoDO> getByBatchCode(String batchCode) {
        Example example = new Example(WalletPayInfoDO.class);
        example.createCriteria().andEqualTo("batchCode", batchCode);
        return walletPayInfoMapper.selectByExample(example);
    }

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