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.web.controller.chat.GroupChatActivityDataController;
import com.gic.haoban.manage.web.qo.chat.GroupChatActivityRewardLogReq;
import com.gic.haoban.manage.web.vo.chat.GroupChatActivityRewardLogExportVO;
import com.gic.haoban.manage.web.vo.chat.GroupChatActivityRewardLogVO;
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:30:10
 */
@Component
public class GroupChatActivityRewardHandle extends DownloadHandlerAbstract<GroupChatActivityRewardLogExportVO> {

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

    @Resource
    private GroupChatActivityDataController groupChatActivityDataController;

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

        List<GroupChatActivityRewardLogExportVO> groupChatActivityRewardLogExportVOS = Lists.newArrayList();
        for (GroupChatActivityRewardLogVO groupChatActivityRewardLogVO : groupChatActivityRewardLogVOS) {

            GroupChatActivityRewardLogExportVO groupChatActivityRewardLogExportVO = new GroupChatActivityRewardLogExportVO();
            BeanUtils.copyProperties(groupChatActivityRewardLogVO, groupChatActivityRewardLogExportVO);

            if (groupChatActivityRewardLogVO.getRewardInvite() == 1) {
                groupChatActivityRewardLogExportVO.setRewardInviteType("邀请奖励");
                groupChatActivityRewardLogExportVO.setInviteLevelType("达成第" + groupChatActivityRewardLogVO.getInviteLevel() + "阶梯邀请奖励");
            } else {
                groupChatActivityRewardLogExportVO.setRewardInviteType("受邀奖励");
                groupChatActivityRewardLogExportVO.setInviteLevelType("受邀进群奖励");
            }

            if (Objects.nonNull(groupChatActivityRewardLogVO.getCreateTime())) {
                groupChatActivityRewardLogExportVO.setRewardTimeDesc(DateUtil.formatDateTime(groupChatActivityRewardLogVO.getCreateTime()));
            }

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

        return groupChatActivityRewardLogExportVOS;
    }

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