package org.ts.ddcs.controller.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.worker.DataProcessHolder;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import org.ts.ddcs.tool.GBConstants;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/test/")
public class TestController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
//
//    @GetMapping("clear")
//    @ResponseBody
//    public Object clear() {
//        JSONObject res = new JSONObject();
//        try {
//            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
//
//            HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
//
//          Set<String> keys= redisTemplate.keys("*:"+ GBConstants.REDIS_KEY_COMMAND_QUERY);
//
//       //     Set<String> keys = opsHash.keys("rtu_online");
//            if (null != keys && keys.size() > 0) {
//                for (String rtuCode : keys) {
//
//                    if(opsHash.hasKey(rtuCode, GBConstants.REDIS_KEY_COMMAND_QUERY)){
//                      //  opsHash.delete(rtuCode, "command");
//                        redisTemplate.delete(rtuCode);
//                        //ListOperations<String, String> opsList = redisTemplate.opsForList();
//                    }
//
//
//
//                }
//            }
//            res.put("code", 0);
//        } catch (Exception e) {
//            e.printStackTrace();
//            res.put("code", 1);
//        }
//        return res;
//    }

//    @GetMapping("clear")
//    @ResponseBody
//    public Object clear() {
//        JSONObject res = new JSONObject();
//        try {
//            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
//
//            HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
//
//            Set<String> keys= redisTemplate.keys("*:"+ GBConstants.REDIS_KEY_COMMAND_QUERY);
//
//            //     Set<String> keys = opsHash.keys("rtu_online");
//            if (null != keys && keys.size() > 0) {
//                for (String rtuCode : keys) {
//
//                    if(opsHash.hasKey(rtuCode, GBConstants.REDIS_KEY_COMMAND_QUERY)){
//                        //  opsHash.delete(rtuCode, "command");
//                        redisTemplate.delete(rtuCode);
//                        //ListOperations<String, String> opsList = redisTemplate.opsForList();
//                    }
//
//
//
//                }
//            }
//            res.put("code", 0);
//        } catch (Exception e) {
//            e.printStackTrace();
//            res.put("code", 1);
//        }
//        return res;
//    }

    @GetMapping("image/list")
    @ResponseBody
    public Object imageList(@RequestParam(name = "name") String name, @RequestParam(name = "age") String age) {
        JSONObject res = new JSONObject();
        try {
            JSONObject data = new JSONObject();


            JSONArray list = new JSONArray();

            JSONObject d = new JSONObject();

            d.put("id","1");
            d.put("bt","1001");
            d.put("tm","2021-03-21 12:00:00");
            d.put("company","大恒");
            d.put("check","0");
            d.put("product","玻璃");
            d.put("productCod","CK10000");
            list.add(d);

             d = new JSONObject();

            d.put("id","2");
            d.put("bt","1002");
            d.put("tm","2021-03-22 12:00:00");
            d.put("company","大恒");
            d.put("check","0");
            d.put("product","玻璃2");
            d.put("productCod","CK10001");
            list.add(d);



            data.put("list", list);
            data.put("count",2);
            data.put("curPage", 1);
            data.put("pageSize", 1);

            res.put("data", data);
            res.put("code", 0);
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 1);
        }
        return res;
    }


    @GetMapping("hdfs/test")
    @ResponseBody
    public Object hdfs() {
        JSONObject res = new JSONObject();
        try {
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tm = timeFormat.format(new Date());
           // DataProcessHolder.newInstance().dataProcess.SubmitFdfsTask(tm);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }


    @GetMapping("kafka/send")
    @ResponseBody
    public Object send() {
        JSONObject res = new JSONObject();
        try {
            JSONObject data = new JSONObject();
            String batchId = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            data.put("id", batchId);
            data.put("kind", "50");
            data.put("stcd", "0012345678");
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyyMMdd");
            data.put("tm", timeFormat.format(new Date()));
            data.put("dt", timeFormat.format(new Date()));
            data.put("drp", "20.2");
            data.put("intv", "1");
            data.put("wth", "0");
            data.put("pdr", "0");
            data.put("dyp", "0");
            data.put("disp_date", timeFormat2.format(new Date()));

            kafkaTemplate.send("fencer1", data.toJSONString());
            //kafkaTemplate.send("xqh", data.toJSONString());

            System.out.println("消息发送成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }
}
