package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.*;

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

@Controller
public class GnssFwdController extends BasicController implements CommonExcelService<FwdRecord, FwdRecord> {
    @Autowired
    GnssGroupFwdMapper gnssGroupFwdMapper;
    @Autowired
    FwdRecordMapper fwdRecordMapper;

    /********* 推送页面 *********/
    @RequestMapping("/page/fwd_agents")
    public String fwdAgents(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/fwd_agents";
    }

    @RequestMapping("/page/fwd_records")
    public String fwdRecords(Model m, HttpSession session) {
        initModel(m, session);
        //以下用于下拉框数据
        List<GnssGroupFwd> gnssGroupFwds = gnssGroupFwdMapper.selectList(null);
        m.addAttribute("gnss_group_fwd_list", gnssGroupFwds);

        return "/page/fwd_records";
    }

    /********* 推送数据 *********/
    @RequestMapping("/fwd/agents")
    @ResponseBody
    public JSONObject listAgents(int page, int limit) {
        Page<GnssGroupFwd> pageable = new Page<>(page, limit);
        IPage<GnssGroupFwd> cs = gnssGroupFwdMapper.selectPage(pageable, null);

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

    @RequestMapping("/fwd/records")
    @ResponseBody
    public JSONObject listRecords(HttpSession session, Integer page, Integer limit, String searchParams) {
        return this.pageList(session, page, limit, searchParams);
    }

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

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