package com.xydl.cac.controller;

import com.xydl.cac.entity.RemoteConfig;
import com.xydl.cac.entity.RemoteDownload;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.RemoteConfigService;
import com.xydl.cac.service.RemoteDownloadService;
import com.xydl.cac.util.SFTPTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(tags = {"远端服务器相关接口"})
@RequestMapping("remote")
@Slf4j
public class RemoteConfigController extends BasicController {

    @Resource
    RemoteConfigService configService;
    @Resource
    RemoteDownloadService downloadService;

    @GetMapping("listAll")
    @ApiOperation("查询全部列表")
    public Response<List<RemoteConfig>> listAll() {
        List<RemoteConfig> result = configService.listAll();
        return Response.success(result);
    }

    @PostMapping("add")
    @ApiOperation("新增")
    public Response<RemoteConfig> add(@Validated @RequestBody RemoteConfig item) throws Exception {
        RemoteConfig result = configService.add(item);
        return Response.success(result);
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public Response<String> update(@Validated @RequestBody RemoteConfig item) throws Exception {
        if (item.getId() == null) {
            throw new BusinessException("id不能为空!");
        }
        configService.update(item);
        return Response.success("OK");
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public Response<String> delete(@Validated @NotNull(message = "id不能为空!") Integer id) throws Exception {
        if (id == null) {
            throw new BusinessException("id不能为空!");
        }
        configService.delete(id);
        return Response.success("OK");
    }

    @GetMapping("test")
    @ApiOperation("测试")
    public Response<String> test(Integer id) throws Exception {
        RemoteConfig item = configService.get(id);
        SFTPTool sftpTool = new SFTPTool();
        sftpTool.connect(item.getIp(), item.getPort(), item.getUser(), item.getPasswd());
        sftpTool.disconnect();
        return Response.success("正常");
    }

    @GetMapping("listDownload")
    @ApiOperation("查询下载记录列表")
    public Response<Page<RemoteDownload>> list(@ApiParam("类型") @RequestParam(value = "configId", required = false) Integer configId,
                                               @ApiParam("页码") @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                               @ApiParam("每页数量") @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
        pageNum = this.initPageNum(pageNum);
        pageSize = this.initPageSize(pageSize);
        Page<RemoteDownload> result = downloadService.list(configId, pageNum, pageSize);
        return Response.success(result);
    }

}
