package io.renren.modules.dataSet.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import io.renren.modules.dataSet.entity.PreviewData;
import io.renren.modules.dataSet.service.DynamicSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController


@RequestMapping("/dataset/dynamic")
public class DynamicSystemController {

    @Autowired
    DynamicSystemService dynamicSystemService;

    @RequestMapping("getheads")
    public R getheads(@RequestBody Map map){
        // MAP 中数据 {id=0, coal=, system=, subsystem=, equipment=子系统1设备1, subEquipment=设备1子设备1, hbaseTableName=hbase1, chosedata=}
        System.out.println(map);
        final List hbaseTableHeaders = dynamicSystemService.getEqumentHead_InMysql(Integer.parseInt(map.get("subEquipmentID").toString()));
//        final List hbaseTableHeaders= dynamicSystemService.getEqumentHead(map.get("hbaseTableName").toString());
//        hbaseTableHeaders.forEach(System.out::println);

        return R.ok().put("hbaseTableHeaders",hbaseTableHeaders);
    }

    @PostMapping("/getContent")
    public  R getContent(@RequestBody Map map){
        final List equmentAll = dynamicSystemService.getEqumentLimit( map.get("hbaseTableName").toString(),100);
        return R.ok().put("equmentAll",equmentAll);
    }

    @RequestMapping("getprecontent")
    public R getPrecontent(@RequestBody List<PreviewData> subeqpments){
        final List preContent = dynamicSystemService.getPreContent(subeqpments,100);
//        preContent.forEach(System.out::println);
        return R.ok().put("preContent",preContent);
    }

    @RequestMapping("getprecontentlimit")
    public R getPrecontentTimeLimit(@RequestBody Map<String, Object> map){
        List<PreviewData> subeqpments = (List<PreviewData>) map.get("data");
        String s = JSON.toJSONString(subeqpments);
        subeqpments = JSON.parseArray(s, PreviewData.class);

        Long start  = (Long) map.getOrDefault("start",0l);
        Long end  = (Long) map.getOrDefault("end",new Date().getTime());
        final List preContent = dynamicSystemService.getPreContentTimeLimit(subeqpments,100,start,end);;
        return R.ok().put("preContent",preContent);
    }



    @RequestMapping("makeDyDataSet")
    public  String makeDyDataSet(@RequestBody Map map){
//        System.out.println("===================");
        String s = dynamicSystemService.runSave(map);
        return s;
    }

    @Transactional
    @RequestMapping("dySubmit")
    public R dySubmit(@RequestBody Map map) throws Exception {
//        System.out.println(map);
        String s = dynamicSystemService.dySubmit(map);
        System.out.println(s);
        if(s.equals("111")){
            return R.ok();
        }else {
            return R.error("数据集创建失败，请联系管理员");
        }
    }

}
