package io.renren.modules.hbase;

import com.alibaba.fastjson.JSON;

import io.renren.modules.hbase.hbasebean.Hbasetable;
import lgh.springboot.starter.hbase.template.HBaseTemplate;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

//import com.google.gson.JsonArray;
//import com.dmd.bigdata.controller.hbase.hbasebean.Hbasetable;

@RestController
@RequestMapping("/hbase")
public class HbaseController {
    @Autowired
    Hbasetable table_01;

    @Autowired
    HBaseTemplate hBaseTemplate;


//    //    根据测点名字模糊查询测点值
//    @GetMapping("/getMeasurePointByname")
//        public List<Object> getMeasurePointByname(@RequestParam String hbasetablename, @RequestParam String qualifier, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) throws Exception {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        Calendar d = Calendar.getInstance();
////        String currdate = df.format( (Date) d.getTime());// new Date()为获取当前系统时间
//        d.add(Calendar.HOUR,-36);
//        Date startTime = (Date) d.getTime();
//        String startRowKey = df.format(startTime);
//        Scan scan = new Scan().withStartRow(Bytes.toBytes(startRowKey));
//        FilterList filterList = new FilterList();
//        PageFilter pageFilter = new PageFilter(20);
//        QualifierFilter qualifierfilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator(qualifier));
//        filterList.addFilter(pageFilter);
//        filterList.addFilter(qualifierfilter);
//        scan.setFilter(filterList);
////
//
//        ResultScanner results = hBaseTemplate.queryByScan(hbasetablename, scan);
//        List<Object> MpInfos = new ArrayList<>();
//        results.forEach(result -> {
//            try {
//                Hbasetable t = table_01.getClass().newInstance();
//
//                for (Cell cell : result.rawCells()) {
//                    String key = Bytes.toString(CellUtil.cloneQualifier(cell));
//                    byte[] val = CellUtil.cloneValue(cell);
//                    t.addProperty(key, Bytes.toString(val));
//                }
////                    t.setRowKey(Bytes.toString(CellUtil.cloneRow(cell)));
//
//                t.setRowKey(Bytes.toString(result.getRow()));
//                MpInfos.add(t);
//
//            } catch (InstantiationException e) {
//                e.printStackTrace();
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//        });
//
//        return MpInfos;
//
//    }

    @GetMapping("/getMpInfoByHbaseTablename/{hbasetablename}")
    public JSON getMpInfoByHbaseTablename(@PathVariable("hbasetablename")String hbasetablename) throws Exception {
        Scan scan=new Scan();
        ResultScanner results = hBaseTemplate.queryByScan(hbasetablename, scan);
        List<Object> MpInfos = new ArrayList<>();
        results.forEach(result -> {
                try {
                    Hbasetable t = table_01.getClass().newInstance();

                    for (Cell cell : result.rawCells()) {
                        String key = Bytes.toString(CellUtil.cloneQualifier(cell));
                        byte[] val = CellUtil.cloneValue(cell);
                        t.addProperty(key, Bytes.toString(val));
                    }

                    t.setRowKey(Bytes.toString(result.getRow()));
                    MpInfos.add(t);

                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
        });

        return (JSON) JSON.toJSON(MpInfos);
    }

}
