package com.sckw.slope.detection.controller;

import com.alibaba.fastjson2.JSONObject;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.constant.NumberConstant;
import com.sckw.core.model.enums.DictEnum;
import com.sckw.core.web.response.HttpResult;
import com.sckw.core.web.response.PhpResult;
import com.sckw.redis.utils.RedissonUtils;
import com.sckw.slope.detection.model.dto.SystemDict;
import com.sckw.slope.detection.service.api.DetectionApiService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author lfdc
 * @description httpDemo
 * @date 2023-10-30 09:10:08
 */
@RestController
@RequestMapping("/http")
public class HttpDemoController {
//    @Autowired
//    RedisLockUtil redisLockUtil;

    @Autowired
    DetectionApiService detectionApiService;

    @RequestMapping(value = "/toSaasGetDemo", method = RequestMethod.GET)
    public void httpToSaasGetDemo(HttpServletResponse response) {
        String s = response.getHeader("a");
        HttpResult saasGetDemo = detectionApiService.toSaasGetDemo();
        System.out.println(saasGetDemo);
    }

    @RequestMapping(value = "/getRedis", method = RequestMethod.GET)
    public void getRedis(HttpServletResponse response) {
        String key = Global.REDIS_SYS_DICT_PREFIX + "type" + Global.POUND + "value";
        String dictCache = RedissonUtils.getString(key);
        RedissonUtils.putString(key, "1", Global.COMMON_EXPIRE);
    }


    @RequestMapping(value = "/toSaasPostDemo", method = RequestMethod.GET)
    public void httpToSaasPostDemo() {
        HttpResult saasPostDemo = detectionApiService.toSaasPostDemo();
        System.out.println(saasPostDemo);
    }

    @RequestMapping(value = "/toSaasGetDict", method = RequestMethod.GET)
    public void httpToSaasGetDict() {
        HashMap<String, Object> mapBody = new HashMap<>(NumberConstant.SIXTEEN);
        mapBody.put("code", DictEnum.THRESHOLD_TYPE.getCodeType());
        HashMap<String, String> mapPara = new HashMap<>(NumberConstant.SIXTEEN);
        mapPara.put("code", DictEnum.THRESHOLD_TYPE.getCodeType());
        mapPara.put("type", "1");
        PhpResult httpResult = detectionApiService.toSaasGetDict("http://10.10.10.185:9501",
                "/common/getDict", mapBody
                , mapPara);
        List<SystemDict> systemDictList = new ArrayList<>();
        if (httpResult.isStatus()) {
            List list = JSONObject.parseObject(JSONObject.toJSONString(httpResult.getData()), List.class);
            if (!CollectionUtils.isEmpty(list)) {
                list.forEach(s -> {
                    SystemDict systemDict = JSONObject.parseObject(s.toString(), SystemDict.class);
                    systemDictList.add(systemDict);
                });
            }
        }
        System.out.println(systemDictList);
    }


}
