package com.ruoyi.ss.channel.service.impl;

import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.account.service.AccountService;
import com.ruoyi.ss.channel.domain.Channel;
import com.ruoyi.ss.channel.domain.ChannelQuery;
import com.ruoyi.ss.channel.domain.ChannelVO;
import com.ruoyi.ss.channel.mapper.ChannelMapper;
import com.ruoyi.ss.channel.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 充值渠道Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class ChannelServiceImpl implements ChannelService
{
    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private AccountService accountService;

    /**
     * 查询充值渠道
     *
     * @param channelId 充值渠道主键
     * @return 充值渠道
     */
    @Override
    public ChannelVO selectSmChannelByChannelId(Long channelId)
    {
        return channelMapper.selectSmChannelByChannelId(channelId);
    }

    /**
     * 查询充值渠道列表
     *
     * @param smChannel 充值渠道
     * @return 充值渠道
     */
    @Override
    public List<ChannelVO> selectSmChannelList(ChannelQuery smChannel)
    {
        return channelMapper.selectSmChannelList(smChannel);
    }

    /**
     * 新增充值渠道
     *
     * @param channel 充值渠道
     * @return 结果
     */
    @Override
    public int insertSmChannel(Channel channel)
    {
        return channelMapper.insertSmChannel(channel);
    }

    /**
     * 修改充值渠道
     *
     * @param channel 充值渠道
     * @return 结果
     */
    @Override
    public int updateSmChannel(Channel channel)
    {
        return channelMapper.updateSmChannel(channel);
    }

    /**
     * 批量删除充值渠道
     *
     * @param channelIds 需要删除的充值渠道主键
     * @return 结果
     */
    @Override
    public int deleteSmChannelByChannelIds(Long[] channelIds)
    {
        return channelMapper.deleteSmChannelByChannelIds(channelIds);
    }

    /**
     * 删除充值渠道信息
     *
     * @param channelId 充值渠道主键
     * @return 结果
     */
    @Override
    public int deleteSmChannelByChannelId(Long channelId)
    {
        return channelMapper.deleteSmChannelByChannelId(channelId);
    }

    @Override
    public List<ChannelVO> selectEnabledRechargeList(ChannelQuery query) {
        query.setEnabled(true);
        return this.selectSmChannelList(query);
    }

    /**
     * 查询充值渠道映射表
     *
     * @param query     查询条件
     * @param keyMapper 映射函数
     */
    @Override
    public <K> Map<K, ChannelVO> selectMap(ChannelQuery query, Function<? super Channel, ? extends K> keyMapper) {
        List<ChannelVO> list = this.selectSmChannelList(query);
        if (CollectionUtils.isEmptyElement(list)) {
            return Collections.emptyMap();
        }
        return list.stream().collect(Collectors.toMap(keyMapper, item -> item));
    }
}
