package com.ruoyi.interfaces.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.interfaces.domain.PtServiceLog;
import com.ruoyi.interfaces.mapper.PtServiceLogMapper;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/pt/service/log")
public class PtServiceLogController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(PtServiceLogController.class);

    @Autowired
    private PtServiceLogMapper ptServiceLogMapper;


    @ApiOperation("数据列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page list(int page, int rows) {
        PageHelper.startPage(page, rows);
        List<PtServiceLog> list = ptServiceLogMapper.selectAll();
        PageInfo<PtServiceLog> pageInfo = new PageInfo<>(list);
        return PageUtils.convert(pageInfo);
    }

    @ApiOperation("数据列表-条件查询")
    @PostMapping(value = "/listByFactor")
    @ResponseBody
    public Page listByFactor(@RequestBody PageParam<PtServiceLog> ptServiceLog) {
        PageHelper.startPage(ptServiceLog);
        List<PtServiceLog> list = ptServiceLogMapper.selectByFactor(ptServiceLog.getData());
        return PageUtils.convert(list);
    }


    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get")
    public ModelAndView get(@RequestParam(required = true) String rid) {
        ModelAndView mav = new ModelAndView();
        PtServiceLog ptServiceLog = ptServiceLogMapper.selectByPrimaryKey(rid);
        if (ptServiceLog == null) {
            return null;
        } else {
            mav.addObject("ptServiceLog", ptServiceLog);
            mav.setViewName("mng/pt_service_log_view");
            return mav;
        }
    }

    @RequestMapping(value = "/statAll", method = RequestMethod.GET)
    @ResponseBody
    public List<PtServiceLog> statAll(String flag) {
        List<PtServiceLog> list = new ArrayList<>();
        if ("StatAppToday".equals(flag)) {
            list = ptServiceLogMapper.statAllToday();
        }
        if ("StatAppThree".equals(flag)) {
            list = ptServiceLogMapper.statAppThree();
        }
        if ("StatAppWeek".equals(flag)) {
            list = ptServiceLogMapper.statAppWeek();
        }
        if ("StatAppCount".equals(flag)) {
            list = ptServiceLogMapper.statAll();
        }

        if ("StatSrvToday".equals(flag)) {
            list = ptServiceLogMapper.statSrvAllToday();
        }
        if ("StatSrvThree".equals(flag)) {
            list = ptServiceLogMapper.statSrvThree();
        }
        if ("StatSrvWeek".equals(flag)) {
            list = ptServiceLogMapper.statSrvWeek();
        }
        if ("StatSrvCount".equals(flag)) {
            list = ptServiceLogMapper.statSrvAll();
        }
        return list;
    }

    @RequestMapping(value = "/statDayByAppId")
    public ModelAndView statDayByAppId(String appId) {
        ModelAndView mav = new ModelAndView();
        List<PtServiceLog> list = ptServiceLogMapper.statDayByAppId(appId);
        mav.addObject("list", list);
        mav.setViewName("pms/service_log_day");
        return mav;
    }

    @RequestMapping(value = "/statDayBySrvId")
    public ModelAndView statDayBySrvId(String srvId) {
        ModelAndView mav = new ModelAndView();
        List<PtServiceLog> list = ptServiceLogMapper.statDayBySrvId(srvId);
        mav.addObject("list", list);
        mav.setViewName("pms/service_log_day_srv");
        return mav;
    }

    @RequestMapping(value = "/check/day/data")
    @ResponseBody
    public ModelAndView checkDayData(String type, String para) {
        ModelAndView mav = new ModelAndView();

        if ("server".equals(type)) {
            List<PtServiceLog> list = ptServiceLogMapper.statDayBySrvId(para);
            mav.addObject("list", list);
            mav.addObject("title", "统计-按服务统计访问总数");
            mav.setViewName("pms/check_day_data");
        }

        return mav;
    }


}
