package com.friday.distributor.controller;

import com.friday.base.common.response.DubboResult;
import com.friday.base.web.response.ResponseAspect;
import com.friday.distributor.service.DistributorOperationService;
import com.friday.operation.api.distributor.dto.JobExecuteLogDto;
import com.friday.operation.api.distributor.dto.TaieJobRecord;
import com.friday.operation.api.distributor.dto.TaieTaskRecordFilter;
import com.friday.operation.api.distributor.dto.TaieTaskRecordPageResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
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 java.util.ArrayList;

@RestController
@RequestMapping(value = "/task/distributor/operation")
@Slf4j
public class OperationController {

    @Autowired
    private DistributorOperationService operationService;
    @Autowired
    private ResponseAspect responseAspect;

    @ApiOperation(value = "分页查询任务执行记录")
    @RequestMapping(value = "/queryTaskRecordByPage", method = RequestMethod.POST)
    public Object queryTaskRecordByPage(@RequestBody TaieTaskRecordFilter filter) {
        DubboResult<TaieTaskRecordPageResult> result = operationService.queryTaskRecordByPage(filter);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "查询任务执行详情")
    @RequestMapping(value = "/getTaskExecDetailByTaskId", method = RequestMethod.POST)
    public Object getTaskExecDetailByTaskId(@ApiParam(name = "taskId") String taskId) {
        DubboResult<ArrayList<TaieJobRecord>> result = operationService.queryTaskExecDetailByTaskId(taskId);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "查询job执行详情")
    @RequestMapping(value = "/getJobExecDetailByInvokeId", method = RequestMethod.POST)
    public Object getJobExecDetailByInvokeId(@ApiParam(name = "invokeId") Integer invokeId) {
        DubboResult<ArrayList<JobExecuteLogDto>> result = operationService.queryJobExecDetailByInvokeId(invokeId);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "查询job module列表")
    @RequestMapping(value = "/queryJobModuleByInvokeId", method = RequestMethod.POST)
    public Object queryJobModuleByInvokeId(@ApiParam(name = "invokeId") Integer invokeId) {
        DubboResult<ArrayList<String>> result = operationService.queryJobModuleByInvokeId(invokeId);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "同步job执行记录")
    @RequestMapping(value = "/syncJobLog", method = RequestMethod.POST)
    public Object syncJobLog(@RequestBody @Validated JobExecuteLogDto executeLog, BindingResult checkResult) {
        if(checkResult.hasErrors()) {
            return this.responseAspect.fail(checkResult.getAllErrors().get(0).getDefaultMessage());
        }
        DubboResult result = operationService.syncJobLog(executeLog);
        return this.responseAspect.response(result);
    }
}
