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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.asset.controller.admin.replacement.vo.*;
import cn.iocoder.yudao.module.asset.convert.replacement.VehicleReplacementConvert;
import cn.iocoder.yudao.module.asset.dal.dataobject.replacement.VehicleReplacementDO;
import cn.iocoder.yudao.module.asset.service.replacement.VehicleReplacementService;
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;

/**
 * 替换车申请 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 替换车管理")
@RestController
@RequestMapping("/asset/vehicle-replacement")
@Validated
public class VehicleReplacementController {

    @Resource
    private VehicleReplacementService replacementService;

    @PostMapping("/create")
    @Operation(summary = "创建替换车申请")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:create')")
    public CommonResult<Long> createReplacement(@Valid @RequestBody VehicleReplacementSaveReqVO createReqVO) {
        return success(replacementService.createReplacement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新替换车申请")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:update')")
    public CommonResult<Boolean> updateReplacement(@Valid @RequestBody VehicleReplacementSaveReqVO updateReqVO) {
        replacementService.updateReplacement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除替换车申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:delete')")
    public CommonResult<Boolean> deleteReplacement(@RequestParam("id") Long id) {
        replacementService.deleteReplacement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得替换车申请详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:query')")
    public CommonResult<VehicleReplacementRespVO> getReplacement(@RequestParam("id") Long id) {
        VehicleReplacementDO replacement = replacementService.getReplacement(id);
        return success(VehicleReplacementConvert.INSTANCE.convert(replacement));
    }

    @GetMapping("/page")
    @Operation(summary = "获得替换车申请分页")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:query')")
    public CommonResult<PageResult<VehicleReplacementRespVO>> getReplacementPage(@Valid VehicleReplacementPageReqVO pageReqVO) {
        PageResult<VehicleReplacementDO> pageResult = replacementService.getReplacementPage(pageReqVO);
        return success(VehicleReplacementConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/submit")
    @Operation(summary = "提交替换车审批")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:update')")
    public CommonResult<String> submitApproval(@RequestParam("id") Long id) {
        return success(replacementService.submitApproval(id));
    }

    @PostMapping("/withdraw")
    @Operation(summary = "撤回替换车审批")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:update')")
    public CommonResult<Boolean> withdrawApproval(@RequestParam("id") Long id) {
        replacementService.withdrawApproval(id);
        return success(true);
    }

    @PostMapping("/confirm-return")
    @Operation(summary = "确认换回（临时替换）")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('asset:vehicle-replacement:update')")
    public CommonResult<Boolean> confirmReturn(@RequestParam("id") Long id) {
        replacementService.confirmReturn(id);
        return success(true);
    }

}
