package com.fdkankan.scene.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.UploadFilePath;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.SpringUtil;
import com.fdkankan.fyun.oss.UploadToOssUtil;
import com.fdkankan.platform.api.dto.Camera;
import com.fdkankan.platform.api.dto.Company;
import com.fdkankan.platform.api.dto.User;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.feign.PlatformUserClient;
import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
import com.fdkankan.rabbitmq.config.ModelingQueueConfig;
import com.fdkankan.rabbitmq.util.RabbitMqProducer;
import com.fdkankan.redis.util.RedisLockUtil;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.scene.service.IFolderService;
import com.fdkankan.scene.service.IScene3dNumService;
import com.fdkankan.scene.service.ISceneService;
import com.google.common.collect.Lists;
import com.yomahub.tlog.task.spring.SpringScheduledTaskAop;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
@RequestMapping("/service/scene/test")
@Slf4j
public class TestController extends BaseController {



    @Autowired
    RedisUtil redisUtil;

    @Autowired
    IFolderService folderService;

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

    @Autowired
    private PlatformUserClient platformUserClient;

    @Autowired
    private PlatformGoodsClient platformGoodsClient;

    @Autowired
    private IScene3dNumService scene3dNumService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedisLockUtil redisLockUtil;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    ModelingQueueConfig modelingQueueConfig;
    @Autowired
    private RabbitMqProducer rabbitMqProducer;

    @Autowired
    private UploadToOssUtil uploadToOssUtil;



    @GetMapping("/test")
    public ResultData test() throws Exception {

        uploadToOssUtil.deleteFile("test_dsx");

        return ResultData.ok();

    }

    public static void main(String[] args) throws Exception {

//        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();
    }

    @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);

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

//        connection.close();

    }

}
