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.DefaultGroup;
import com.multictrl.common.validator.group.UpdateGroup;
import com.multictrl.modules.business.dto.DockDTO;
import com.multictrl.modules.business.service.DockService;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;

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

/**
 * 机库信息表
 *
 * @author Sdy
 * @since 1.0.0 2026-04-17
 */
@RestController
@RequestMapping("business/dock")
@Tag(name = "机库管理")
@ApiOrder(1)
@RequiredArgsConstructor
public class DockController {
    private final DockService dockService;

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

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

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("bus:dock:info")
    public Result<DockDTO> get(@PathVariable("id") Long id) {
        DockDTO data = dockService.get(id);

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

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

        return new Result<>();
    }

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

        return new Result<>();
    }

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

        return new Result<>();
    }

}
