package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.DataFilter;
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.DockDTO;
import com.multictrl.modules.business.dto.multi.MultiDockDTO;
import com.multictrl.modules.business.dto.multi.MultiGroupDTO;
import com.multictrl.modules.business.dto.multi.MultiGroupDeviceDTO;
import com.multictrl.modules.business.service.MultiService;
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.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

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

/**
 * 阿罗斯机场蛙跳任务
 *
 * @author Sdy
 * @since 1.0.0 2026/6/8
 */
@RestController
@RequestMapping("business/multi")
@Tag(name = "机场蛙跳任务")
@ApiOrder(21)
@RequiredArgsConstructor
public class MultiController {
    private final MultiService multiService;

    @GetMapping("page")
    @Operation(summary = "蛙跳设备分页列表")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "key", description = "机库名称、机库SN码")
    })
    @DataFilter
    @RequiresPermissions("bus:dock:page")
    public Result<PageData<DockDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<DockDTO> page = multiService.pageList(params);

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

    @GetMapping("pageMultiGroup")
    @Operation(summary = "蛙跳组分页列表")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "name", description = "蛙跳组名")
    })
    @DataFilter
    @RequiresPermissions("bus:dock:pageMultiGroup")
    public Result<PageData<MultiGroupDTO>> pageMultiGroup(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<MultiGroupDTO> page = multiService.pageMultiGroup(params);

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

    @PostMapping("/saveMultiGroup")
    @Operation(summary = "新增蛙跳组")
    @LogOperation("新增蛙跳组")
    @RequiresPermissions("bus:multi:saveMultiGroup")
    public Result<Object> saveMultiGroup(@RequestBody MultiGroupDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        multiService.saveMultiGroup(dto);

        return new Result<>();
    }

    @PutMapping("/updateMultiGroup")
    @Operation(summary = "修改蛙跳组")
    @LogOperation("修改蛙跳组")
    @RequiresPermissions("bus:multi:updateMultiGroup")
    public Result<Object> updateMultiGroup(@RequestBody MultiGroupDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        multiService.updateMultiGroup(dto);

        return new Result<>();
    }

    @DeleteMapping("/deleteMultiGroup")
    @Operation(summary = "删除蛙跳组")
    @LogOperation("删除蛙跳组")
    @RequiresPermissions("bus:multi:deleteMultiGroup")
    public Result<Object> deleteMultiGroup(@RequestBody List<Long> ids) {
        AssertUtils.isListEmpty(ids, "ids");
        multiService.deleteMultiGroup(ids);

        return new Result<>();
    }

    @PostMapping("/saveMultiGroupDevice")
    @Operation(summary = "蛙跳组绑定设备")
    @LogOperation("蛙跳组绑定设备")
    @RequiresPermissions("bus:multi:saveMultiGroupDevice")
    public Result<Object> saveMultiGroupDevice(@RequestBody MultiGroupDeviceDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        multiService.saveMultiGroupDevice(dto);

        return new Result<>();
    }

    @DeleteMapping("/deleteMultiGroupDevice")
    @Operation(summary = "蛙跳组解绑设备")
    @LogOperation("蛙跳组解绑设备")
    @RequiresPermissions("bus:multi:deleteMultiGroupDevice")
    public Result<Object> deleteMultiGroupDevice(@RequestBody List<String> dockSn) {
        //效验数据
        AssertUtils.isListEmpty(dockSn, "dockSn");
        multiService.deleteMultiGroupDevice(dockSn);

        return new Result<>();
    }

    @GetMapping("/getMultiGroupDockList")
    @Operation(summary = "获取蛙跳组机库列表")
    public Result<List<DockDTO>> getMultiGroupDockList(@RequestParam Long multiGroupId) {
        List<DockDTO> list = multiService.getMultiGroupDockList(multiGroupId);
        return new Result<List<DockDTO>>().ok(list);
    }
}
