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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.enums.PlatformChannelEnum;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.web.vo.moment.MomentDataOrderVO;
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 MomentDataOrderExcelHandler extends DownloadHandlerAbstract<MomentDataOrderVO> {

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

    @Override
    public List<MomentDataOrderVO> getData(Context context, String json, Integer currentPage) {
        JSONObject jsonObject = JSONObject.parseObject(json) ;
        Map<String, Object> res = DataApiUtils.http(json, "data_qw_moment_order_detail");
        Page page =  DataApiUtils.getPageData(res);
        List<MomentDataOrderVO> voList = EntityUtil.changeEntityListByJSON(MomentDataOrderVO.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 (MomentDataOrderVO 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.setMemberScene(3);
                }
                PlatformChannelEnum channel = PlatformChannelEnum.getEnumByChannelCode(vo.getChannelCode()) ;
                if(null != channel) {
                    vo.setChannelName(channel.getChannelName());
                }
            }
            Set<String> storeSet = voList.stream().filter(o -> StringUtils.isNotBlank(o.getOrderStoreId()) && !"-1".equals(o.getOrderStoreId())).map(o -> o.getOrderStoreId()).collect(Collectors.toSet());
            Map<String, StoreDTO> storeMap = this.qwMomentDataController.storeMap(storeSet);
            for (MomentDataOrderVO vo : voList) {
                if (null != storeMap.get(vo.getOrderStoreId())) {
                    vo.setOrderStoreCode(storeMap.get(vo.getOrderStoreId()).getStoreCode());
                    vo.setOrderStoreName(storeMap.get(vo.getOrderStoreId()).getStoreName());
                }
            }
        }
        return voList ;
    }

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

    @Override
    public List<String> getColumnNames(Context context) {
        return Arrays.asList("会员姓名", "会员昵称", "手机号", "会员卡号", "订单渠道", "订单编号", "订单实付金额", "消费门店名称", "消费门店 code", "下单时间");
    }

    @Override
    public List<String> getColumns(Context context) {
        return Arrays.asList("memberName", "memberNick", "memberPhone", "memberCardNo", "channelName", "orderCode", "matlConvsAmt", "orderStoreName", "orderStoreCode", "orderTime");
    }

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

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