package com.gic.haoban.manage.api.dto.chat;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.google.common.base.Objects;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 社群邀请有礼奖励规则表(TabHaobanGroupChatActivityReward)实体类
 *
 * @author makejava
 * @since 2024-09-27 16:09:56
 */
public class GroupChatActivityRewardBaseDTO implements Serializable {
    private static final long serialVersionUID = -78111537762472232L;
    /**
     * 社群活动奖励规则id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long chatActivityRewardId;
    /**
     * 内容活动id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long chatActivityId;
    /**
     * 奖励邀请人数
     */
    private Integer inviteNumber;
    /**
     * 奖励阶梯 固定奖励默认1
     */
    private Integer inviteLevel;
    /**
     * 奖励人类型 1邀请人2受邀人
     */
    private Integer rewardInvite;
    /**
     * 奖励类型 1积分2成长值3卡券
     */
    private Integer rewardType;
    /**
     * 最多奖励数量
     */
    private Integer limitReward;
    /**
     * 奖励内容 积分值 成长值 卡券id（逗号分隔）
     */
    private String rewardValue;
    /**
     * 奖励名称 积分名称 成长值名称 卡券名称（逗号分隔）
     */
    private String rewardName;

    public Long getChatActivityRewardId() {
        return chatActivityRewardId;
    }

    public void setChatActivityRewardId(Long chatActivityRewardId) {
        this.chatActivityRewardId = chatActivityRewardId;
    }

    public Long getChatActivityId() {
        return chatActivityId;
    }

    public void setChatActivityId(Long chatActivityId) {
        this.chatActivityId = chatActivityId;
    }

    public Integer getInviteNumber() {
        return inviteNumber;
    }

    public void setInviteNumber(Integer inviteNumber) {
        this.inviteNumber = inviteNumber;
    }

    public Integer getInviteLevel() {
        return inviteLevel;
    }

    public void setInviteLevel(Integer inviteLevel) {
        this.inviteLevel = inviteLevel;
    }

    public Integer getRewardInvite() {
        return rewardInvite;
    }

    public void setRewardInvite(Integer rewardInvite) {
        this.rewardInvite = rewardInvite;
    }

    public Integer getRewardType() {
        return rewardType;
    }

    public void setRewardType(Integer rewardType) {
        this.rewardType = rewardType;
    }

    public Integer getLimitReward() {
        return limitReward;
    }

    public void setLimitReward(Integer limitReward) {
        this.limitReward = limitReward;
    }

    public String getRewardValue() {
        return rewardValue;
    }

    public void setRewardValue(String rewardValue) {
        this.rewardValue = rewardValue;
    }

    public String getRewardName() {
        return rewardName;
    }

    public void setRewardName(String rewardName) {
        this.rewardName = rewardName;
    }

    public static List<GroupChatActivityRewardBaseDTO> mapConvert(Map<Integer, List<GroupChatActivityRewardDTO>> rewardMap) {
        List<GroupChatActivityRewardBaseDTO> list = new ArrayList<>();
        rewardMap.forEach((k,v)->{
            for (GroupChatActivityRewardDTO rewardDTO : v) {
                List<GroupChatActivityRewardDetailDTO> detailList = rewardDTO.getDetailList();
                for (GroupChatActivityRewardDetailDTO detailDTO : detailList) {
                    GroupChatActivityRewardBaseDTO rewardBaseDTO = new GroupChatActivityRewardBaseDTO();
                    rewardBaseDTO.setChatActivityRewardId(detailDTO.getChatActivityRewardId());
                    rewardBaseDTO.setRewardInvite(k);
                    rewardBaseDTO.setChatActivityId(rewardDTO.getChatActivityId());
                    rewardBaseDTO.setInviteNumber(rewardDTO.getInviteNumber());
                    rewardBaseDTO.setInviteLevel(rewardDTO.getInviteLevel());
                    rewardBaseDTO.setLimitReward(rewardDTO.getLimitReward());
                    rewardBaseDTO.setRewardType(detailDTO.getRewardType());
                    rewardBaseDTO.setRewardValue(detailDTO.getRewardValue());
                    rewardBaseDTO.setRewardName(detailDTO.getRewardName());
                    list.add(rewardBaseDTO);
                }
            }
        });
        return list;
    }

    public static Map<Integer, List<GroupChatActivityRewardDTO>> reverseMapConvert(List<GroupChatActivityRewardBaseDTO> baseDTOList) {
        Map<Integer, List<GroupChatActivityRewardDTO>> rewardMap = new HashMap<>();

        // 按 rewardInvite 分组
        Map<Integer, Map<Long, GroupChatActivityRewardDTO>> groupedMap = new HashMap<>();

        for (GroupChatActivityRewardBaseDTO baseDTO : baseDTOList) {
            int rewardInvite = baseDTO.getRewardInvite();
            Long chatActivityId = baseDTO.getChatActivityId();

            groupedMap.putIfAbsent(rewardInvite, new HashMap<>());
            Map<Long, GroupChatActivityRewardDTO> rewardDTOMap = groupedMap.get(rewardInvite);

            GroupChatActivityRewardDTO rewardDTO = rewardDTOMap.computeIfAbsent(chatActivityId, id -> {
                GroupChatActivityRewardDTO newRewardDTO = new GroupChatActivityRewardDTO();
                newRewardDTO.setChatActivityId(baseDTO.getChatActivityId());
                newRewardDTO.setInviteNumber(baseDTO.getInviteNumber());
                newRewardDTO.setInviteLevel(baseDTO.getInviteLevel());
                newRewardDTO.setLimitReward(baseDTO.getLimitReward());
                newRewardDTO.setDetailList(new ArrayList<>());
                return newRewardDTO;
            });

            GroupChatActivityRewardDetailDTO detailDTO = new GroupChatActivityRewardDetailDTO();
            detailDTO.setChatActivityRewardId(baseDTO.getChatActivityRewardId());
            detailDTO.setRewardType(baseDTO.getRewardType());
            detailDTO.setRewardValue(baseDTO.getRewardValue());
            detailDTO.setRewardName(baseDTO.getRewardName());
            rewardDTO.getDetailList().add(detailDTO);
        }

        groupedMap.forEach((rewardInvite, rewardDTOMap) -> {
            List<GroupChatActivityRewardDTO> rewardDTOList = new ArrayList<>(rewardDTOMap.values());
            rewardMap.put(rewardInvite, rewardDTOList);
        });

        return rewardMap;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GroupChatActivityRewardBaseDTO that = (GroupChatActivityRewardBaseDTO) o;
        return Objects.equal(chatActivityId, that.chatActivityId) && Objects.equal(inviteNumber, that.inviteNumber) && Objects.equal(inviteLevel, that.inviteLevel) && Objects.equal(rewardInvite, that.rewardInvite) && Objects.equal(rewardType, that.rewardType) && Objects.equal(limitReward, that.limitReward) && Objects.equal(rewardValue, that.rewardValue);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(chatActivityId, inviteNumber, inviteLevel, rewardInvite, rewardType, limitReward, rewardValue);
    }
}

