package net.vtstar.zhongtong.avi.monitoring.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.utils.asserts.ParamAssert;
import net.vtstar.utils.domain.Return;
import net.vtstar.zhongtong.avi.monitoring.service.PDAService;
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;

@Slf4j
@Api(description = "PDA相关操作模块")
@RestController
@RequestMapping("/PDA")
public class PDAController {

    @Autowired
    private PDAService pdaService;

    @ApiOperation(value = "焊装车间钣金上撬请求")
    @PostMapping("/hanzhuang/bound")
    private Return hanZhuangBound(@RequestBody JSONObject json) {

        log.info(json.toJSONString());
        String vehicleNo = json.getString("vehicleNo");
        String busNo = json.getString("busNo");
        String workOrderNo = json.getString("workOrderNo");
        ParamAssert.notNull(vehicleNo,"vehicleNo is null");
        ParamAssert.notNull(workOrderNo,"workOrderNo is null");
        if (!pdaService.bound(vehicleNo, busNo, workOrderNo)){
            return Return.failed("未查询到工单号!");
        };
        return Return.success();
    }

    @ApiOperation(value = "换撬请求")
    @PostMapping("/change/skid")
    private Return changeSkid(@RequestBody JSONObject json) {

        log.info(json.toJSONString());
        String orderNo = json.getString("orderNo");
        String sourceStation = json.getString("sourceStation");
        String targetStation = json.getString("targetStation");
        ParamAssert.notNull(json.get("sourceStation"),"sourceStation is null");
        ParamAssert.notNull(json.get("sourceStation"),"sourceStation is null");
        pdaService.changeSkid(sourceStation,targetStation);
        return Return.success();
    }

    @ApiOperation(value = "返修车请求")
    @PostMapping("/repair")
    private Return repair(@RequestBody JSONObject json) {

        log.info(json.toJSONString());
        log.info(json.toJSONString());
        String vehicleNo = json.getString("vehicleNo");
        String busNo = json.getString("busNo");
        ParamAssert.notNull(vehicleNo,"param is null");
        pdaService.repair(vehicleNo,busNo);
        return Return.success();
    }

    @ApiOperation(value = "电泳重新绑定电泳工单号")
    @PostMapping("/elec/bound/order")
    private Return elecBoundOrder(@RequestBody JSONObject json) {

        ParamAssert.notNull(json.get("sourceStation"),"sourceStation is null");
        ParamAssert.notNull(json.get("sourceStation"),"sourceStation is null");
        return Return.success(true);
    }
}
