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

import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PrchOrdDtlPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PrchOrdDtlRespVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PrchOrdDtlSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PrchOrdDtlDO;
import com.zt.plat.module.contractorder.service.purchaseorder.PrchOrdDtlService;
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/prch-ord-dtl")
@Validated
public class PrchOrdDtlController implements BusinessControllerMarker {


    @Resource
    private PrchOrdDtlService prchOrdDtlService;

    @PostMapping("/create")
    @Operation(summary = "创建采购订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:create','purchase:order:list:OrderList:add')")
    public CommonResult<PrchOrdDtlRespVO> createPrchOrdDtl(@Valid @RequestBody PrchOrdDtlSaveReqVO createReqVO) {
        return success(prchOrdDtlService.createPrchOrdDtl(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新采购订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:update','purchase:order:list:OrderList:edit')")
    public CommonResult<Boolean> updatePrchOrdDtl(@Valid @RequestBody PrchOrdDtlSaveReqVO updateReqVO) {
        prchOrdDtlService.updatePrchOrdDtl(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除采购订单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:delete','purchase:order:list:OrderList:delete')")
    public CommonResult<Boolean> deletePrchOrdDtl(@RequestParam("id") Long id) {
        prchOrdDtlService.deletePrchOrdDtl(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除采购订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:delete','purchase:order:list:OrderList:delete','purchase:order:list:OrderList:delete')")
    public CommonResult<Boolean> deletePrchOrdDtlList(@RequestBody BatchDeleteReqVO req) {
        prchOrdDtlService.deletePrchOrdDtlListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得采购订单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:query','purchase:order:list:OrderList:query')")
    public CommonResult<PrchOrdDtlRespVO> getPrchOrdDtl(@RequestParam("id") Long id) {
        PrchOrdDtlDO prchOrdDtl = prchOrdDtlService.getPrchOrdDtl(id);
        return success(BeanUtils.toBean(prchOrdDtl, PrchOrdDtlRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得采购订单明细分页")
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:query','purchase:order:list:OrderList:query')")
    public CommonResult<PageResult<PrchOrdDtlRespVO>> getPrchOrdDtlPage(@Valid PrchOrdDtlPageReqVO pageReqVO) {
        PageResult<PrchOrdDtlDO> pageResult = prchOrdDtlService.getPrchOrdDtlPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PrchOrdDtlRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出采购订单明细 Excel")
    @PreAuthorize("@ss.hasAnyPermissions('base:prch-ord-dtl:export','purchase:order:list:OrderList:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPrchOrdDtlExcel(@Valid PrchOrdDtlPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PrchOrdDtlDO> list = prchOrdDtlService.getPrchOrdDtlPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "采购订单明细.xls", "数据", PrchOrdDtlRespVO.class,
                        BeanUtils.toBean(list, PrchOrdDtlRespVO.class));
    }

}
