package com.cf.imes.module.executor.controller.admin.manage.pack;


import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.executor.controller.admin.manage.pack.vo.*;
import com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.OrderPackageVO;
import com.cf.imes.module.executor.controller.admin.manage.query.vo.pack.PackageDetailsRespVO;
import com.cf.imes.module.executor.service.manage.pack.PackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

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

@Tag(name = "管理后台 - 生产打包 ")
@RestController
@RequestMapping("/manage/pack")
@Validated
public class PackController {



    @Resource
    private PackService packService;

    @GetMapping("/page")
    @Operation(summary = "打包分页查询")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<PackRespVO>> getPackPlatePage(@Valid PackPageReqVO pageReqVO) {

        return CommonResult.success(packService.getPackPage(pageReqVO));

    }

    @PostMapping("/unpack/details")
    @Operation(summary = "获取订单列表下未打包明细")
    @Parameter(name = "orderIds", description = "订单ID列表", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:pack')")
    public CommonResult<OrdersUnpackDetailRespVO> getOrdersUnpackDetail(@RequestBody @NotEmpty(message = "订单ID不能为空") List<Long> orderIds) {
        return success(packService.getOrdersUnpackDetails(orderIds));
    }


    @PostMapping("/getOrderPack")
    @Operation(summary = "查看订单列表对应的包裹列表")
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:export-pack-num','manage:pack:pack')")
    public CommonResult<List<OrderPackageVO>> getOrderPack(@Valid @RequestBody PackageDetailsReqVO reqVO) {
        return success(packService.getOrderPack(reqVO));
    }

    @PostMapping("/packageDetail")
    @Operation(summary = "查询包裹编号对应的包裹中的明细")
    @Parameter(name = "packageNo", description = "包裹编号",  required = true)
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:pack-details','manage:pack:query')")
    public CommonResult<List<PackageDetailsRespVO>> queryOrdersPackageDetail(@Valid @RequestBody PackageDetailReqVO reqVO){
        return success(packService.getOrdersPackageDetails(reqVO));
    }

    @GetMapping("/packageDetails/{orderId}/{packageNo}")
    @Operation(summary = "查询订单编号下对应包裹编号的包裹中的明细")
    @Parameter(name = "orderId", description = "订单编号",  required = true)
    @Parameter(name = "packageNo", description = "包裹编号",  required = true)
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:pack-details','manage:pack:query')")
    public CommonResult<List<PackageDetailsRespVO>> queryOrderPackageDetails(@PathVariable Long orderId, @PathVariable String packageNo) {
        return success(packService.getOrderPackageDetails(orderId, packageNo));
    }


    @PostMapping("/getPageOrderPack")
    @Operation(summary = "查看生产订单对应的包裹信息-分页")
    @PreAuthorize("@ss.hasPermission('manage:pack:pack-details')")
    public CommonResult<PageResult<OrderPackageVO>> getPageOrderPack(@Valid @RequestBody PackageDetailsReqVO reqVO) {
        return success(packService.getPageOrderPack(reqVO));
    }

    @PutMapping("/updatePack")
    @Operation(summary = "封包")
    @PreAuthorize("@ss.hasPermission('manage:pack:update')")
    public CommonResult<OrderPackRespVO> updatePack(@Valid @RequestBody OrderPackReqVO packageVO) {
        return success(packService.updatePack(packageVO));
    }

    @GetMapping("/selectPack")
    @Operation(summary = "查询包裹")
    @Parameter(name = "orderId", description = "订单Id", required = true)
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<List<OrderPackRespVO>> selectPack(@Valid @RequestParam("orderId") Long orderId) {

        return success(packService.selectPack(orderId));

    }



    @DeleteMapping("/deletePack")
    @Operation(summary = "删除包裹及其关联的包裹信息(删除全部)")
    @PreAuthorize("@ss.hasPermission('manage:pack:delete')")
    public CommonResult<Boolean> deleteOrderPack (@Valid @RequestBody PackReqVO reqVO) {

        packService.deleteOrderPack(reqVO);
        return success(true);

    }


    @PutMapping("/unpacking")
    @Operation(summary = "拆包-更改包裹为未封包")
    @PreAuthorize("@ss.hasPermission('manage:pack:unpacking')")
    public CommonResult<OrderPackRespVO> unpacking(@Valid @RequestBody PackingReqVO reqVO) {

        OrderPackRespVO unpacking = packService.unpacking(reqVO.getPackageNo());

        return success(unpacking);
    }


    @PostMapping("/scan/correlation")
    @Operation(summary = "查询扫描类型相关的板件+配件id")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<List<String>> getComponentCorrelation(@Valid @RequestBody PackScanComponentCorrelationReqVO reqVO) {
        return success(packService.getComponentCorrelation(reqVO));
    }
}
