package cn.iocoder.yudao.module.remote.service.speed;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.system.api.remote.RemoteRobotMaxSpeedApi;
import cn.iocoder.yudao.module.system.api.remote.dto.RemoteRobotMaxSpeedDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

@Slf4j
@Service
public class RemoteRobotMaxSpeedServiceImpl implements RemoteRobotMaxSpeedService{

    @Resource
    private RemoteRobotMaxSpeedApi remoteRobotMaxSpeedApi;

    @Value("${remote.robot-max-speed:5}")
    private int robotMaxSpeed;

    @Override
    public List<RemoteRobotMaxSpeedDTO> getRobotMaxSpeedPage() {
        return remoteRobotMaxSpeedApi.getRobotMaxSpeedPage();
    }

    @Override
    public void updateRobotMaxSpeedPage(List<RemoteRobotMaxSpeedDTO> list) {
        for (RemoteRobotMaxSpeedDTO remoteRobotMaxSpeedDTO : list) {
            int speed = ObjectUtil.isEmpty(remoteRobotMaxSpeedDTO.getMaxSpeed()) ? 0 : Integer.parseInt(remoteRobotMaxSpeedDTO.getMaxSpeed());
            if (speed <= 0) {
                throw exception(REMOTE_ROBOT_SPEED_EMPTY);
            }else if (speed > robotMaxSpeed) {
                String str = REMOTE_ROBOT_MAX_SPEED_LIMIT.getMsg() + " 最大限速为 " +robotMaxSpeed +" m/s";
                throw exception0(REMOTE_ROBOT_MAX_SPEED_LIMIT.getCode(),str);
            }
        }
         remoteRobotMaxSpeedApi.updateRobotMaxSpeedPage(list);
    }

}
