package com.xindazhou.platform.controller.admin.service.maintenance;

import com.xindazhou.platform.controller.admin.service.maintenance.vo.MaintenanceOrderPageReqVO;
import com.xindazhou.platform.controller.admin.service.maintenance.vo.MaintenanceOrderRespVO;
import com.xindazhou.platform.controller.admin.service.maintenance.vo.MaintenanceOrderSaveReqVO;
import com.xindazhou.platform.controller.admin.service.maintenance.vo.MaintenanceOrderStatusUpdateReqVO;
import com.xindazhou.platform.service.service.maintenance.MaintenanceOrderService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 管理后台 - 维保订单 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 维保管理")
@RestController("platformMaintenanceOrderController")
@RequestMapping("/admin/service/maintenance/order")
@Validated
@Slf4j
public class MaintenanceOrderController {

    @Resource
    private MaintenanceOrderService maintenanceOrderService;

    @GetMapping("/page")
    @Operation(summary = "获得维保订单分页列表")
    @PreAuthorize("@ss.hasPermission('service:maintenance:query')")
    public CommonResult<PageResult<MaintenanceOrderRespVO>> getMaintenanceOrderPage(@Valid MaintenanceOrderPageReqVO pageReqVO) {
        PageResult<MaintenanceOrderRespVO> pageResult = maintenanceOrderService.getMaintenanceOrderPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获得维保订单")
    @Parameter(name = "id", description = "订单ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('service:maintenance:query')")
    public CommonResult<MaintenanceOrderRespVO> getMaintenanceOrder(@PathVariable("id") Long id) {
        MaintenanceOrderRespVO order = maintenanceOrderService.getMaintenanceOrder(id);
        return success(order);
    }

    @PostMapping
    @Operation(summary = "创建维保订单")
    @PreAuthorize("@ss.hasPermission('service:maintenance:create')")
    public CommonResult<Long> createMaintenanceOrder(@Valid @RequestBody MaintenanceOrderSaveReqVO createReqVO) {
        Long id = maintenanceOrderService.createMaintenanceOrder(createReqVO);
        return success(id);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新维保订单")
    @Parameter(name = "id", description = "订单ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('service:maintenance:update')")
    public CommonResult<Boolean> updateMaintenanceOrder(@PathVariable("id") Long id,
                                                         @Valid @RequestBody MaintenanceOrderSaveReqVO updateReqVO) {
        updateReqVO.setId(id);
        maintenanceOrderService.updateMaintenanceOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除维保订单")
    @Parameter(name = "id", description = "订单ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('service:maintenance:delete')")
    public CommonResult<Boolean> deleteMaintenanceOrder(@PathVariable("id") Long id) {
        maintenanceOrderService.deleteMaintenanceOrder(id);
        return success(true);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新维保订单状态")
    @Parameter(name = "id", description = "订单ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('service:maintenance:update')")
    public CommonResult<Boolean> updateMaintenanceOrderStatus(@PathVariable("id") Long id,
                                                               @Valid @RequestBody MaintenanceOrderStatusUpdateReqVO statusUpdateReqVO) {
        maintenanceOrderService.updateMaintenanceOrderStatus(id, statusUpdateReqVO);
        return success(true);
    }

}

