package com.fdkankan.scene.controller;

import cn.hutool.core.collection.ConcurrentHashSet;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ConstantFilePath;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.MatrixToImageWriterUtil;
import com.fdkankan.common.util.StrExtUtil;
import com.fdkankan.mq.message.BuildSceneMqMessage;
import com.fdkankan.mq.util.RocketMQProducer;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.feign.PlatformUserClient;
import com.fdkankan.platform.api.vo.Camera;
import com.fdkankan.platform.api.vo.Company;
import com.fdkankan.platform.api.vo.User;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.scene.service.IFolderService;
import com.fdkankan.scene.service.ISceneService;
import com.fdkankan.scene.vo.SceneVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

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



    @Autowired
    RedisUtil redisUtil;

    @Autowired
    IFolderService folderService;

    @GetMapping("/testdb")
    public List testdb(){
        return folderService.list();
    }

    @Autowired
    private PlatformUserClient platformUserClient;

    @Autowired
    private PlatformGoodsClient platformGoodsClient;

    @Autowired
    private RocketMQProducer rocketMQProducer;


    @GetMapping("/test")
    public String test() throws Exception {
        for (int i = 0; i< 10; i++){
            rocketMQProducer.syncSend("qwe",i+"",  "消息体"+i);
        }
        return "123";

    }

    public static void main(String[] args) throws Exception {
        MatrixToImageWriterUtil.createQRCode("http://baidu.com" + "123123", ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+"123123"+".png",
                null);

//        MatrixToImageWriterUtil.createQRCode("http://baidu.com" + "123123", ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+"123123"+".png",
//                ConstantFilePath.SCENE_PATH + "images/images" + "123123" + "/QRShareLogo.png");
    }

    @GetMapping("/testSetStr")
    public void testSetStr() throws Exception {
        redisUtil.set("testIncr", "1");
        redisUtil.incr("testIncr",1);
        String testIncr = redisUtil.get("testIncr");
        System.out.println(testIncr);


    }

    @GetMapping("/testGetStr")
    public String testGetStr() throws Exception {
        boolean b = redisUtil.hasKey("90c82bdac_202201050951313830");
        String abc = (String)redisUtil.get("abc");
        return "123";
    }

    @GetMapping("/testSetObject")
    public boolean testSetObject() throws Exception {
//        User user = new User();
//        user.setCity("珠海");
//        user.setId(123l);
//
//        String jsons = JSON.toJSONString(user);
//
//        redisTemplate.opsForValue().set("test:66", jsons);
//        redisTemplate.opsForValue().set("test:67", jsons);

        ResultData<Camera>  cameraById = platformGoodsClient.getCameraById(111L);

        Camera data = cameraById.getData();

        ResultData<Company> companyById = platformUserClient.getCompanyById(111L);






        return true;
    }

//
    @GetMapping("/testGetObject")
    public User testGetObject() throws Exception {
        String test12 = redisUtil.get("test66");
        User user = JSONObject.parseObject(test12, User.class);
        return user;
    }


    @GetMapping("/testSetList")
    public boolean testSetList() throws Exception {

        List<String> users = Lists.newArrayList();
        users.add("sdfsdf");
        users.add("dfgdfg");
        return redisUtil.lRightPushAll("test5595jj", users);
    }

    @GetMapping("/testGetList")
    public List<String> testGetList() throws Exception {
        return redisUtil.lGet("test5595jj", 0, -1);
    }

    @GetMapping("/testSetMap")
    public boolean testSetMap() throws Exception {

        Map<String, User> userMap = new HashMap<>();

        User user1 = new User();
        user1.setCity("珠海");
        user1.setId(123l);

        User user2 = new User();
        user2.setCity("广州");
        user2.setId(124l);

        userMap.put("user1", user1);
        userMap.put("user2", user2);


        return redisUtil.hmset("userMap", userMap);
    }

    @GetMapping("/testGetMap")
    public Map<String ,User> testGetMap() throws Exception {
        return redisUtil.hmget("userMap");
    }

    @Autowired
    ISceneService sceneService;

    @GetMapping("/testSchedule")
    public void testSchedule(){
        sceneService.updatePv();
    }

    @Value("${rocketmq.build-scene.topicName.topic-modeling-a}")
    private String topicModelingA;

    @GetMapping("/testMQ")
    public void testMQ(){
//        SceneVO body = SceneVO.builder().buildType("123").childName("sdfsdfsdf").dataSource("sadfdhgf").id(1213L).build();
        BuildSceneMqMessage body = new BuildSceneMqMessage();
        body.setPayStatus(1);
        rocketMQProducer.sendOneWay(topicModelingA, body);
    }

    @GetMapping("/testAsynMQ")
    public void testAsynMQ(){
        SceneVO body = SceneVO.builder().buildType("123").childName("sdfsdfsdf").dataSource("sadfdhgf").id(1213L).build();
        rocketMQProducer.asyncSend("abc","tag3", UUID.randomUUID().toString(), body,null,5000l);
    }

    @GetMapping("/testsenOnway")
    public void testsenOnway(){
        SceneVO body = SceneVO.builder().buildType("123").childName("sdfsdfsdf").dataSource("sadfdhgf").id(1213L).build();
        rocketMQProducer.sendOneWay("qqq","tag6", UUID.randomUUID().toString(), body);
    }

    @RefreshScope
    @GetMapping("/testsenOnway1")
    public void testsenOnway1(){
        SceneVO body = SceneVO.builder().buildType("123").childName("sdfsdfsdf").dataSource("sadfdhgf").id(1213L).build();
        rocketMQProducer.sendOneWay(topicModelingA, null);
    }

    @RefreshScope
    @GetMapping("/testResult")
    public void testResult(){
//        rocketMQProducer.sendOneWay("topic_modeling_a_result", BuildSceneResultMqMessage.builder().buildSuccess(true).cameraType("3").build());
        String dsfsdf = (String) redisUtil.get("dsfsdf");
        System.out.println(2134);

    }

    @Autowired
    DataSource dataSource;

    @GetMapping("/testDatasource")
    public void testDatasource() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);

        DruidDataSource druidDataSource = (DruidDataSource) dataSource;
        System.out.println("druidDataSource 数据源最大连接数：" + druidDataSource.getMaxActive());
        System.out.println("druidDataSource 数据源初始化连接数：" + druidDataSource.getInitialSize());

        connection.close();

    }

}
