text: const cloneDeep = require('lodash/cloneDeep');
  const ChatBot = require('dingtalk-robot-sender');
  const { transformBinanceUsdtWsDepthData ,transformBinanceSpotWsDepthData} = require('./transform')
  const uniquBy = require('lodash/uniqBy');
  const forEach = require('lodash/forEach');
  //influxdb
  const { DBLink } = require('./../../../dbLink/link_influx');
  const coin_depth = require('./../../../schema/coin_depth.json');
  const dbLink = new DBLink(host = 'localhost', port = 8086, database = 'bfs');
  
  
  dbLink.loadModelConfigs([coin_depth]);
  var binance_depth_1s_arr = [];
  
  //30s执行一次看保存的时间和当前时间之间差值是否大于三分钟
  
  const robot = new ChatBot({
      // webhook: 'https://oapi.dingtalk.com/robot/send?access_token=a4f986512c65e050ca89e6ab644511d9ccf7ce73b1774ca26fe888e8ab11df7a',//正式群
      webhook: 'https://oapi.dingtalk.com/robot/send?access_token=c6598be3d46fab68932af4d9409b31d46396a450772edf9bd44044c04d474207'//单独群
  });
  
  let lastWsDataCheckTime = Date.now();
  const wsDataCacheMap = {};
  const wsInstanceMap = {};
  let wsIsConn = true;
  let isSendOk = true;
  
  let sendMessage = (title, board) => {
      if (board === '') return
      let textContent = {
          "msgtype": "markdown",
          "markdown": {
              "title": `[${title}]`,
              "text": `${board}`
          },
          "at": {
              "atMobiles": [
                  "18437151653"
              ],
              "isAtAll": false
          }
      }
      console.log('发送消息之前', board)
      // return 
      //   发送钉钉消息
      robot.send(textContent)
          .then((res) => {
              // TODO
              console.log(res.data, 'robot1发送成功')
              isSendOk = true;
          }).catch(e => console.log('发送失败原因', e));
  
  }
  
  function checkWsData(data, ws) {
      // console.log(data.length,'...')
      if (!data.length) return
      let key = data[0].instrument_id;
      if (!wsInstanceMap[ws.id]) {
          wsInstanceMap[ws.id] = ws;
      }
      wsDataCacheMap[key] = { time: data[0].time, ws_id: ws.id };
  
      if (Date.now() - lastWsDataCheckTime < 30 * 1000) {
          return;
      }
      lastWsDataCheckTime = Date.now();
  
      Object.keys(wsDataCacheMap).forEach(key => {
          let res = wsDataCacheMap[key];
          if (!res) {
              return;
          }
          let time = res.time;
          let ws_id = res.ws_id;
          // console.log(Date.now()-time,'........',time)
          if (Date.now() - time > 30 * 1 * 1000) {
              console.error("data timeout!", key);
              wsIsConn = false;
              isSendOk = false;
              let board = `#### [加一][深度信息] @18437151653 \n>`;
              let o = `#### ${'data timeout'}:${key}\n>`;
              sendMessage('深度信息',board+o);
              let ws = wsInstanceMap[ws_id];
              if (ws) {
                  console.log("restart ws", ws.stream);
                  ws.restart();
              }
          }else{
              wsIsConn = true
          }
      });
  }
  
  function loop(fn, time) {
      fn();
      setTimeout(() => loop(fn, time), time);
    }
  loop(()=>{
      if(wsIsConn&&isSendOk===false){
          let board = `#### [加一][深度信息] @18437151653 \n>`;
          let o = `#### ${'data ok'}:恢复连接\n>`;
          sendMessage('深度信息',board+o);
      }
  },3 * 60 * 1000)
  
  async function saveWsData(data, ws) {
      if (!data) return
      if(data.e&&data.e==='depthUpdate'){
          const handleDep = transformBinanceUsdtWsDepthData(data);
          if(handleDep.length>0){
              binance_depth_1s_arr.push(handleDep[0]);
              checkWsData(handleDep, ws);
          }
      }else if(data.stream&&data.data){
          // console.log(ws,'...')
          const handleDep = transformBinanceSpotWsDepthData(data);
          if(handleDep.length>0){
              binance_depth_1s_arr.push(handleDep[0]);
              checkWsData(handleDep, ws);
          }
      }
  }
  
  
  //binance
  async function batchBinanceDatas() {
      setInterval(() => {
          // console.log(depth_10s_arr.length, 'depthArr')
          var handDatas = []
          if (binance_depth_1s_arr.length > 0) {
              handDatas = uniquBy(binance_depth_1s_arr, 'instrument_id');
              // handDatas.map(l=>console.log(l.instrument_id,l.asset_type))
              // return
              dbLink.batchUpsert('coin_depth', handDatas, { batchN: handDatas.length - 1 });
              console.log(handDatas.length,'binanceDeptHandleData');
              binance_depth_1s_arr = [];
              handDatas = [];
          }
      }, 1000)
  }
  
  module.exports = {
      saveWsData,
      batchBinanceDatas
  }