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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.web.vo.moment.MomentDataMemberVO;
import com.gic.member.api.dto.es.MemberDataDTO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import com.gic.web.common.utils.DataApiUtils;
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 MomentDataMemberExcelHandler extends DownloadHandlerAbstract<MomentDataMemberVO> {

    @Autowired
    private QwMomentController qwMomentController ;
    @Autowired
    private QwMomentDataController qwMomentDataController ;

    @Override
    public List<MomentDataMemberVO> getData(Context context, String json, Integer currentPage) {
        JSONObject jsonObject = JSONObject.parseObject(json) ;
        Map<String, Object> res = DataApiUtils.http(json, "data_qw_moment_mbr_detail");
        Page page =  DataApiUtils.getPageData(res);
        List<MomentDataMemberVO> voList = EntityUtil.changeEntityListByJSON(MomentDataMemberVO.class, page.getResult());
        if(CollectionUtils.isNotEmpty(voList)) {
            List<String> memberIdList = voList.stream().map(o->o.getMbrId()).collect(Collectors.toList());
            Map<String, MemberDataDTO> map = this.qwMomentController.getMemberMap(jsonObject.getString("enterpriseId"),memberIdList) ;
            for (MomentDataMemberVO vo : voList) {
                String memberId = vo.getMbrId();
                vo.setMemberId(memberId);
                if(null == map.get(memberId)) {
                    vo.setMemberScene(2);
                }else {
                    MemberDataDTO data = map.get(memberId) ;
                    vo.setMemberName(data.getName());
                    vo.setMemberNick(data.getNickName());
                    vo.setMemberCardNo(data.getCardNo());
                    vo.setMemberPhone(data.getPhoneNumber());
                    vo.setMemberImage(data.getDefineHeadImg());
                    vo.setClerkId(data.getMainClerkId());
                    vo.setStoreId(data.getMainStoreId());
                    vo.setMemberScene(3);
                }
            }
            Set<String> storeSet = voList.stream().filter(o-> StringUtils.isNotBlank(o.getStoreId()) && !"-1".equals(o.getStoreId())).map(o->o.getStoreId()).collect(Collectors.toSet()) ;
            Set<String> clerkSet = voList.stream().filter(o->StringUtils.isNotBlank(o.getClerkId()) && !"-1".equals(o.getClerkId())).map(o->o.getClerkId()).collect(Collectors.toSet());
            Map<String, StoreDTO> storeMap = this.qwMomentDataController.storeMap(storeSet) ;
            Map<String, ClerkDTO> clerkMap = this.qwMomentDataController.clerkMap(clerkSet) ;
            for (MomentDataMemberVO vo : voList) {
                if(null != storeMap.get(vo.getStoreId())) {
                    vo.setStoreCode(storeMap.get(vo.getStoreId()).getStoreCode());
                    vo.setStoreName(storeMap.get(vo.getStoreId()).getStoreName());
                }
                if(null != clerkMap.get(vo.getClerkId())) {
                    vo.setClerkCode(clerkMap.get(vo.getClerkId()).getClerkCode());
                    vo.setClerkName(clerkMap.get(vo.getClerkId()).getClerkName());
                }
            }
        }

        return voList ;
    }

    @Override
    public List<String> getColumnNames() {
        return Arrays.asList();
    }

    @Override
    public List<String> getColumnNames(Context context) {
          return Arrays.asList("会员姓名", "会员昵称", "手机号", "会员卡号", "专属导购姓名", "专属导购code", "所属门店名称", "所属门店code", "转化金额");
    }

    @Override
    public List<String> getColumns(Context context) {
        return Arrays.asList("memberName", "memberNick", "memberPhone", "memberCardNo", "clerkName", "clerkCode", "storeName", "storeCode", "matlConvsAmt");
    }

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

    @Override
    public List<String> getColumns() {
        return Arrays.asList();
    }
}
