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

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.errorrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.errorrecord.ErrorRecordDO;
import cn.iocoder.yudao.module.mes.service.errorrecord.ErrorRecordService;

@Tag(name = "管理后台 - 错误记录")
@RestController
@RequestMapping("/mes/error-record")
@Validated
public class ErrorRecordController {

    @Resource
    private ErrorRecordService errorRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建错误记录")
    @PreAuthorize("@ss.hasPermission('mes:error-record:create')")
    public CommonResult<Long> createErrorRecord(@Valid @RequestBody ErrorRecordSaveReqVO createReqVO) {
        return success(errorRecordService.createErrorRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新错误记录")
    @PreAuthorize("@ss.hasPermission('mes:error-record:update')")
    public CommonResult<Boolean> updateErrorRecord(@Valid @RequestBody ErrorRecordSaveReqVO updateReqVO) {
        errorRecordService.updateErrorRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除错误记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:error-record:delete')")
    public CommonResult<Boolean> deleteErrorRecord(@RequestParam("id") Long id) {
        errorRecordService.deleteErrorRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得错误记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:error-record:query')")
    public CommonResult<ErrorRecordRespVO> getErrorRecord(@RequestParam("id") Long id) {
        ErrorRecordDO errorRecord = errorRecordService.getErrorRecord(id);
        return success(BeanUtils.toBean(errorRecord, ErrorRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得错误记录分页")
    @PreAuthorize("@ss.hasPermission('mes:error-record:query')")
    public CommonResult<PageResult<ErrorRecordRespVO>> getErrorRecordPage(@Valid ErrorRecordPageReqVO pageReqVO) {
        PageResult<ErrorRecordDO> pageResult = errorRecordService.getErrorRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErrorRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出错误记录 Excel")
    @PreAuthorize("@ss.hasPermission('mes:error-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErrorRecordExcel(@Valid ErrorRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErrorRecordDO> list = errorRecordService.getErrorRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "错误记录.xls", "数据", ErrorRecordRespVO.class,
                        BeanUtils.toBean(list, ErrorRecordRespVO.class));
    }

}