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

import com.xindazhou.business.api.service.MaintenanceOrderApi;
import com.xindazhou.business.controller.admin.service.maintenance.vo.MaintenanceOrderPageReqVO;
import com.xindazhou.business.controller.admin.service.maintenance.vo.MaintenanceOrderRespVO;
import com.xindazhou.business.controller.admin.service.maintenance.vo.MaintenanceOrderSaveReqVO;
import com.xindazhou.business.controller.admin.service.maintenance.vo.MaintenanceOrderStatusUpdateReqVO;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.common.util.json.JsonUtils;
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 java.util.Map;

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

/**
 * 管理后台 - 维保订单代理 Controller（统一前端调用路径）
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 维保管理")
@RestController("serviceMaintenanceOrderProxyController")
@RequestMapping("/admin/service/maintenance/order")
@Validated
@Slf4j
public class MaintenanceOrderProxyController {

    @Resource
    private MaintenanceOrderApi maintenanceOrderApi;

    @GetMapping("/page")
    @Operation(summary = "获得维保订单分页列表")
    @PreAuthorize("@ss.hasPermission('service:maintenance:query')")
    public CommonResult<PageResult<MaintenanceOrderRespVO>> getMaintenanceOrderPage(@Valid MaintenanceOrderPageReqVO pageReqVO) {
        CommonResult<PageResult<Map<String, Object>>> result = maintenanceOrderApi.getMaintenanceOrderPage(
                pageReqVO.getPageNo(),
                pageReqVO.getPageSize(),
                pageReqVO.getKeyword(),
                pageReqVO.getStatus(),
                pageReqVO.getStartTime() != null ? pageReqVO.getStartTime().toString() : null,
                pageReqVO.getEndTime() != null ? pageReqVO.getEndTime().toString() : null
        );
        // 转换为VO
        PageResult<Map<String, Object>> data = result.getData();
        PageResult<MaintenanceOrderRespVO> pageResult = JsonUtils.parseObject(JsonUtils.toJsonString(data), PageResult.class);
        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) {
        CommonResult<Map<String, Object>> result = maintenanceOrderApi.getMaintenanceOrder(id);
        MaintenanceOrderRespVO order = JsonUtils.parseObject(JsonUtils.toJsonString(result.getData()), MaintenanceOrderRespVO.class);
        return success(order);
    }

    @PostMapping
    @Operation(summary = "创建维保订单")
    @PreAuthorize("@ss.hasPermission('service:maintenance:create')")
    public CommonResult<Long> createMaintenanceOrder(@Valid @RequestBody MaintenanceOrderSaveReqVO createReqVO) {
        Map<String, Object> createMap = JsonUtils.parseObject(JsonUtils.toJsonString(createReqVO), Map.class);
        CommonResult<Long> result = maintenanceOrderApi.createMaintenanceOrder(createMap);
        return success(result.getData());
    }

    @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);
        Map<String, Object> updateMap = JsonUtils.parseObject(JsonUtils.toJsonString(updateReqVO), Map.class);
        CommonResult<Boolean> result = maintenanceOrderApi.updateMaintenanceOrder(id, updateMap);
        return success(result.getData());
    }

    @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) {
        CommonResult<Boolean> result = maintenanceOrderApi.deleteMaintenanceOrder(id);
        return success(result.getData());
    }

    @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) {
        Map<String, Object> statusMap = JsonUtils.parseObject(JsonUtils.toJsonString(statusUpdateReqVO), Map.class);
        CommonResult<Boolean> result = maintenanceOrderApi.updateMaintenanceOrderStatus(id, statusMap);
        return success(result.getData());
    }

}

