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

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.changerecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.changerecord.ProduceReportChangeRecordDO;
import cn.iocoder.yudao.module.mes.service.changerecord.ProduceReportChangeRecordService;

@Tag(name = "管理后台 - 报工变更记录")
@RestController
@RequestMapping("/mes/produce-report-change-record")
@Validated
public class ProduceReportChangeRecordController {

    @Resource
    private ProduceReportChangeRecordService produceReportChangeRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建报工变更记录")
    @PreAuthorize("@ss.hasPermission('mes:produce-report-change-record:create')")
    public CommonResult<Long> createProduceReportChangeRecord(@Valid @RequestBody ProduceReportChangeRecordSaveReqVO createReqVO) {
        return success(produceReportChangeRecordService.createProduceReportChangeRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报工变更记录")
    @PreAuthorize("@ss.hasPermission('mes:produce-report-change-record:update')")
    public CommonResult<Boolean> updateProduceReportChangeRecord(@Valid @RequestBody ProduceReportChangeRecordSaveReqVO updateReqVO) {
        produceReportChangeRecordService.updateProduceReportChangeRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报工变更记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:produce-report-change-record:delete')")
    public CommonResult<Boolean> deleteProduceReportChangeRecord(@RequestParam("id") Long id) {
        produceReportChangeRecordService.deleteProduceReportChangeRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报工变更记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:produce-report-change-record:query')")
    public CommonResult<ProduceReportChangeRecordRespVO> getProduceReportChangeRecord(@RequestParam("id") Long id) {
        ProduceReportChangeRecordDO produceReportChangeRecord = produceReportChangeRecordService.getProduceReportChangeRecord(id);
        return success(BeanUtils.toBean(produceReportChangeRecord, ProduceReportChangeRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报工变更记录分页")
    @PreAuthorize("@ss.hasPermission('mes:produce-report-change-record:query')")
    public CommonResult<PageResult<ProduceReportChangeRecordRespVO>> getProduceReportChangeRecordPage(@Valid ProduceReportChangeRecordPageReqVO pageReqVO) {
        PageResult<ProduceReportChangeRecordDO> pageResult = produceReportChangeRecordService.getProduceReportChangeRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProduceReportChangeRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报工变更记录 Excel")
    @PreAuthorize("@ss.hasPermission('mes:produce-report-change-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProduceReportChangeRecordExcel(@Valid ProduceReportChangeRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProduceReportChangeRecordDO> list = produceReportChangeRecordService.getProduceReportChangeRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报工变更记录.xls", "数据", ProduceReportChangeRecordRespVO.class,
                        BeanUtils.toBean(list, ProduceReportChangeRecordRespVO.class));
    }

}