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

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.assets.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.AssetsSecondmentRecordDO;
import cn.iocoder.yudao.module.system.service.assets.AssetsSecondmentRecordService;

@Tag(name = "管理后台 - 资产借调记录")
@RestController
@RequestMapping("/system/assets-secondment-record")
@Validated
public class AssetsSecondmentRecordController {

    @Resource
    private AssetsSecondmentRecordService assetsSecondmentRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建资产借调记录")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment-record:create')")
    public CommonResult<Long> createAssetsSecondmentRecord(@Valid @RequestBody AssetsSecondmentRecordSaveReqVO createReqVO) {
        return success(assetsSecondmentRecordService.createAssetsSecondmentRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产借调记录")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment-record:update')")
    public CommonResult<Boolean> updateAssetsSecondmentRecord(@Valid @RequestBody AssetsSecondmentRecordSaveReqVO updateReqVO) {
        assetsSecondmentRecordService.updateAssetsSecondmentRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产借调记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:assets-secondment-record:delete')")
    public CommonResult<Boolean> deleteAssetsSecondmentRecord(@RequestParam("id") Long id) {
        assetsSecondmentRecordService.deleteAssetsSecondmentRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产借调记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment-record:query')")
    public CommonResult<AssetsSecondmentRecordRespVO> getAssetsSecondmentRecord(@RequestParam("id") Long id) {
        AssetsSecondmentRecordDO assetsSecondmentRecord = assetsSecondmentRecordService.getAssetsSecondmentRecord(id);
        return success(BeanUtils.toBean(assetsSecondmentRecord, AssetsSecondmentRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产借调记录分页")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment-record:query')")
    public CommonResult<PageResult<AssetsSecondmentRecordRespVO>> getAssetsSecondmentRecordPage(@Valid AssetsSecondmentRecordPageReqVO pageReqVO) {
        PageResult<AssetsSecondmentRecordRespVO> pageResult = assetsSecondmentRecordService.getAssetsSecondmentRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetsSecondmentRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产借调记录 Excel")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment-record:export')")
    @OperateLog(type = EXPORT)
    public void exportAssetsSecondmentRecordExcel(@Valid AssetsSecondmentRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AssetsSecondmentRecordRespVO> list = assetsSecondmentRecordService.getAssetsSecondmentRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资产借调记录.xls", "数据", AssetsSecondmentRecordRespVO.class,
                        BeanUtils.toBean(list, AssetsSecondmentRecordRespVO.class));
    }

}