package com.tzld.piaoquan.api.service.contentplatform.impl;

import com.alibaba.excel.EasyExcel;
import com.stuuudy.commons.external.filestorage.enums.EnumFileType;
import com.stuuudy.commons.external.filestorage.enums.EnumPublicBuckets;
import com.tzld.piaoquan.api.dao.mapper.contentplatform.ext.ContentPlatformDataStatMapperExt;
import com.tzld.piaoquan.api.model.config.LoginUserContext;
import com.tzld.piaoquan.api.model.param.contentplatform.GzhDatastatListParam;
import com.tzld.piaoquan.api.model.param.contentplatform.QwDatastatListParam;
import com.tzld.piaoquan.api.model.po.contentplatform.*;
import com.tzld.piaoquan.api.model.vo.contentplatform.*;
import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformCooperateAccountService;
import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformDatastatService;
import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformPlanService;
import com.tzld.piaoquan.api.util.AliOssFileTool;
import com.tzld.piaoquan.api.util.CdnUtil;
import com.tzld.piaoquan.growth.common.utils.page.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ContentPlatformDatastatServiceImpl implements ContentPlatformDatastatService {

    @Autowired
    ContentPlatformDataStatMapperExt dataStatMapperExt;
    @Autowired
    ContentPlatformCooperateAccountService cooperateAccountService;
    @Autowired
    ContentPlatformPlanService planService;

    @Override
    public Page<GzhDatastatItemVO> gzhDatastatList(GzhDatastatListParam param) {
        switch (param.getType()) {
            case 0:
                return gzhTotalDatastatList(param);
            case 1:
                return gzhAccountDatastatList(param);
            case 4:
                return gzhAccountVideoDatastatList(param);
            case 2:
                return fwhTotalDatastatList(param);
            case 3:
                return fwhAccountDatastatList(param);
            case 5:
                return gzhPushTotalDatastatList(param);
            case 6:
                return gzhPushAccountDatastatList(param);
            default:
                return gzhAccountDatastatList(param);
        }
    }

    private Page<GzhDatastatItemVO> gzhTotalDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getGzhTotalDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformGzhDataStatTotal> datastatList = dataStatMapperExt.getGzhTotalDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildGzhDatastatTotalItemVOList(datastatList);
        result.setObjs(list);
        return result;
    }

    private List<GzhDatastatItemVO> buildGzhDatastatTotalItemVOList(List<ContentPlatformGzhDataStatTotal> datastatList) {
        if (CollectionUtils.isEmpty(datastatList)) {
            return null;
        }
        List<GzhDatastatItemVO> result = new ArrayList<>();
        for (ContentPlatformGzhDataStatTotal datastat : datastatList) {
            GzhDatastatItemVO vo = new GzhDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setFansIncreaseCount(datastat.getFansIncreaseCount());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            vo.setUnitPrice(datastat.getUnitPrice());
            vo.setSettlementAmount(datastat.getSettlementAmount());
            if (Objects.nonNull(vo.getFansIncreaseCount()) && vo.getFansIncreaseCount() > 0) {
                BigDecimal num = BigDecimal.valueOf(vo.getFirstLevel() / (double) vo.getFansIncreaseCount());
                BigDecimal rounded = num.setScale(3, RoundingMode.HALF_UP);
                vo.setOpenRate(rounded.doubleValue());
            }
            result.add(vo);
        }
        return result;
    }

    private Page<GzhDatastatItemVO> gzhAccountDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getGzhAccountDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformGzhDataStat> datastatList = dataStatMapperExt.getGzhAccountDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildGzhDatastatItemVOList(param.getType(), datastatList);
        result.setObjs(list);
        return result;
    }

    private Page<GzhDatastatItemVO> gzhAccountVideoDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getGzhAccountVideoDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformGzhVideoDataStat> datastatList = dataStatMapperExt.getGzhAccountVideoDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildGzhVideoDatastatItemVOList(datastatList);
        result.setObjs(list);
        return result;
    }

    private List<GzhDatastatItemVO> buildGzhVideoDatastatItemVOList(List<ContentPlatformGzhVideoDataStat> datastatList) {
        if (CollectionUtils.isEmpty(datastatList)) {
            return null;
        }
        List<Long> accountIds = datastatList.stream().map(ContentPlatformGzhVideoDataStat::getAccountId).distinct().collect(Collectors.toList());
        List<ContentPlatformGzhAccount> accountList = cooperateAccountService.getAccountListByIds(accountIds);
        Map<Long, ContentPlatformGzhAccount> accountMap = accountList.stream().collect(Collectors.toMap(ContentPlatformGzhAccount::getId, a -> a));
        List<GzhDatastatItemVO> result = new ArrayList<>();
        for (ContentPlatformGzhVideoDataStat datastat : datastatList) {
            GzhDatastatItemVO vo = new GzhDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            ContentPlatformGzhAccount account = accountMap.get(datastat.getAccountId());
            if (Objects.nonNull(account)) {
                vo.setName(account.getName());
                vo.setGhId(account.getGhId());
            }
            vo.setVideoId(datastat.getVideoId());
            vo.setTitle(datastat.getTitle());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            result.add(vo);
        }
        return result;
    }

    private Page<GzhDatastatItemVO> fwhTotalDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getFwhTotalDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformFwhDataStatTotal> datastatList = dataStatMapperExt.getFwhTotalDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildFwhDatastatTotalItemVOList(datastatList);
        result.setObjs(list);
        return result;
    }

    private List<GzhDatastatItemVO> buildFwhDatastatTotalItemVOList(List<ContentPlatformFwhDataStatTotal> datastatList) {
        if (CollectionUtils.isEmpty(datastatList)) {
            return null;
        }
        List<GzhDatastatItemVO> result = new ArrayList<>();
        for (ContentPlatformFwhDataStatTotal datastat : datastatList) {
            GzhDatastatItemVO vo = new GzhDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setFansIncreaseCount(datastat.getFansIncreaseCount());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            vo.setUnitPrice(datastat.getUnitPrice());
            vo.setSettlementAmount(datastat.getSettlementAmount());
            if (Objects.nonNull(vo.getFansIncreaseCount()) && vo.getFansIncreaseCount() > 0) {
                BigDecimal num = BigDecimal.valueOf(vo.getFirstLevel() / (double) vo.getFansIncreaseCount());
                BigDecimal rounded = num.setScale(3, RoundingMode.HALF_UP);
                vo.setOpenRate(rounded.doubleValue());
            }
            result.add(vo);
        }
        return result;
    }

    private Page<GzhDatastatItemVO> fwhAccountDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getFwhAccountDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformGzhDataStat> datastatList = dataStatMapperExt.getFwhAccountDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildGzhDatastatItemVOList(param.getType(), datastatList);
        result.setObjs(list);
        return result;
    }

    private List<GzhDatastatItemVO> buildGzhDatastatItemVOList(Integer type, List<ContentPlatformGzhDataStat> datastatList) {
        if (CollectionUtils.isEmpty(datastatList)) {
            return null;
        }
        Map<Long, ContentPlatformGzhAccount> accountMap = new HashMap<>();
        if (Arrays.asList(1, 3, 6).contains(type)) {
            List<Long> accountIds = datastatList.stream().map(ContentPlatformGzhDataStat::getAccountId).distinct().collect(Collectors.toList());
            List<ContentPlatformGzhAccount> accountList = cooperateAccountService.getAccountListByIds(accountIds);
            accountMap = accountList.stream().collect(Collectors.toMap(ContentPlatformGzhAccount::getId, a -> a));
        }
        List<GzhDatastatItemVO> result = new ArrayList<>();
        for (ContentPlatformGzhDataStat datastat : datastatList) {
            GzhDatastatItemVO vo = new GzhDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            ContentPlatformGzhAccount account = accountMap.get(datastat.getAccountId());
            if (Objects.nonNull(account)) {
                vo.setName(account.getName());
                vo.setGhId(account.getGhId());
            }
            vo.setFansIncreaseCount(datastat.getFansIncreaseCount());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            if (Objects.nonNull(vo.getFansIncreaseCount()) && vo.getFansIncreaseCount() > 0) {
                BigDecimal num = BigDecimal.valueOf(vo.getFirstLevel() / (double) vo.getFansIncreaseCount());
                BigDecimal rounded = num.setScale(3, RoundingMode.HALF_UP);
                vo.setOpenRate(rounded.doubleValue());
            }
            result.add(vo);
        }
        return result;
    }

    private Page<GzhDatastatItemVO> gzhPushAccountDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getGzhPushAccountDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformGzhDataStat> datastatList = dataStatMapperExt.getGzhPushAccountDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildGzhDatastatItemVOList(param.getType(), datastatList);
        result.setObjs(list);
        return result;
    }

    private List<GzhDatastatItemVO> buildGzhPushDatastatTotalItemVOList(List<ContentPlatformGzhPushDataStatTotal> datastatList) {
        if (CollectionUtils.isEmpty(datastatList)) {
            return null;
        }
        List<GzhDatastatItemVO> result = new ArrayList<>();
        for (ContentPlatformGzhPushDataStatTotal datastat : datastatList) {
            GzhDatastatItemVO vo = new GzhDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setFansIncreaseCount(datastat.getFansIncreaseCount());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            vo.setUnitPrice(datastat.getUnitPrice());
            vo.setSettlementAmount(datastat.getSettlementAmount());
            if (Objects.nonNull(vo.getFansIncreaseCount()) && vo.getFansIncreaseCount() > 0) {
                BigDecimal num = BigDecimal.valueOf(vo.getFirstLevel() / (double) vo.getFansIncreaseCount());
                BigDecimal rounded = num.setScale(3, RoundingMode.HALF_UP);
                vo.setOpenRate(rounded.doubleValue());
            }
            result.add(vo);
        }
        return result;
    }

    private Page<GzhDatastatItemVO> gzhPushTotalDatastatList(GzhDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<GzhDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getGzhPushTotalDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformGzhPushDataStatTotal> datastatList = dataStatMapperExt.getGzhPushTotalDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<GzhDatastatItemVO> list = buildGzhPushDatastatTotalItemVOList(datastatList);
        result.setObjs(list);
        return result;
    }

    @Override
    public Page<QwDatastatItemVO> qwDatastatList(QwDatastatListParam param) {
        switch (param.getType()) {
            case 0:
                return qwTotalDatastatList(param);
            case 1:
                return qwSceneDatastatList(param, 0);
            case 2:
                return qwSceneDatastatList(param, 1);
            case 3:
                return qwReplyDatastatList(param);
            case 4:
                return qwRootSourceIdDatastatList(param);
            case 5:
                return qwSubChannelDatastatList(param);
            default:
                return qwRootSourceIdDatastatList(param);
        }
    }

    private Page<QwDatastatItemVO> qwTotalDatastatList(QwDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<QwDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getQwTotalDatastatCount(param, user.getChannel());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformQwDataStatTotal> datastatList = dataStatMapperExt.getQwTotalDatastatList(param,
                user.getChannel(), offset, param.getPageSize());
        List<QwDatastatItemVO> list = datastatList.stream().map(datastat -> {
            QwDatastatItemVO vo = new QwDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            vo.setUnitPrice(datastat.getUnitPrice());
            vo.setSettlementAmount(datastat.getSettlementAmount());
            return vo;
        }).collect(Collectors.toList());
        result.setObjs(list);
        return result;
    }

    private Page<QwDatastatItemVO> qwSceneDatastatList(QwDatastatListParam param, Integer scene) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<QwDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getQwSceneDatastatCount(param, scene, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformQwDataStat> datastatList = dataStatMapperExt.getQwSceneDatastatList(param, scene,
                user.getId(), offset, param.getPageSize());
        List<QwDatastatItemVO> list = buildQwRootSourceIdDatastatItemVOList(datastatList);
        result.setObjs(list);
        return result;
    }

    private Page<QwDatastatItemVO> qwReplyDatastatList(QwDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<QwDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getQwReplyTotalDatastatCount(param, user.getChannel());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformQwDataStatReplyTotal> datastatList = dataStatMapperExt.getQwReplyTotalDatastatList(param,
                user.getChannel(), offset, param.getPageSize());
        List<QwDatastatItemVO> list = datastatList.stream().map(datastat -> {
            QwDatastatItemVO vo = new QwDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
            return vo;
        }).collect(Collectors.toList());
        result.setObjs(list);
        return result;
    }

    private Page<QwDatastatItemVO> qwSubChannelDatastatList(QwDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<QwDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getQwSubChannelDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformQwDataStatSubChannel> datastatList = dataStatMapperExt.getQwSubChannelDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<QwDatastatItemVO> list = datastatList.stream().map(datastat -> {
            QwDatastatItemVO vo = new QwDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setSubChannel(datastat.getSubChannel());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            vo.setScore(datastat.getScore());
//            vo.setUnitPrice(datastat.getUnitPrice());
//            vo.setSettlementAmount(datastat.getSettlementAmount());
            return vo;
        }).collect(Collectors.toList());
        result.setObjs(list);
        return result;
    }

    private Page<QwDatastatItemVO> qwRootSourceIdDatastatList(QwDatastatListParam param) {
        ContentPlatformAccount user = LoginUserContext.getUser();
        Page<QwDatastatItemVO> result = new Page<>(param.getPageNum(), param.getPageSize());
        int offset = (param.getPageNum() - 1) * param.getPageSize();
        int count = dataStatMapperExt.getQwRootSourceIdDatastatCount(param, user.getId());
        result.setTotalSize(count);
        if (count == 0) {
            return result;
        }
        List<ContentPlatformQwDataStat> datastatList = dataStatMapperExt.getQwRootSourceIdDatastatList(param,
                user.getId(), offset, param.getPageSize());
        List<QwDatastatItemVO> list = buildQwRootSourceIdDatastatItemVOList(datastatList);
        result.setObjs(list);
        return result;
    }

    private List<QwDatastatItemVO> buildQwRootSourceIdDatastatItemVOList(List<ContentPlatformQwDataStat> datastatList) {
        if (CollectionUtils.isEmpty(datastatList)) {
            return null;
        }
        List<String> rootSourceIds = datastatList.stream().map(ContentPlatformQwDataStat::getRootSourceId).distinct().collect(Collectors.toList());
        List<ContentPlatformQwPlan> qwPlanList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(rootSourceIds)) {
            qwPlanList = planService.getQwPlanListByRootSourceIds(rootSourceIds);
        }
        Map<String, Long> qwPlanMap = qwPlanList.stream().collect(Collectors.toMap(ContentPlatformQwPlan::getRootSourceId, ContentPlatformQwPlan::getId));
        List<Long> planIds = qwPlanList.stream().map(ContentPlatformQwPlan::getId).distinct().collect(Collectors.toList());
        Map<Long, List<ContentPlatformQwPlanVideo>> qwPlanVideoMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(planIds)) {
            List<ContentPlatformQwPlanVideo> qwPlanVideoList = planService.getQwPlanVideoListByPlanIds(planIds);
            qwPlanVideoMap = qwPlanVideoList.stream().collect(Collectors.groupingBy(ContentPlatformQwPlanVideo::getPlanId));
        }
        List<QwDatastatItemVO> result = new ArrayList<>();
        for (ContentPlatformQwDataStat datastat : datastatList) {
            QwDatastatItemVO vo = new QwDatastatItemVO();
            vo.setDateStr(datastat.getDateStr());
            vo.setSubChannel(datastat.getSubChannel());
            vo.setFirstLevel(datastat.getFirstLevelCount());
            if (Objects.nonNull(datastat.getScore())) {
                BigDecimal num = BigDecimal.valueOf(datastat.getScore());
                BigDecimal rounded = num.setScale(2, RoundingMode.HALF_UP);
                vo.setScore(rounded.doubleValue());
            }
            Long planId = qwPlanMap.get(datastat.getRootSourceId());
            if (Objects.nonNull(planId)) {
                List<ContentPlatformQwPlanVideo> planVideoList = qwPlanVideoMap.get(planId);
                if (CollectionUtils.isNotEmpty(planVideoList)) {
                    vo.setVideoId(planVideoList.get(0).getVideoId());
                    vo.setTitle(planVideoList.get(0).getTitle());
                }
            }
            result.add(vo);
        }
        return result;
    }

    @Override
    public String gzhDatastatExport(GzhDatastatListParam param) {
        param.setPageNum(1);
        param.setExportPageSize(10000);
        Page<GzhDatastatItemVO> pageResult = gzhDatastatList(param);
        List<GzhDatastatItemVO> dataList = pageResult.getObjs();
        if (param.getType() == 1 || param.getType() == 3 || param.getType() == 6) {
            return generateGzhDatastatExcelFile(dataList);
        } else if (param.getType() == 4) {
            return generateGzhAccountVideoDatastatExcelFile(dataList);
        } else {
            return generateTotalGzhDatastatExcelFile(dataList);
        }
    }

    public String generateTotalGzhDatastatExcelFile(List<GzhDatastatItemVO> dataList) {
        ArrayList<GzhTotalDatastatItemExportVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (GzhDatastatItemVO data : dataList) {
                list.add(new GzhTotalDatastatItemExportVO(data.getDateStr(), data.getFansIncreaseCount(),
                        data.getFirstLevel(), data.getOpenRate(), data.getScore(), data.getUnitPrice(), data.getSettlementAmount()));
            }
        } else {
            list.add(new GzhTotalDatastatItemExportVO());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, GzhTotalDatastatItemExportVO.class).sheet("").doWrite(list);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        String fileName = "公众号数据统计_" + System.currentTimeMillis() + ".xls";
        AliOssFileTool.saveInPublicReturnHost(byteArrayInputStream, EnumPublicBuckets.PUBBUCKET, fileName, EnumFileType.TEMP_PICTURE);
        return CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE + fileName;
    }

    public String generateGzhDatastatExcelFile(List<GzhDatastatItemVO> dataList) {
        ArrayList<GzhDatastatItemExportVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (GzhDatastatItemVO data : dataList) {
                list.add(new GzhDatastatItemExportVO(data.getDateStr(), data.getName(), data.getGhId(),
                        data.getFansIncreaseCount(), data.getFirstLevel(), data.getOpenRate(), data.getScore()));
            }
        } else {
            list.add(new GzhDatastatItemExportVO());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, GzhDatastatItemExportVO.class).sheet("").doWrite(list);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        String fileName = "公众号数据统计_" + System.currentTimeMillis() + ".xls";
        AliOssFileTool.saveInPublicReturnHost(byteArrayInputStream, EnumPublicBuckets.PUBBUCKET, fileName, EnumFileType.TEMP_PICTURE);
        return CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE + fileName;
    }

    public String generateGzhAccountVideoDatastatExcelFile(List<GzhDatastatItemVO> dataList) {
        ArrayList<GzhAccountVideoDatastatItemExportVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (GzhDatastatItemVO data : dataList) {
                list.add(new GzhAccountVideoDatastatItemExportVO(data.getDateStr(), data.getName(), data.getVideoId(),
                        data.getTitle(), data.getFirstLevel(), data.getScore()));
            }
        } else {
            list.add(new GzhAccountVideoDatastatItemExportVO());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, GzhAccountVideoDatastatItemExportVO.class).sheet("").doWrite(list);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        String fileName = "公众号数据统计_" + System.currentTimeMillis() + ".xls";
        AliOssFileTool.saveInPublicReturnHost(byteArrayInputStream, EnumPublicBuckets.PUBBUCKET, fileName, EnumFileType.TEMP_PICTURE);
        return CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE + fileName;
    }

    @Override
    public String qwDatastatExport(QwDatastatListParam param) {
        param.setPageNum(1);
        param.setExportPageSize(10000);
        Page<QwDatastatItemVO> pageResult = qwDatastatList(param);
        List<QwDatastatItemVO> dataList = pageResult.getObjs();
        if (param.getType() == 4) {
            return generateQwDatastatExcelFile(dataList);
        } else if (param.getType() == 5) {
            return generateQwSubChannelDatastatExcelFile(dataList);
        } else {
            return generateGroupQwDatastatExcelFile(dataList);
        }
    }

    public String generateQwDatastatExcelFile(List<QwDatastatItemVO> dataList) {
        ArrayList<QwDatastatItemExportVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (QwDatastatItemVO data : dataList) {
                list.add(new QwDatastatItemExportVO(data.getDateStr(), data.getSubChannel(), data.getTitle(), data.getVideoId(), data.getScore()));
            }
        } else {
            list.add(new QwDatastatItemExportVO());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, QwDatastatItemExportVO.class).sheet("").doWrite(list);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        String fileName = "企微数据统计_" + System.currentTimeMillis() + ".xls";
        AliOssFileTool.saveInPublicReturnHost(byteArrayInputStream, EnumPublicBuckets.PUBBUCKET, fileName, EnumFileType.TEMP_PICTURE);
        return CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE + fileName;
    }

    public String generateQwSubChannelDatastatExcelFile(List<QwDatastatItemVO> dataList) {
        ArrayList<QwSubChannelDatastatItemExportVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (QwDatastatItemVO data : dataList) {
//                list.add(new QwSubChannelDatastatItemExportVO(data.getDateStr(), data.getSubChannel(),
//                        data.getFirstLevel(), data.getScore(), data.getUnitPrice(), data.getSettlementAmount()));
                list.add(new QwSubChannelDatastatItemExportVO(data.getDateStr(), data.getSubChannel(),
                        data.getFirstLevel(), data.getScore()));
            }
        } else {
            list.add(new QwSubChannelDatastatItemExportVO());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, QwSubChannelDatastatItemExportVO.class).sheet("").doWrite(list);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        String fileName = "企微数据统计_" + System.currentTimeMillis() + ".xls";
        AliOssFileTool.saveInPublicReturnHost(byteArrayInputStream, EnumPublicBuckets.PUBBUCKET, fileName, EnumFileType.TEMP_PICTURE);
        return CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE + fileName;
    }

    public String generateGroupQwDatastatExcelFile(List<QwDatastatItemVO> dataList) {
        ArrayList<QwGroupDatastatItemExportVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dataList)) {
            for (QwDatastatItemVO data : dataList) {
                list.add(new QwGroupDatastatItemExportVO(data.getDateStr(), data.getFirstLevel(), data.getScore(),
                        data.getUnitPrice(), data.getSettlementAmount()));
            }
        } else {
            list.add(new QwGroupDatastatItemExportVO());
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        EasyExcel.write(out, QwGroupDatastatItemExportVO.class).sheet("").doWrite(list);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
        String fileName = "企微数据统计_" + System.currentTimeMillis() + ".xls";
        AliOssFileTool.saveInPublicReturnHost(byteArrayInputStream, EnumPublicBuckets.PUBBUCKET, fileName, EnumFileType.TEMP_PICTURE);
        return CdnUtil.DOWNLOAD_CDN_URL_HOST_PICTURE + fileName;
    }
}
