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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.robot.vo.RobotWorkStatusDTO;
import cn.iocoder.yudao.module.system.constant.robot.RobotExecutionStateConstant;
import cn.iocoder.yudao.module.system.controller.admin.log.vo.RobotOperationLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotTaskDetailDO;
import cn.iocoder.yudao.module.system.dal.mysql.robot.RobotTaskDetailMapper;
import cn.iocoder.yudao.module.system.enums.path.PathIsReachEnum;
import cn.iocoder.yudao.module.system.enums.robot.CommandTypeEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotTaskDetailStatusEnum;
import cn.iocoder.yudao.module.system.enums.robot.RobotTaskTypeEnum;
import cn.iocoder.yudao.module.system.enums.robot.task.RobotTaskStageEnum;
import cn.iocoder.yudao.module.system.enums.robot.task.WorkProgressEnum;
import cn.iocoder.yudao.module.system.enums.robot.task.event.EventTypeEnum;
import cn.iocoder.yudao.module.system.mq.message.task.TaskDistributionMessage;
import cn.iocoder.yudao.module.system.mq.producer.task.TaskDistributionProducer;
import cn.iocoder.yudao.module.system.service.log.RobotOperationLogService;
import cn.iocoder.yudao.module.system.service.path.PathPlanningService;
import cn.iocoder.yudao.module.system.service.robot.RobotInformationService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

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

    @Resource
    private RobotOperationLogService operationLogService;

    @Autowired
    private RobotInformationService robotInformationService;

    @Resource
    private PathPlanningService pathPlanningService;

    @Autowired
    private RobotTaskDetailMapper robotTaskDetailMapper;

    @Resource
    private TaskDistributionProducer taskDistributionProducer;

    @Override
    public void robotWorkStatus(String message) {
        log.info("111111作业实时行为上报: {}", message);

        TenantContextHolder.setTenantId(1L);
        RobotWorkStatusDTO data = JSON.parseObject(message, RobotWorkStatusDTO.class);
        if (ObjectUtil.isEmpty(data.getOrderId())) {
            log.info("任务id为空,不进行处理");
            return;
        }
        String robotNo = robotInformationService.getRobotNoByMac(data.getMac());
        RobotOperationLogSaveReqVO createReqVO = RobotOperationLogSaveReqVO.builder()
                .orderId(Long.valueOf(data.getOrderId()))
                .robotNo(robotNo)
                .orderType(data.getOrderType())
                .commandType(data.getCommandType())
                .workProgress(data.getWorkProgress())
                .executionState(data.getExecutionState())
                .build();
        operationLogService.createOperationLog(createReqVO);

        TaskDistributionMessage mess  = new TaskDistributionMessage(this,message,
                EventTypeEnum.TASK_TIME.getType());
        taskDistributionProducer.sendSmsSendMessage(mess);

        if ((CommandTypeEnum.WORK_PICK_UP_GOODS_MOVE_TO_CHECK.getType().equals(data.getCommandType())
                || CommandTypeEnum.WORK_DROP_OFF_GOODS_MOVE_TO_CHECK.getType().equals(data.getCommandType()))
                && RobotExecutionStateConstant.DOING.equals(Integer.valueOf(data.getExecutionState()))) {
            pathPlanningService.updateBehavior(String.valueOf(data.getOrderId()), robotNo
                    , data.getCommandType(), PathIsReachEnum.START_WORK.getType());
        }else if ((CommandTypeEnum.WORK_PICK_UP_GOODS_MOVE_TO_CHECK.getType().equals(data.getCommandType())
                || CommandTypeEnum.WORK_DROP_OFF_GOODS_MOVE_TO_CHECK.getType().equals(data.getCommandType()))
                && RobotExecutionStateConstant.DONE.equals(Integer.valueOf(data.getExecutionState()))) {
            pathPlanningService.updateBehavior(String.valueOf(data.getOrderId()), robotNo
                    , data.getCommandType(), PathIsReachEnum.END_WORK.getType());
        }

        if (CommandTypeEnum.WORK_PICK_UP_GOODS.getType().equals(data.getCommandType())
                && RobotExecutionStateConstant.DONE.equals(Integer.valueOf(data.getExecutionState()))
                && WorkProgressEnum.FORK_OFFSET.getType().equals(data.getWorkProgress())) {
            setTakeDone(data.getOrderId());
        }else if (CommandTypeEnum.WORK_DROP_OFF_GOODS.getType().equals(data.getCommandType())
                && RobotExecutionStateConstant.DONE.equals(Integer.valueOf(data.getExecutionState()))
                && WorkProgressEnum.FORK_OFFSET.getType().equals(data.getWorkProgress())) {
            setReleaseDone(data.getOrderId());
        }

    }

    private void setReleaseDone(String orderId) {
        RobotTaskDetailDO data = new RobotTaskDetailDO();
        data.setId(Long.valueOf(orderId));
        data.setTaskStage(RobotTaskStageEnum.DONE.getType());
        data.setTaskStatus(RobotTaskDetailStatusEnum.DONE.getType());
        robotTaskDetailMapper.updateRobotDetailById(data);
    }

    private void setTakeDone(String orderId) {
        RobotTaskDetailDO robotTaskDetailDO = robotTaskDetailMapper.selectById(orderId);
        if (RobotTaskTypeEnum.TAKE_RELEASE.getType().equals(robotTaskDetailDO.getTaskType())) {
            RobotTaskDetailDO data = new RobotTaskDetailDO();
            data.setId(robotTaskDetailDO.getId());
            data.setTaskStage(RobotTaskStageEnum.GO_RELEASE.getType());
            robotTaskDetailMapper.updateRobotDetailById(data);
        }else if (RobotTaskTypeEnum.TAKE.getType().equals(robotTaskDetailDO.getTaskType())){
            RobotTaskDetailDO data = new RobotTaskDetailDO();
            data.setId(robotTaskDetailDO.getId());
            data.setTaskStage(RobotTaskStageEnum.DONE.getType());
            data.setTaskStatus(RobotTaskDetailStatusEnum.DONE.getType());
            robotTaskDetailMapper.updateRobotDetailById(data);
        }
    }




















}
