package cn.iocoder.yudao.module.system.service.mqtt;

import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.robot.dto.RobotGenericsDataDTO;
import cn.iocoder.yudao.module.system.controller.admin.log.dto.RobotWalkingDistanceDTO;
import cn.iocoder.yudao.module.system.service.log.RobotTaskTimeConsumingService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class PlanningRobotWalkingDistanceServiceImpl implements MqttService {

    @Resource
    private RobotTaskTimeConsumingService robotTaskTimeConsumingService;

    @Override
    public void analysisMessage(String message) {
        TenantContextHolder.setTenantId(1L);
        log.info("111111更新任务 :{}", message);
        RobotWalkingDistanceDTO data = JSON.parseObject(message, RobotWalkingDistanceDTO.class);
        data.setCheckDistance(spit(data.getCheckDistance()));
        data.setTotalDistance(spit(data.getTotalDistance()));
        log.info("更新后的任务 :{}",JSON.toJSONString(data));
        robotTaskTimeConsumingService.updateTime(data);
    }

    public String spit(String str) {
        if (!str.contains(".")) {
            return str;
        }
        return String.format("%.2f", Double.valueOf(str));
    }
}