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.AssetsDO;
import cn.iocoder.yudao.module.system.service.assets.AssetsService;

@Tag(name = "管理后台 - 资产")
@RestController
@RequestMapping("/system/assets")
@Validated
public class AssetsController {

    @Resource
    private AssetsService assetsService;

    @PostMapping("/create")
    @Operation(summary = "创建资产")
    @PreAuthorize("@ss.hasPermission('system:assets:create')")
    public CommonResult<Long> createAssets(@Valid @RequestBody AssetsSaveReqVO createReqVO) {
        return success(assetsService.createAssets(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产")
    @PreAuthorize("@ss.hasPermission('system:assets:update')")
    public CommonResult<Boolean> updateAssets(@Valid @RequestBody AssetsSaveReqVO updateReqVO) {
        assetsService.updateAssets(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:assets:query')")
    public CommonResult<AssetsRespVO> getAssets(@RequestParam("id") Long id) {
        AssetsDO assets = assetsService.getAssets(id);
        return success(BeanUtils.toBean(assets, AssetsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产分页")
    @PreAuthorize("@ss.hasPermission('system:assets:query')")
    public CommonResult<PageResult<AssetsRespVO>> getAssetsPage(@Valid AssetsPageReqVO pageReqVO) {
        PageResult<AssetsDO> pageResult = assetsService.getAssetsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产 Excel")
    @PreAuthorize("@ss.hasPermission('system:assets:export')")
    @OperateLog(type = EXPORT)
    public void exportAssetsExcel(@Valid AssetsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AssetsDO> list = assetsService.getAssetsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资产.xls", "数据", AssetsRespVO.class,
                        BeanUtils.toBean(list, AssetsRespVO.class));
    }

}