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.TaieJobRecord;
import com.friday.operation.api.distributor.dto.TaieTaskRecordFilter;
import com.friday.operation.api.distributor.dto.TaieTaskRecordPageResult;
import com.friday.operation.api.executor.dto.module.ModuleRegisterDto;
import com.friday.operation.api.executor.service.IDynamicModuleService;
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.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/dynamic")
@Slf4j
public class DynamicController {

    @Autowired
    private IDynamicModuleService dynamicService;
    @Autowired
    private ResponseAspect responseAspect;

    @ApiOperation(value = "加载代码")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Object registerModule(@RequestBody ModuleRegisterDto register) {
        DubboResult result = dynamicService.registerModule(register);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

    @ApiOperation(value = "移除代码")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Object removeModule(@ApiParam(name = "moduleName") String moduleName, @ApiParam(name = "moduleVersion") String moduleVersion) {
        DubboResult result = dynamicService.removeModule(moduleName, moduleVersion);
        if (result.success()){
            return this.responseAspect.success(result.getObject());
        } else {
            return this.responseAspect.fail(result.getMessage());
        }
    }

}
