package com.gic.haoban.manage.web.controller.chat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.JSONResponse;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.AuthorizedUser;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.api.constants.StatisticsApolloKeyConstant;
import com.gic.haoban.manage.api.dto.chat.*;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityInviteLogQDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityRewardLogQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatActivityApiService;
import com.gic.haoban.manage.api.service.chat.GroupChatActivityLogApiService;
import com.gic.haoban.manage.api.service.chat.GroupChatApiService;
import com.gic.haoban.manage.web.handle.GroupChatActivityInviterHandle;
import com.gic.haoban.manage.web.handle.GroupChatActivityJoinHandle;
import com.gic.haoban.manage.web.handle.GroupChatActivityRewardHandle;
import com.gic.haoban.manage.web.qo.chat.*;
import com.gic.haoban.manage.web.vo.chat.*;
import com.gic.member.api.constant.RewardResultTypeEnum;
import com.gic.member.api.dto.MemberDTO;
import com.gic.member.api.service.MemberService;
import com.gic.qcloud.BucketNameEnum;
import com.gic.web.common.download.DownloadTask;
import com.gic.web.common.download.DownloadTaskUtil;
import com.gic.web.common.download.constants.TaskTypeEnum;
import com.gic.web.common.utils.DataApiUtils;
import com.gic.web.common.utils.SessionContextUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 社群邀请有礼数据
 *
 * @author shangfeng
 * @date 2024-10-10 15:56:56
 */
@RestController
@RequestMapping("/group-chat-activity-data")
public class GroupChatActivityDataController {

    @Resource
    private GroupChatActivityLogApiService groupChatActivityLogApiService;

    @Resource
    private GroupChatActivityApiService groupChatActivityApiService;

    @Resource
    private GroupChatApiService groupChatApiService;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private MemberService memberService;

    @Resource
    private GroupChatActivityJoinHandle groupChatActivityJoinHandle;

    @Resource
    private GroupChatActivityInviterHandle groupChatActivityInviterHandle;

    @Resource
    private GroupChatActivityRewardHandle groupChatActivityRewardHandle;


    /**
     * 列表统计数据
     *
     * @param joinDataReq
     * @return
     */
    @RequestMapping("get-join-data-list")
    @ResponseBody
    public RestResponse<List<GroupChatActivityJoinDataVO>> getJoinDataList(@RequestBody GroupChatActivityJoinDataReq joinDataReq) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();

        GroupChatActivityDataOverviewReq overviewReq = new GroupChatActivityDataOverviewReq();
        overviewReq.setEnterpriseId(user.getEnterpriseId());
        overviewReq.setChatActivityId(StringUtils.join(joinDataReq.getChatActivityIdList(), ","));
        overviewReq.setPageSize(joinDataReq.getChatActivityIdList().size());

        Map<String, Object> res = DataApiUtils.http(JSON.toJSONString(overviewReq), StatisticsApolloKeyConstant.data_mkt_group_chat_data_overview);
        Map<String, GroupChatDataOverviewVO> map = Maps.newHashMap();
        List<GroupChatDataOverviewVO> list = DataApiUtils.parsePageDataList(res, GroupChatDataOverviewVO.class);

