package com.gic.haoban.manage.web.handle;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.api.dto.security.DownloadReportDTO;
import com.gic.haoban.manage.api.enums.chat.ChatActivityInvalidInviteTypeEnum;
import com.gic.haoban.manage.web.controller.chat.GroupChatActivityDataController;
import com.gic.haoban.manage.web.qo.chat.GroupChatActivityInviteLogReq;
import com.gic.haoban.manage.web.vo.chat.GroupChatActivityInviteLogExportVO;
import com.gic.haoban.manage.web.vo.chat.GroupChatActivityInviteLogVO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author shangfeng
 * @date 2024-10-11 17:27:51
 */
@Component
public class GroupChatActivityInviterHandle extends DownloadHandlerAbstract<GroupChatActivityInviteLogExportVO> {

    private static Logger log = LogManager.getLogger(GroupChatActivityInviterHandle.class);

    @Resource
    private GroupChatActivityDataController groupChatActivityDataController;

    @Override
    public List<GroupChatActivityInviteLogExportVO> getData(Context context, String searchDataParams, Integer currentPage) {
        GroupChatActivityInviteLogReq inviteLogReq = JSON.parseObject(searchDataParams, GroupChatActivityInviteLogReq.class);
        inviteLogReq.setPageNum(currentPage);
        inviteLogReq.setPageSize(getPageSize());
        log.info("社群邀请有礼邀请明细数据导出，请求参数={}", JSON.toJSONString(inviteLogReq));
        RestResponse<Page<GroupChatActivityInviteLogVO>> dataPageVORestResponse = groupChatActivityDataController.inviterDetail(inviteLogReq);
        List<GroupChatActivityInviteLogVO> groupChatActivityInviteLogVOS = dataPageVORestResponse.getResult().getResult();
        if (CollectionUtils.isEmpty(groupChatActivityInviteLogVOS)) {
            return Lists.newArrayList();
        }

        List<GroupChatActivityInviteLogExportVO> groupChatActivityInviteLogExportVOS = Lists.newArrayList();
        for (GroupChatActivityInviteLogVO groupChatActivityInviteLogVO : groupChatActivityInviteLogVOS) {
            GroupChatActivityInviteLogExportVO groupChatActivityRewardLogExportVO = new GroupChatActivityInviteLogExportVO();
            BeanUtils.copyProperties(groupChatActivityInviteLogVO, groupChatActivityRewardLogExportVO);
            if(groupChatActivityInviteLogVO.getMemberDeleteFlag() == 1){
                groupChatActivityInviteLogVO.setName("账号已注销");
            }

            if (groupChatActivityInviteLogVO.getInviteType() == 0) {
                groupChatActivityRewardLogExportVO.setInviteTypeDesc("否");
            }else {
                groupChatActivityRewardLogExportVO.setInviteTypeDesc("是");
            }

            if (Objects.nonNull(groupChatActivityInviteLogVO.getInvalidInviteType())) {
                if(groupChatActivityInviteLogVO.getInvalidInviteType().equals(ChatActivityInvalidInviteTypeEnum.ONE.value)){
                    groupChatActivityRewardLogExportVO.setInvalidInviteTypeDesc("非首次参与活动受邀入群");
                }else if (groupChatActivityInviteLogVO.getInvalidInviteType().equals(ChatActivityInvalidInviteTypeEnum.TWO.value)){
                    groupChatActivityRewardLogExportVO.setInvalidInviteTypeDesc("入群前已处于品牌群聊内");
                }else if (groupChatActivityInviteLogVO.getInvalidInviteType().equals(ChatActivityInvalidInviteTypeEnum.THREE.value)){
                    groupChatActivityRewardLogExportVO.setInvalidInviteTypeDesc("非首次加入品牌群聊");
                }
            }

            if (Objects.nonNull(groupChatActivityInviteLogVO.getAddMemberType())) {
                if (groupChatActivityInviteLogVO.getAddMemberType() == 1) {
                    groupChatActivityRewardLogExportVO.setAddMemberTypeDesc("是");
                }else {
                    groupChatActivityRewardLogExportVO.setAddMemberTypeDesc("否");
                }
            }

            if(Objects.nonNull(groupChatActivityInviteLogVO.getCreateTime())){
                groupChatActivityRewardLogExportVO.setJoinGroupChatTimeDesc(DateUtil.formatDateTime(groupChatActivityInviteLogVO.getCreateTime()));
            }

            groupChatActivityInviteLogExportVOS.add(groupChatActivityRewardLogExportVO);
        }
        log.info("社群邀请有礼邀请明细数据导出={}", JSON.toJSONString(groupChatActivityInviteLogExportVOS));

        return groupChatActivityInviteLogExportVOS;
    }


    @Override
    public List<String> getColumnNames() {
        return Lists.newArrayList();
    }

    @Override
    public LinkedHashMap<String, List<String>> doubleColumnNames() {
        return null;
    }

    @Override
    public List<String> getColumns() {
        return Lists.newArrayList();
    }

    @Override
    public List<String> getColumns(Context context) {
        DownloadReportDTO downloadReportDTO = context.getDownloadReportDTO();
        String searchDataParams = downloadReportDTO.getSearchDataParams();
        JSONObject jsonObject = JSONObject.parseObject(searchDataParams);
        List<String> list = (List<String>) jsonObject.get("fieldList");
        return list;
    }

    @Override
    public List<String> getColumnNames(Context context) {
        DownloadReportDTO downloadReportDTO = context.getDownloadReportDTO();
        String searchDataParams = downloadReportDTO.getSearchDataParams();
        JSONObject jsonObject = JSONObject.parseObject(searchDataParams);
        List<String> list = (List<String>) jsonObject.get("indexList");
        return list;
    }

}
