package com.cf.imes.module.executor.controller.admin.ordercomponent;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPrintListReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPrintRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrdersViewCadViewInfoRespVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.OrderComponentRespVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.OrderDetailComponentListReqVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.OrderDetailComponentPageReqVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.OrdersTopComponentListReqVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.OrderViewPartPageRespVO;
import com.cf.imes.module.executor.controller.admin.plate.vo.OrderViewPlatePageRespVO;
import com.cf.imes.module.executor.service.assemblylist.AssemblyListService;
import com.cf.imes.module.executor.service.ordercomponent.OrderComponentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

/**
 *
 *
 * @author Gqr
 * @since 2026/1/28 9:27
 */
@Tag(name = "管理后台 - 订单部件管理")
@RestController
@RequestMapping("/executor/order/component")
@Validated
public class OrderComponentController {
    @Resource
    private OrderComponentService orderComponentService;

    @Resource
    private AssemblyListService assemblyListService;

    @PostMapping("/detail/list")
    @Operation(summary = "查询订单详情部件列表")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<List<OrderComponentRespVO>> getOrderComponentList(@Valid @RequestBody OrderDetailComponentListReqVO reqVO) {
        return success(orderComponentService.getOrderComponentList(reqVO));
    }

    @PostMapping("/top/list")
    @Operation(summary = "查询订单顶级部件列表")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<List<OrderComponentRespVO>> getOrderTopComponentList(@Valid @RequestBody OrdersTopComponentListReqVO reqVO) {
            return success(assemblyListService.getOrderTopComponentList(reqVO));
    }

    @PostMapping("/view/list")
    @Operation(summary = "查询订单详情cad视图部件列表")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<List<OrderComponentRespVO>> getOrderViewComponentList(@Valid @RequestBody OrderDetailComponentListReqVO reqVO) {
        return success(orderComponentService.getComponentListOnTopId(reqVO));
    }

    @GetMapping("/view/{id}/list")
    @Operation(summary = "查询指定顶级部件下部件列表")
    @Parameter(name = "id", description = "部件编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('systemconfig:assemblyconfig:query')")
    public CommonResult<List<OrderComponentRespVO>> getChildViewComponentList(@PathVariable Long id) {
        return success(orderComponentService.getChildComponentListOnTopId(id));
    }

    @PostMapping("/view/plate/page")
    @Operation(summary = "获取部件下的板件视图分页列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<PageResult<OrderViewPlatePageRespVO>> getComponentViewPlatesPage(@Valid @RequestBody OrderDetailComponentPageReqVO pageReqVO) {
        Long orderId = pageReqVO.getOrderId();
        pageReqVO.setOrderIds(List.of(orderId));
        return success(orderComponentService.getViewComponentPlatesPage(pageReqVO));
    }

    @PostMapping("/view/part/page")
    @Operation(summary = "获取部件下的配件视图分页列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<PageResult<OrderViewPartPageRespVO>> getComponentViewPartsPage(@Valid @RequestBody OrderDetailComponentPageReqVO pageReqVO) {
        Long orderId = pageReqVO.getOrderId();
        pageReqVO.setOrderIds(List.of(orderId));
        return success(orderComponentService.getViewComponentPartsPage(pageReqVO));
    }

    @PostMapping("/view/cadinfo")
    @Operation(summary = "获取部件下的所有板件+配件视图列表数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:detail')")
    public CommonResult<OrdersViewCadViewInfoRespVO> getComponentViewPlates(@Valid @RequestBody OrderDetailComponentPageReqVO pageReqVO) {
        return success(orderComponentService.getViewComponentCadInfo(pageReqVO));
    }

    @DeleteMapping("/{orderId}/{id}")
    @Operation(summary = "删除订单部件")
    @PreAuthorize("@ss.hasPermission('production:manager-list:deleteComponent')")
    public CommonResult<Boolean> deleteOrderComponent(@PathVariable("orderId") Long orderId, @PathVariable("id") Long id) {
        orderComponentService.deleteOrderComponent(orderId, id);
        return success(true);
    }

    @PutMapping("/{orderId}/{id}")
    @Operation(summary = "恢复订单部件")
    @PreAuthorize("@ss.hasPermission('production:manager-list:restoreComponent')")
    public CommonResult<Boolean> restoreOrderComponent(@PathVariable("orderId") Long orderId, @PathVariable("id") Long id) {
        orderComponentService.restoreOrderComponent(orderId, id);
        return success(true);
    }

    @PostMapping("/print")
    @Operation(summary = "获取订单部件打印列表")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:print')")
    public CommonResult<OrderPrintRespVO> getOrderComponentPrintList(@Valid @RequestBody OrderPrintListReqVO reqVO) {
        return success(orderComponentService.getOrderComponentPrintList(reqVO));
    }
}
