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

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.factorykiln.vo.*;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorykiln.FactoryKilnDO;
import cn.iocoder.yudao.module.smartfactory.service.factorykiln.FactoryKilnService;

@Tag(name = "管理后台 - 工厂窑炉")
@RestController
@RequestMapping("/smartfactory/factory-kiln")
@Validated
public class FactoryKilnController {

    @Resource
    private FactoryKilnService factoryKilnService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂窑炉")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-kiln:create')")
    public CommonResult<Long> createFactoryKiln(@Valid @RequestBody FactoryKilnSaveReqVO createReqVO) {
        return success(factoryKilnService.createFactoryKiln(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂窑炉")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-kiln:update')")
    public CommonResult<Boolean> updateFactoryKiln(@Valid @RequestBody FactoryKilnSaveReqVO updateReqVO) {
        factoryKilnService.updateFactoryKiln(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂窑炉")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-kiln:delete')")
    public CommonResult<Boolean> deleteFactoryKiln(@RequestParam("id") Long id) {
        factoryKilnService.deleteFactoryKiln(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂窑炉")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-kiln:query')")
    public CommonResult<FactoryKilnRespVO> getFactoryKiln(@RequestParam("id") Long id) {
        FactoryKilnDO factoryKiln = factoryKilnService.getFactoryKiln(id);
        return success(BeanUtils.toBean(factoryKiln, FactoryKilnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂窑炉分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-kiln:query')")
    public CommonResult<PageResult<FactoryKilnRespVO>> getFactoryKilnPage(@Valid FactoryKilnPageReqVO pageReqVO) {
        PageResult<FactoryKilnRespVO> pageResult = factoryKilnService.getFactoryKilnPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂窑炉 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-kiln:export')")
    @OperateLog(type = EXPORT)
    public void exportFactoryKilnExcel(@Valid FactoryKilnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryKilnRespVO> list = factoryKilnService.getFactoryKilnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂窑炉.xls", "数据", FactoryKilnRespVO.class,
                list);
    }

}
