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.*;
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.util.List;

/**
 * gnss设备解算数据 控制器
 */
@Controller
public class GnssCalcDataController extends BasicController implements CommonExcelService<GnssCalcData, GnssCalcData> {
    @Autowired
    GnssCalcDataMapper dataMapper;
    @Autowired
    TenantMapper tenantMapper;
    boolean isJoinQuery = false;

    @RequestMapping("/page/gnss_data_calc")
    public String gnssData(Model m, HttpSession session) {
        initModel(m, session);
        List<Tenant> tenants = tenantMapper.selectList(null);
        m.addAttribute("tenant_list", tenants);
        return "/page/gnss_data_calc";
    }

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

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

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

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

    @Override
    public void checkSearchParams(JSONObject search){
        if(search != null) {
            String parentId = search.getString("sl_d.parentid");
            isJoinQuery = (parentId != null && !parentId.isEmpty());
        }
        else isJoinQuery=false;
    }
    @Override
    public MPJQueryWrapper<GnssCalcData> prepareQueryWrapper() {
        if(isJoinQuery) {
            return new MPJQueryWrapper<GnssCalcData>()
                    .selectAll(GnssCalcData.class)
                    .leftJoin("gnssdevices d on t.deviceid = d.deviceid")
                    .orderByDesc("t.createtime");
        }
        else{
            return new MPJQueryWrapper<GnssCalcData>()
                    .selectAll(GnssCalcData.class)
                    .orderByDesc("createtime");
        }
    }

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

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