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

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.ticketresults.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.ticketresults.TicketResultsDO;
import cn.iocoder.yudao.module.mes.service.ticketresults.TicketResultsService;

@Tag(name = "管理后台 - 工单检验结果")
@RestController
@RequestMapping("/mes/ticket-results")
@Validated
public class TicketResultsController {

    @Resource
    private TicketResultsService ticketResultsService;

    @PostMapping("/create")
    @Operation(summary = "创建工单检验结果")
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:create')")
    public CommonResult<Long> createTicketResults(@Valid @RequestBody TicketResultsSaveReqVO createReqVO) {
        return success(ticketResultsService.createTicketResults(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单检验结果")
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:update')")
    public CommonResult<Boolean> updateTicketResults(@Valid @RequestBody TicketResultsSaveReqVO updateReqVO) {
        ticketResultsService.updateTicketResults(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单检验结果")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:delete')")
    public CommonResult<Boolean> deleteTicketResults(@RequestParam("id") Long id) {
        ticketResultsService.deleteTicketResults(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单检验结果")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:query')")
    public CommonResult<TicketResultsRespVO> getTicketResults(@RequestParam("id") Long id) {
        TicketResultsDO ticketResults = ticketResultsService.getTicketResults(id);
        return success(BeanUtils.toBean(ticketResults, TicketResultsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单检验结果分页")
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:query')")
    public CommonResult<PageResult<TicketResultsRespVO>> getTicketResultsPage(@Valid TicketResultsPageReqVO pageReqVO) {
        PageResult<TicketResultsDO> pageResult = ticketResultsService.getTicketResultsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TicketResultsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单检验结果 Excel")
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTicketResultsExcel(@Valid TicketResultsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TicketResultsDO> list = ticketResultsService.getTicketResultsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单检验结果.xls", "数据", TicketResultsRespVO.class,
                        BeanUtils.toBean(list, TicketResultsRespVO.class));
    }

    @PutMapping("/batchUpdate")
    @Operation(summary = "批量更新工单检验结果")
    @PreAuthorize("@ss.hasPermission('mes:ticket-results:update')")
    public CommonResult<Boolean> batchUpdateTicketResults(
            @Valid @RequestBody List<TicketResultsSaveReqVO> updateReqVOList) {
        ticketResultsService.batchUpdateTicketResults(updateReqVOList);
        return success(true);
    }

}