package com.xydl.cac.controller;

import com.beanit.iec61850bean.FileInformation;
import com.xydl.cac.entity.IedDlConfig;
import com.xydl.cac.entity.IedDlRecord;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.IedDlConfigService;
import com.xydl.cac.service.IedDlRecordService;
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.Date;
import java.util.List;

@RestController
@Api(tags = {"61850下载文件相关接口"})
@RequestMapping("ieddl")
@Slf4j
public class IedDlController extends BasicController {

    @Resource
    IedDlConfigService configService;
    @Resource
    IedDlRecordService recordService;

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

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

    @PostMapping("update")
    @ApiOperation("更新")
    public Response<String> update(@Validated @RequestBody IedDlConfig 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("listDownload")
    @ApiOperation("查询下载记录列表")
    public Response<Page<IedDlRecord>> list(@ApiParam("配置Id") @RequestParam(value = "configId", required = false) Integer configId,
                                            @ApiParam("devId") @RequestParam(value = "devId", required = false) Integer devId,
                                            @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) Date startTime,
                                            @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) Date endTime,
                                            @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<IedDlRecord> result = recordService.list(configId, devId, startTime, endTime, pageNum, pageSize);
        return Response.success(result);
    }

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

    @GetMapping("listFiles")
    @ApiOperation("查询文件和目录")
    public Response<List<FileInformation>> listFiles(Integer iedId, String path) throws Exception {
        List<FileInformation> result = configService.listFiles(iedId, path);
        return Response.success(result);
    }

}
