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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.inspect.access.base.feign.AccessFeign;
import com.inspect.access.base.model.RecordCompleteModel;
import com.inspect.access.base.vo.DeviceStorageVo;
import com.inspect.access.base.vo.RecordCompleteVo;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.mapper.BasedataEqpBookMapper;
import com.inspect.metadata.statis.domain.BasedataStatisNvr;
import com.inspect.metadata.statis.mapper.BasedataStatisNvrMapper;
import com.inspect.metadata.task.service.IBasedataNvrStorageStatusTaskService;
import com.inspect.base.websocket.configure.WsPool;
import com.inspect.base.core.domain.Response;
import com.inspect.base.core.utils.DateUtils;
import com.inspect.base.redis.service.RedisService;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BasedataNvrStorageStatusTaskServiceImpl implements IBasedataNvrStorageStatusTaskService {
   @Autowired
   BasedataEqpBookMapper basedataEqpbookMapper;
   @Autowired
   BasedataStatisNvrMapper basedataStatisNvrMapper;
   @Autowired
   RabbitTemplate rabbitTemplate;
   @Autowired
   AccessFeign accessFeign;
   @Autowired
   RedisService redisService;

   public void synNvrStorageStatusFromSdk() {
      Date statisDay = DateUtils.theDayBeforeStartTime(new Date());
      BasedataEqpBook basedataEqpbook = new BasedataEqpBook();
      basedataEqpbook.setType("11");
      List<BasedataEqpBook> eqpbooks = this.basedataEqpbookMapper.selectBasedataEqpBookList(basedataEqpbook);
      if(null != eqpbooks && eqpbooks.size() != 0) {
         Iterator var4 = eqpbooks.iterator();

         while(true) {
            while(true) {
               BasedataEqpBook book;
               RecordCompleteModel loginModel;
               RecordCompleteVo vo;
               do {
                  Response completeVo;
                  do {
                     String nvr_ip;
                     String nvr_port;
                     String nvr_username;
                     String nvr_password;
                     do {
                        do {
                           do {
                              do {
                                 if(!var4.hasNext()) {
                                    return;
                                 }

                                 book = (BasedataEqpBook)var4.next();
                                 nvr_ip = book.getIpAddr();
                                 nvr_port = book.getPort();
                                 nvr_username = book.getUser();
                                 nvr_password = book.getPassword();
                              } while(null == nvr_ip);
                           } while(null == nvr_port);
                        } while(null == nvr_username);
                     } while(null == nvr_password);

                     System.out.println("调用nvr获得录像完整性，时间：" + DateUtils.getTime());
                     loginModel = new RecordCompleteModel();
                     loginModel.setIp(nvr_ip);
                     loginModel.setPort(Integer.parseInt(nvr_port));
                     loginModel.setUsername(nvr_username);
                     loginModel.setPassword(nvr_password);
                     loginModel.setDate(DateUtils.theDayBeforeStartTime(new Date()));
                     completeVo = this.accessFeign.record(loginModel);
                     System.out.println("获得nvr获得录像完整性：" + completeVo.toString());
                  } while(null == completeVo);

                  vo = (RecordCompleteVo)completeVo.getData();
               } while(null == vo);

               int complete = vo.getComplete();
               int duration = vo.getDuration();
               int cyle = vo.getCycle();
               System.out.println("完整性，complete::" + complete);
               System.out.println("录像时长，duration::" + duration);
               System.out.println("录像周期，cyle::" + cyle);
               BasedataStatisNvr basedataStatisNvr = new BasedataStatisNvr();
               basedataStatisNvr.setStatisDay(statisDay);
               basedataStatisNvr.setEqpBookId(book.getEqpBookId());
               List<BasedataStatisNvr> list = this.basedataStatisNvrMapper.selectBasedataStatisNvrList(basedataStatisNvr);
               if(null != list && list.size() != 0) {
                  BasedataStatisNvr basedataStatisNvrForUpdate = (BasedataStatisNvr)list.get(0);
                  basedataStatisNvrForUpdate.setComplete(complete + "");
                  basedataStatisNvrForUpdate.setDuration(duration + "");
                  basedataStatisNvrForUpdate.setCycle(Long.valueOf((long)cyle));
                  basedataStatisNvrForUpdate.setPatrolDeviceCode(book.getPatrolDeviceCode());
                  basedataStatisNvrForUpdate.setPatrolDeviceName(book.getPatrolDeviceName());
                  basedataStatisNvrForUpdate.setUpdateTime(DateUtils.getNowDate());
                  this.basedataStatisNvrMapper.updateBasedataStatisNvr(basedataStatisNvrForUpdate);
                  this.gerAllcapacityFromSdk(basedataStatisNvrForUpdate, loginModel);
                  this.parseMessage(basedataStatisNvrForUpdate);
               } else {
                  basedataStatisNvr.setComplete(complete + "");
                  basedataStatisNvr.setDuration(duration + "");
                  basedataStatisNvr.setCycle(Long.valueOf((long)cyle));
                  basedataStatisNvr.setPatrolDeviceCode(book.getPatrolDeviceCode());
                  basedataStatisNvr.setPatrolDeviceName(book.getPatrolDeviceName());
                  basedataStatisNvr.setCreateTime(DateUtils.getNowDate());
                  this.basedataStatisNvrMapper.insertBasedataStatisNvr(basedataStatisNvr);
                  this.gerAllcapacityFromSdk(basedataStatisNvr, loginModel);
                  this.parseMessage(basedataStatisNvr);
               }
            }
         }
      }
   }

   public void parseMessage(BasedataStatisNvr basedataStatisNvr) {
      String dateTimeNow = DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss");
      JSONObject returnJsonObject = new JSONObject();
      JSONObject montJsonObject = new JSONObject();
      montJsonObject.put("msgType", "montNvrInfo");
      returnJsonObject.put("data", basedataStatisNvr);
      returnJsonObject.put("receiveTime", dateTimeNow);
      returnJsonObject.put("event", "mont/montNvrInfo");
      String jsonString = JSON.toJSONString(returnJsonObject);
      this.redisService.setCacheObject("mont_nvr_info:" + basedataStatisNvr.getPatrolDeviceCode(), returnJsonObject);
      WsPool.sendMessageToAll(jsonString);
   }

   public void gerAllcapacityFromSdk(BasedataStatisNvr basedataStatisNvr, RecordCompleteModel loginModel) {
      if(null != basedataStatisNvr.getStatisNvrId()) {
         System.out.println("查询nvr存储容量信息，时间：" + DateUtils.getTime());
         Response<DeviceStorageVo> storageVoResponse = this.accessFeign.storage(loginModel);
         System.out.println("查询nvr存储容量结果，storageVoR：" + storageVoResponse.toString());
         if(null == storageVoResponse) {
            return;
         }

         DeviceStorageVo storageVoRData = (DeviceStorageVo) storageVoResponse.getData();
         double totalSpace = storageVoRData.getnTotalSpace();
         double usecapacity = storageVoRData.getnUsedSpace();
         basedataStatisNvr.setAllCapacity(String.valueOf(totalSpace));
         basedataStatisNvr.setUseCapacity(String.valueOf(usecapacity));
         this.basedataStatisNvrMapper.updateBasedataStatisNvr(basedataStatisNvr);
      }

   }
}
