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.entity.Tenant;
import com.imdroid.beidou.entity.TenantMapper;
import com.imdroid.beidou.service.CommonExcelService;
import com.imdroid.secapi.dto.GnssStatus;
import com.imdroid.secapi.dto.GnssStatusJoin;
import com.imdroid.secapi.dto.GnssStatusMapper;
import com.imdroid.secapi.dto.WarningCfg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GnssStatusController extends BasicController implements CommonExcelService<GnssStatus, GnssStatusJoin> {
    @Autowired
    GnssStatusMapper gnssStatusMapper;
    @Autowired
    TenantMapper tenantMapper;

    /**** 推送页面  *****/
    @RequestMapping("/page/gnss_status")
    public String gnssStatus(Model m, HttpSession session) {
        initModel(m, session);
        List<Tenant> tenants = tenantMapper.selectList(null);
        m.addAttribute("tenant_list", tenants);
        return "/page/gnss_status";
    }


    /**** 推送数据  *****/
    @RequestMapping("/gnss/status/list")
    @ResponseBody
    public JSONObject list(HttpSession session, Integer page, Integer limit, String searchParams) {
        return this.pageList(session, page, limit, searchParams);
    }

    @RequestMapping("/gnss/status/info")
    @ResponseBody
    public String info(@RequestParam Integer warningCode) {
        List<String> warningInfo= WarningCfg.parseCode(warningCode);
        if(warningInfo.size() == 0) return "无告警";
        else return warningInfo.toString();
    }

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

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

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

    /**
     * 获取排序字段，默认降序排序
     *
     * @return 排序字段
     */
    @Override
    public String getOrderByColumn() {
        return null;
    }

    /**
     * 准备QueryWrapper，额外处理
     */
    @Override
    public MPJQueryWrapper<GnssStatus> prepareQueryWrapper() {
        return new MPJQueryWrapper<GnssStatus>()
                .selectAll(GnssStatus.class)
                .select("d.devicetype as devicetype")
                .leftJoin("gnssdevices d on t.deviceid = d.deviceid")
                .orderByDesc("t.updatetime");
    }

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

    @Override
    public String tenantIdField() {
        return "d.tenantid";
    }
}
