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.chat.GroupChatUserQuitExcelVO;
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 GroupChatUserQuitExcelHandler extends DownloadHandlerAbstract<GroupChatUserQuitExcelVO> {

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

    @Override
    public List<GroupChatUserQuitExcelVO> 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<GroupChatUserQuitExcelVO> voList = new ArrayList<>() ;
        for (GroupChatUserDTO dto : resp.getResult().getResult()) {
            GroupChatUserQuitExcelVO vo = EntityUtil.changeEntityByJSON(GroupChatUserQuitExcelVO.class, dto);
            vo.setUserQuitTimeExcel(DateUtil.dateToStr(dto.getUserQuitTime(),"yyyy-MM-dd HH:mm:ss"));
            if(dto.getUserType()==1) {
                vo.setUserTypeExcel("企业成员");
            }else if(dto.getUserType()==2) {
                vo.setUserTypeExcel("外部成员");
            }else {
                vo.setUserTypeExcel("客户");
            }
             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 (GroupChatUserQuitExcelVO 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", "chatName", "staffName", "quitStaffStore" , "userQuitTimeExcel");
    }
}
