package cn.iocoder.yudao.module.smartfactory.controller.admin.linefixedassets;

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.smartfactory.controller.admin.linefixedassets.vo.*;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.linefixedassets.LineFixedAssetsDO;
import cn.iocoder.yudao.module.smartfactory.service.linefixedassets.LineFixedAssetsService;

@Tag(name = "管理后台 - 打包线固定资产")
@RestController
@RequestMapping("/smartfactory/line-fixed-assets")
@Validated
public class LineFixedAssetsController {

    @Resource
    private LineFixedAssetsService lineFixedAssetsService;

    @PostMapping("/create")
    @Operation(summary = "创建打包线固定资产")
    @PreAuthorize("@ss.hasPermission('smartfactory:line-fixed-assets:create')")
    public CommonResult<Long> createLineFixedAssets(@Valid @RequestBody LineFixedAssetsSaveReqVO createReqVO) {
        return success(lineFixedAssetsService.createLineFixedAssets(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打包线固定资产")
    @PreAuthorize("@ss.hasPermission('smartfactory:line-fixed-assets:update')")
    public CommonResult<Boolean> updateLineFixedAssets(@Valid @RequestBody LineFixedAssetsSaveReqVO updateReqVO) {
        lineFixedAssetsService.updateLineFixedAssets(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除打包线固定资产")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:line-fixed-assets:delete')")
    public CommonResult<Boolean> deleteLineFixedAssets(@RequestParam("id") Long id) {
        lineFixedAssetsService.deleteLineFixedAssets(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得打包线固定资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:line-fixed-assets:query')")
    public CommonResult<LineFixedAssetsRespVO> getLineFixedAssets(@RequestParam("id") Long id) {
        LineFixedAssetsDO lineFixedAssets = lineFixedAssetsService.getLineFixedAssets(id);
        return success(BeanUtils.toBean(lineFixedAssets, LineFixedAssetsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打包线固定资产分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:line-fixed-assets:query')")
    public CommonResult<PageResult<LineFixedAssetsRespVO>> getLineFixedAssetsPage(@Valid LineFixedAssetsPageReqVO pageReqVO) {
        PageResult<LineFixedAssetsDO> pageResult = lineFixedAssetsService.getLineFixedAssetsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LineFixedAssetsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打包线固定资产 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:line-fixed-assets:export')")
    @OperateLog(type = EXPORT)
    public void exportLineFixedAssetsExcel(@Valid LineFixedAssetsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LineFixedAssetsDO> list = lineFixedAssetsService.getLineFixedAssetsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打包线固定资产.xls", "数据", LineFixedAssetsRespVO.class,
                        BeanUtils.toBean(list, LineFixedAssetsRespVO.class));
    }

}
