package cn.iocoder.yudao.module.system.api.robot;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.system.api.robot.dto.RobotGenericsDataDTO;
import cn.iocoder.yudao.module.system.config.ratelimiter.SystemRateLimiter;
import cn.iocoder.yudao.module.system.constant.robot.RobotTaskChcheConstant;
import cn.iocoder.yudao.module.system.util.redis.RedisUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class RobotGenericsStatusApiImpl implements RobotGenericsStatusApi {

    @Resource
    private RedisUtil redisUtil;


    @Override
    @SystemRateLimiter(time = 1, count = 20, keyArg = "updateRobotCommonStatus",message = "机器人上报车辆其他信息")
    public void updateRobotCommonStatus(RobotGenericsDataDTO robotStatusData) {
        doUpdateRobotCommonStatus(robotStatusData);
    }

    public void doUpdateRobotCommonStatus(RobotGenericsDataDTO robotStatusData) {
//        log.info("111111车机每3秒上报其他状态信息 :{}", JSONUtil.toJsonStr(robotStatusData));
        if (ObjectUtil.isEmpty(robotStatusData) || ObjectUtil.isEmpty(robotStatusData.getHwStates())
                || ObjectUtil.isEmpty(robotStatusData.getMac())) {
            log.info("参数不全 :{}", JSON.toJSONString(robotStatusData));
        }

        String mac = robotStatusData.getMac();
        String socKey = RobotTaskChcheConstant.ROBOT_INFORMATION_SOC +mac;

        String batSoc = robotStatusData.getHwStates().getBatSoc();
        if (ObjectUtil.isNotEmpty(batSoc)) {
            String[] split = batSoc.split("\\.");
            batSoc = split[1];
            if (batSoc.length() > 2) {
                batSoc = batSoc.substring(0,2);
            }

            if (batSoc.startsWith("0")) {
                batSoc = batSoc.substring(1);
            }
        redisUtil.set(socKey,batSoc);
        }
    }
}
