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

import com.zt.plat.module.contractorder.controller.admin.entrustorder.vo.EntrustOrderOrderPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.entrustorder.vo.EntrustOrderOrderRespVO;
import com.zt.plat.module.contractorder.controller.admin.entrustorder.vo.EntrustOrderOrderSaveReqVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.DownOrUpOrderReqVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.DownOrUpOrderRespVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.LinkOrderReqVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PurchaseOrderRespVO;
import com.zt.plat.module.contractorder.dal.dataobject.entrustorder.EntrustOrderOrderDO;
import com.zt.plat.module.contractorder.service.entrustorder.EntrustOrderOrderService;
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.*;
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")
@Validated
public class EntrustOrderOrderController implements BusinessControllerMarker {


    @Resource
    private EntrustOrderOrderService entrustOrderOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建委托订单")//process:order:list:OrderList:add
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:create','process:order:list:OrderList:add')")
    public CommonResult<EntrustOrderOrderRespVO> createEntrustOrderOrder(@Valid @RequestBody EntrustOrderOrderSaveReqVO createReqVO) {
        return success(entrustOrderOrderService.createEntrustOrderOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新委托订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:update','process:order:list:OrderList:edit')")
    public CommonResult<Boolean> updateEntrustOrderOrder(@Valid @RequestBody EntrustOrderOrderSaveReqVO updateReqVO) {
        entrustOrderOrderService.updateEntrustOrderOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除委托订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:delete','process:order:list:OrderList:delete')")
    public CommonResult<Boolean> deleteEntrustOrderOrder(@RequestParam("id") Long id) {
        entrustOrderOrderService.deleteEntrustOrderOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除委托订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:delete','process:order:list:OrderList:delete')")
    public CommonResult<Boolean> deleteEntrustOrderOrderList(@RequestBody BatchDeleteReqVO req) {
        entrustOrderOrderService.deleteEntrustOrderOrderListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得委托订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:query','process:order:list:OrderList:query','process:order:list:OrderList:detail')")
    public CommonResult<EntrustOrderOrderRespVO> getEntrustOrderOrder(@RequestParam("id") Long id) {
        EntrustOrderOrderDO EntrustOrderOrder = entrustOrderOrderService.getEntrustOrderOrder(id);
        EntrustOrderOrderRespVO bean = BeanUtils.toBean(EntrustOrderOrder, EntrustOrderOrderRespVO.class);
        if (bean != null) {
            bean.setDetails(entrustOrderOrderService.getEntrustOrderDetailListByOrderId(bean.getId()));
        }
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得委托订单分页")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:query','process:order:list:OrderList:query','process:order:list:OrderList:detail')")
    public CommonResult<PageResult<EntrustOrderOrderRespVO>> getEntrustOrderOrderPage(@Valid EntrustOrderOrderPageReqVO pageReqVO) {
        PageResult<EntrustOrderOrderDO> pageResult = entrustOrderOrderService.getEntrustOrderOrderPage(pageReqVO);
        PageResult<EntrustOrderOrderRespVO> bean = BeanUtils.toBean(pageResult, EntrustOrderOrderRespVO.class);
        if (bean != null && !bean.getList().isEmpty()) {
            bean.getList().forEach(item -> {
                item.setDetails(entrustOrderOrderService.getEntrustOrderDetailListByOrderId(item.getId()));
            });
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出委托订单 Excel")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:export','process:order:list:OrderList:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEntrustOrderOrderExcel(@Valid EntrustOrderOrderPageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EntrustOrderOrderDO> list = entrustOrderOrderService.getEntrustOrderOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "委托订单.xls", "数据", EntrustOrderOrderRespVO.class,
                BeanUtils.toBean(list, EntrustOrderOrderRespVO.class));
    }

    //关联订单
    //关联订单
    @PostMapping("/link-order")
    @Operation(summary = "关联订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:update','process:order:list:OrderList:edit')")
    public CommonResult<Boolean> linkOrder(@RequestBody @Validated LinkOrderReqVO req) {
        return success(entrustOrderOrderService.linkOrder(req));
    }

    @PostMapping("/order-by-order-id-and-type")
    @Operation(summary = "根据订单id和方式获取上或下游订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:query','process:order:list:OrderList:query')")
    public CommonResult<List<DownOrUpOrderRespVO>> getOrderByOrderIdAndType(@RequestBody DownOrUpOrderReqVO reqVO) {
        return success(entrustOrderOrderService.getOrderByOrderIdAndType(reqVO));
    }


    @PostMapping("/bound-order")
    @Operation(summary = "获取已绑定的订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:query','process:order:list:OrderList:query')")
    public CommonResult<List<EntrustOrderOrderRespVO>> boundOrder(@RequestBody DownOrUpOrderReqVO reqVO) {
        return success(entrustOrderOrderService.getBindOrderByOrder(reqVO));
    }

    //关闭订单
    @PostMapping("/close-order")
    @Operation(summary = "关闭订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:update','process:order:list:OrderList:close')")
    public CommonResult<Boolean> closeOrder(@RequestBody List<Long> ids) {
        entrustOrderOrderService.closeOrder(ids);
        return success(true);
    }
    //委托加工订单提交erp062
    @PostMapping("/submit-erp061")
    @Operation(summary = "委托加工订单提交erp061")
    @PreAuthorize("@ss.hasAnyPermissions('base:purchase-order:update','process:order:list:OrderList:edit')")
    public CommonResult<Boolean> submitErp(@RequestParam("id") Long id) {
        entrustOrderOrderService.submitErp061(id);
        return success(true);
    }


}