package com.inspect.metadata.montdata.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspect.metadata.montdata.service.IBasedataMontQueryService;
import com.inspect.metadata.prodevmnt.service.IBasedataDeviceService;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.common.security.annotation.RequiresPermissions;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping({"/montdata/querydata"})
public class BasedataMontQueryController extends BaseController {
    @Resource
    private IBasedataMontQueryService basedataMontQueryService;
    @Resource
    private IBasedataDeviceService basedataDeviceService;

    @RequiresPermissions({"basedata/montdata:evndata:list"})
    @GetMapping({"/getMontEvndDta/{deviceCode}"})
    public AjaxResult getEvnData(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontEvnData(deviceCode));
    }

    @RequiresPermissions({"basedata/montdata:evndata:list"})
    @GetMapping({"/getMontPatrolDeviceAlmabnDataByKey/{deviceCode}"})
    public AjaxResult getMontPatrolDeviceAlmAbnData(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontPatrolDeviceAlmAbnData(deviceCode));
    }

    @GetMapping({"/getPatrolDeviceRunDataCacheByKey/{deviceCode}"})
    public AjaxResult getPatrolDeviceRunDataCacheByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontPatrolDeviceRunData(deviceCode));
    }

    @GetMapping({"/getPatrolDeviceStaDataCacheByKey/{deviceCode}"})
    public AjaxResult getPatrolDeviceStaDataCacheByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontPatrolDeviceStaData(deviceCode));
    }

    @GetMapping({"/getAllNvrDeviceOlineStatusDataByCache"})
    public AjaxResult getAllNvrDeviceOlineStatusDataByCache() {
        return AjaxResult.success(this.basedataMontQueryService.getAllNvrDeviceOlineStatusDataByCache());
    }

    @GetMapping({"/getMontNestRunDataCacheByKey/{deviceCode}"})
    public AjaxResult getMontNestRunDataCacheByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontNestRunData(deviceCode));
    }

    @GetMapping({"/getMontNestStaDataCacheByKey/{deviceCode}"})
    public AjaxResult getMontNestStaDataCacheByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontNestStaData(deviceCode));
    }

    @GetMapping({"/getMontPatDevCoordCacheByKey/{deviceCode}"})
    public AjaxResult getMontPatDevCoordCacheByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontPatDevCoord(deviceCode));
    }

    @GetMapping({"/getMontPatDevPatRouteCacheByKey/{deviceCode}"})
    public AjaxResult getMontPatDevPatRouteCacheByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontPatDevPatRoute(deviceCode));
    }

    @GetMapping({"/getMsgToWrz/{deviceCode}/{type}"})
    public JSONObject getMsgToWrz(@PathVariable("deviceCode") String deviceCode, @PathVariable("type") String type) {
        JSONObject json;
        HashMap map;
        if ("2".equals(type)) {
            logger.info("DEVICE-RUNNING-DATA: {}, key: {}, type: {}", new Date(), deviceCode, type);
            json = this.basedataMontQueryService.getMontPatrolDeviceRunData(deviceCode);
            if (null == json) {
                map = new HashMap<>();
                map.put("Items", new ArrayList<>());
                json = new JSONObject();
                json.put("data", map);
            }

            logger.info("DEVICE-RUNNING-DATA: {}, result: {}", new Date(), json);
            return json;
        } else if ("21".equals(type)) {
            json = this.basedataMontQueryService.getMontEvnData(deviceCode);
            if (null == json) {
                map = new HashMap<>();
                map.put("Items", new ArrayList<>());
                json = new JSONObject();
                json.put("data", map);
            }

            logger.info("ENVIRONMENT-DATA: {}, key: {}, type: {}", new Date(), deviceCode, type);
            logger.info("ENVIRONMENT-DATA: {}, result: {} ", new Date(), json);
            return json;
        } else if ("1".equals(type)) {
            json = this.basedataMontQueryService.getMontPatrolDeviceStaData(deviceCode);
            if (null == json) {
                map = new HashMap<>();
                map.put("Items", new ArrayList<>());
                json = new JSONObject();
                json.put("data", map);
            }

            logger.info("DEVICE-STATE-DATA: {}, key: {}, type: {}", new Date(), deviceCode, type);
            logger.info("DEVICE-STATE-DATA: {}, result: {}", new Date(), json);
            return json;
        } else if ("4".equals(type)) {
            json = this.basedataMontQueryService.getMontPatDevCoord(deviceCode);
            if (null == json) {
                map = new HashMap<>();
                map.put("Items", new ArrayList<>());
                json = new JSONObject();
                json.put("data", map);
            }

            logger.info("DEVICE-POS-DATA: {}, key: {}, type: {}", new Date(), deviceCode, type);
            logger.info("DEVICE-POS-DATA: {}, result: {}", new Date(), json);
            return json;
        } else {
            JSONObject resultJson = new JSONObject();
            resultJson.put("code", 200);
            resultJson.put("msg", "无此类型！");
            return resultJson;
        }
    }

    @GetMapping({"/getMsgToZk/{type}"})
    public AjaxResult getMsgToZk(@PathVariable("type") int type) {
        List<JSONObject> JsonList = new ArrayList<>();
        List<String> list = this.basedataDeviceService.getDeviceAllDeviceCode();
        Iterator iterator;
        Object str;
        JSONObject montNestStaData;
        switch (type) {
            case 2:
                iterator = list.iterator();

                while (iterator.hasNext()) {
                    str = iterator.next();
                    montNestStaData = this.basedataMontQueryService.getMontPatrolDeviceRunData(str.toString());
                    if (null != montNestStaData) {
                        JsonList.add(montNestStaData.getJSONObject("data"));
                    }
                }

                return AjaxResult.success(JsonList);
            case 21:
                if (CollectionUtils.isNotEmpty(list)) {
                    String key = list.get(0);
                    JSONObject montEvnData = this.basedataMontQueryService.getMontEvnData(key);
                    if (montEvnData != null) {
                        JsonList.add(montEvnData.getJSONObject("data"));
                    }
                }
                break;
            case 10004:
                iterator = list.iterator();

                while (iterator.hasNext()) {
                    str = iterator.next();
                    montNestStaData = this.basedataMontQueryService.getMontNestRunData(str.toString());
                    if (null != montNestStaData) {
                        JsonList.add(montNestStaData.getJSONObject("data"));
                    }
                }

                return AjaxResult.success(JsonList);
            case 20001:
                iterator = list.iterator();

                while (iterator.hasNext()) {
                    str = iterator.next();
                    montNestStaData = this.basedataMontQueryService.getMontNestStaData(str.toString());
                    if (null != montNestStaData) {
                        JsonList.add(montNestStaData.getJSONObject("data"));
                    }
                }

                return AjaxResult.success(JsonList);
            default:
                System.out.println("---------getMsgToZk-----" + new Date() + "-----类型=" + type);
                System.out.println("---------getMsgToZk-----" + new Date() + "-----结果=" + AjaxResult.success(JsonList));
        }

        return AjaxResult.success(JsonList);
    }

    @GetMapping({"/getMontNvrByKey/{deviceCode}"})
    public AjaxResult getMontNvrByKey(@PathVariable("deviceCode") String deviceCode) {
        return AjaxResult.success(this.basedataMontQueryService.getMontNvrByKey(deviceCode));
    }
}
