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

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.delivery.vo.*;
import cn.iocoder.yudao.module.asset.dal.dataobject.delivery.DeliveryOrderDO;
import cn.iocoder.yudao.module.asset.service.delivery.DeliveryOrderService;
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 static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 交车单")
@RestController
@RequestMapping("/asset/delivery-order")
@Validated
public class DeliveryOrderController {

    @Resource
    private DeliveryOrderService deliveryOrderService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得交车单分页")
    @PreAuthorize("@ss.hasPermission('asset:delivery-order:query')")
    public CommonResult<PageResult<DeliveryOrderRespVO>> getDeliveryOrderPage(@Valid DeliveryOrderPageReqVO pageReqVO) {
        PageResult<DeliveryOrderDO> pageResult = deliveryOrderService.getDeliveryOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryOrderRespVO.class));
    }

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

}
