package com.gic.haoban.manage.web.export;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.DateUtil;
import com.gic.enterprise.api.dto.security.DownloadReportDTO;
import com.gic.haoban.manage.api.util.notify.NoticeMessageUtil;
import com.gic.haoban.manage.web.controller.content.adaptor.MaterialDataAdaptor;
import com.gic.haoban.manage.web.qo.content.statistics.MaterialAnalyzeDataQO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialClerkUsedDataVO;
import com.gic.web.common.download.DownloadHandlerAbstract;
import com.gic.web.common.download.context.Context;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

@Component
public class ContentMaterialClerkUsedDataHandler extends DownloadHandlerAbstract<MaterialClerkUsedDataVO> {

    private static final Logger log = LoggerFactory.getLogger(ContentMaterialClerkUsedDataHandler.class);

    @Autowired
    private MaterialDataAdaptor materialDataAdaptor;

    @Override
    public List<MaterialClerkUsedDataVO> getData(Context context, String searchDataParams, Integer currentPage) {

        MaterialAnalyzeDataQO materialStatisticsBaseQO = JSON.parseObject(searchDataParams, MaterialAnalyzeDataQO.class);
        materialStatisticsBaseQO.setPageNum(currentPage);
        materialStatisticsBaseQO.setPageSize(this.getPageSize());
        Page<MaterialClerkUsedDataVO> clerkUsedData = materialDataAdaptor.queryMaterialClerkUsedData(materialStatisticsBaseQO,materialStatisticsBaseQO.getStoreIdList());
        List<MaterialClerkUsedDataVO> result = clerkUsedData.getResult();
        if (CollectionUtils.isEmpty(result)) {
            result.forEach(x -> {
                x.setConvsUserNum(x.getConvsUserNum() + x.getOfflineConvsUserNum());
                x.setConvsAmt(x.getConvsAmt().add(x.getOfflineConvsAmt()));
            });
            return Collections.emptyList();
        }
        return result;
    }

    @Override
    public List<String> getColumns() {
        return Lists.newArrayList("storeName", "storeCode", "clerkName", "clerkCode",
                "useMatlDayNum", "useMatlNum", "singleMatlAvgCnt", "convsUserNum", "convsAmt");
    }

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

    @Override
    public List<String> getColumnNames() {
        return Lists.newArrayList("门店名称", "门店code", "导购姓名", "导购code",
                "使用素材天数", "使用素材数", "单个素材平均使用次数", "转化客户数", "转化金额");
    }

    @Override
    public void callBack(Context context, boolean success) {
        if (success) {
            DownloadReportDTO downloadReportDTO = context.getDownloadReportDTO();
            String clerkId = downloadReportDTO.getApplyClerkId();
            String enterpriseId = downloadReportDTO.getEnterpriseId();
            NoticeMessageUtil.sendDataExportNotify(enterpriseId, clerkId,
                    downloadReportDTO.getReportName(), downloadReportDTO.getApplyTime());
        }
    }
}
