package com.ruoyi.web.controller.parkLot;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.constant.CacheConstants.LEFT_PLACE;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/parkLot")
public class ParkLotController extends BaseController {
    @Autowired
    private RedisCache redisCache;

    /**
     * 提供给皖事通剩余地面车位数量
     */
    @PostMapping("/parkLotNum")
    public String parkLotNum() {
        //先去查redis
        String leftPlaceRedis = redisCache.getCacheObject(LEFT_PLACE);
        if (StringUtils.isEmpty(leftPlaceRedis)){
            leftPlaceRedis = "0";
            String result = GetCameraPreviewURL.GetCameraPreviewURL();
            JSONObject jsonObject = JSONObject.parseObject(result);
            String code = jsonObject.getString("code");
            if ("0".equals(code)) {
                JSONArray data = jsonObject.getJSONArray("data");
                leftPlaceRedis = data.getJSONObject(0).getString("leftPlace");
                System.out.println(leftPlaceRedis);
            }else {
                System.out.println(jsonObject.getString("msg"));
            }
            redisCache.setCacheObject(LEFT_PLACE,leftPlaceRedis,5, TimeUnit.SECONDS);
        }
        return leftPlaceRedis;
    }

}
