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

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.chat.GroupChatUserDTO;
import com.gic.haoban.manage.api.dto.qdto.chat.GroupChatUserSearchQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatApiService;
import com.gic.haoban.manage.web.controller.moment.QwMomentController;
import com.gic.haoban.manage.web.vo.chat.GroupChatUserExcelVO;
import com.gic.haoban.manage.web.vo.moment.MomentDataExcelVO;
import com.gic.member.api.dto.es.MemberDataDTO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GroupChatUserExcelHandler  extends DownloadHandlerAbstract<GroupChatUserExcelVO> {

    @Autowired
    private GroupChatApiService groupChatApiService;
    @Autowired
    private QwMomentController qwMomentController ;

    @Override
    public List<GroupChatUserExcelVO> getData(Context context, String searchDataParams, Integer currentPage) {
        GroupChatUserSearchQDTO qdto = JSON.parseObject(searchDataParams, GroupChatUserSearchQDTO.class);
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageSize(this.getPageSize());
        basePageInfo.setPageNum(currentPage);
        ServiceResponse<Page<GroupChatUserDTO>> resp = this.groupChatApiService.listUserPage(qdto, basePageInfo);
        List<GroupChatUserExcelVO> voList = new ArrayList<>() ;
        for (GroupChatUserDTO dto : resp.getResult().getResult()) {
            GroupChatUserExcelVO vo = EntityUtil.changeEntityByJSON(GroupChatUserExcelVO.class, dto);
            vo.setAddTimeExcel(DateUtil.dateToStr(dto.getJoinTime(), "yyyy-MM-dd HH:mm:ss"));
            if (dto.getUserType() == 1) {
                vo.setUserTypeExcel("企业成员");
            } else if (dto.getUserType() == 2) {
                vo.setUserTypeExcel("外部成员");
            } else {
                vo.setUserTypeExcel("客户");
            }
            Integer joinScene = dto.getJoinScene();
            if (null != joinScene) {
                String invitor_user_name = dto.getInvitorUserName();
                if (StringUtils.isBlank(invitor_user_name)) {
                    invitor_user_name = "群成员";
                }
                if (joinScene == 1) {
                    // 由群成员邀请入群（直接邀请入群）
                    vo.setSceneExcel(String.format("由%s直接邀请入群", invitor_user_name));
                } else if (joinScene == 2) {
                    // 由群成员邀请入群（通过邀请链接入群）
                    vo.setSceneExcel(String.format("通过%s分享链接入群", invitor_user_name));
                } else if (joinScene == 3) {
                    // 通过扫描群二维码入群
                    vo.setSceneExcel(String.format("通过扫描%s分享的二维码入群", invitor_user_name));
                }
                voList.add(vo);
            }
        }
        List<String> memberIdList = voList.stream().filter(o->StringUtils.isNotBlank(o.getMemberId())).map(o->o.getMemberId()).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(memberIdList)) {
            Map<String, MemberDataDTO> map = this.qwMomentController.getMemberMap(qdto.getEnterpriseId(),memberIdList) ;
            for (GroupChatUserExcelVO vo : voList) {
                String memberId = vo.getMemberId() ;
                if(null == map.get(memberId)) {
                    vo.setMemberScene(2);
                }else {
                    MemberDataDTO data = map.get(memberId) ;
                    vo.setMemberName(data.getName());
                    vo.setMemberNick(data.getNickName());
                    vo.setMemberCard(data.getCardNo());
                    vo.setMemberPhone(data.getPhoneNumber());
                    vo.setMemberScene(3);
                }
            }
        }
        return voList;
    }

    @Override
    public List<String> getColumnNames() {
        return Arrays.asList("群成员名称", "群成员昵称", "身份", "客户姓名","客户昵称","手机号","卡号","入群方式", "入群时间");
    }

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

    @Override
    public List<String> getColumns() {
        return  Arrays.asList("userName","nickName","userTypeExcel","memberName","memberNick","memberPhone","memberCard", "sceneExcel", "addTimeExcel");
    }
}
