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

import com.zt.plat.module.contractorder.controller.admin.salesorder.vo.SalesOrderDetailPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.salesorder.vo.SalesOrderDetailRespVO;
import com.zt.plat.module.contractorder.controller.admin.salesorder.vo.SalesOrderDetailSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.salesorder.SalesOrderDetailDO;
import com.zt.plat.module.contractorder.service.salesorder.SalesOrderDetailService;
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/sales-order-detail")
@Validated
public class SalesOrderDetailController implements BusinessControllerMarker {


    @Resource
    private SalesOrderDetailService salesOrderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建销售订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:create','sale:order:list:OrderList:add')")
    public CommonResult<SalesOrderDetailRespVO> createSalesOrderDetail(@Valid @RequestBody SalesOrderDetailSaveReqVO createReqVO) {
        return success(salesOrderDetailService.createSalesOrderDetail(createReqVO));
    }

    @PostMapping("/create-batch")
    @Operation(summary = "批量创建销售订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:create','sale:order:list:OrderList:add')")
    public CommonResult<List<SalesOrderDetailRespVO>> createSalesOrderDetail(@Valid @NotEmpty(message = "需要保存的数据为空") @RequestBody List<SalesOrderDetailSaveReqVO> createReqVOS) {
        return success(salesOrderDetailService.createSalesOrderDetail(createReqVOS));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:update','sale:order:list:OrderList:edit')")
    public CommonResult<Boolean> updateSalesOrderDetail(@Valid @RequestBody SalesOrderDetailSaveReqVO updateReqVO) {
        salesOrderDetailService.updateSalesOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售订单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:delete','sale:order:list:OrderList:delete')")
    public CommonResult<Boolean> deleteSalesOrderDetail(@RequestParam("id") Long id) {
        salesOrderDetailService.deleteSalesOrderDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除销售订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:delete','sale:order:list:OrderList:delete')")
    public CommonResult<Boolean> deleteSalesOrderDetailList(@RequestBody BatchDeleteReqVO req) {
        salesOrderDetailService.deleteSalesOrderDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售订单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:query','sale:order:list:OrderList:query')")
    public CommonResult<SalesOrderDetailRespVO> getSalesOrderDetail(@RequestParam("id") Long id) {
        SalesOrderDetailDO salesOrderDetail = salesOrderDetailService.getSalesOrderDetail(id);
        return success(BeanUtils.toBean(salesOrderDetail, SalesOrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售订单明细分页")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:query','sale:order:list:OrderList:query')")
    public CommonResult<PageResult<SalesOrderDetailRespVO>> getSalesOrderDetailPage(@Valid SalesOrderDetailPageReqVO pageReqVO) {
        PageResult<SalesOrderDetailDO> pageResult = salesOrderDetailService.getSalesOrderDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SalesOrderDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售订单明细 Excel")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:export','sale:order:list:OrderList:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSalesOrderDetailExcel(@Valid SalesOrderDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SalesOrderDetailDO> list = salesOrderDetailService.getSalesOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售订单明细.xls", "数据", SalesOrderDetailRespVO.class,
                        BeanUtils.toBean(list, SalesOrderDetailRespVO.class));
    }

    //通过销售订单主键获取销售订单明细
    @GetMapping("/get-by-order-id")
    @Operation(summary = "通过销售订单主键获取销售订单明细")
    @PreAuthorize("@ss.hasAnyPermissions('base:sales-order-detail:query','sale:order:list:OrderList:query')")
    public CommonResult<List<SalesOrderDetailRespVO>> getSalesOrderDetailByOrderId(@RequestParam("orderId") String orderId) {

        return success(salesOrderDetailService.getSalesOrderDetailByOrderId(Long.valueOf(orderId)));
    }

}
