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.VeneerDecomposeCreateReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDecomposeGoodsPageReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDecomposeGoodsRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDecomposeOrderPageReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDecomposeOrderRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDecomposePlateDataRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerDecomposePlatePageReqVO;
import com.cf.imes.module.executor.service.veneer.VeneerDecomposeService;
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 static com.cf.imes.framework.common.pojo.CommonResult.success;

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

    @Resource
    private VeneerDecomposeService veneerDecomposeService;

    /**
     * 分页查询存在未贴皮分解板件的订单。
     *
     * @param req 分页及筛选条件
     * @return 订单分页列表
     */
    @PostMapping("/order/page")
    @Operation(summary = "获取贴皮分解订单分页列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<PageResult<VeneerDecomposeOrderRespVO>> getVeneerDecomposeOrderPage(
            @Valid @RequestBody VeneerDecomposeOrderPageReqVO req) {
        return success(veneerDecomposeService.getOrderPage(req));
    }

    /**
     * 分页查询存在未贴皮分解板件的板材。
     *
     * @param req 分页及筛选条件
     * @return 板材分页列表
     */
    @PostMapping("/goods/page")
    @Operation(summary = "获取贴皮分解板材分页列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<PageResult<VeneerDecomposeGoodsRespVO>> getVeneerDecomposeGoodsPage(
            @Valid @RequestBody VeneerDecomposeGoodsPageReqVO req) {
        return success(veneerDecomposeService.getGoodsPage(req));
    }

    /**
     * 按订单查询未贴皮分解板件列表。
     *
     * @param req 查询条件，必须包含订单ID列表
     * @return 板件及板件造型数据
     */
    @PostMapping("/order/plate/page")
    @Operation(summary = "获取订单下待贴皮分解板件列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<VeneerDecomposePlateDataRespVO> getVeneerDecomposeOrderPlateData(
            @Valid @RequestBody VeneerDecomposePlatePageReqVO req) {
        return success(veneerDecomposeService.getOrderPlateList(req));
    }

    /**
     * 按板材查询未贴皮分解板件列表。
     *
     * @param req 查询条件，必须包含商品编码列表
     * @return 板件及板件造型数据
     */
    @PostMapping("/goods/plate/page")
    @Operation(summary = "获取材质下待贴皮分解板件列表")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<VeneerDecomposePlateDataRespVO> getVeneerDecomposeGoodsPlateData(
            @Valid @RequestBody VeneerDecomposePlatePageReqVO req) {
        return success(veneerDecomposeService.getGoodsPlateList(req));
    }

    /**
     * 将选中的未分解原板件分解为 A 面和/或 B 面贴皮记录。
     *
     * @param req 分解参数
     * @return 是否成功
     */
    @PostMapping("/decompose")
    @Operation(summary = "执行贴皮分解")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<Boolean> createVeneerDecompose(
            @Valid @RequestBody VeneerDecomposeCreateReqVO req) {
        veneerDecomposeService.decompose(req);
        return success(true);
    }
}