        if (CollectionUtils.isNotEmpty(list)) {
            map = list.stream().collect(Collectors.toMap(GroupChatDataOverviewVO::getChatActivityId, groupChatDataOverviewVO -> groupChatDataOverviewVO));
        }
        Map<String, GroupChatDataOverviewVO> finalMap = map;
        List<GroupChatActivityJoinDataVO> joinDataVOList = joinDataReq.getChatActivityIdList().stream().map(chatActivityId -> {
            GroupChatActivityJoinDataVO groupChatActivityJoinDataVO = new GroupChatActivityJoinDataVO();
            groupChatActivityJoinDataVO.setChatActivityId(chatActivityId);
            GroupChatDataOverviewVO groupChatDataOverviewVO = finalMap.get(chatActivityId.toString());
            if (Objects.nonNull(groupChatDataOverviewVO)) {
                if (Objects.nonNull(groupChatDataOverviewVO.getInviterNum())) {
                    groupChatActivityJoinDataVO.setInviterNum(groupChatDataOverviewVO.getInviterNum());
                }
                if (Objects.nonNull(groupChatDataOverviewVO.getInviteeNum())) {
                    groupChatActivityJoinDataVO.setInviteeNum(groupChatDataOverviewVO.getInviteeNum());
                }
                if (Objects.nonNull(groupChatDataOverviewVO.getEffectiveInviteeNum())) {
                    groupChatActivityJoinDataVO.setEffectiveInviteeNum(groupChatDataOverviewVO.getEffectiveInviteeNum());
                }
            }
            return groupChatActivityJoinDataVO;
        }).collect(Collectors.toList());

