package com.prolog.eis.controller.wcs;


import com.prolog.eis.common.util.WcsResult;
import com.prolog.eis.dto.wcs.AlarmReportDto;
import com.prolog.eis.dto.wcs.CarryListDto;
import com.prolog.eis.dto.wcs.McsResponseDto;
import com.prolog.eis.dto.wcs.StackerTaskDto;
import com.prolog.eis.message.WcsRestMessage;
import com.prolog.eis.service.wcs.WcsService;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.fx.inf.core.InterfaceType;
import com.prolog.fx.inf.core.annotation.InterfaceMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@Api(tags = "MCS接口服务")
@RequestMapping(value = "api/v1/mcs")
public class McsController {

    private final WcsService wcsService;

    @ApiOperation(notes = "扫码请求上报(异步)", value = "扫码请求上报(异步)")
    @RequestMapping(method = RequestMethod.POST, value = "/task/mcsRequest")
    @InterfaceMethod(key = "扫码请求上报(异步)", name = "WCS->EIS扫码请求上报(异步)", type = InterfaceType.Receive)
    public WcsRestMessage<List<McsResponseDto>> mcsRequest(@RequestBody CarryListDto carryListDto) {
        List<McsResponseDto> mcsResponseDtoList = wcsService.mcsRequest(carryListDto);
        return WcsRestMessage.newInstance(true, "操作成功", mcsResponseDtoList);
    }

    @ApiOperation(value = "叠盘机请求", notes = "叠盘机请求")
    @PostMapping("/task/stackerReport")
    @InterfaceMethod(key = "叠盘机请求", name = "MCS->EIS叠盘机请求", type = InterfaceType.Receive)
    public WcsResult<String> stackerRequest(@RequestBody StackerTaskDto taskDto) throws Exception {
        try {
            wcsService.stackerRequest(taskDto);
            return WcsResult.newInstance(true, "请求成功", null);
        } catch (Exception e) {
            log.error("叠盘机请求{} 回告失败" + e.getMessage(), JsonUtils.toString(taskDto), e);
            return WcsResult.newInstance(false, "请求失败" + e.getMessage(), taskDto.getTaskId());
        }
    }

    @ApiOperation(value = "设备故障上报", notes = "设备故障上报")
    @PostMapping("/task/equip/alarmReport")
    @InterfaceMethod(key = "设备故障上报", name = "MCS->EIS设备故障上报", type = InterfaceType.Receive)
    public WcsResult<String> alarmReport(@RequestBody AlarmReportDto taskDto) throws Exception {
        try {
            wcsService.alarmReport(taskDto);
            return WcsResult.newInstance(true, "请求成功", null);
        } catch (Exception e) {
            log.error("叠盘机请求{} 回告失败" + e.getMessage(), JsonUtils.toString(taskDto), e);
            return WcsResult.newInstance(false, "请求失败" + e.getMessage(), null);
        }
    }
}
