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

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

    @Resource
    private AssetsTypeService assetsTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建资产类型")
    @PreAuthorize("@ss.hasPermission('system:assets-type:create')")
    public CommonResult<Long> createAssetsType(@Valid @RequestBody AssetsTypeSaveReqVO createReqVO) {
        return success(assetsTypeService.createAssetsType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产类型")
    @PreAuthorize("@ss.hasPermission('system:assets-type:update')")
    public CommonResult<Boolean> updateAssetsType(@Valid @RequestBody AssetsTypeSaveReqVO updateReqVO) {
        assetsTypeService.updateAssetsType(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/getByPid")
    @Operation(summary = "获得资产类型列表")
    public CommonResult<List<AssetsTypeRespVO>> getByPid(@RequestParam("pid") Long pid) {
        List<AssetsTypeDO> assetsTypes = assetsTypeService.getByPid(pid);
        return success(BeanUtils.toBean(assetsTypes, AssetsTypeRespVO.class));
    }

    @GetMapping("/getAllList")
    @Operation(summary = "获取全部类型")
    public CommonResult<List<AssetsTypeRespVO>> getAllList() {
        List<AssetsTypeDO> assetsTypes = assetsTypeService.getAllList();
        return success(BeanUtils.toBean(assetsTypes, AssetsTypeRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:assets-type:query')")
    public CommonResult<AssetsTypeRespVO> getAssetsType(@RequestParam("id") Long id) {
        AssetsTypeDO assetsType = assetsTypeService.getAssetsType(id);
        return success(BeanUtils.toBean(assetsType, AssetsTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产类型分页")
    @PreAuthorize("@ss.hasPermission('system:assets-type:query')")
    public CommonResult<PageResult<AssetsTypeRespVO>> getAssetsTypePage(@Valid AssetsTypePageReqVO pageReqVO) {
        PageResult<AssetsTypeDO> pageResult = assetsTypeService.getAssetsTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetsTypeRespVO.class));
    }

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

}