        return RestResponse.successResult(joinDataVOList);
    }

    /**
     * 数据概览
     *
     * @param overviewReq
     * @return
     */
    @RequestMapping("overview")
    @ResponseBody
    public RestResponse<GroupChatDataOverviewVO> overview(@RequestBody GroupChatActivityDataOverviewReq overviewReq) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        overviewReq.setEnterpriseId(user.getEnterpriseId());
        Map<String, Object> res = DataApiUtils.http(JSON.toJSONString(overviewReq), StatisticsApolloKeyConstant.data_mkt_group_chat_data_overview);
        Page page = DataApiUtils.getPageData(res);
        List<JSONObject> result = page.getResult();
        if (CollectionUtils.isEmpty(result)) {
            return RestResponse.successResult(null);
        }
        GroupChatDataOverviewVO groupChatDataOverviewVO = JSON.parseObject(JSON.toJSONString(result.get(0)), GroupChatDataOverviewVO.class);
        return RestResponse.successResult(groupChatDataOverviewVO);
    }

    /**
     * 阶梯奖励数据
     *
     * @param rewardLevelReq
     * @return
     */
    @RequestMapping("reward_level")
    @ResponseBody
    public RestResponse<List<GroupChatDataRewardLevelVO>> rewardLevel(@RequestBody GroupChatActivityDataRewardLevelReq rewardLevelReq) {
        List<GroupChatDataRewardLevelVO> groupChatDataOverviewVOList = Lists.newArrayList();
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        rewardLevelReq.setEnterpriseId(user.getEnterpriseId());
        Map<String, Object> res = DataApiUtils.http(JSON.toJSONString(rewardLevelReq), StatisticsApolloKeyConstant.data_mkt_group_chat_data_reward_level);
        Page page = DataApiUtils.getPageData(res);
        if(CollectionUtils.isEmpty(page.getResult())){
            return RestResponse.successResult(groupChatDataOverviewVOList);
        }
        groupChatDataOverviewVOList = JSON.parseArray(JSON.toJSONString(page.getResult()), GroupChatDataRewardLevelVO.class);
        return RestResponse.successResult(groupChatDataOverviewVOList);
    }

    /**
     * 参与客户明细
     *
     * @param inviterDetailReq
     * @return
     */
    @RequestMapping("join_detail")
    @ResponseBody
    public RestResponse<Page<GroupChatDataJoinDetailVO>> joinDetail(@RequestBody GroupChatActivityJoinDetailReq inviterDetailReq) {
        if (StringUtils.isEmpty(inviterDetailReq.getEnterpriseId())) {
            WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
            inviterDetailReq.setEnterpriseId(user.getEnterpriseId());
        }
        if(Objects.isNull(inviterDetailReq.getOrderByFields())){
            inviterDetailReq.setOrderByFields(3);
        }
        if(Objects.isNull(inviterDetailReq.getOrderByType())){
            inviterDetailReq.setOrderByType(2);
        }

        Map<String, Object> res = DataApiUtils.http(JSON.toJSONString(inviterDetailReq), StatisticsApolloKeyConstant.data_mkt_group_chat_data_inviter_detail);
        Page page = DataApiUtils.getPageData(res);
        if (CollectionUtils.isNotEmpty(page.getResult())) {
            List<GroupChatDataJoinDetailVO> groupChatDataJoinDetailVOS = JSON.parseArray(JSON.toJSONString(page.getResult()), GroupChatDataJoinDetailVO.class);
            List<String> memberIdList = groupChatDataJoinDetailVOS.stream().map(GroupChatDataJoinDetailVO::getInviterMemberId).collect(Collectors.toList());
            Map<String, MemberDTO> memberDTOMap = queryMemberByMemberIdList(memberIdList);

            groupChatDataJoinDetailVOS.forEach(groupChatDataJoinDetailVO -> {
                MemberDTO memberDTO = memberDTOMap.get(groupChatDataJoinDetailVO.getInviterMemberId());
                if (Objects.nonNull(memberDTO)) {
                    groupChatDataJoinDetailVO.setName(memberDTO.getMemberName());
                    groupChatDataJoinDetailVO.setNickName(memberDTO.getThirdNickname());
                    groupChatDataJoinDetailVO.setPhoneNumber(memberDTO.getPhoneNumber());
                    groupChatDataJoinDetailVO.setMemberDeleteFlag(0);
                } else {
                    groupChatDataJoinDetailVO.setMemberDeleteFlag(1);
                }
            });

            page.setResult(groupChatDataJoinDetailVOS);
        }
        return RestResponse.successResult(page);
    }

    /**
     * 参与客户明细导出
     *
     * @param inviterDetailReq
     * @return
     */
    @RequestMapping("join_detail_export")
    @ResponseBody
    public RestResponse<Object> joinDetailExport(@RequestBody GroupChatActivityJoinDetailReq inviterDetailReq) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        inviterDetailReq.setEnterpriseId(user.getEnterpriseId());

        AuthorizedUser loginUser = SessionContextUtils.getLoginUser();

        DownloadTask task = new DownloadTask();
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(inviterDetailReq));

        task.setTaskTypeEnum(TaskTypeEnum.GROUP_CHAT_ACTIVITY_JOIN);
        task.setDataType(inviterDetailReq.getDataType());
        task.setUser(loginUser);
        task.setTotalCount(joinDetail(inviterDetailReq).getResult().getTotalCount());

        //查询奖励配置
        GroupChatActivityDTO groupChatActivityDTO = getActivityDetail(inviterDetailReq.getEnterpriseId(), Long.parseLong(inviterDetailReq.getChatActivityId()));

        boolean isSettingIntegral = isSettingRewardType(groupChatActivityDTO,RewardResultTypeEnum.integral.getType());
        boolean isSettingGrowth = isSettingRewardType(groupChatActivityDTO,RewardResultTypeEnum.growth.getType());
        boolean isSettingCoupon = isSettingRewardType(groupChatActivityDTO,RewardResultTypeEnum.coupon.getType());

        List<String> fieldList = new ArrayList<>();
        List<String> indexList = new ArrayList<>();
        fieldList.add("name");
        indexList.add("邀请人姓名");

        fieldList.add("nickName");
        indexList.add("邀请人昵称");

        fieldList.add("phoneNumber");
        indexList.add("邀请人手机号");

        fieldList.add("inviteeNum");
        indexList.add("邀请进群人数");

        fieldList.add("effectiveInviteeNum");
        indexList.add("有效进群人数");

        fieldList.add("newMemberNum");
        indexList.add("新增会员数");

        if (isSettingIntegral) {
            fieldList.add("integralCnt");
            indexList.add("奖励积分");
        }

        if (isSettingGrowth) {
            fieldList.add("growCnt");
            indexList.add("奖励成长值");
        }

        if (isSettingCoupon) {
            fieldList.add("cardCnt");
            indexList.add("奖励卡券数");
        }

        task.setFileName("社群邀请有礼-" + groupChatActivityDTO.getActivityName() + "-参与客户");
        task.setChannelName("社群邀请有礼-" + groupChatActivityDTO.getActivityName() + "-参与客户");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());

        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(groupChatActivityJoinHandle);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);

        return RestResponse.successResult(downloadTask.getResult());
    }

    /**
     * 邀请明细
     *
     * @param inviteLogQDTO
     * @return
     */
    @RequestMapping("inviter_detail")
    @ResponseBody
    public RestResponse<Page<GroupChatActivityInviteLogVO>> inviterDetail(@RequestBody GroupChatActivityInviteLogReq inviteLogQDTO) {
        if (StringUtils.isEmpty(inviteLogQDTO.getEnterpriseId())) {
            WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
            inviteLogQDTO.setEnterpriseId(user.getEnterpriseId());
        }
        GroupChatActivityInviteLogQDTO activityInviteLogQDTO = new GroupChatActivityInviteLogQDTO();
        BeanUtils.copyProperties(inviteLogQDTO, activityInviteLogQDTO);
        activityInviteLogQDTO.setSortFiled("create_time");
        activityInviteLogQDTO.setSortType("desc");

        ServiceResponse<Page<GroupChatActivityInviteLogDTO>> listServiceResponse = groupChatActivityLogApiService.queryInviteLogDetails(activityInviteLogQDTO);
        if (!listServiceResponse.isSuccess()) {
            return RestResponse.failure(listServiceResponse.getCode(), listServiceResponse.getMessage());
        }
        Page<GroupChatActivityInviteLogVO> page = new Page<>(inviteLogQDTO.getPageNum(), inviteLogQDTO.getPageSize(), 0, 0);
        Page<GroupChatActivityInviteLogDTO> groupChatActivityInviteLogPage = listServiceResponse.getResult();

        if (CollectionUtils.isEmpty(groupChatActivityInviteLogPage.getResult())) {
            return RestResponse.successResult(page);
        }

        List<GroupChatActivityInviteLogVO> groupChatActivityInviteLogVOList = convertToInviterDetailVO(groupChatActivityInviteLogPage.getResult());
        page.setResult(groupChatActivityInviteLogVOList);
        page.setTotalCount(groupChatActivityInviteLogPage.getTotalCount());
        page.setTotalPage(groupChatActivityInviteLogPage.getTotalPage());
        return RestResponse.successResult(page);
    }

    /**
     * 邀请明细导出
     *
     * @param inviteLogQDTO
     * @return
     */
    @RequestMapping("inviter_detail_export")
    @ResponseBody
    public RestResponse<Object> inviterDetailExport(@RequestBody GroupChatActivityInviteLogReq inviteLogQDTO) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        inviteLogQDTO.setEnterpriseId(user.getEnterpriseId());

        AuthorizedUser loginUser = SessionContextUtils.getLoginUser();

        DownloadTask task = new DownloadTask();
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(inviteLogQDTO));

        task.setTaskTypeEnum(TaskTypeEnum.GROUP_CHAT_ACTIVITY_INVITER);
        task.setDataType(inviteLogQDTO.getDataType());
        task.setUser(loginUser);
        task.setTotalCount(inviterDetail(inviteLogQDTO).getResult().getTotalCount());

        //查询奖励配置
        GroupChatActivityDTO groupChatActivityDTO = getActivityDetail(inviteLogQDTO.getEnterpriseId(), inviteLogQDTO.getChatActivityId());


        List<String> fieldList = new ArrayList<>();
        List<String> indexList = new ArrayList<>();
        fieldList.add("name");
        indexList.add("邀请人姓名");

        fieldList.add("nickName");
        indexList.add("邀请人昵称");

        fieldList.add("phoneNumber");
        indexList.add("邀请人手机号");

        fieldList.add("inviteeName");
        indexList.add("受邀人姓名");

        fieldList.add("inviteeNickName");
        indexList.add("受邀人昵称");

        fieldList.add("inviteePhoneNumber");
        indexList.add("受邀人手机号");

        fieldList.add("inviteTypeDesc");
        indexList.add("是否有效邀请");

        fieldList.add("invalidInviteTypeDesc");
        indexList.add("无效邀请说明");

        fieldList.add("addMemberTypeDesc");
        indexList.add("新增会员");

        fieldList.add("groupChatName");
        indexList.add("加入群聊名称");

        fieldList.add("groupChatId");
        indexList.add("加入群聊ID");

        fieldList.add("joinGroupChatTimeDesc");
        indexList.add("入群时间");

        task.setFileName("社群邀请有礼-" + groupChatActivityDTO.getActivityName() + "-邀请明细");
        task.setChannelName("社群邀请有礼-" + groupChatActivityDTO.getActivityName() + "-邀请明细");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());

        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(groupChatActivityInviterHandle);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);

        return RestResponse.successResult(downloadTask.getResult());

    }

    /**
     * 奖励明细
     *
     * @param rewardLogReq
     * @return
     */
    @RequestMapping("reward_detail")
    @ResponseBody
    public RestResponse<Page<GroupChatActivityRewardLogVO>> rewardDetail(@RequestBody GroupChatActivityRewardLogReq rewardLogReq) {
        if (StringUtils.isEmpty(rewardLogReq.getEnterpriseId())) {
            WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
            rewardLogReq.setEnterpriseId(user.getEnterpriseId());
        }
        GroupChatActivityRewardLogQDTO rewardLogQDTO = new GroupChatActivityRewardLogQDTO();
        BeanUtils.copyProperties(rewardLogReq, rewardLogQDTO);
        ServiceResponse<Page<GroupChatActivityRewardLogDTO>> listServiceResponse = groupChatActivityLogApiService.queryRewardLogDetails(rewardLogQDTO);
        if (!listServiceResponse.isSuccess()) {
            return RestResponse.failure(listServiceResponse.getCode(), listServiceResponse.getMessage());
        }
        Page<GroupChatActivityRewardLogVO> page = new Page<>(rewardLogQDTO.getPageNum(), rewardLogQDTO.getPageSize(), 0, 0);
        Page<GroupChatActivityRewardLogDTO> groupChatActivityInviteLogPage = listServiceResponse.getResult();

        if (CollectionUtils.isEmpty(groupChatActivityInviteLogPage.getResult())) {
            return RestResponse.successResult(page);
        }

        List<GroupChatActivityRewardLogVO> groupChatActivityInviteVOList = convertToRewardDetailVO(groupChatActivityInviteLogPage.getResult());
        page.setResult(groupChatActivityInviteVOList);
        page.setResult(groupChatActivityInviteVOList);
        page.setTotalCount(groupChatActivityInviteLogPage.getTotalCount());
        page.setTotalPage(groupChatActivityInviteLogPage.getTotalPage());
        return RestResponse.successResult(page);

    }

    /**
     * 奖励明细导出
     *
     * @param rewardLogReq
     * @return
     */
    @RequestMapping("reward_detail_export")
    @ResponseBody
    public RestResponse<Object> rewardDetailExport(@RequestBody GroupChatActivityRewardLogReq rewardLogReq) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        rewardLogReq.setEnterpriseId(user.getEnterpriseId());

        AuthorizedUser loginUser = SessionContextUtils.getLoginUser();

        DownloadTask task = new DownloadTask();
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(rewardLogReq));

        task.setTaskTypeEnum(TaskTypeEnum.GROUP_CHAT_ACTIVITY_REWARD);
        task.setDataType(rewardLogReq.getDataType());
        task.setUser(loginUser);
        task.setTotalCount(rewardDetail(rewardLogReq).getResult().getTotalCount());

        //查询奖励配置
        GroupChatActivityDTO groupChatActivityDTO = getActivityDetail(rewardLogReq.getEnterpriseId(), rewardLogReq.getChatActivityId());

        boolean isSettingIntegral = isSettingRewardType(groupChatActivityDTO,RewardResultTypeEnum.integral.getType());
        boolean isSettingGrowth = isSettingRewardType(groupChatActivityDTO,RewardResultTypeEnum.growth.getType());
        boolean isSettingCoupon = isSettingRewardType(groupChatActivityDTO,RewardResultTypeEnum.coupon.getType());

        List<String> fieldList = new ArrayList<>();
        List<String> indexList = new ArrayList<>();
        fieldList.add("name");
        indexList.add("客户姓名");

        fieldList.add("nickName");
        indexList.add("客户昵称");

        fieldList.add("phoneNumber");
        indexList.add("客户手机号");

        fieldList.add("rewardInviteType");
        indexList.add("奖励类型");

        fieldList.add("inviteLevelType");
        indexList.add("奖励说明");

        if (isSettingIntegral) {
            fieldList.add("integral");
            indexList.add("奖励积分");
        }

        if (isSettingGrowth) {
            fieldList.add("growth");
            indexList.add("奖励成长值");
        }

        if (isSettingCoupon) {
            fieldList.add("cardInfo");
            indexList.add("奖励卡券");
        }

        fieldList.add("rewardTimeDesc");
        indexList.add("发放时间");

        task.setFileName("社群邀请有礼-" + groupChatActivityDTO.getActivityName() + "-奖励明细");
        task.setChannelName("社群邀请有礼-" + groupChatActivityDTO.getActivityName() + "-奖励明细");
        task.setBucketName(BucketNameEnum.REPORT_50000.getName());

        jsonObject.put("indexList", indexList);
        jsonObject.put("fieldList", fieldList);
        task.setSearchDataParams(jsonObject.toJSONString());
        task.setHandler(groupChatActivityRewardHandle);
        JSONResponse downloadTask = DownloadTaskUtil.createDownloadTask(task);

        return RestResponse.successResult(downloadTask.getResult());

    }

    /**
     * 奖励明细数据转换
     *
     * @param groupChatActivityRewardLogDTOS
     * @return
     */
    private List<GroupChatActivityRewardLogVO> convertToRewardDetailVO(List<GroupChatActivityRewardLogDTO> groupChatActivityRewardLogDTOS) {
        List<String> memberIds = groupChatActivityRewardLogDTOS.stream().map(GroupChatActivityRewardLogDTO::getMemberId).collect(Collectors.toList());
        Map<String, MemberDTO> memberDTOMap = queryMemberByMemberIdList(memberIds);

        return groupChatActivityRewardLogDTOS.stream().map(groupChatActivityRewardLogDTO -> {
            GroupChatActivityRewardLogVO groupChatActivityRewardLogVO = new GroupChatActivityRewardLogVO();
            BeanUtils.copyProperties(groupChatActivityRewardLogDTO, groupChatActivityRewardLogVO);
            MemberDTO memberDTO = memberDTOMap.get(groupChatActivityRewardLogDTO.getMemberId());
            if (Objects.nonNull(memberDTO)) {
                groupChatActivityRewardLogVO.setName(memberDTO.getMemberName());
                groupChatActivityRewardLogVO.setNickName(memberDTO.getThirdNickname());
                groupChatActivityRewardLogVO.setPhoneNumber(memberDTO.getPhoneNumber());
                groupChatActivityRewardLogVO.setMemberDeleteFlag(0);
            } else {
                groupChatActivityRewardLogVO.setMemberDeleteFlag(1);
            }
            if (CollectionUtils.isNotEmpty(groupChatActivityRewardLogDTO.getRewardDetailLogDTOList())) {
                int integral = 0;
                int growth = 0;
                List<String> cardList = Lists.newArrayList();
                List<GroupChatActivityRewardLogVO.CouponInfo> couponInfoList = Lists.newArrayList();
                for (GroupChatActivityRewardDetailLogDTO groupChatActivityRewardDetailLogDTO : groupChatActivityRewardLogDTO.getRewardDetailLogDTOList()) {
                    if (groupChatActivityRewardDetailLogDTO.getRewardType().equals(RewardResultTypeEnum.integral.getType())) {
                        integral += Integer.parseInt(groupChatActivityRewardDetailLogDTO.getRewardValue());
                    }
                    if (groupChatActivityRewardDetailLogDTO.getRewardType().equals(RewardResultTypeEnum.growth.getType())) {
                        growth += Integer.parseInt(groupChatActivityRewardDetailLogDTO.getRewardValue());
                    }
                    if (groupChatActivityRewardDetailLogDTO.getRewardType().equals(RewardResultTypeEnum.coupon.getType())) {
                        String card = groupChatActivityRewardDetailLogDTO.getRewardName() + "(";
                        if (groupChatActivityRewardDetailLogDTO.getSuccessFlag() == 1) {
                            card += groupChatActivityRewardDetailLogDTO.getRewardValue() + ")";
                        } else {
                            card += groupChatActivityRewardDetailLogDTO.getRewardRemark() + ")";
                        }
                        cardList.add(card);
                        GroupChatActivityRewardLogVO.CouponInfo couponInfo = new GroupChatActivityRewardLogVO.CouponInfo();
                        couponInfo.setCouponCardId(groupChatActivityRewardDetailLogDTO.getRewardValue());
                        couponInfo.setCouponName(groupChatActivityRewardDetailLogDTO.getRewardName());
                        couponInfo.setSuccessFlag(groupChatActivityRewardDetailLogDTO.getSuccessFlag());
                        couponInfo.setFailReason(groupChatActivityRewardDetailLogDTO.getRewardRemark());
                        couponInfoList.add(couponInfo);
                    }
                }
                groupChatActivityRewardLogVO.setIntegral(integral);
                groupChatActivityRewardLogVO.setGrowth(growth);
                groupChatActivityRewardLogVO.setCardInfo(StringUtils.join(cardList, "、"));
                groupChatActivityRewardLogVO.setCouponInfoList(couponInfoList);
            }
            return groupChatActivityRewardLogVO;
        }).collect(Collectors.toList());
    }

    /**
     * 邀请明细数据转换
     *
     * @param groupChatActivityInviteLogDTOS
     * @return
     */
    private List<GroupChatActivityInviteLogVO> convertToInviterDetailVO(List<GroupChatActivityInviteLogDTO> groupChatActivityInviteLogDTOS) {
        //会员信息
        List<String> memberIds = Lists.newArrayList();
        groupChatActivityInviteLogDTOS.forEach(groupChatActivityInviteLogDTO -> {
            if (StringUtils.isNotEmpty(groupChatActivityInviteLogDTO.getInviterMemberId())) {
                memberIds.add(groupChatActivityInviteLogDTO.getInviterMemberId());
            }
            if (StringUtils.isNotEmpty(groupChatActivityInviteLogDTO.getInviteeMemberId())) {
                memberIds.add(groupChatActivityInviteLogDTO.getInviteeMemberId());
            }
        });
        Map<String, MemberDTO> memberDTOMap = queryMemberByMemberIdList(memberIds);

        //企微群信息
        Map<Long, GroupChatDTO> groupChatDTOMap = Maps.newHashMap();
        List<Long> groupChatIdList = groupChatActivityInviteLogDTOS.stream().map(GroupChatActivityInviteLogDTO::getGroupChatId).distinct().collect(Collectors.toList());
        ServiceResponse<List<GroupChatDTO>> groupChatDTOResponse = groupChatApiService.listChatByIdList(groupChatActivityInviteLogDTOS.get(0).getWxEnterpriseId(), groupChatIdList);
        if (CollectionUtils.isNotEmpty(groupChatDTOResponse.getResult())) {
            groupChatDTOMap = groupChatDTOResponse.getResult().stream()
                    .collect(Collectors.toMap(GroupChatDTO::getGroupChatId, groupChatDTO -> groupChatDTO));
        }
        Map<Long, GroupChatDTO> finalGroupChatDTOMap = groupChatDTOMap;

        return groupChatActivityInviteLogDTOS.stream().map(groupChatActivityInviteLogDTO -> {
            GroupChatActivityInviteLogVO groupChatActivityInviteLogVO = new GroupChatActivityInviteLogVO();
            BeanUtils.copyProperties(groupChatActivityInviteLogDTO, groupChatActivityInviteLogVO);
            MemberDTO inviterMemberDTO = memberDTOMap.get(groupChatActivityInviteLogDTO.getInviterMemberId());
            if (Objects.nonNull(inviterMemberDTO)) {
                groupChatActivityInviteLogVO.setName(inviterMemberDTO.getMemberName());
                groupChatActivityInviteLogVO.setNickName(inviterMemberDTO.getThirdNickname());
                groupChatActivityInviteLogVO.setPhoneNumber(inviterMemberDTO.getPhoneNumber());
                groupChatActivityInviteLogVO.setMemberDeleteFlag(0);
            } else {
                groupChatActivityInviteLogVO.setMemberDeleteFlag(1);
            }

            MemberDTO inviteeMemberDTO = memberDTOMap.get(groupChatActivityInviteLogDTO.getInviteeMemberId());
            if (Objects.nonNull(inviteeMemberDTO)) {
                groupChatActivityInviteLogVO.setInviteeName(inviteeMemberDTO.getMemberName());
                groupChatActivityInviteLogVO.setInviteeNickName(inviteeMemberDTO.getThirdNickname());
                groupChatActivityInviteLogVO.setInviteePhoneNumber(inviteeMemberDTO.getPhoneNumber());
            } else {
                groupChatActivityInviteLogVO.setInviteeName(groupChatActivityInviteLogDTO.getUserName());
                groupChatActivityInviteLogVO.setInviteeNickName(groupChatActivityInviteLogDTO.getNickName());
            }

            GroupChatDTO groupChatDTO = finalGroupChatDTOMap.get(groupChatActivityInviteLogDTO.getGroupChatId());
            if (Objects.nonNull(groupChatDTO)) {
                groupChatActivityInviteLogVO.setGroupChatName(groupChatDTO.getName());
            }
            return groupChatActivityInviteLogVO;
        }).collect(Collectors.toList());
    }

    /**
     * 查询会员信息
     *
     * @param memberIdList
     * @return
     */
    private Map<String, MemberDTO> queryMemberByMemberIdList(List<String> memberIdList) {
        Map<String, MemberDTO> memberDTOMap = Maps.newHashMap();
        if (CollectionUtils.isEmpty(memberIdList)) {
            return memberDTOMap;
        }
        //会员信息
        List<MemberDTO> memberDtoList = memberService.getMemberDtoList(memberIdList);
        //会员map
        memberDTOMap = memberDtoList.stream().collect(Collectors.toMap(MemberDTO::getMemberId, m -> m, (m1, m2) -> m2));

        return memberDTOMap;

    }

    private GroupChatActivityDTO getActivityDetail(String enterpriseId, Long chatActivityId) {
        ServiceResponse<GroupChatActivityDTO> serviceActivityDetail = groupChatActivityApiService
                .getActivityDetail(enterpriseId, chatActivityId);
        return serviceActivityDetail.getResult();
    }

    /**
     * 是否配置指定奖项、
     *
     * @param groupChatActivityDTO
     * @param rewardType
     * @return
     */
    private boolean isSettingRewardType(GroupChatActivityDTO groupChatActivityDTO,Integer rewardType){
        //获取所有奖励类型
        Set<Integer> rewardTypeList = Sets.newHashSet();
        Map<Integer, List<GroupChatActivityRewardDTO>> rewardMap = groupChatActivityDTO.getRewardMap();
        rewardMap.forEach((key, value) -> {
            value.forEach(groupChatActivityRewardDTO -> {
                groupChatActivityRewardDTO.getDetailList().forEach(groupChatActivityRewardDetailDTO -> {
                    rewardTypeList.add(groupChatActivityRewardDetailDTO.getRewardType());
                });
            });
        });
        return rewardTypeList.contains(rewardType);
    }
}
