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

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.dto.EnterpriseDTO;
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.GroupChatActivityInviteLogDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityRewardLogDTO;
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.GroupChatActivityLogApiService;
import com.gic.haoban.manage.web.dto.HmOverviewDTO;
import com.gic.haoban.manage.web.qo.chat.GroupChatActivityDataInviterDetailReq;
import com.gic.haoban.manage.web.qo.chat.GroupChatActivityDataOverviewReq;
import com.gic.haoban.manage.web.qo.chat.GroupChatActivityDataRewardLevelReq;
import com.gic.haoban.manage.web.vo.chat.*;
import com.gic.member.api.dto.MemberDocDTO;
import com.gic.member.api.service.MemberService;
import com.gic.web.common.utils.DataApiUtils;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
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.List;
import java.util.Map;
import java.util.Objects;
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 EnterpriseService enterpriseService;

    @Resource
    private MemberService memberService;

    /**
     * 数据概览
     *
     * @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);
        List<GroupChatDataOverviewVO> resList = DataApiUtils.parseDataList(res, HmOverviewDTO.class);
        GroupChatDataOverviewVO groupChatDataOverviewVO = CollectionUtils.isEmpty(resList) ? null : resList.get(0);
        return RestResponse.successResult(groupChatDataOverviewVO);
    }

    /**
     * 阶梯奖励数据
     *
     * @param rewardLevelReq
     * @return
     */
    @RequestMapping("reward_level")
    @ResponseBody
    public RestResponse<GroupChatDataRewardLevelVO> rewardLevel(@RequestBody GroupChatActivityDataRewardLevelReq rewardLevelReq) {
        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);
        List<GroupChatDataRewardLevelVO> resList = DataApiUtils.parseDataList(res, HmOverviewDTO.class);
        GroupChatDataRewardLevelVO groupChatDataOverviewVO = CollectionUtils.isEmpty(resList) ? null : resList.get(0);
        return RestResponse.successResult(groupChatDataOverviewVO);
    }

    /**
     * 参与客户明细
     *
     * @param inviterDetailReq
     * @return
     */
    @RequestMapping("inviter_detail")
    @ResponseBody
    public RestResponse<GroupChatDataInviterDetailVO> joinDetail(@RequestBody GroupChatActivityDataInviterDetailReq inviterDetailReq) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        inviterDetailReq.setEnterpriseId(user.getEnterpriseId());
        Map<String, Object> res = DataApiUtils.http(JSON.toJSONString(inviterDetailReq), StatisticsApolloKeyConstant.data_mkt_group_chat_data_inviter_detail);
        List<GroupChatDataInviterDetailVO> resList = DataApiUtils.parseDataList(res, HmOverviewDTO.class);
        GroupChatDataInviterDetailVO groupChatDataOverviewVO = CollectionUtils.isEmpty(resList) ? null : resList.get(0);
        return RestResponse.successResult(groupChatDataOverviewVO);
    }


    /**
     * 邀请明细
     *
     * @param inviteLogQDTO
     * @return
     */
    @RequestMapping("inviter_detail")
    @ResponseBody
    public RestResponse<Page<GroupChatActivityInviteVO>> inviterDetail(@RequestBody GroupChatActivityInviteLogQDTO inviteLogQDTO) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        inviteLogQDTO.setEnterpriseId(user.getEnterpriseId());
        inviteLogQDTO.setSortFiled("create_time");
        inviteLogQDTO.setSortType("desc");
        ServiceResponse<Page<GroupChatActivityInviteLogDTO>> listServiceResponse = groupChatActivityLogApiService.queryInviteLogDetails(inviteLogQDTO);
        if (!listServiceResponse.isSuccess()) {
            return RestResponse.failure(listServiceResponse.getCode(), listServiceResponse.getMessage());
        }
        Page<GroupChatActivityInviteVO> page = new Page<>(inviteLogQDTO.getPageNum(), inviteLogQDTO.getPageSize(), 0, 0);
        Page<GroupChatActivityInviteLogDTO> groupChatActivityInviteLogPage = listServiceResponse.getResult();

        if (CollectionUtils.isEmpty(groupChatActivityInviteLogPage.getResult())) {
            return RestResponse.successResult(page);
        }
        Map<String, MemberDocDTO> memberDocDTOMap = Maps.newHashMap();
        List<String> inviteeMemberIdList = groupChatActivityInviteLogPage.getResult().stream()
                .map(GroupChatActivityInviteLogDTO::getInviteeMemberId).filter(Objects::nonNull).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(inviteeMemberIdList)) {
            EnterpriseDTO enterpriseDTO = enterpriseService.getEnterpriseById(inviteLogQDTO.getEnterpriseId());
            List<MemberDocDTO> memberDocDTOList = memberService.getMembersFromSearchIndex(enterpriseDTO.getEnterpriseMicroSignal(), inviteeMemberIdList);
            memberDocDTOMap = memberDocDTOList.stream().collect(Collectors.toMap(MemberDocDTO::getMemberId, memberDocDTO -> memberDocDTO));
        }

        Map<String, MemberDocDTO> finalMemberDocDTOMap = memberDocDTOMap;
        List<GroupChatActivityInviteVO> groupChatActivityInviteVOList = groupChatActivityInviteLogPage.getResult().stream().map(groupChatActivityInviteLogDTO -> {
            GroupChatActivityInviteVO groupChatActivityInviteVO = new GroupChatActivityInviteVO();
            groupChatActivityInviteVO.setNickName(groupChatActivityInviteLogDTO.getNickName());
            MemberDocDTO memberDocDTO = finalMemberDocDTOMap.get(groupChatActivityInviteLogDTO.getInviteeMemberId());
            if (Objects.nonNull(memberDocDTO)) {
                groupChatActivityInviteVO.setPhotoUrl(memberDocDTO.getPhotoUrl());
            }
            return groupChatActivityInviteVO;
        }).collect(Collectors.toList());
        page.setResult(groupChatActivityInviteVOList);
        page.setResult(groupChatActivityInviteVOList);
        page.setTotalCount(groupChatActivityInviteLogPage.getTotalCount());
        page.setTotalPage(groupChatActivityInviteLogPage.getTotalPage());
        return RestResponse.successResult(page);
    }

    /**
     * 奖励明细
     *
     * @param rewardLogQDTO
     * @return
     */
    @RequestMapping("reward_detail")
    @ResponseBody
    public RestResponse<Page<GroupChatActivityRewardVO>> rewardDetail(@RequestBody GroupChatActivityRewardLogQDTO rewardLogQDTO) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        rewardLogQDTO.setEnterpriseId(user.getEnterpriseId());

        ServiceResponse<Page<GroupChatActivityRewardLogDTO>> listServiceResponse = groupChatActivityLogApiService.queryRewardLogDetails(rewardLogQDTO);
        if (!listServiceResponse.isSuccess()) {
            return RestResponse.failure(listServiceResponse.getCode(), listServiceResponse.getMessage());
        }
        Page<GroupChatActivityRewardVO> page = new Page<>(rewardLogQDTO.getPageNum(), rewardLogQDTO.getPageSize(), 0, 0);
        Page<GroupChatActivityRewardLogDTO> groupChatActivityInviteLogPage = listServiceResponse.getResult();

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

        List<GroupChatActivityRewardVO> groupChatActivityInviteVOList = groupChatActivityInviteLogPage.getResult().stream().map(groupChatActivityRewardLogDTO -> {
            GroupChatActivityRewardVO groupChatActivityRewardVO = new GroupChatActivityRewardVO();
            BeanUtils.copyProperties(groupChatActivityRewardLogDTO, groupChatActivityRewardVO);
            if (CollectionUtils.isNotEmpty(groupChatActivityRewardLogDTO.getRewardDetailLogDTOList())) {
                List<GroupChatActivityRewardDetailVO> groupChatActivityRewardDetailVOS = groupChatActivityRewardLogDTO.getRewardDetailLogDTOList().stream().map(groupChatActivityRewardDetailLogDTO -> {
                    GroupChatActivityRewardDetailVO groupChatActivityRewardDetailVO = new GroupChatActivityRewardDetailVO();
                    BeanUtils.copyProperties(groupChatActivityRewardDetailLogDTO, groupChatActivityRewardDetailVO);
                    return groupChatActivityRewardDetailVO;
                }).collect(Collectors.toList());
                groupChatActivityRewardVO.setGroupChatActivityRewardDetailVOList(groupChatActivityRewardDetailVOS);
            }
            return groupChatActivityRewardVO;
        }).collect(Collectors.toList());
        page.setResult(groupChatActivityInviteVOList);
        page.setResult(groupChatActivityInviteVOList);
        page.setTotalCount(groupChatActivityInviteLogPage.getTotalCount());
        page.setTotalPage(groupChatActivityInviteLogPage.getTotalPage());
        return RestResponse.successResult(page);

    }
}
