package com.weather.modules.weather.filescan.controller;

import com.weather.common.annotation.LogOperation;
import com.weather.common.constant.Constant;
import com.weather.common.page.PageData;
import com.weather.common.utils.Result;
import com.weather.common.validator.AssertUtils;
import com.weather.modules.weather.filescan.dto.DeptFileGroupVO;
import com.weather.modules.weather.filescan.dto.ModelFileGroupVO;
import com.weather.modules.weather.filescan.dto.WeatherFileScanRecordDTO;
import com.weather.modules.weather.filescan.service.WeatherFileScanRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("filescan/record")
@Tag(name = "文件扫描")
public class WeatherFileScanRecordController {

    @Resource
    private WeatherFileScanRecordService weatherFileScanRecordService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String")
    })
    @RequiresPermissions("filescan:record:page")
    public Result<PageData<WeatherFileScanRecordDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<WeatherFileScanRecordDTO> page = weatherFileScanRecordService.page(params);
        return new Result<PageData<WeatherFileScanRecordDTO>>().ok(page);
    }

    @GetMapping("tree")
    @Operation(summary = "部门文件树")
    @RequiresPermissions("filescan:record:tree")
    public Result<List<DeptFileGroupVO>> tree(@RequestParam Long deptId) {
        List<DeptFileGroupVO> data = weatherFileScanRecordService.getDeptFileTree(deptId);
        return new Result<List<DeptFileGroupVO>>().ok(data);
    }

    @GetMapping("model")
    @Operation(summary = "模式预报文件列表")
    @RequiresPermissions("filescan:record:model:list")
    public Result<ModelFileGroupVO> model() {
        ModelFileGroupVO data = weatherFileScanRecordService.getModelFiles();
        return new Result<ModelFileGroupVO>().ok(data);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("filescan:record:info")
    public Result<WeatherFileScanRecordDTO> get(@PathVariable("id") Long id) {
        WeatherFileScanRecordDTO data = weatherFileScanRecordService.get(id);
        return new Result<WeatherFileScanRecordDTO>().ok(data);
    }

    @PostMapping("archive/{id}")
    @Operation(summary = "归档")
    @LogOperation("归档")
    @RequiresPermissions("filescan:record:archive")
    public Result archive(@PathVariable("id") Long id) {
        weatherFileScanRecordService.archive(id);
        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("filescan:record:delete")
    public Result delete(@RequestBody Long[] ids) {
        AssertUtils.isArrayEmpty(ids, "id");
        weatherFileScanRecordService.delete(ids);
        return new Result();
    }
}
