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

import com.zt.plat.module.contractorder.controller.admin.entrustorder.vo.EntrustOrderDetailPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.entrustorder.vo.EntrustOrderDetailRespVO;
import com.zt.plat.module.contractorder.controller.admin.entrustorder.vo.EntrustOrderDetailSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.entrustorder.EntrustOrderDetailDO;
import com.zt.plat.module.contractorder.service.entrustorder.EntrustOrderDetailService;
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/entrust-order-detail")
@Validated
public class EntrustOrderDetailController implements BusinessControllerMarker {


    @Resource
    private EntrustOrderDetailService entrustOrderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建委托加工订单明细")
    @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:create')")
    public CommonResult<EntrustOrderDetailRespVO> createEntrustOrderDetail(@Valid @RequestBody EntrustOrderDetailSaveReqVO createReqVO) {
        return success(entrustOrderDetailService.createEntrustOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新委托加工订单明细")
    @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:update')")
    public CommonResult<Boolean> updateEntrustOrderDetail(@Valid @RequestBody EntrustOrderDetailSaveReqVO updateReqVO) {
        entrustOrderDetailService.updateEntrustOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除委托加工订单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:delete')")
    public CommonResult<Boolean> deleteEntrustOrderDetail(@RequestParam("id") Long id) {
        entrustOrderDetailService.deleteEntrustOrderDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除委托加工订单明细")
                @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:delete')")
    public CommonResult<Boolean> deleteEntrustOrderDetailList(@RequestBody BatchDeleteReqVO req) {
        entrustOrderDetailService.deleteEntrustOrderDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得委托加工订单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:query')")
    public CommonResult<EntrustOrderDetailRespVO> getEntrustOrderDetail(@RequestParam("id") Long id) {
        EntrustOrderDetailDO entrustOrderDetail = entrustOrderDetailService.getEntrustOrderDetail(id);
        return success(BeanUtils.toBean(entrustOrderDetail, EntrustOrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得委托加工订单明细分页")
    @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:query')")
    public CommonResult<PageResult<EntrustOrderDetailRespVO>> getEntrustOrderDetailPage(@Valid EntrustOrderDetailPageReqVO pageReqVO) {
        PageResult<EntrustOrderDetailDO> pageResult = entrustOrderDetailService.getEntrustOrderDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EntrustOrderDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出委托加工订单明细 Excel")
    @PreAuthorize("@ss.hasPermission('base:entrust-order-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEntrustOrderDetailExcel(@Valid EntrustOrderDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EntrustOrderDetailDO> list = entrustOrderDetailService.getEntrustOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "委托加工订单明细.xls", "数据", EntrustOrderDetailRespVO.class,
                        BeanUtils.toBean(list, EntrustOrderDetailRespVO.class));
    }

}