package com.zt.plat.module.base.controller.admin.processingoperationmaterial;

import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.base.controller.admin.processingoperationmaterial.vo.*;
import com.zt.plat.module.base.dal.dataobject.processingoperationmaterial.ProcessingOperationMaterialDO;
import com.zt.plat.module.base.service.processingoperationmaterial.ProcessingOperationMaterialService;

@Tag(name = "管理后台 - 工艺工序物料")
@RestController
@RequestMapping("/base/processing-operation-material")
@Validated
public class ProcessingOperationMaterialController {


    @Resource
    private ProcessingOperationMaterialService processingOperationMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建工艺工序物料")
    @PreAuthorize("@ss.hasPermission('base:processing-operation-material:create')")
    public CommonResult<ProcessingOperationMaterialRespVO> createProcessingOperationMaterial(@Valid @RequestBody ProcessingOperationMaterialSaveReqVO createReqVO) {
        return success(processingOperationMaterialService.createProcessingOperationMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工艺工序物料")
    @PreAuthorize("@ss.hasPermission('base:processing-operation-material:update')")
    public CommonResult<Boolean> updateProcessingOperationMaterial(@Valid @RequestBody ProcessingOperationMaterialSaveReqVO updateReqVO) {
        processingOperationMaterialService.updateProcessingOperationMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工艺工序物料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:processing-operation-material:delete')")
    public CommonResult<Boolean> deleteProcessingOperationMaterial(@RequestParam("id") Long id) {
        processingOperationMaterialService.deleteProcessingOperationMaterial(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除工艺工序物料")
                @PreAuthorize("@ss.hasPermission('base:processing-operation-material:delete')")
    public CommonResult<Boolean> deleteProcessingOperationMaterialList(@RequestBody BatchDeleteReqVO req) {
        processingOperationMaterialService.deleteProcessingOperationMaterialListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工艺工序物料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:processing-operation-material:query')")
    public CommonResult<ProcessingOperationMaterialRespVO> getProcessingOperationMaterial(@RequestParam("id") Long id) {
        ProcessingOperationMaterialDO processingOperationMaterial = processingOperationMaterialService.getProcessingOperationMaterial(id);
        return success(BeanUtils.toBean(processingOperationMaterial, ProcessingOperationMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工艺工序物料分页")
    @PreAuthorize("@ss.hasPermission('base:processing-operation-material:query')")
    public CommonResult<PageResult<ProcessingOperationMaterialRespVO>> getProcessingOperationMaterialPage(@Valid ProcessingOperationMaterialPageReqVO pageReqVO) {
        PageResult<ProcessingOperationMaterialDO> pageResult = processingOperationMaterialService.getProcessingOperationMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProcessingOperationMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工艺工序物料 Excel")
    @PreAuthorize("@ss.hasPermission('base:processing-operation-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProcessingOperationMaterialExcel(@Valid ProcessingOperationMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProcessingOperationMaterialDO> list = processingOperationMaterialService.getProcessingOperationMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工艺工序物料.xls", "数据", ProcessingOperationMaterialRespVO.class,
                        BeanUtils.toBean(list, ProcessingOperationMaterialRespVO.class));
    }

}