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

import com.ruoyi.bst.channel.domain.Channel;
import com.ruoyi.bst.channel.domain.ChannelQuery;
import com.ruoyi.bst.channel.domain.ChannelVO;
import com.ruoyi.bst.channel.domain.vo.ChannelNameVO;
import com.ruoyi.bst.channel.mapper.ChannelMapper;
import com.ruoyi.bst.channel.service.ChannelConverter;
import com.ruoyi.bst.channel.service.ChannelService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

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 ChannelConverter channelConverter;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private RedisCache redisCache;

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

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

    /**
     * 新增充值渠道
     *
     * @param channel 充值渠道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        Integer result = transactionTemplate.execute(status -> {
            int insert = channelMapper.insertChannel(channel);

            if (insert == 1) {
                clearCache();
            }

            return insert;
        });

        return result == null ? 0 : result;
    }

    /**
     * 修改充值渠道
     *
     * @param channel 充值渠道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel)
    {
        Integer result = transactionTemplate.execute(status -> {
            int update = channelMapper.updateChannel(channel);

            if (update == 1) {
                clearCache();
            }

            return update;
        });

        return result == null ? 0 :result;
    }

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

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

    @Override
    public int logicDel(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return 0;
        }

        List<ChannelVO> channelList = this.selectByIds(ids);
        if (CollectionUtils.isEmpty(channelList)) {
            return 0;
        }

        // 校验
//        for (ChannelVO channel : channelList) {
//            ServiceUtil.assertion(ChannelType.SYSTEM.getType().equals(channel.getType()), "不允许删除系统预设渠道 \"%s\"}", channel.getName());
//        }

        int del = channelMapper.logicDel(ids);
        if (del > 0) {
            clearCache();
        }
        return del;
    }

    @Override
    public List<ChannelVO> selectByIds(List<Long> ids) {
        if (CollectionUtils.isEmptyElement(ids)) {
            return Collections.emptyList();
        }
        ChannelQuery query = new ChannelQuery();
        query.setChannelIds(ids);
        return this.selectChannelList(query);
    }

    @Override
    public List<ChannelNameVO> selectAllChannelNameList() {
        List<ChannelNameVO> list = redisCache.getCacheList(CacheConstants.CHANNEL_NAME_LIST);
        if (CollectionUtils.isEmpty(list)) {
            list = channelMapper.selectNameList(new ChannelQuery());
            if (CollectionUtils.isNotEmpty(list)) {
                redisCache.setCacheList(CacheConstants.CHANNEL_NAME_LIST, list);
            }
        }
        return list;
    }

    /**
     * 清除缓存
     */
    private void clearCache() {
        redisCache.deleteObject(CacheConstants.CHANNEL_NAME_LIST);
    }
}
