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.context.chat.BusinessManager;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatActivityJoinMapper;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivity;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityJoin;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityJoinService;
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;

/**
 * 社群邀请有礼入群规则表(TabHaobanGroupChatActivityJoin)表服务实现类
 *
 * @author makejava
 * @since 2024-09-27 16:09:56
 */
@Service("groupChatActivityJoinService")
public class GroupChatActivityJoinServiceImpl implements GroupChatActivityJoinService {
    @Autowired
    private GroupChatActivityJoinMapper groupChatActivityJoinMapper;
    @Autowired
    private BusinessManager businessManager;

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

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

    @Override
    public void saveActivityJoin(TabGroupChatActivity activity, List<TabGroupChatActivityJoin> joinList) {
        Long chatActivityId = activity.getChatActivityId();
        List<TabGroupChatActivityJoin> oldJoinList = listByActivityId(chatActivityId);
        Map<Long, TabGroupChatActivityJoin> oldRewardMap = oldJoinList.stream().collect(
                Collectors.toMap(TabGroupChatActivityJoin::getChatActivityJoinId, v -> v));
        //先删除
        businessManager.dealMemberTagByActivity(activity,null,true);
        joinList.forEach(rule->{
            Long chatActivityJoinId = rule.getChatActivityJoinId();
            if (chatActivityJoinId != null) {
                oldRewardMap.remove(chatActivityJoinId);
            }
            if (chatActivityJoinId == null) {
                rule.setChatActivityJoinId(UniqueIdUtils.uniqueLong());
                groupChatActivityJoinMapper.insert(rule);
            }else {
                groupChatActivityJoinMapper.update(rule);
            }
            businessManager.dealMemberTagByActivity(activity,rule,false);
        });
        oldRewardMap.forEach((k, v) -> groupChatActivityJoinMapper.deleteById(v.getChatActivityJoinId()));

    }
}
