package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imdroid.beidou.service.CommonExcelService;
import com.imdroid.secapi.dto.GnssSingleData;
import com.imdroid.secapi.dto.GnssSingleDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Controller
public class GnssSingleDataController extends BasicController implements CommonExcelService<GnssSingleData, GnssSingleData>  {

    @Autowired
    GnssSingleDataMapper gnssSingleDataMapper;

    final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    @RequestMapping("/page/gnss_single_data")
    public String gnssSingleData(Model m, HttpSession session){
        initModel(m, session);

        return "/page/gnss_single_data";
    }


    @RequestMapping("/gnss/data/list_single_data")
    @ResponseBody
    public JSONObject listData(HttpSession session, Integer page, Integer limit, String searchParams) {
        // 检查查询条件
        JSONObject search = null;
        if (searchParams != null) {
            search = (JSONObject) JSONObject.parse(searchParams);

            String deviceId = search.getString("sl_deviceid");  // sql where like deviceid
            Integer freqency = search.getInteger("freqency");
            String begin = search.getString("dgt_.createtime");
            String end = search.getString("dlt_.createtime");

            if(deviceId != null && !deviceId.isEmpty() && freqency!=0){
                Page pageable = new Page<>(page == null ? 1 : page, limit == null ? 10 : limit);
                // 缺省按1小时采样，如果时间跨度较大，则按最多300条记录的采样率采样
                int sample = 6;

                if(freqency == 2){
                    LocalDateTime endTime = LocalDateTime.now();
                    LocalDateTime beginTime = endTime.minusDays(30);
                    if(begin!=null && !begin.isEmpty()){
                        beginTime = LocalDateTime.parse(begin,df);
                    }
                    if(end!=null && !end.isEmpty()){
                        endTime = LocalDateTime.parse(end,df);
                    }
                    Duration duration = Duration.between(beginTime, endTime);
                    int hours = (int)duration.toHours();
                    if(hours > 300) sample = hours*6/300;
                }
                Page<GnssSingleData> calcDataList = gnssSingleDataMapper.queryByDeviceId(pageable, deviceId,begin,end,sample);

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", 0);
                jsonObject.put("msg", "");
                jsonObject.put("count", calcDataList.getTotal());
                jsonObject.put("data", calcDataList.getRecords());
                return jsonObject;
            }
        }

        return this.pageList(session, page, limit, searchParams);
    }

    @RequestMapping("/gnss/data/single/export")
    @ResponseBody
    public void exportData(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.export(session, request, response);
    }

    @Override
    public Class<GnssSingleData> getEntityClass() {
        return GnssSingleData.class;
    }

    @Override
    public BaseMapper<GnssSingleData> getMapper() {
        return gnssSingleDataMapper;
    }
}
