package cn.iocoder.yudao.module.server.controller.admin.manufacturer;

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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.server.controller.admin.manufacturer.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.manufacturer.ManufacturerDO;
import cn.iocoder.yudao.module.server.service.manufacturer.ManufacturerService;

@Tag(name = "管理后台 - 技术运维单位")
@RestController
@RequestMapping("/server/manufacturer")
@Validated
public class ManufacturerController {

    @Resource
    private ManufacturerService manufacturerService;

    @PostMapping("/create")
    @Operation(summary = "创建技术运维单位")
    @PreAuthorize("@ss.hasPermission('server:manufacturer:create')")
    public CommonResult<Long> createManufacturer(@Valid @RequestBody ManufacturerSaveReqVO createReqVO) {
        return success(manufacturerService.createManufacturer(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新技术运维单位")
    @PreAuthorize("@ss.hasPermission('server:manufacturer:update')")
    public CommonResult<Boolean> updateManufacturer(@Valid @RequestBody ManufacturerSaveReqVO updateReqVO) {
        manufacturerService.updateManufacturer(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除技术运维单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:manufacturer:delete')")
    public CommonResult<Boolean> deleteManufacturer(@RequestParam("id") Long id) {
        manufacturerService.deleteManufacturer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得技术运维单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:manufacturer:query')")
    public CommonResult<ManufacturerRespVO> getManufacturer(@RequestParam("id") Long id) {
        ManufacturerDO manufacturer = manufacturerService.getManufacturer(id);
        return success(BeanUtils.toBean(manufacturer, ManufacturerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得技术运维单位分页")
    @PreAuthorize("@ss.hasPermission('server:manufacturer:query')")
    public CommonResult<PageResult<ManufacturerRespVO>> getManufacturerPage(@Valid ManufacturerPageReqVO pageReqVO) {
        PageResult<ManufacturerDO> pageResult = manufacturerService.getManufacturerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ManufacturerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出技术运维单位 Excel")
    @PreAuthorize("@ss.hasPermission('server:manufacturer:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportManufacturerExcel(@Valid ManufacturerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ManufacturerDO> list = manufacturerService.getManufacturerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "技术运维单位.xls", "数据", ManufacturerRespVO.class,
                        BeanUtils.toBean(list, ManufacturerRespVO.class));
    }

}