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

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.Valid;
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 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.processinginfomationoperation.vo.*;
import com.zt.plat.module.base.service.processinginfomationoperation.ProcessingInfomationOperationService;

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


    @Resource
    private ProcessingInfomationOperationService processingInfomationOperationService;

    @PostMapping("/create")
    @Operation(summary = "创建工艺工序")
    @PreAuthorize("@ss.hasPermission('base:processing-infomation-operation:create')")
    public CommonResult<ProcessingInfomationOperationDetailRespVO> createProcessingInfomationOperation(@Valid @RequestBody ProcessingInfomationOperationSaveReqVO createReqVO) {
        return success(processingInfomationOperationService.createProcessingInfomationOperation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工艺工序")
    @PreAuthorize("@ss.hasPermission('base:processing-infomation-operation:update')")
    public CommonResult<Boolean> updateProcessingInfomationOperation(@Valid @RequestBody ProcessingInfomationOperationSaveReqVO updateReqVO) {
        processingInfomationOperationService.updateProcessingInfomationOperation(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得工艺工序")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:processing-infomation-operation:query')")
    public CommonResult<ProcessingInfomationOperationDetailRespVO> getProcessingInfomationOperation(@RequestParam("id") Long id) {
        return success(processingInfomationOperationService.getProcessingInfomationOperation(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工艺工序分页")
    @PreAuthorize("@ss.hasPermission('base:processing-infomation-operation:query')")
    public CommonResult<PageResult<ProcessingInfomationOperationRespVO>> getProcessingInfomationOperationPage(@Valid ProcessingInfomationOperationPageReqVO pageReqVO) {
        PageResult<ProcessingInfomationOperationRespVO> pageResult = processingInfomationOperationService.getProcessingInfomationOperationDetailPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/organization/page")
    @Operation(summary = "获得组织工艺工序分页")
    @PreAuthorize("@ss.hasPermission('base:processing-infomation-operation:query')")
    public CommonResult<PageResult<ProcessingInfomationOperationOrganizationRespVO>> getOrganizationOperationPage(@Valid ProcessingInfomationOperationOrganizationPageReqVO pageReqVO) {
        return success(processingInfomationOperationService.getOrganizationOperationPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工艺工序 Excel")
    @PreAuthorize("@ss.hasPermission('base:processing-infomation-operation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProcessingInfomationOperationExcel(@Valid ProcessingInfomationOperationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProcessingInfomationOperationRespVO> list = processingInfomationOperationService.getProcessingInfomationOperationDetailList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "工艺工序.xls", "数据", ProcessingInfomationOperationRespVO.class,
                        list);
    }

}