package cn.iocoder.yudao.module.mes.controller.admin.zjtype;

import cn.iocoder.yudao.module.iot.dal.dataobject.deviceattributetype.DeviceAttributeTypeDO;
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.mes.controller.admin.zjtype.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.zjtype.ZjTypeDO;
import cn.iocoder.yudao.module.mes.service.zjtype.ZjTypeService;

@Tag(name = "管理后台 - 质量管理-检验类型")
@RestController
@RequestMapping("/mes/zj-type")
@Validated
public class ZjTypeController {

    @Resource
    private ZjTypeService zjTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建质量管理-检验类型")
    @PreAuthorize("@ss.hasPermission('mes:zj-type:create')")
    public CommonResult<Long> createZjType(@Valid @RequestBody ZjTypeSaveReqVO createReqVO) {
        return success(zjTypeService.createZjType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新质量管理-检验类型")
    @PreAuthorize("@ss.hasPermission('mes:zj-type:update')")
    public CommonResult<Boolean> updateZjType(@Valid @RequestBody ZjTypeSaveReqVO updateReqVO) {
        zjTypeService.updateZjType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质量管理-检验类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:zj-type:delete')")
    public CommonResult<Boolean> deleteZjType(@RequestParam("id") Long id) {
        zjTypeService.deleteZjType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质量管理-检验类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:zj-type:query')")
    public CommonResult<ZjTypeRespVO> getZjType(@RequestParam("id") Long id) {
        ZjTypeDO zjType = zjTypeService.getZjType(id);
        return success(BeanUtils.toBean(zjType, ZjTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得质量管理-检验类型分页")
    @PreAuthorize("@ss.hasPermission('mes:zj-type:query')")
    public CommonResult<PageResult<ZjTypeRespVO>> getZjTypePage(@Valid ZjTypePageReqVO pageReqVO) {
        PageResult<ZjTypeDO> pageResult = zjTypeService.getZjTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZjTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质量管理-检验类型 Excel")
    @PreAuthorize("@ss.hasPermission('mes:zj-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZjTypeExcel(@Valid ZjTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZjTypeDO> list = zjTypeService.getZjTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "质量管理-检验类型.xls", "数据", ZjTypeRespVO.class,
                        BeanUtils.toBean(list, ZjTypeRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得检验类型列表")
    public CommonResult<List<ZjTypeDO>> getDeviceModelList() {
        List<ZjTypeDO> zjTypeDOListjTypeDOList = zjTypeService.getZjTypeList();
        return success(zjTypeDOListjTypeDOList);

    }
}