package io.renren.modules.sys.controller.dataaccess;

import io.renren.common.utils.R;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.dataSet.DataSetUtils.ToEnglish;
import io.renren.modules.sys.entity.dataaccess.Reflect;
import io.renren.modules.sys.service.DataAccessService;
import io.renren.modules.sys.service.MeasurePointService;
import io.renren.modules.sys.service.ReflectService;
import io.renren.modules.sys.service.SubEquipmentService;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/getTemplate")
@DataSource("bigData")
public class getTemplateController {
    @Value("${realTimeKafka1}")
    String realTimeKafka1;

    @Value("${realTimeKafka2}")
    String realTimeKafka2;

    @Value("${realTimeKafka3}")
    String realTimeKafka3;

    @Value("${resultToDB}")
    String resultToDB;

    @Autowired
    ReflectService reflectService;
    @Autowired
    DataAccessService dataAccessService;
    @Autowired
    MeasurePointService measurePointService;
    @Autowired
    SubEquipmentService subEquipmentService;

    /**
     * Description:根据所选的测点，生成获取该测点实时数据的python模板
     * @param measurepoint
     * @return
     */
    @GetMapping("/pythonKafka")
    public R pythonKafka(String measurepoint){
        if(measurepoint.length()==0)    return R.ok().put("code","所选测点暂无数据");
        System.out.println("测点信息为"+measurepoint);
        String[] fieldIds=measurepoint.split(",");
        int[] measurepoints=new int[fieldIds.length];
        for(int n=0;n<fieldIds.length;n++){
            measurepoints[n]= Integer.parseInt(fieldIds[n]);
        }
        String temp="import happybase\n" +
                "\n" +
                "connection = happybase.Connection(host='"+resultToDB+"', port=9090)\n";
        StringBuilder stringBuilder=new StringBuilder(temp);
        for(int i=0;i<measurepoints.length;i++){
            String measurePointName=measurePointService.selectByPrimaryKey(measurepoints[i]).getName().substring(4);
            String hbaseTableName=subEquipmentService.selectByPrimaryKey(measurePointService.getSubEquipmentId(measurepoints[i])).getHbaseTableName();
            String measurePointPinYin=ToEnglish.getPinYin(measurePointName);
            stringBuilder.append("name='"+hbaseTableName+"'\n");
            stringBuilder.append("table=happybase.Table(name, connection)\n");
            stringBuilder.append(measurePointPinYin+"=[]  #"+measurePointName+"数据\n");
            stringBuilder.append("for key, value in table.scan():\n");
            stringBuilder.append("  "+measurePointPinYin+".append(float(value[b'data:"+measurePointPinYin+"_after'].decode()))\n\n");

        }
        return R.ok().put("code",stringBuilder.toString());
    }

    /**
     * Description:生成将数据插入到hbase的python模板
     * @return
     */
    @GetMapping("/pythonHbase")
    public R pythonHbase(){
        String hbaseStrnig="import happybase\n" +
                "import time\n" +
                "\n" +
                "connection = happybase.Connection(host='"+resultToDB+"', port=9090)\n" +
                "name=\"请在这里输入要创建的算法名称\"\n" +
                "families={\n" +
                "    \"data\":dict()\n" +
                "}\n" +
                "connection.create_table(name,families)\n" +
                "#插入数据\n" +
                "table = happybase.Table(name,connection)\n" +
                "data={\"data:key\":\"value\"}#请在key,value处分别写出要存的键和值，如果要存多种信息，可通过逗号分隔\n" +
                "table.put(str(int(time.time())),data)";
        return R.ok().put("code",hbaseStrnig);
    }


}
