package com.inspect.patrol.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.exception.ServiceException;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.base.redis.service.RedisService;
import com.inspect.patrol.service.BasedataEqpbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Video2NvrComponent {
    private final String PATROL_EQP_BOOK_VIDEO_NVR = "PATROL_EQPBOOK_VIDEO_NVR";
    private final BasedataEqpbookService basedataEqpbookService;
    private final RedisService redisService;

    public Video2NvrComponent(BasedataEqpbookService basedataEqpbookService, RedisService redisService) {
        this.basedataEqpbookService = basedataEqpbookService;
        this.redisService = redisService;
    }

    public String toNvrCode(String videoCode) {
        if (StringUtils.isEmpty(videoCode)) {
            return "";
        } else {
            Object value = this.redisService.redisTemplate.opsForHash().get(PATROL_EQP_BOOK_VIDEO_NVR, videoCode);
            if (value == null) {
//                Object object = this.basedataEqpbookService.list((new JSONObject()).fluentPut("patrolDeviceCode", videoCode)).getRows().get(0);
                TableDataInfo tableDataInfo = basedataEqpbookService.list((new JSONObject()).fluentPut("patrolDeviceCode", videoCode));
                if (tableDataInfo == null || tableDataInfo.getRows() == null || tableDataInfo.getRows().isEmpty()) {
                    return "";
                }
                Object object = tableDataInfo.getRows().get(0);
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));

                String videoNvrCode = jsonObject.getString("videoNvrCode");
                if (StringUtils.isEmpty(videoCode)) {
                    return "";
                } else {
                    this.redisService.redisTemplate.opsForHash().put(PATROL_EQP_BOOK_VIDEO_NVR, videoCode, videoNvrCode);
                    return videoNvrCode;
                }
            } else {
                return (String) value;
            }
        }
    }
}
