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

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

    @Resource
    private DeptAssetsService deptAssetsService;

    @GetMapping("/get")
    @Operation(summary = "获得部门资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept-assets:query')")
    public CommonResult<DeptAssetsRespVO> getDeptAssets(@RequestParam("id") Long id) {
        DeptAssetsDO deptAssets = deptAssetsService.getDeptAssets(id);
        return success(BeanUtils.toBean(deptAssets, DeptAssetsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门资产分页")
    @PreAuthorize("@ss.hasPermission('system:dept-assets:query')")
    public CommonResult<PageResult<DeptAssetsRespVO>> getDeptAssetsPage(@Valid DeptAssetsPageReqVO pageReqVO) {
        PageResult<DeptAssetsRespVO> pageResult = deptAssetsService.getDeptAssetsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptAssetsRespVO.class));
    }


    @GetMapping("/deptAssetsPage")
    @Operation(summary = "获得部门资产(一点数据)")
    public CommonResult<PageResult<DeptAssetsLittleVO>> deptAssetsLittlePage(@Valid DeptAssetsPageReqVO pageReqVO) {
        PageResult<DeptAssetsLittleVO> pageResult = deptAssetsService.deptAssetsLittlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptAssetsLittleVO.class));
    }

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

}