package com.sw.mes.wms.controller;


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
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 com.fasterxml.jackson.core.JsonProcessingException;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.dto.wms.MesasnDto;
import com.sw.mes.core.dto.wms.RematDto;
import com.sw.mes.core.dto.wms.RemesstaDto;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.wms.service.WMSOutputService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * @Author 李儒
 * @Description：晶科WSM数据请求接口
 */
@RestController
@RequestMapping("/wms/out")
@Slf4j
@Api(description = "晶科WSM数据请求接口")
@Jinko(value = "晶科WMS对接请求接口控制器", author = "liru")
public class WMSOutputController {


    @Autowired
    private WMSOutputService wmsInputService;
    @Autowired
    private YbFeignClients ybFeignClients;


    @ApiOperation(value = "成品返工车间MES接收状态回传接口")
    @PostMapping("/remessta")
    public Result remessta(@RequestBody @Valid List<RemesstaDto> data) throws JsonProcessingException {
        try {
            wmsInputService.sendremessta(data);
        } catch (Exception e) {
            log.error("error_in_remessta", e);
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }


    @PostMapping("/testremessta")
    public Result testremessta(@RequestBody @Valid List<RemesstaDto> data) throws JsonProcessingException {
        Result result = null;
        try {
            result = ybFeignClients.remessta(data);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }


    @ApiOperation(value = "车间退料入库接口")
    @PostMapping("/remat")
    public Result remat(@RequestBody @Valid RematDto data) throws JsonProcessingException {
        try {
            wmsInputService.sendremat(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }


    @ApiOperation(value = "成品MES入库接口")
    @PostMapping("/mesasn")
    public Result mesasn(@Valid @RequestBody List<MesasnDto> data) throws JsonProcessingException {
        try {
            wmsInputService.sendMesasn(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }

    @ApiOperation(value = "返工箱号车间接收/拒收状态信息接口")
    @PostMapping("/rework")
    public Result rework(@Valid @RequestBody List<RemesstaDto> data) throws JsonProcessingException {
        try {
            wmsInputService.sendrework(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }


    @ApiOperation(value = "库存地点查询公共接口")
    @PostMapping("/synPlantStorage")
    public Result synPlantStorage(@Valid @RequestBody String werks) throws JsonProcessingException {
        try {
            wmsInputService.synPlantStorage(werks);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }


}
