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.prePack.PrePackReqVO;
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 com.cf.imes.module.executor.service.manage.query.ProductionStatusService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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;

    @Resource
    private ProductionStatusService productionStatusService;

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

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

    }


    @GetMapping("/getOrderPlate")
    @Operation(summary = "查看生产订单对应的没有打包的板材信息")
    @Parameter(name = "orderId", description = "生产单Id", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:export-lost-plate','manage:pack:pack')")
    public CommonResult<List<PlateDetailsRespVO>> getOrderPlateList(@Valid @RequestParam("orderId") Long orderId) {

        return success(packService.getOrderPlateList(orderId));

    }


    @GetMapping("/getOrderPlateList")
    @Operation(summary = "查询多个生产单对应的板材信息")
    @Parameter(name = "orderIds", description = "生产单ID", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:create','manage:pack:pre-pack','manage:pack:pack')")
    public CommonResult<List<PlateDetailsRespVO>> getOrdersPlateList(@Valid @RequestParam("orderIds") List<Long> orderIds) {

        return success(packService.getOrdersPlateList(orderIds));

    }


    @GetMapping("/getOrderPack")
    @Operation(summary = "查看生产订单对应的包裹信息-未分页")
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:export-pack-num','manage:pack:pack')")
    public CommonResult<List<OrderPackageVO>> getOrderPack(@Valid PackageDetailsReqVO reqVO) {

        return success(packService.getOrderPack(reqVO));

    }

    @GetMapping("/queryPackageDetails")
    @Operation(summary = "查询包裹的详细信息")
    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:pack-details','manage:pack:query')")
    public CommonResult<PageResult<PackageDetailsRespVO>>  queryPackageDetails(@Valid PackDetailsReqVO reqVO){

        return CommonResult.success(productionStatusService.queryPackageDetails(reqVO));

    }


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

        return success(packService.getPageOrderPack(reqVO));

    }





    @GetMapping("/getParts")
    @Operation(summary = "查看配件列表")
    @PreAuthorize("@ss.hasPermission('manage:pack-parts:query')")
    public CommonResult<List<OrderPartsRespVO>> getParts(@Valid OrderPartsReqVO reqVO) {

        return success(packService.getPartsList(reqVO));


    }






    @PutMapping("/updatePack")
    @Operation(summary = "板材包裹更新-即封包")
    @PreAuthorize("@ss.hasPermission('manage:pack:update')")
    public CommonResult<OrderPackRespVO> updatePack(@Valid @RequestBody OrderPackReqVO packageVO) {

        return success(packService.updatePack(packageVO));

    }




    @PutMapping("/updateParts")
    @Operation(summary = "配件包裹更新-即封包")
    @PreAuthorize("@ss.hasPermission('manage:pack:update')")
    public CommonResult<OrderPackRespVO> updateParts(@Valid @RequestBody OrderPartsReqVO reqVO) {

        return success(packService.updateParts(reqVO));


    }




    @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.getPackageId());

        return success(unpacking);
    }




    @PostMapping("/insertOtherPack")
    @Operation(summary = "新增外部配件-即新增一个包裹")
    @PreAuthorize("@ss.hasPermission('manage:pack:external-parts')")
    public CommonResult<OrderPackRespVO> addNewOtherPack(@Valid @RequestBody OtherPackReqVO reqVO) {

        return success(packService.addNewOtherPack(reqVO));

    }


    @PutMapping("/insertOther")
    @Operation(summary = "新增外部配件")
    @PreAuthorize("@ss.hasPermission('manage:pack:external-parts')")
    public CommonResult<OrderPartsRespVO> addNewOtherParts (@Valid @RequestBody OtherPackReqVO reqVO) {

        return success(packService.addNewOtherParts(reqVO));

    }



    @DeleteMapping("/deleteOtherParts")
    @Operation(summary = "删除包裹中的外部配件")
    @PreAuthorize("@ss.hasPermission('manage:pack:delete')")
    public CommonResult<Boolean> deleteOtherParts (@Valid @RequestBody DeleteOtherPartsReqVO reqVO) {

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


    }




    @GetMapping("/getPrePackInfo")
    @Operation(summary = "查询预打包优化信息")
    @PreAuthorize("@ss.hasPermission('manage:pack:pre-pack')")
    public CommonResult<String> getPrePackInfo ( @RequestParam("orderId") Long orderId) {

        return success(packService.getPrePackInfo(orderId));

    }




    @PostMapping("/saveOptimizeInfo")
    @Operation(summary = "预打包保存优化信息")
    @PreAuthorize("@ss.hasPermission('manage:pack:pre-pack')")
    public CommonResult<Boolean> saveOptimizeInfo(@Valid @RequestBody PrePackReqVO reqVO) {

        packService.saveOptimizeInfo(reqVO);

        return success(true);


    }


}
