package com.prolog.eis.controller.pda;

import cn.hutool.json.JSONObject;
import com.prolog.eis.dto.rf.DemonstrateTask;
import com.prolog.eis.dto.rf.DemonstrateTaskDto;
import com.prolog.eis.dto.rf.DemonstrateTaskInfo;
import com.prolog.eis.dto.rf.DemonstrateTaskReqDto;
import com.prolog.eis.service.page.PdaService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/pda")
@RequiredArgsConstructor
@Api(tags = "PDA接口服务")
public class PdaDemonstrateController {

    @Autowired
    private PdaService pdaService;



    @ApiOperation(value = "演示作业库存查询", notes = "演示作业库存查询")
    @RequestMapping(method = RequestMethod.POST, value = "/demonstrateList")
    @InterfaceMethod(key = "演示作业库存查询", name = "演示作业库存查询", type = InterfaceType.Receive)
    public RestMessage<Page<DemonstrateTask>> demonstrateList(@Valid @RequestBody DemonstrateTaskDto demonstrateTaskDto) {
        try {
            PageUtils.startPage(demonstrateTaskDto.getPageNum(), demonstrateTaskDto.getPageSize());
            List<DemonstrateTask> list = pdaService.demonstrateList(demonstrateTaskDto);
            return RestMessage.newInstance(true, "查询成功", PageUtils.getPage(list));
        } catch (Exception e) {
            log.error("演示作业查询异常:{}", e);
            return RestMessage.newInstance(false, "查询异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "演示作业下发", notes = "演示作业下发")
    @RequestMapping(method = RequestMethod.POST, value = "/demonstrateTaskReq")
    @InterfaceMethod(key = "演示作业下发", name = "演示作业下发", type = InterfaceType.Receive)
    public RestMessage<Object> demonstrateTaskReq(@Valid @RequestBody DemonstrateTaskReqDto demonstrateTaskReqDto) {
        try {
            pdaService.demonstrateTaskReq(demonstrateTaskReqDto);
            return RestMessage.newInstance(true, "下发成功");
        } catch (Exception e) {
            log.error("演示作业查询异常:{}", e);
            return RestMessage.newInstance(false, "下发异常" + e.getMessage());
        }
    }


    @ApiOperation(value = "演示作业任务详情", notes = "演示作业任务详情")
    @RequestMapping(method = RequestMethod.POST, value = "/demonstrateTaskInfo")
    @InterfaceMethod(key = "演示作业任务详情", name = "演示作业任务详情", type = InterfaceType.Receive)
    public RestMessage<Object> demonstrateTaskInfo(@Valid @RequestBody JSONObject req) {
        try {
            DemonstrateTaskReqDto demonstrateTaskReqDto = new DemonstrateTaskReqDto();
            demonstrateTaskReqDto.setContainerSubNo(req.getStr("containerSubNo"));
             DemonstrateTaskInfo demonstrateTaskInfo =  pdaService.demonstrateTaskInfo(demonstrateTaskReqDto);
            return RestMessage.newInstance(true, "成功",demonstrateTaskInfo);
        } catch (Exception e) {
            log.error("演示作业查询异常:{}", e);
            return RestMessage.newInstance(false, "下发异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "演示作业回库", notes = "演示作业回库")
    @RequestMapping(method = RequestMethod.POST, value = "/demonstrateBack")
    @InterfaceMethod(key = "演示作业回库", name = "演示作业回库", type = InterfaceType.Receive)
    public RestMessage<Object> demonstrateBack(@Valid @RequestBody JSONObject req) {
        try {
            DemonstrateTaskReqDto demonstrateTaskReqDto = new DemonstrateTaskReqDto();
            demonstrateTaskReqDto.setContainerSubNo(req.getStr("containerSubNo"));
            pdaService.demonstrateBack(demonstrateTaskReqDto);
            return RestMessage.newInstance(true, "成功");
        } catch (Exception e) {
            log.error("演示作业查询异常:{}", e);
            return RestMessage.newInstance(false, "下发异常" + e.getMessage());
        }
    }


}
