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.RouteDTO;
import com.multictrl.modules.business.service.RouteService;
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-04-20
 */
@RestController
@RequestMapping("business/route")
@Tag(name = "航线管理")
@ApiOrder(2)
@RequiredArgsConstructor
public class RouteController {

    private final RouteService routeService;

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

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

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("bus:route:info")
    public Result<RouteDTO> get(@PathVariable("id") Long id) {
        RouteDTO data = routeService.getRoute(id);

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

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

        return new Result<>();
    }

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

        return new Result<>();
    }

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

        return new Result<>();
    }
}
