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

import com.ruoyi.common.pay.xy.config.XyPayConfig;
import com.ruoyi.common.pay.xy.service.XyWxPayService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
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.domain.enums.ChannelApiType;
import com.ruoyi.ss.channel.mapper.ChannelMapper;
import com.ruoyi.ss.channel.service.ChannelConverter;
import com.ruoyi.ss.channel.service.ChannelService;
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 XyWxPayService xyWxPayService;

    @Autowired
    private ChannelConverter channelConverter;

    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询充值渠道
     *
     * @param channelId 充值渠道主键
     * @return 充值渠道
     */
    @Override
    public ChannelVO selectSmChannelByChannelId(Long channelId)
    {
        if (channelId == null) {
            return null;
        }
        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)
    {
        channel.setCreateTime(DateUtils.getNowDate());
        Integer result = transactionTemplate.execute(status -> {
            int insert = channelMapper.insertSmChannel(channel);

            if (insert == 1) {
                ChannelVO vo = selectSmChannelByChannelId(channel.getChannelId());
                this.connectCust(vo);
            }

            return insert;
        });

        return result == null ? 0 : result;
    }

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

            if (update == 1) {
                ChannelVO vo = selectSmChannelByChannelId(channel.getChannelId());
                this.connectCust(vo);
            }

            return update;
        });

        return result == null ? 0 :result;
    }

    /**
     * 关联商户和机构
     */
    private boolean connectCust(ChannelVO vo) {
        ServiceUtil.assertion(vo == null, "渠道不存在");
        if (ChannelApiType.XY_WX.getType().equals(vo.getApiType())) {
            return xyWxPayService.connectCust((XyPayConfig) channelConverter.toConfig(vo));
        }
        return true;
    }

    /**
     * 批量删除充值渠道
     *
     * @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));
    }

    @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());
//        }

        return channelMapper.logicDel(ids);
    }

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