package com.xindazhou.vehicle.controller.admin.vehicle;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleCategoryPageReqVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleCategoryRespVO;
import com.xindazhou.vehicle.controller.admin.vehicle.vo.VehicleCategorySaveReqVO;
import com.xindazhou.vehicle.service.category.VehicleCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 分类管理 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 分类管理")
@RestController
@RequestMapping("/admin/vehicle/category")
@Validated
public class VehicleCategoryController {

    @Resource
    private VehicleCategoryService vehicleCategoryService;

    @GetMapping("/page")
    @Operation(summary = "获得分类管理分页")
    @PreAuthorize("@ss.hasPermission('vehicle:category:query')")
    public CommonResult<PageResult<VehicleCategoryRespVO>> getVehicleCategoryPage(@Valid VehicleCategoryPageReqVO pageReqVO) {
        PageResult<VehicleCategoryRespVO> pageResult = vehicleCategoryService.getVehicleCategoryPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分类管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('vehicle:category:query')")
    public CommonResult<VehicleCategoryRespVO> getVehicleCategory(@RequestParam("id") Long id) {
        VehicleCategoryRespVO category = vehicleCategoryService.getVehicleCategory(id);
        return success(category);
    }

    @PostMapping("/create")
    @Operation(summary = "创建分类管理")
    @PreAuthorize("@ss.hasPermission('vehicle:category:create')")
    public CommonResult<Long> createVehicleCategory(@Valid @RequestBody VehicleCategorySaveReqVO createReqVO) {
        return success(vehicleCategoryService.createVehicleCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分类管理")
    @PreAuthorize("@ss.hasPermission('vehicle:category:update')")
    public CommonResult<Boolean> updateVehicleCategory(@Valid @RequestBody VehicleCategorySaveReqVO updateReqVO) {
        vehicleCategoryService.updateVehicleCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分类管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('vehicle:category:delete')")
    public CommonResult<Boolean> deleteVehicleCategory(@RequestParam("id") Long id) {
        vehicleCategoryService.deleteVehicleCategory(id);
        return success(true);
    }

    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除分类管理")
    @PreAuthorize("@ss.hasPermission('vehicle:category:delete')")
    public CommonResult<Boolean> batchDeleteVehicleCategory(@RequestParam("ids") Collection<Long> ids) {
        vehicleCategoryService.batchDeleteVehicleCategory(ids);
        return success(true);
    }

    @PostMapping("/batchDisplay")
    @Operation(summary = "批量显示/不显示分类管理")
    @PreAuthorize("@ss.hasPermission('vehicle:category:update')")
    public CommonResult<Boolean> batchDisplayVehicleCategory(@RequestParam("ids") Collection<Long> ids,
                                                              @RequestParam("isDisplay") Integer isDisplay) {
        vehicleCategoryService.batchDisplayVehicleCategory(ids, isDisplay);
        return success(true);
    }

}

