package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.imdroid.beidou.service.CommonExcelService;
import com.imdroid.secapi.dto.*;
import lombok.RequiredArgsConstructor;
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.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * gnss统计消息 控制器
 *
 * @author LiGang
 * @date 2024/1/6 15:29
 */
@Controller
@RequiredArgsConstructor
public class GnssEhmController extends BasicController implements CommonExcelService<Ehm, EhmJoin> {
    final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    EhmMapper ehmMapper;

    final List<String> sortItems = new ArrayList<>();
    final List<String> sortSqls = new ArrayList<>();
    @RequestMapping("/page/gnss_ehm")
    public String gnssTrxMsg(Model m, HttpSession session) {
        initModel(m, session);
        if(sortItems.size()==0) {
           sortItems.add("离线次数");sortSqls.add("order by offlinecount desc");
           sortItems.add("离线时长");sortSqls.add("order by offlineminutes desc");
           sortItems.add("连续无效解时长");sortSqls.add("order by invalidresultminutes desc");
           sortItems.add("东标准差");sortSqls.add("order by stdeve desc");
           sortItems.add("北标准差");sortSqls.add("order by stdevn desc");
           sortItems.add("天标准差");sortSqls.add("order by stdevu desc");
           sortItems.add("有效率");sortSqls.add("order by validrate");
           sortItems.add("固定率");sortSqls.add("order by fixrate");
           sortItems.add("周跳次数");sortSqls.add("order by jumpcount desc");
        }
        m.addAttribute("sort_items", sortItems);
        m.addAttribute("begin_time",LocalDateTime.now().minusDays(1).format(dateFormatter));
        return "/page/gnss_ehm";
    }

    @RequestMapping("/gnss/ehm/list")
    @ResponseBody
    public JSONObject listTrxMsg(HttpSession session, int page, int limit, String searchParams) {
        if(searchParams==null){
            searchParams = "{'dgt_t.createtime':'"+ LocalDateTime.now().minusDays(1)+"','_sorttype':'离线次数'}";
        }
        return this.pageList(session, page, limit, searchParams);
    }

    @Override
    public String getSortSql(JSONObject search){
        if(search != null) {
            String sortItem = search.getString("_sorttype");
            int id = 0;
            for(String item:sortItems){
                if(item.equals(sortItem)){
                    return sortSqls.get(id);
                }
                id++;
            }
        }
        return null;
    }


    /**
     * 导出excel
     *
     * @param session      HttpSession
     * @param request HttpServletRequest
     * @param response     HttpServletResponse
     * @throws Exception
     */
    @RequestMapping("/gnss/ehm/export")
    @ResponseBody
    public void exportData(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.export(session, request, response);
    }

    /**
     * 获取实体类的class
     *
     * @return 实体类的class
     */
    @Override
    public Class<EhmJoin> getEntityClass() {
        return EhmJoin.class;
    }

    /**
     * 获取实体类对应的mybatis mapper
     *
     * @return 实体类对应的mybatis mapper
     */
    @Override
    public BaseMapper<Ehm> getMapper() {
        return ehmMapper;
    }

    @Override
    public String getOrderByColumn() {
        return null;
    }

    /**
     * 获取排序方式，默认降序
     *
     * @return 排序方式
     */
    @Override
    public String getOrder() {
        return null;
    }

    @Override
    public MPJQueryWrapper<Ehm> prepareQueryWrapper() {
        return new MPJQueryWrapper<Ehm>()
                .selectAll(Ehm.class)
                .select("d.devicetype as devicetype,d.model as model")
                .leftJoin("gnssdevices d on t.deviceid = d.deviceid");
    }

    /**
     * 是否联表查询
     *
     * @return 是否联表查询
     */
    @Override
    public boolean isJoinSelect() {
        return true;
    }

}
