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.MaintenanceDTO;
import com.multictrl.modules.business.service.MaintenanceService;
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.Map;


/**
 * 设备维护信息
 *
 * @author Sdy
 * @since 1.0.0 2026-06-12
 */
@RestController
@RequestMapping("business/maintenance")
@Tag(name = "设备维护")
@ApiOrder(23)
@RequiredArgsConstructor
public class MaintenanceController {
    private final MaintenanceService maintenanceService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "dockSn", description = "机库SN"),
            @Parameter(name = "date", description = "维保日期 yyyy-MM-dd")
    })
    @RequiresPermissions("bus:maintenance:page")
    public Result<PageData<MaintenanceDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<MaintenanceDTO> page = maintenanceService.pageList(params);

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

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("bus:maintenance:info")
    public Result<MaintenanceDTO> get(@PathVariable("id") Long id) {
        MaintenanceDTO data = maintenanceService.getInfo(id);

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

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

        return new Result<>();
    }

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

        return new Result<>();
    }

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

        return new Result<>();
    }

}
