package com.friday.distributor.controller;

import com.friday.base.common.response.DubboResult;
import com.friday.base.web.response.ResponseAspect;
import com.friday.distributor.service.DistributorService;
import com.friday.operation.api.distributor.dto.*;
import io.swagger.annotations.ApiOperation;
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;

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

    @Autowired
    private DistributorService distributorService;
    @Autowired
    private ResponseAspect responseAspect;

    @ApiOperation(value = "启动任务")
    @RequestMapping(value = "/startTask", method = RequestMethod.POST)
    public Object startTask(@RequestBody StartTaskReq start) {
        DubboResult<TaskStartResult> result = distributorService.startTask(start);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "全部队列轮询")
    @RequestMapping(value = "/polling", method = RequestMethod.POST)
    public void startPolling() {
        distributorService.startQueuePolling();
    }

    @ApiOperation(value = "重启任务")
    @RequestMapping(value = "/restartTask", method = RequestMethod.POST)
    public Object restartTask(@RequestBody RestartTaskReq restart) {
        DubboResult<TaskStartResult> result = distributorService.restartTask(restart);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "终止任务")
    @RequestMapping(value = "/stopTask", method = RequestMethod.POST)
    public Object stopTask(@RequestBody StopTaskReq stop) {
        DubboResult result = distributorService.stopTask(stop);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "获取任务执行结果")
    @RequestMapping(value = "/getTaskResult", method = RequestMethod.POST)
    public Object getTaskResult(@RequestBody TaskResultFilter filter) {
        DubboResult result = distributorService.getTaskResult(filter);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "数据交互-GET")
    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    public Object getData(@RequestBody DataRequest req) {
        DubboResult result = distributorService.getData(req);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "数据交互-POST")
    @RequestMapping(value = "/syncData", method = RequestMethod.POST)
    public Object syncData(@RequestBody DataRequest req) {
        DubboResult result = distributorService.syncData(req);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

}
