package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.HttpResult;
import com.imdroid.beidou.service.CommonExcelService;
import com.imdroid.secapi.client.RtcmClient;
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.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class WarningController extends BasicController implements CommonExcelService<WarningMsg, WarningMsg> {
    @Autowired
    WarningMsgMapper warningMsgMapper;

    @Autowired
    WarningCfgMapper warningCfgMapper;
    @Autowired
    RtcmClient rtcmClient;

    public static final Map<Integer, String> warningMap = new HashMap<>();

    @PostConstruct
    public static void init() {
        warningMap.put(WarningCfg.TYPE_DEVICE_OFF_LINE, WarningCfg.TYPE_NAME_DEVICE_OFF_LINE);
        //warningMap.put(WarningCfg.TYPE_BS_NO_RESULT, WarningCfg.TYPE_NAME_BS_NO_RESULT);
        warningMap.put(WarningCfg.TYPE_NO_FIXED_RESULT, WarningCfg.TYPE_NAME_NO_FIXED_RESULT);
        warningMap.put(WarningCfg.TYPE_LESS_INUSE_SAT, WarningCfg.TYPE_NAME_LESS_INUSE_SAT);
        warningMap.put(WarningCfg.TYPE_LESS_D3XX, WarningCfg.TYPE_NAME_LESS_D3XX);
        warningMap.put(WarningCfg.TYPE_LESS_B562, WarningCfg.TYPE_NAME_LESS_B562);
        warningMap.put(WarningCfg.TYPE_LOW_VOLTAGE, WarningCfg.TYPE_NAME_LOW_VOLTAGE);
        warningMap.put(WarningCfg.TYPE_LOW_RSSI, WarningCfg.TYPE_NAME_LOW_RSSI);
        warningMap.put(WarningCfg.TYPE_RX_MUCH_UNKNOWN, WarningCfg.TYPE_NAME_RX_MUCH_UNKNOWN);
        warningMap.put(WarningCfg.TYPE_INCLINE, WarningCfg.TYPE_NAME_INCLINE);
    }

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

    @RequestMapping("/page/warning_cfg")
    public String warningCfg(Model m, HttpSession session) {
        initModel(m, session);
        return "/page/warning_cfg";
    }

    @RequestMapping("/page/table/warning_add")
    public String warningUpdate(Model m)  {
        m.addAttribute("warn_map", warningMap);
        return "/page/table/warning_add";
    }


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

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

    @RequestMapping("/warning/cfg")
    @ResponseBody
    public JSONObject listCfg(int page, int limit, String searchParams) {
        Page<WarningCfg> pageable = new Page<>(page, limit);

        IPage<WarningCfg> cs = warningCfgMapper.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;
    }

    @PostMapping("/warning/update")
    @ResponseBody
    public String update(@RequestBody JSONObject object) throws Exception {
        // 从请求参数中创建对象
        WarningCfg cfg = JSONObject.toJavaObject(object, WarningCfg.class);
        cfg.setTypename(warningMap.get(cfg.getType()));

        // 检测该对象是否已存在
        int num = 0;
        QueryWrapper<WarningCfg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("devicetype", cfg.getDevicetype());
        queryWrapper.eq("type", cfg.getType());
        if (warningCfgMapper.selectCount(queryWrapper) == 0) {
            num = warningCfgMapper.insert(cfg);
        } else {
            num = warningCfgMapper.update(cfg, queryWrapper);
        }

        if (num == 0) {
            return HttpResult.failed();
        } else {
            rtcmClient.warningParamChanged();
            return HttpResult.ok();
        }

    }

    @PostMapping("/warning/delete")
    @ResponseBody
    public String delete(@RequestParam Integer del_id) throws Exception {
        int num = warningCfgMapper.deleteById(del_id);
        if (num == 0) {
            return HttpResult.failed();
        } else {
            rtcmClient.warningParamChanged();
            return HttpResult.ok();
        }
    }

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

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