package com.gic.haoban.manage.service.service.chat.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatActivityRewardMapper;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityReward;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatActivityRewardBO;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityRewardService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 社群邀请有礼奖励规则表(TabHaobanGroupChatActivityReward)表服务实现类
 *
 * @author makejava
 * @since 2024-09-27 16:09:57
 */
@Service("groupChatActivityRewardService")
public class GroupChatActivityRewardServiceImpl implements GroupChatActivityRewardService {
    @Autowired
    private GroupChatActivityRewardMapper groupChatActivityRewardMapper;

    @Override
    public List<TabGroupChatActivityReward> listByActivityId(Long chatActivityId) {
        if (chatActivityId == null) {
            return new ArrayList<>();
        }
        List<TabGroupChatActivityReward> activityRewards =
                groupChatActivityRewardMapper.queryByChatActivityIds(Lists.newArrayList(chatActivityId));
        return activityRewards;
    }

    @Override
    public Map<Long, List<TabGroupChatActivityReward>> listByActivityIdList(List<Long> chatActivityIdList) {
        if (CollectionUtil.isEmpty(chatActivityIdList)) {
            return new HashMap<>();
        }
        List<TabGroupChatActivityReward> list = groupChatActivityRewardMapper.queryByChatActivityIds(Lists.newArrayList(chatActivityIdList));
        Map<Long, List<TabGroupChatActivityReward>> map = list.stream().collect(Collectors.groupingBy(TabGroupChatActivityReward::getChatActivityId));
        return map;
    }

    @Override
    public void saveActivityReward(Long chatActivityId, List<TabGroupChatActivityReward> rewardList) {
        List<TabGroupChatActivityReward> oldRewardList = listByActivityId(chatActivityId);
        Map<GroupChatActivityRewardBO, TabGroupChatActivityReward> oldRewardMap = oldRewardList.stream().collect(
                Collectors.toMap(x ->
                        new GroupChatActivityRewardBO(x.getChatActivityId(), x.getInviteLevel(), x.getRewardInvite(), x.getRewardType()), v -> v));
        rewardList.forEach(rule->{
            GroupChatActivityRewardBO ruleKey = new GroupChatActivityRewardBO(rule.getChatActivityId(), rule.getInviteLevel(), rule.getRewardInvite(), rule.getRewardType());
            TabGroupChatActivityReward oldRule = oldRewardMap.get(ruleKey);
            if (oldRule != null) {
                oldRewardMap.remove(ruleKey);
                rule.setChatActivityRewardId(oldRule.getChatActivityRewardId());
            }
            Long chatActivityRewardId = rule.getChatActivityRewardId();
            if (chatActivityRewardId == null) {
                rule.setChatActivityRewardId(UniqueIdUtils.uniqueLong());
                groupChatActivityRewardMapper.insert(rule);
            }else {
                groupChatActivityRewardMapper.update(rule);
            }
        });
        oldRewardMap.forEach((k, v) -> groupChatActivityRewardMapper.deleteById(v.getChatActivityRewardId()));
    }
}
