package com.inspect.metadata.montdata.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.mapper.BasedataEqpBookMapper;
import com.inspect.metadata.montdata.mapper.BasedataMontEvnDataMapper;
import com.inspect.metadata.montdata.service.IBasedataMontQueryService;
import com.inspect.base.redis.service.RedisService;

import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class BasedataMontQueryServiceImpl implements IBasedataMontQueryService {
    private final BasedataMontEvnDataMapper basedataMontEvndataMapper;
    final
    RedisService redisService;
    final
    BasedataEqpBookMapper basedataEqpbookMapper;

    public BasedataMontQueryServiceImpl(BasedataMontEvnDataMapper basedataMontEvndataMapper, RedisService redisService, BasedataEqpBookMapper basedataEqpbookMapper) {
        this.basedataMontEvndataMapper = basedataMontEvndataMapper;
        this.redisService = redisService;
        this.basedataEqpbookMapper = basedataEqpbookMapper;
    }

    public JSONObject getMontEvnData(String deviceCode) {
        JSONObject jsonObject = null;
        if (this.redisService.hasKey("mont_evn_info:" + deviceCode)) {
            jsonObject = this.redisService.getCacheObject("mont_evn_info:" + deviceCode);
        } else {
            jsonObject = this.redisService.getCacheObject("mont_evn_info:");
        }

        return jsonObject;
    }

    public JSONObject getMontPatrolDeviceAlmAbnData(String patrolDeviceCode) {
        return this.redisService.getCacheObject("mont_patrol_device_almabn:" + patrolDeviceCode);
    }

    public JSONObject getMontPatrolDeviceRunData(String patrolDeviceCode) {
        return this.redisService.getCacheObject("mont_patrol_device_rundata:" + patrolDeviceCode);
    }

    public JSONObject getMontPatrolDeviceStaData(String patrolDeviceCode) {
        return this.redisService.getCacheObject("mont_patrol_device_stadata:" + patrolDeviceCode);
    }

    public JSONArray getAllNvrDeviceOlineStatusDataByCache() {
        JSONArray jsonArray = new JSONArray();
        BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
        basedataEqpbook.setType("11");
        List<BasedataEqpBook> eqpBooks = this.basedataEqpbookMapper.selectBasedataEqpBookList(basedataEqpbook);
        if (null != eqpBooks && !eqpBooks.isEmpty()) {

            for (BasedataEqpBook book : eqpBooks) {
                String patrolDeviceCode = book.getPatrolDeviceCode();
                if (null != patrolDeviceCode) {
                    JSONObject jsonObject = this.redisService.getCacheObject("mont_patrol_device_online_status:" + patrolDeviceCode);
                    if (null != jsonObject) {
                        jsonObject.put("eqpbookId", book.getEqpBookId());
                        jsonArray.add(jsonObject);
                    }
                }
            }
        }

        return jsonArray;
    }

    public JSONObject getMontNestRunData(String nestCode) {
        return this.redisService.getCacheObject("mont_nest_run_data:" + nestCode);
    }

    public JSONObject getMontNestStaData(String nestCode) {
        return this.redisService.getCacheObject("mont_nest_stadata:" + nestCode);
    }

    public JSONObject getMontPatDevCoord(String deviceCode) {
        return this.redisService.getCacheObject("mont_patrol_device_coord:" + deviceCode);
    }

    public JSONObject getMontPatDevPatRoute(String deviceCode) {
        return this.redisService.getCacheObject("mont_patdev_patroute:" + deviceCode);
    }

    public JSONObject getMontNvrByKey(String deviceCode) {
        return this.redisService.getCacheObject("mont_nvr_info:" + deviceCode);
    }
}
