package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.AssertUtils;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.common.validator.group.AddGroup;
import com.multictrl.common.validator.group.UpdateGroup;
import com.multictrl.modules.business.dto.NoflyZoneDTO;
import com.multictrl.modules.business.service.NoflyZoneService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;

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


/**
 * 禁飞区
 *
 * @author Sdy
 * @since 1.0.0 2026-06-09
 */
@RestController
@RequestMapping("business/noflyzone")
@Tag(name = "禁飞区")
@ApiOrder(22)
@RequiredArgsConstructor
public class NoflyZoneController {
    private final NoflyZoneService noflyZoneService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "name", description = "名称")
    })
    @RequiresPermissions("bus:noflyzone:page")
    public Result<PageData<NoflyZoneDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<NoflyZoneDTO> page = noflyZoneService.pageList(params);

        return new Result<PageData<NoflyZoneDTO>>().ok(page);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("bus:noflyzone:save")
    public Result<Object> save(@RequestBody NoflyZoneDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        noflyZoneService.saveNoFlying(dto);

        return new Result<>();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("bus:noflyzone:update")
    public Result<Object> update(@RequestBody NoflyZoneDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        noflyZoneService.update(dto);

        return new Result<>();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("bus:noflyzone:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        noflyZoneService.delete(ids);

        return new Result<>();
    }

    @GetMapping("/getNoFlying")
    @Operation(summary = "获取机库禁飞区", description = "返回机库所属组织以及上级组织设置的禁飞区")
    public Result<List<NoflyZoneDTO>> getNoFlying(@RequestParam String dockSn) {
        List<NoflyZoneDTO> list = noflyZoneService.getNoFlyZoneByDockSn(dockSn);

        return new Result<List<NoflyZoneDTO>>().ok(list);
    }
}
