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.Live;
import com.yutou.bilibili.BiliBili.LiveUtils;
import com.yutou.bilibili.BiliBili.Services.IBiliBiliLiveService;
import com.yutou.bilibili.Tools.AppTools;
import com.yutou.bilibili.Tools.Log;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveInfo;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliUpInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/realTimeData/")
public class RealTimeDataController {
    @Resource
    IBiliBiliLiveService service;
    @ResponseBody
    @RequestMapping("get/query.do")
    public JSONObject queryToDayLiveData(int roomid,Date startTime,Date endTime){
        JSONObject json = new JSONObject();
        BilibiliUpInfo info = new BilibiliUpInfo();
        info.setRoomid(roomid);
        Live live = LiveUtils.liveContains(info);
        if(startTime==null){
            startTime=AppTools.getToDayStartTime();
        }
        if(endTime==null){
            endTime=AppTools.getToDayNowTime();
        }
        int userCount = service.queryLiveData(info.getRoomid(), startTime, endTime, new String[]{LiveData.INTERACT_WORD}).size();
        int vipCount = service.queryLiveData(info.getRoomid(),  startTime, endTime, new String[]{LiveData.ENTRY_EFFECT}).size();
        JSONObject data = new JSONObject();
        data.put("roomid", roomid);
        data.put("popular",live==null?0:live.getInfo().getPopular());
        data.put("userLength", userCount);
        data.put("vipLength", vipCount);

        JSONArray price = new JSONArray();
        //改写这个sql可以按时段统计数据
        List<Map<String, BigDecimal>> ps=service.queryTimeGroup(roomid, startTime, endTime);
        for (Map<String, BigDecimal> map : ps) {
            JSONObject pr = new JSONObject();
            pr.put("price", map.containsKey("price") ? map.get("price") : 0);
            pr.put("time", map.get("time"));
            price.add(pr);
        }

        JSONArray giftArray = new JSONArray();
        Map<String, Integer> map = service.queryGiftSize(roomid,startTime,endTime);
        for (String key : map.keySet()) {
            JSONObject item = new JSONObject();
            if(key==null||key.equals("null"))
                continue;
            item.put("giftName",key);
            item.put("size",map.get(key));
            giftArray.add(item);
        }
        data.put("price", price);
        data.put("gift", giftArray);
        json.put("data", data);
        json.put("code",0);
        return json;
    }
}
