package com.yutou.bilibili.Tools;

import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveDataDao;
import com.yutou.bilibili.mybatis.Bili.mybatis.dao.BilibiliLiveInfoDao;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import static com.yutou.bilibili.BiliBili.Datas.LiveData.*;

public class ExcelUtils implements ApplicationContextAware {

    @Resource
    BilibiliLiveDataDao dataDao;
    @Resource
    BilibiliLiveInfoDao infoDao;

    public static File getInstance(int roomId, Date startTime, Date endTime,String fileName) {
        ExcelUtils utils = new ExcelUtils();
        long timer=System.currentTimeMillis();
        com.yutou.bilibili.Tools.Log.i("开始注入bean");
        utils.dataDao = getBean(BilibiliLiveDataDao.class);
        utils.infoDao = getBean(BilibiliLiveInfoDao.class);
        com.yutou.bilibili.Tools.Log.i("注入完毕:"+(System.currentTimeMillis()-timer));
        return utils.initTable(roomId, startTime, endTime,fileName);
    }

    public ExcelUtils() {

    }

    private File initTable(int roomId, Date startTime, Date endTime,String fileName) {
        long timer=System.currentTimeMillis();
        com.yutou.bilibili.Tools.Log.i("进入统计:"+timer);
        Workbook workbook = new XSSFWorkbook();
        Sheet liveData = workbook.createSheet("直播数据");
        Sheet liveInfo = workbook.createSheet("小时统计");
        Row dataRow = liveData.createRow(0);
        Row infoRow = liveInfo.createRow(0);
        createCell(dataRow.createCell(0), "id");
        createCell(dataRow.createCell(1), "uid");
        createCell(dataRow.createCell(2), "roomId");
        createCell(dataRow.createCell(3), "类型");
        createCell(dataRow.createCell(4), "内容");
        createCell(dataRow.createCell(5), "礼物id");
        createCell(dataRow.createCell(6), "礼物名称");
        createCell(dataRow.createCell(7), "礼物数量");
        createCell(dataRow.createCell(8), "金瓜子");
        createCell(dataRow.createCell(9), "时间");

        createCell(infoRow.createCell(0), "id");
        createCell(infoRow.createCell(1), "房间号");
        createCell(infoRow.createCell(2), "人气");
        createCell(infoRow.createCell(3), "普通用户入场数量");
        createCell(infoRow.createCell(4), "舰长入场数量");
        createCell(infoRow.createCell(5), "送礼人数");
        createCell(infoRow.createCell(6), "记录时间");

        com.yutou.bilibili.Tools.Log.i("表头插入完毕:"+(System.currentTimeMillis()-timer));
        List<BilibiliLiveData> dataList = dataDao.queryLiveData(roomId, startTime, endTime, new String[]{
                INTERACT_WORD,
                ENTRY_EFFECT,
                DANMU_MSG,
                SEND_GIFT,
                COMBO_SEND,
                SUPER_CHAT_MESSAGE,
                NOTICE_MSG,
                GUARD_BUY,
                UNKNOWN_MESSAGE
        });
        List<BilibiliLiveInfo> infoList = infoDao.queryTimeOfRoomid(roomId, startTime, endTime);
        int index = 1;
        for (BilibiliLiveData data : dataList) {
            dataRow = liveData.createRow(index++);
            createCell(dataRow.createCell(0), data.getId());
            createCell(dataRow.createCell(1), data.getUid());
            createCell(dataRow.createCell(2), data.getRoomid());
            createCell(dataRow.createCell(3), data.getType());
            createCell(dataRow.createCell(4), data.getMsg());
            createCell(dataRow.createCell(5), data.getGiftid());
            createCell(dataRow.createCell(6), data.getGiftname());
            createCell(dataRow.createCell(7), data.getGiftindex());
            createCell(dataRow.createCell(8), data.getPrice());
            createCell(dataRow.createCell(9), AppTools.getToDayTimeToString(data.getSubtime()));
        }
        index = 1;
        for (BilibiliLiveInfo info : infoList) {
            infoRow = liveInfo.createRow(index++);
            createCell(infoRow.createCell(0), info.getId());
            createCell(infoRow.createCell(1), info.getRoomid());
            createCell(infoRow.createCell(2), info.getPopular());
            createCell(infoRow.createCell(3), info.getUserindex());
            createCell(infoRow.createCell(4), info.getVipuserindex());
            createCell(infoRow.createCell(5), info.getGiftuser());
            createCell(infoRow.createCell(6), AppTools.getToDayTimeToString(info.getSubtime()));
        }
        com.yutou.bilibili.Tools.Log.i("数据填充完毕:"+(System.currentTimeMillis()-timer));
        try {
            if(!new File("excel").exists()){
                new File("excel").mkdirs();
            }
            File file = new File("excel"+File.separator+fileName+".tmp");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            workbook.write(fileOutputStream);
            workbook.close();
            com.yutou.bilibili.Tools.Log.i("文件写入完毕:"+(System.currentTimeMillis()-timer));
            file.renameTo(new File("excel"+File.separator+fileName));
            return file;
        } catch (Exception e) {
            com.yutou.bilibili.Tools.Log.e(e);
        }

        return null;
    }

    private void createCell(Cell cell, Object data) {
        if (data == null)
            data = 0;
        if (data instanceof String) {
            cell.setCellType(CellType.STRING);
            cell.setCellValue((String) data);
        } else {
            cell.setCellType(CellType.NUMERIC);

            cell.setCellValue((int) data);
        }


    }

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (ExcelUtils.applicationContext == null) {
            ExcelUtils.applicationContext = applicationContext;
        }
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
