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

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageGoodsPageReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageGoodsRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageOrderPageReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageOrderRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManagePlateListReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManagePlateRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageRemoveReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerManageReselectReqVO;
import com.cf.imes.module.executor.service.veneer.VeneerManageService;
import io.swagger.v3.oas.annotations.Operation;
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.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;

/**
 * 贴皮管理接口。
 */
@Tag(name = "生产管理 - 贴皮管理")
@RestController
@RequestMapping("/executor/veneer-manage")
@Validated
public class VeneerManageController {

    @Resource
    private VeneerManageService veneerManageService;

    /**
     * 分页查询存在可维护未排单贴皮的订单。
     *
     * @param req 分页及筛选条件
     * @return 订单分页列表
     */
    @PostMapping("/order/page")
    @Operation(summary = "获取贴皮管理订单分页列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<PageResult<VeneerManageOrderRespVO>> getVeneerManageOrderPage(
            @Valid @RequestBody VeneerManageOrderPageReqVO req) {
        return success(veneerManageService.getOrderPage(req));
    }

    /**
     * 分页查询存在可维护未排单贴皮的板材。
     *
     * @param req 分页及筛选条件
     * @return 板材分页列表
     */
    @PostMapping("/goods/page")
    @Operation(summary = "获取贴皮管理板材分页列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<PageResult<VeneerManageGoodsRespVO>> getVeneerManageGoodsPage(
            @Valid @RequestBody VeneerManageGoodsPageReqVO req) {
        return success(veneerManageService.getGoodsPage(req));
    }

    /**
     * 按订单查询可维护的未排单板件贴皮列表。
     *
     * @param req 查询条件，必须包含订单ID列表，可按房间和柜体筛选
     * @return 未排单板件贴皮列表
     */
    @PostMapping("/order/plate/list")
    @Operation(summary = "按订单获取可维护的未排单板件贴皮列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<List<VeneerManagePlateRespVO>> getVeneerManageOrderPlateList(
            @Valid @RequestBody VeneerManagePlateListReqVO req) {
        return success(veneerManageService.getOrderPlateList(req));
    }

    /**
     * 按板材查询可维护的未排单板件贴皮列表。
     *
     * @param req 查询条件，必须包含板材商品编码列表
     * @return 未排单板件贴皮列表
     */
    @PostMapping("/goods/plate/list")
    @Operation(summary = "按板材获取可维护的未排单板件贴皮列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<List<VeneerManagePlateRespVO>> getVeneerManageGoodsPlateList(
            @Valid @RequestBody VeneerManagePlateListReqVO req) {
        return success(veneerManageService.getGoodsPlateList(req));
    }

    /**
     * 批量为未排单板件贴皮重新选择贴皮库数据。
     *
     * @param req 板件贴皮记录ID及新贴皮库ID
     * @return 是否成功
     */
    @PostMapping("/plate/reselect")
    @Operation(summary = "批量重选贴皮")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<Boolean> reselectVeneerManageSource(
            @Valid @RequestBody VeneerManageReselectReqVO req) {
        veneerManageService.reselectVeneer(req);
        return success(true);
    }

    /**
     * 批量移除待排单板件贴皮。
     *
     * @param req 待移除的板件贴皮记录ID列表
     * @return 是否成功
     */
    @PostMapping("/plate/remove")
    @Operation(summary = "批量移除待排单贴皮")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<Boolean> removeVeneerManageRecord(
            @Valid @RequestBody VeneerManageRemoveReqVO req) {
        veneerManageService.removeVeneer(req);
        return success(true);
    }
}
