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

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.processingoperation.vo.*;
import com.zt.plat.module.base.dal.dataobject.processingoperation.ProcessingOperationDO;
import com.zt.plat.module.base.service.processingoperation.ProcessingOperationService;

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


    @Resource
    private ProcessingOperationService processingOperationService;

    @PostMapping("/create")
    @Operation(summary = "创建工序")
    @PreAuthorize("@ss.hasPermission('base:processing-operation:create')")
    public CommonResult<ProcessingOperationRespVO> createProcessingOperation(@Valid @RequestBody ProcessingOperationSaveReqVO createReqVO) {
        return success(processingOperationService.createProcessingOperation(createReqVO));
    }

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得工序")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:processing-operation:query')")
    public CommonResult<ProcessingOperationRespVO> getProcessingOperation(@RequestParam("id") Long id) {
        ProcessingOperationDO processingOperation = processingOperationService.getProcessingOperation(id);
        return success(BeanUtils.toBean(processingOperation, ProcessingOperationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工序分页")
    @PreAuthorize("@ss.hasPermission('base:processing-operation:query')")
    public CommonResult<PageResult<ProcessingOperationRespVO>> getProcessingOperationPage(@Valid ProcessingOperationPageReqVO pageReqVO) {
        PageResult<ProcessingOperationDO> pageResult = processingOperationService.getProcessingOperationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProcessingOperationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工序 Excel")
    @PreAuthorize("@ss.hasPermission('base:processing-operation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProcessingOperationExcel(@Valid ProcessingOperationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProcessingOperationDO> list = processingOperationService.getProcessingOperationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工序.xls", "数据", ProcessingOperationRespVO.class,
                        BeanUtils.toBean(list, ProcessingOperationRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得工序简单列表")
    @PreAuthorize("@ss.hasPermission('base:processing-operation:query')")
    public CommonResult<List<ProcessingOperationSimpleRespVO>> getProcessingOperationSimpleList(
            @RequestParam(value = "typeValue", required = false) String typeValue) {
        List<ProcessingOperationDO> list = processingOperationService.getProcessingOperationListByType(typeValue);
        return success(BeanUtils.toBean(list, ProcessingOperationSimpleRespVO.class));
    }

}