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 com.google.common.collect.Lists;
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) {
        // 按 rewardInvite、chatActivityId 和 inviteLevel 进行分组
        Map<Integer, List<GroupChatActivityRewardDTO>> rewardMap = baseDTOList.stream()
                .collect(Collectors.groupingBy(
                        GroupChatActivityRewardBaseDTO::getRewardInvite,
                        Collectors.collectingAndThen(
                                Collectors.groupingBy(baseDTO -> Lists.newArrayList(baseDTO.getChatActivityId(), baseDTO.getInviteLevel())),
                                chatActivityGroups -> chatActivityGroups.values().stream()
                                        .map(group -> {
                                            GroupChatActivityRewardBaseDTO firstDTO = group.get(0);
                                            GroupChatActivityRewardDTO rewardDTO = new GroupChatActivityRewardDTO();
                                            rewardDTO.setRewardInvite(firstDTO.getRewardInvite());
                                            rewardDTO.setChatActivityId(firstDTO.getChatActivityId());
                                            rewardDTO.setInviteNumber(firstDTO.getInviteNumber());
                                            rewardDTO.setInviteLevel(firstDTO.getInviteLevel());
                                            rewardDTO.setLimitReward(firstDTO.getLimitReward());

                                            List<GroupChatActivityRewardDetailDTO> detailList = group.stream()
                                                    .sorted(Comparator.comparing(GroupChatActivityRewardBaseDTO::getRewardType))
                                                    .map(baseDTO -> {
                                                        GroupChatActivityRewardDetailDTO detailDTO = new GroupChatActivityRewardDetailDTO();
                                                        detailDTO.setChatActivityRewardId(baseDTO.getChatActivityRewardId());
                                                        detailDTO.setRewardType(baseDTO.getRewardType());
                                                        detailDTO.setRewardValue(baseDTO.getRewardValue());
                                                        detailDTO.setRewardName(baseDTO.getRewardName());
                                                        return detailDTO;
                                                    })
                                                    .collect(Collectors.toList());

                                            rewardDTO.setDetailList(detailList);
                                            return rewardDTO;
                                        })
                                        .sorted(Comparator.comparing(GroupChatActivityRewardDTO::getInviteLevel))
                                        .collect(Collectors.toList())
                        )
                ));

        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);
    }
}

