package org.jeecg.modules.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.manual.entity.Manual;
import org.jeecg.modules.manual.service.IManualService;
import org.jeecg.modules.toolBorrowingRecord.service.IToolBorrowingRecordService;
import org.jeecg.modules.workOrder.entity.WorkRecord;
import org.jeecg.modules.workOrder.model.WorkOrderDetailRecord;
import org.jeecg.modules.workOrder.service.IWorkRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 智能工具传数接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@Api(tags="API智能工具对接")
@RequestMapping("/business")
public class ToolDataApiController {

    private final IToolBorrowingRecordService toolBorrowingRecordService;
    private final IManualService manualService;
    private final IWorkRecordService workRecordService;

    /**
     * 无线轮径尺测量数据接收接口
     * @param id
     * @param rel
     * @param dateTime
     * @return
     */
    @ApiOperation(value="无线轮径尺测量数据接收接口", notes="无线轮径尺测量数据接收接口")
    @GetMapping("/ruler/syncMeasurements/{id}/{rel}/{dateTime}")
    public Result<String> rulerMeasure(@RequestParam(name="仪器ID") Long id,
                                       @RequestParam(name="测量值") String rel,
                                       @RequestParam(name="测量时间") String dateTime) {
        return Result.OK("操作成功");
    }

    @ApiOperation(value="任务工单-创建工单的测量项记录", notes="任务工单-创建工单的测量项记录")
    @PostMapping(value = "/workOrder/record")
    public Result<List<WorkRecord>> createRecord(@RequestBody WorkRecord workRecord) {
        try{
            return Result.ok(workRecordService.initWorkRecordTwo(workRecord));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value="任务工单-根据工单id获取测量项记录", notes="任务工单-根据工单id获取测量项记录")
    @GetMapping(value = "/workOrder/record/list")
    public Result<List<WorkOrderDetailRecord>> listWorkRecordByWorkOrder(@RequestParam(name="workOrder") String workOrder) {
        return Result.OK(workRecordService.listWorkRecordByWorkOrder(workOrder));
    }

    @ApiOperation(value="任务工单-根据记录id指定当前测量项", notes="任务工单-根据记录id指定当前测量项")
    @PutMapping(value = "/workOrder/record/now")
    public Result<?> nowRecord(@RequestParam(name="recordId") String recordId) {
        return Result.OK(workRecordService.nowWorkRecordTwo(recordId));
    }

    @ApiOperation(value="任务工单-根据工单id获取已借用的工器具", notes="任务工单-根据工单id获取已借用的工器具")
    @GetMapping("/workOrder/tool/{id}")
    public Result<?> queryToolsByWorkOrder(@PathVariable("id") String id){
        try{
            return Result.ok(toolBorrowingRecordService.queryToolByOrder(id));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value="作业指导书-通过workOrder查询", notes="作业指导书-通过workOrder查询")
    @GetMapping(value = "/manual/queryByWorkOrder")
    public Result<List<Manual>> getManualByWorkOrder(@RequestParam(name="workOrder",required=true) String workOrder) {
        List<Manual> manuals = manualService.queryByWorkOrder(workOrder);
        if(manuals.isEmpty()) {
            return Result.error("未找到作业指导书");
        }
        return Result.OK(manuals);
    }

}
