package cn.iocoder.yudao.module.system.controller.admin.assetreceive;

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.system.controller.admin.assetreceive.vo.AssetReceivePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.assetreceive.vo.AssetReceiveRespVO;
import cn.iocoder.yudao.module.system.controller.admin.assetreceive.vo.AssetReceiveSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.assetreceive.AssetReceiveDO;
import cn.iocoder.yudao.module.system.service.assetreceive.AssetReceiveService;
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 javax.annotation.Resource;
import javax.validation.Valid;

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

@Tag(name = "管理后台 - 资产领用")
@RestController
@RequestMapping("/system/asset-receive")
@Validated
public class AssetReceiveController {

    @Resource
    private AssetReceiveService assetReceiveService;

    @PostMapping("/create")
    @Operation(summary = "创建资产领用")
    @PreAuthorize("@ss.hasPermission('system:asset-receive:create')")
    public CommonResult<Long> createAssetReceive(@Valid @RequestBody AssetReceiveSaveReqVO createReqVO) {
        return success(assetReceiveService.createAssetReceive(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产领用")
    @PreAuthorize("@ss.hasPermission('system:asset-receive:update')")
    public CommonResult<Boolean> updateAssetReceive(@Valid @RequestBody AssetReceiveSaveReqVO updateReqVO) {
        assetReceiveService.updateAssetReceive(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产领用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:asset-receive:delete')")
    public CommonResult<Boolean> deleteAssetReceive(@RequestParam("id") Long id) {
        assetReceiveService.deleteAssetReceive(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产领用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:asset-receive:query')")
    public CommonResult<AssetReceiveRespVO> getAssetReceive(@RequestParam("id") Long id) {
        AssetReceiveDO assetReceive = assetReceiveService.getAssetReceive(id);
        return success(BeanUtils.toBean(assetReceive, AssetReceiveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产领用分页")
    @PreAuthorize("@ss.hasPermission('system:asset-receive:query')")
    public CommonResult<PageResult<AssetReceiveRespVO>> getAssetReceivePage(@Valid AssetReceivePageReqVO pageReqVO) {
        PageResult<AssetReceiveDO> pageResult = assetReceiveService.getAssetReceivePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetReceiveRespVO.class));
    }
}