package com.youchain.businessdata.rest;

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.inputJson.MissionStateCallback;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.businessdata.service.dto.AgvTaskDto;
import com.youchain.exception.handler.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

/**
 * @author 92525
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "KMReS")
@RequestMapping("/interfaces/api/amr")
@Slf4j
public class KMReSController {

    private final AgvTaskService agvTaskService;

    @PostMapping("/missionStateCallback")
    @Log("KMReS接口回调")
    @ApiOperation("KMReS接口回调")
    @AnonymousAccess
    public ResponseEntity<Object> missionStateCallback(@RequestBody MissionStateCallback missionStateCallback) {
        String id = missionStateCallback.getMissionCode();//作业 id
        String containerCode = missionStateCallback.getContainerCode();//容器编号
        String missionStatus = missionStateCallback.getMissionStatus();//作业当前状态
        try {
            //货架任务
            AgvTaskDto agvTaskDto = agvTaskService.findById(Long.parseLong(id));
            AgvTask agvTask = agvTaskService.toEntity(agvTaskDto);//转实体
            agvTaskService.agvTaskCallback(agvTask, missionStatus, containerCode);
            return new ResponseEntity<>(ApiResult.success(OK.value(), "", ""), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.fail(BAD_REQUEST.value(), e.getMessage(), ""), HttpStatus.BAD_REQUEST);
        }
    }
}
