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


import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.positionmap.vo.PositionMapSaveReqVO;
import cn.iocoder.yudao.module.system.service.path.PathPlanningService;
import cn.iocoder.yudao.module.system.service.robot.RobotTaskService;
import cn.iocoder.yudao.module.system.service.robot.RobotWarnMsgService;
import cn.iocoder.yudao.module.system.service.tool.ToolsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static cn.iocoder.yudao.module.system.config.SystemJobConfiguration.NOTIFY_THREAD_POOL_TASK_EXECUTOR;

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

    @Resource
    private PathPlanningService pathPlanningService;

    @Resource
    private ToolsService toolsService;

    @Resource
    private RobotTaskService taskService;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Resource
    private RobotWarnMsgService warnMsgService;

    /**
     * 发送初始化信息给PP
     */
    @Override
    public void pathInitData() {
        taskExecutor.execute(() -> {
            TenantContextHolder.setTenantId(1L);
            log.info("初始化数据发送个PP---开始");
            //同步点位信息
            PositionMapSaveReqVO positionMapSaveReqVO = new PositionMapSaveReqVO();
            pathPlanningService.synchronousAllItem(positionMapSaveReqVO);
            //同步线的信息
            pathPlanningService.synchronousPoint(positionMapSaveReqVO);
            //车辆尺寸
            pathPlanningService.robotDimensions();
            //距离、优先级、时间权重
            toolsService.sendPPsortConfig();
            log.info("初始化数据发送个PP---完成");
        });
    }

    /**
     * PP分配任务
     * @param message
     */
    @Override
    public void ppDistributionTask(String message) {
        taskExecutor.execute(() -> {
            TenantContextHolder.setTenantId(1L);
            if (ObjectUtil.isEmpty(message)) {
                return;
            }
            taskService.ppDistributionTask(message);
        });
    }

    /**
     * PP处理任务失败
     * @param message
     */
    @Override
    public void ppDistributionTaskFail(String message) {
        warnMsgService.addWarnMsg(message);
    }


}
