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

import cn.hutool.core.bean.BeanUtil;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.executor.controller.admin.assemblylist.vo.AssemblyListComponentPageReqVO;
import com.cf.imes.module.executor.controller.admin.assemblylist.vo.AssemblyListComponentFlatReqVO;
import com.cf.imes.module.executor.controller.admin.assemblylist.vo.AssemblyListComponentFlatRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderPageReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrdersViewCadViewInfoRespVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.AssemblyComponentChildPrintReqVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.AssemblyComponentChildPrintRespVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.OrderComponentRespVO;
import com.cf.imes.module.executor.controller.admin.ordercomponent.vo.OrderDetailComponentPageReqVO;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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/2/28 18:20
 */
@Tag(name = "管理后台 - 装配清单管理")
@RestController
@RequestMapping("/executor/assembly")
@Validated
public class AssemblyListController {
    @Resource
    private AssemblyListService assemblyListService;

    @Resource
    private OrderComponentService orderComponentService;

    @GetMapping("/order/page")
    @Operation(summary = "获得装配清单订单分页")
    @PreAuthorize("@ss.hasPermission('productManager:List')")
    public CommonResult<PageResult<OrderRespVO>> getMainOrderPage(@Valid OrderPageReqVO pageReqVO) {
        return success(BeanUtils.toBean(assemblyListService.getMainOrderPage(pageReqVO), OrderRespVO.class));
    }

    @PostMapping("/list")
    @Operation(summary = "装配清单-获取部件列表")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<List<OrderComponentRespVO>> getComponentList(@Valid @RequestBody AssemblyListComponentPageReqVO reqVO) {
        return CommonResult.success(assemblyListService.getComponentList(reqVO));
    }

    @PostMapping("/flat-list")
    @Operation(summary = "装配清单-获取按名称汇总的平铺部件层级列表")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<List<AssemblyListComponentFlatRespVO>> getComponentFlatList(
            @Valid @RequestBody AssemblyListComponentFlatReqVO reqVO) {
        return success(assemblyListService.getComponentFlatList(reqVO));
    }

    @PostMapping("/component/direct-children")
    @Operation(summary = "装配清单-获取部件直接下级列表")
    @Parameter(name = "componentId", description = "部件 ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<List<AssemblyComponentChildPrintRespVO>> getDirectChildComponentList(
            @Valid @RequestBody AssemblyComponentChildPrintReqVO reqVO) {
        return success(assemblyListService.getDirectChildComponentList(reqVO));
    }

    @PostMapping("/list/view/plate/page")
    @Operation(summary = "装配清单-获取订单部件下的板件视图分页列表数据")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<PageResult<OrderViewPlatePageRespVO>> getComponentViewPlatesPage(@Valid @RequestBody AssemblyListComponentPageReqVO pageReqVO) {
        OrderDetailComponentPageReqVO req = BeanUtil.copyProperties(pageReqVO, OrderDetailComponentPageReqVO.class);
        return success(orderComponentService.getViewComponentPlatesPage(req));
    }

    @PostMapping("/list/view/part/page")
    @Operation(summary = "装配清单-获取订单部件下的配件视图分页列表数据")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<PageResult<OrderViewPartPageRespVO>> getComponentViewPartsPage(@Valid @RequestBody AssemblyListComponentPageReqVO pageReqVO) {
        OrderDetailComponentPageReqVO req = BeanUtil.copyProperties(pageReqVO, OrderDetailComponentPageReqVO.class);
        return success(orderComponentService.getViewComponentPartsPage(req));
    }

    @PostMapping("/list/view/cadinfo")
    @Operation(summary = "装配清单-获取订单部件下的所有板件+配件视图列表数据")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:query')")
    public CommonResult<OrdersViewCadViewInfoRespVO> getComponentViewPlates(@Valid @RequestBody AssemblyListComponentPageReqVO pageReqVO) {
        return success(assemblyListService.getViewComponentPlatesCadInfo(pageReqVO));
    }

    @PostMapping("/process/{id}")
    @Operation(summary = "装配清单-加工部件")
    @Parameter(name = "id", description = "部件编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:process')")
    public CommonResult<Boolean> processComponent(@PathVariable Long id) {
        assemblyListService.processComponent(id);
        return CommonResult.success(true);
    }

    @PostMapping("/restore/{id}")
    @Operation(summary = "装配清单-恢复未加工")
    @Parameter(name = "id", description = "部件编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('production:assemblyList:process')")
    public CommonResult<Boolean> restoreComponent(@PathVariable Long id) {
        assemblyListService.restoreComponent(id);
        return CommonResult.success(true);
    }
}
