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

import com.alibaba.fastjson.JSON;
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.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDataDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanDataListQDTO;
import com.gic.haoban.manage.api.service.moment.QwMomentApiService;
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 MomentDataExcelHandler extends DownloadHandlerAbstract<MomentDataExcelVO> {

    @Autowired
    private QwMomentApiService qwMomentApiService ;

    @Autowired
    private StoreService storeService  ;
    @Autowired
    private QwMomentController qwMomentController ;


    @Override
    public List<MomentDataExcelVO> getData(Context context, String json, Integer currentPage) {
        PlanDataListQDTO qdto = JSON.parseObject(json, PlanDataListQDTO.class);
        qdto.setPageSize(this.getPageSize());
        qdto.setPageNum(currentPage);
        ServiceResponse<Page<QwMomentPlanDataDTO>> pageResp = this.qwMomentApiService.dataList(qdto);
        List<MomentDataExcelVO> voList = new ArrayList<>();
        if (null != pageResp && CollectionUtils.isNotEmpty(pageResp.getResult().getResult())) {
            for(QwMomentPlanDataDTO item : pageResp.getResult().getResult()) {
                MomentDataExcelVO vo = EntityUtil.changeEntityByJSON(MomentDataExcelVO.class,item) ;
                if(qdto.getType()==1 && null != vo.getLikeTime()) {
                    vo.setTimeExcel(DateUtil.dateToStr(vo.getLikeTime(),"yyyy-MM-dd HH:mm:ss"));
                }
                if(qdto.getType()==2 && null != vo.getCommentTime()) {
                    vo.setTimeExcel(DateUtil.dateToStr(vo.getCommentTime(),"yyyy-MM-dd HH:mm:ss"));
                }
                if(StringUtils.isNotBlank(vo.getStoreId())) {
                    StoreDTO store = this.storeService.getStore(vo.getStoreId());
                    vo.setStoreName(store.getStoreName());
                    vo.setStoreCode(store.getStoreCode());
                }
                voList.add(vo) ;
            }
            List<String> memberIdList = voList.stream().map(o->o.getMemberId()).collect(Collectors.toList());
            Map<String, MemberDataDTO> map = this.qwMomentController.getMemberMap(qdto.getEnterpriseId(),memberIdList) ;
            for (MomentDataExcelVO 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.setMemberImage(data.getPhotoUrl());
                    vo.setMemberPhone(data.getPhoneNumber());
                    vo.setMemberScene(3);
                }
            }
        }
        return voList;
    }

    @Override
    public List<String> getColumnNames() {
        return new ArrayList<>() ;
    }

    @Override
    public List<String> getColumnNames(Context context) {
        String json = context.getDownloadReportDTO().getSearchDataParams();
        PlanDataListQDTO qdto = JSON.parseObject(json, PlanDataListQDTO.class);
        if(qdto.getType()==1) {
            return Arrays.asList("员工姓名",  "导购名称", "导购code","所属门店名称", "门店code","客户姓名","客户手机号","点赞时间");
        }else {
            return Arrays.asList("员工姓名",  "导购名称", "导购code","所属门店名称", "门店code","客户姓名","客户手机号","评论时间");
        }
    }

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

    @Override
    public List<String> getColumns() {
        return Arrays.asList("staffName",  "clerkName", "clerkCode","storeName", "storeCode","memberName","memberPhone","timeExcel");
    }
}
