package com.gyee.wisdom.dataadapter.write;

import com.gyee.wisdom.common.data.timeseries.BooleanTsData;
import com.gyee.wisdom.common.data.timeseries.GeneralTsData;
import com.gyee.wisdom.common.data.timeseries.TsData;
import com.gyee.wisdom.common.data.timeseries.TsPointData;
//import com.rtdb.model.DoubleData;
//import com.rtdb.model.Entity;
//import com.rtdb.model.IntData;
//import com.rtdb.model.MinPoint;
//import com.rtdb.service.impl.BaseImpl;
//import com.rtdb.service.impl.ServerImpl;
//import com.rtdb.service.impl.SnapshotImpl;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.ToIntFunction;

/**
 * @descrition:
 * @author:Wanghs
 * @date:2019-04-26
 */
@Component
public class ReadAndWrite extends Thread {
//    public void run() {
//        try {
//            ServerImpl readConnect = this.getConnect();
//            ServerImpl writeConnect = this.getConnect();
//            PointList.pointListInit();
//
//            while (true) {
//                Thread.sleep(5000);
//                //获取所有key
//                Set<String> keys = PointList.mapPoint.keySet();
//
//                String tagNames[] = new String[keys.size()];
//                int countIndx = 0;
//                for (String getKey : PointList.mapPoint.keySet()) {
//                    tagNames[countIndx] = getKey;
//                    countIndx++;
//                }
//
//                Map<String, TsData> readMap = this.read(readConnect, tagNames);
//                List<TsPointData> writeList = new ArrayList();
//                Iterator iter = readMap.keySet().iterator();
//
//                while (iter.hasNext()) {
//                    String resultMapKey = (String) iter.next();
//                    BooleanTsData data = (BooleanTsData) readMap.get(resultMapKey);
//                    TsPointData writeData = new TsPointData();
//
//                    GeneralTsData gData = new GeneralTsData();
//                    gData.setTs((new Date()).getTime());
//                    if (data.getBooleanValue()) {
//                        gData.setDoubleValue(Optional.ofNullable(1.0));
//                    } else {
//                        gData.setDoubleValue(Optional.ofNullable(0.0));
//                    }
//
//                    //String tagName = key.replace("SBQDQ", "NXDQDI").replace("XSDQ", "NXDQDI").replace("QSDQ", "NXDQDI").replace("NSSDQ", "NXDQDI").replace("MHSDQ", "NXDQDI");
//                 //如果MapPoint中包含查询结果的点，则通过map找要写入的点(value)
//                   if(PointList.mapPoint.containsKey(resultMapKey)){
//                       String writeTagName=PointList.mapPoint.get(resultMapKey);
//                       writeData.setTagName(writeTagName);
//                       writeData.setTsData(gData);
//                       writeList.add(writeData);
//                   }
//
//                }
//
//                System.out.println(writeList.size() + "查询完毕");
//               int count = this.write(writeConnect, writeList);
//               System.out.println("写入数量" + count);
//            }
//        } catch (Exception ex) {
//            System.out.println(ex.getMessage());
//        }
//    }
//
//    public Map<String, TsData> read(ServerImpl connect, String... tagNames) throws Exception {
//        HashMap tsDataMap = new HashMap();
//
//        try {
//            SnapshotImpl snap = new SnapshotImpl(connect);
//            BaseImpl base = new BaseImpl(connect);
//            List<MinPoint> lstPoint = base.findPoints(tagNames);
//            int[] ids = lstPoint.stream().mapToInt(MinPoint::getId).toArray();
//            Entity<IntData> entityD = snap.getIntSnapshots(ids);
//
//            for (int i = 0; i < tagNames.length; ++i) {
//                tsDataMap.put(tagNames[i], new BooleanTsData(((IntData) entityD.getList().get(i)).getDateTime().getTime(), ((IntData) entityD.getList().get(i)).getQuality(), ((IntData) entityD.getList().get(i)).getValue() != 0.0D));
//            }
//
//            return tsDataMap;
//        } catch (Exception var13) {
//            return tsDataMap;
//        } finally {
//            ;
//        }
//    }
//
//    public int write(ServerImpl connect, List<TsPointData> list) throws Exception {
//        try {
//            SnapshotImpl snap = new SnapshotImpl(connect);
//            BaseImpl base = new BaseImpl(connect);
//            String[] tagNames = (String[]) list.stream().map(TsPointData::getTagName).toArray((x$0) -> {
//                return new String[x$0];
//            });
//            List<MinPoint> pointList = base.findPoints(tagNames);
//            List<DoubleData> doubleList = new ArrayList();
//
//            int count;
//            for (count = 0; count < list.size(); ++count) {
//                Date time = new Date();
//                time.setTime(((TsPointData) list.get(0)).getTsData().getTs());
//                DoubleData data = new DoubleData(((MinPoint) pointList.get(count)).getId(), time, (Double) ((TsPointData) list.get(count)).getTsData().getDoubleValue().get(), (short) 0, 0);
//                doubleList.add(data);
//            }
//
//            count = snap.putDoubleSnapshots(doubleList);
//            return count;
//        } catch (Exception var11) {
//            return -1;
//        }
//    }
//
//    private ServerImpl getConnect() throws Exception {
//        String serverIp = "192.168.1.100";
//        short serverPort = 6327;
//        String userName = "sa";
//        String password = "golden";
//        ServerImpl conn = new ServerImpl(serverIp, serverPort, userName, password);
//        return conn;
//    }

}
