package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imdroid.beidou.entity.Tenant;
import com.imdroid.secapi.dto.GnssRawData;
import com.imdroid.secapi.dto.GnssRawDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class GnssRawDataController extends BasicController{
    @Autowired
    GnssRawDataMapper dataMapper;
    @RequestMapping("/page/gnss_data_raw")
    public String gnssDataRaw(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/gnss_data_raw";
    }

    /**** 推送数据  *****/
    @RequestMapping("/gnss/data/list_raw")
    @ResponseBody
    public JSONObject list(HttpSession session, int page, int limit, String searchParams) {
        Page<GnssRawData> pageable = new Page<>(page, limit);
        QueryWrapper<GnssRawData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createtime");
        Integer tenantId = getTenantId(session);
        if(tenantId != null && tenantId != Tenant.SAAS_PROVIDER_ID) {
            queryWrapper.eq("tenantid", tenantId);
        }

        // 条件查询
        if(searchParams != null) {
            JSONObject search = (JSONObject) JSONObject.parse(searchParams);
            //设备号
            String deviceid = search.getString("deviceid");
            if (!StringUtils.isEmpty(deviceid)) {
                queryWrapper.like("deviceid", deviceid);
            }
            //时间范围
            String q_start = search.getString("q_start");
            if (!StringUtils.isEmpty(q_start)) {
                queryWrapper.ge("createtime", q_start);
            }
            String q_end = search.getString("q_end");
            if (!StringUtils.isEmpty(q_end)) {
                queryWrapper.le("createtime", q_end+" 23:59:59");
            }
        }
        IPage<GnssRawData> cs = dataMapper.selectPage(pageable, queryWrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", cs.getTotal());
        jsonObject.put("data", cs.getRecords());
        return jsonObject;
    }
}
