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

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.common.controller.admin.moldticketresults.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.moldticketresults.MoldTicketResultsDO;
import cn.iocoder.yudao.module.mes.service.moldticketresults.MoldTicketResultsService;

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

    @Resource
    private MoldTicketResultsService moldticketResultsService;

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

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

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

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

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

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

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

}