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

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
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.qo.content.statistics.MaterialStoreAnalyzeDataQO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialClerkUsedDataVO;
import com.gic.haoban.manage.web.vo.content.statistics.MaterialStoreUsedDataVO;
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;
import java.util.Objects;

@Component
public class ContentMaterialStoreUsedDataHandler extends DownloadHandlerAbstract<MaterialStoreUsedDataVO> {

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

    @Autowired
    private MaterialDataAdaptor materialDataAdaptor;

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

        MaterialStoreAnalyzeDataQO materialStatisticsBaseQO = JSON.parseObject(searchDataParams, MaterialStoreAnalyzeDataQO.class);
        materialStatisticsBaseQO.setPageNum(currentPage);
        materialStatisticsBaseQO.setPageSize(this.getPageSize());
        Page<MaterialStoreUsedDataVO> storeUsedData = materialDataAdaptor.queryMaterialStoreUsedData(materialStatisticsBaseQO, materialStatisticsBaseQO.getStoreIdList());
        List<MaterialStoreUsedDataVO> result = storeUsedData.getResult();
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        Integer mallType = materialStatisticsBaseQO.getMallType();
        if (Objects.equals(mallType, 0)) {
            result.forEach(x -> {
                x.setConvUserNum(x.getOfflineConvUserNum());
                x.setConvSalesNmt(x.getOfflineConvSalesNmt());
            });
        }
        return result;
    }

    @Override
    public List<String> getColumns() {
        return Lists.newArrayList("storeName", "storeCode",
                "dayAvgUseRate", "clickUserNum", "convUserNum", "convSalesNmt");
    }

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

    @Override
    public List<String> getColumnNames() {
        return Lists.newArrayList("门店名称", "门店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());
        }
    }
}
