package cn.iocoder.yudao.module.asset.controller.admin.returnorder;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.asset.controller.admin.returnorder.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.returnorder.ReturnOrderDO;
import cn.iocoder.yudao.module.asset.service.returnorder.ReturnOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 还车管理")
@RestController
@RequestMapping("/asset/return-order")
@Validated
public class ReturnOrderController {

    @Resource
    private ReturnOrderService returnOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建还车单")
    @PreAuthorize("@ss.hasPermission('asset:return-order:create')")
    public CommonResult<Long> createReturnOrder(@Valid @RequestBody ReturnOrderSaveReqVO createReqVO) {
        return success(returnOrderService.createReturnOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新还车单")
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Boolean> updateReturnOrder(@Valid @RequestBody ReturnOrderSaveReqVO updateReqVO) {
        returnOrderService.updateReturnOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除还车单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:delete')")
    public CommonResult<Boolean> deleteReturnOrder(@RequestParam("id") Long id) {
        returnOrderService.deleteReturnOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得还车单详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:query')")
    public CommonResult<ReturnOrderRespVO> getReturnOrder(@RequestParam("id") Long id) {
        return success(returnOrderService.getReturnOrderDetail(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得还车单分页")
    @PreAuthorize("@ss.hasPermission('asset:return-order:query')")
    public CommonResult<PageResult<ReturnOrderRespVO>> getReturnOrderPage(@Valid ReturnOrderPageReqVO pageReqVO) {
        PageResult<ReturnOrderDO> pageResult = returnOrderService.getReturnOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReturnOrderRespVO.class));
    }

    @PutMapping("/complete-inspection")
    @Operation(summary = "完成验车")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Boolean> completeInspection(@RequestParam("id") Long id) {
        returnOrderService.completeInspection(id);
        return success(true);
    }

    @PutMapping("/settle")
    @Operation(summary = "结算还车单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Boolean> settleReturnOrder(@RequestParam("id") Long id) {
        returnOrderService.settleReturnOrder(id);
        return success(true);
    }

    @PostMapping("/create-from-delivery")
    @Operation(summary = "从交车单创建还车单")
    @PreAuthorize("@ss.hasPermission('asset:return-order:create')")
    public CommonResult<Long> createFromDelivery(@RequestParam("deliveryOrderId") Long deliveryOrderId,
                                                  @RequestParam("vehicleIds") List<Long> vehicleIds) {
        return success(returnOrderService.createFromDelivery(deliveryOrderId, vehicleIds));
    }

    @PostMapping("/start-inspection")
    @Operation(summary = "开始单车验车")
    @Parameter(name = "returnOrderVehicleId", description = "还车车辆ID", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Long> startVehicleInspection(@RequestParam("returnOrderVehicleId") Long returnOrderVehicleId) {
        return success(returnOrderService.startVehicleInspection(returnOrderVehicleId));
    }

    @PostMapping("/complete-vehicle-inspection")
    @Operation(summary = "完成单车验车")
    @Parameter(name = "returnOrderVehicleId", description = "还车车辆ID", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Boolean> completeVehicleInspection(@RequestParam("returnOrderVehicleId") Long returnOrderVehicleId) {
        returnOrderService.completeVehicleInspection(returnOrderVehicleId);
        return success(true);
    }

    @PostMapping("/submit")
    @Operation(summary = "提交审批")
    @Parameter(name = "id", description = "还车单ID", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Boolean> submitApproval(@RequestParam("id") Long id) {
        returnOrderService.submitApproval(id);
        return success(true);
    }

    @PostMapping("/withdraw")
    @Operation(summary = "撤回审批")
    @Parameter(name = "id", description = "还车单ID", required = true)
    @PreAuthorize("@ss.hasPermission('asset:return-order:update')")
    public CommonResult<Boolean> withdrawApproval(@RequestParam("id") Long id) {
        returnOrderService.withdrawApproval(id);
        return success(true);
    }

}
