package com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing;

import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingDetailPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingDetailRespVO;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingDetailSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.originalmaterialprocessing.OriginalMaterialProcessingDetailDO;
import com.zt.plat.module.contractorder.service.originalmaterialprocessing.OriginalMaterialProcessingDetailService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

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.*;


@Tag(name = "管理后台 - 来料加工订单明细")
@RestController
@RequestMapping("/base/original-material-processing-detail")
@Validated
public class OriginalMaterialProcessingDetailController implements BusinessControllerMarker {


    @Resource
    private OriginalMaterialProcessingDetailService originalMaterialProcessingDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建来料加工订单明细")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:create')")
    public CommonResult<OriginalMaterialProcessingDetailRespVO> createOriginalMaterialProcessingDetail(@Valid @RequestBody OriginalMaterialProcessingDetailSaveReqVO createReqVO) {
        return success(originalMaterialProcessingDetailService.createOriginalMaterialProcessingDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新来料加工订单明细")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:update')")
    public CommonResult<Boolean> updateOriginalMaterialProcessingDetail(@Valid @RequestBody OriginalMaterialProcessingDetailSaveReqVO updateReqVO) {
        originalMaterialProcessingDetailService.updateOriginalMaterialProcessingDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除来料加工订单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:delete')")
    public CommonResult<Boolean> deleteOriginalMaterialProcessingDetail(@RequestParam("id") Long id) {
        originalMaterialProcessingDetailService.deleteOriginalMaterialProcessingDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除来料加工订单明细")
                @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:delete')")
    public CommonResult<Boolean> deleteOriginalMaterialProcessingDetailList(@RequestBody BatchDeleteReqVO req) {
        originalMaterialProcessingDetailService.deleteOriginalMaterialProcessingDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得来料加工订单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:query')")
    public CommonResult<OriginalMaterialProcessingDetailRespVO> getOriginalMaterialProcessingDetail(@RequestParam("id") Long id) {
        OriginalMaterialProcessingDetailDO originalMaterialProcessingDetail = originalMaterialProcessingDetailService.getOriginalMaterialProcessingDetail(id);
        return success(BeanUtils.toBean(originalMaterialProcessingDetail, OriginalMaterialProcessingDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得来料加工订单明细分页")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:query')")
    public CommonResult<PageResult<OriginalMaterialProcessingDetailRespVO>> getOriginalMaterialProcessingDetailPage(@Valid OriginalMaterialProcessingDetailPageReqVO pageReqVO) {
        PageResult<OriginalMaterialProcessingDetailDO> pageResult = originalMaterialProcessingDetailService.getOriginalMaterialProcessingDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OriginalMaterialProcessingDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出来料加工订单明细 Excel")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOriginalMaterialProcessingDetailExcel(@Valid OriginalMaterialProcessingDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OriginalMaterialProcessingDetailDO> list = originalMaterialProcessingDetailService.getOriginalMaterialProcessingDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "来料加工订单明细.xls", "数据", OriginalMaterialProcessingDetailRespVO.class,
                        BeanUtils.toBean(list, OriginalMaterialProcessingDetailRespVO.class));
    }

}