package com.yutou.bilibili.BiliBili.Controllers;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.bilibili.BiliBili.Datas.LiveData;
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
import com.yutou.bilibili.Tools.AppTools;
import com.yutou.bilibili.Tools.ExcelUtils;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class OverviewController {
    @Resource
    IBiliBiliLiveService manager;

    @RequestMapping("/overview/get/query.do")
    @ResponseBody
    public JSONObject queryOverview(Date startTime, Date endTime, BilibiliUpInfo upData) {
        JSONObject json = new JSONObject();
        JSONObject data = new JSONObject();
        //BiliBiliLiveDatabasesManager manager = BiliBiliLiveDatabasesManager.getInstance();
        data.put("giftCount", manager.queryGiftSize(upData.getRoomid(), -1, startTime, endTime));
        data.put("giftUserCount", manager.queryGiftUserToDistinct(upData, startTime, endTime, new String[]{
                LiveData.SEND_GIFT
                , LiveData.COMBO_SEND
                , LiveData.GUARD_BUY}));
        data.put("price", manager.queryPriceSize(upData.getRoomid(), startTime, endTime));
        long h = (endTime.getTime() - startTime.getTime()) % (1000 * 24 * 60 * 60) / (1000 * 60 * 60);
        com.yutou.bilibili.Tools.Log.i("相差:" + h + "小时");
        try {
            data.put("hprice", data.getInteger("price") / h);
        } catch (Exception e) {
            data.put("hprice", 0);
        }


        JSONArray array=new JSONArray();
        List<BilibiliLiveInfo> infos=manager.queryLiveInfo(upData,startTime,endTime);
        int maxUser=0;
        for (BilibiliLiveInfo info : infos) {
            com.yutou.bilibili.Tools.Log.i(info);
            maxUser+=info.getUserindex()+info.getVipuserindex();
            JSONObject item=new JSONObject();
            String nowTime = new SimpleDateFormat("HH").format(info.getSubtime());
            item.put("time",nowTime);
            item.put("popular",info.getPopular());
            item.put("user",info.getUserindex()+info.getVipuserindex());
            array.add(item);
        }
        try {
            Map<String, Integer> map = manager.queryUserGiftCount(upData, startTime, endTime);
            int huserGift = 0;
            for (String s : map.keySet()) {
                huserGift += map.get(s);
            }
            data.put("userGift", huserGift / maxUser);
        } catch (Exception e) {
            data.put("userGift", 0);
        }
        data.put("map",array);
        json.put("data",data);
        json.put("code",0);
        return json;
    }

    @ResponseBody
    @RequestMapping("/overview/get/down.do")
    public JSONObject downExcel(Date startTime, Date endTime, BilibiliUpInfo upData){
        final String fileName=System.currentTimeMillis()+".xlsx";
        new Thread(new Runnable() {
            @Override
            public void run() {
                ExcelUtils.getInstance(upData.getRoomid(),startTime,endTime,fileName);
            }
        }).start();
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("data",fileName);
        return json;

    }

    @RequestMapping("/overview/get/download.do")
    public ResponseEntity<FileSystemResource> downloadExcel(HttpServletRequest request, String fname){
        File file=new File("excel"+File.separator+fname);
        if(file.exists()) {
            return AppTools.getFile(file);
        }else{
            return null;
        }
    }
}
