package com.yourtion.bigdata.spark.project.web.controller;

import com.yourtion.bigdata.spark.project.web.dao.CourseClickCountDAO;
import com.yourtion.bigdata.spark.project.web.domain.CourseClickCount;
import com.yourtion.bigdata.spark.project.web.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * web层
 *
 * @author yourtion
 */
@RestController
public class StatApp {
    private static Map<String, String> courses = new HashMap<>();

    static {
        courses.put("112", "Spark SQL慕课网日志分析");
        courses.put("128", "10小时入门大数据");
        courses.put("145", "深度学习之神经网络核心原理与算法");
        courses.put("146", "强大的Node.js在Web开发的应用");
        courses.put("131", "Vue+Django实战");
        courses.put("130", "Web前端性能优化");
    }

    @Autowired
    CourseClickCountDAO courseClickCountDAO;


    @RequestMapping(value = "/course_click_count_dynamic", method = RequestMethod.POST)
    @ResponseBody
    public List<CourseClickCount> courseClickCount(@RequestParam Optional<String> day) throws Exception {

        List<CourseClickCount> list = courseClickCountDAO.query(day.orElseGet(Utils::getDay));
        for (CourseClickCount model : list) {
            model.setName(courses.get(model.getName().substring(9)));
        }

        return list;
    }

    @RequestMapping(value = "/echarts", method = RequestMethod.GET)
    public ModelAndView echarts() {
        return new ModelAndView("echarts");
    }

}
