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.VeneerPlanAddPlateReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanCreateReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanPageReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanPageRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanPlateRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanProduceReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanRemovePlateReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanSortReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanSortRespVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanUpdateReqVO;
import com.cf.imes.module.executor.controller.admin.veneer.vo.VeneerPlanWithdrawProduceReqVO;
import com.cf.imes.module.executor.service.veneer.VeneerPlanService;
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 jakarta.validation.constraints.Size;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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-plan")
@Validated
public class VeneerPlanController {

    @Resource
    private VeneerPlanService veneerPlanService;

    /**
     * 按前端提交的贴皮商品编号分组批量创建贴皮排单，每个分组生成一个排单。
     *
     * @param req 前端整理的贴皮商品编号分组
     * @return 是否创建成功
     */
    @PostMapping("/create")
    @Operation(summary = "批量创建贴皮排单")
    @PreAuthorize("@ss.hasPermission('unplannedOrders:query')")
    public CommonResult<Boolean> createVeneerPlan(
            @Valid @RequestBody VeneerPlanCreateReqVO req) {
        veneerPlanService.createPlan(req);
        return success(true);
    }

    /**
     * 更新未进入开料阶段的贴皮排单加工方案。
     *
     * @param req 排单ID及加工方案
     * @return 是否更新成功
     */
    @PostMapping("/process-scheme/update")
    @Operation(summary = "更新贴皮排单加工方案")
    @PreAuthorize("@ss.hasPermission('placeorder:update')")
    public CommonResult<Boolean> updateVeneerPlanProcessScheme(
            @Valid @RequestBody VeneerPlanUpdateReqVO req) {
        veneerPlanService.updatePlan(req);
        return success(true);
    }

    /**
     * 将贴皮排单投入生产。
     *
     * @param req 待投入生产的排单ID
     * @return 是否操作成功
     */
    @PostMapping("/produce")
    @Operation(summary = "贴皮排单投入生产")
    @PreAuthorize("@ss.hasPermission('placeorder:update')")
    public CommonResult<Boolean> produceVeneerPlan(
            @Valid @RequestBody VeneerPlanProduceReqVO req) {
        veneerPlanService.producePlan(req);
        return success(true);
    }

    /**
     * 撤回贴皮排单的投入生产状态。
     *
     * @param req 待撤回生产的排单ID
     * @return 是否操作成功
     */
    @PostMapping("/withdraw-produce")
    @Operation(summary = "撤回贴皮排单生产")
    @PreAuthorize("@ss.hasPermission('placeorder:update')")
    public CommonResult<Boolean> withdrawVeneerPlanProduction(
            @Valid @RequestBody VeneerPlanWithdrawProduceReqVO req) {
        veneerPlanService.withdrawProduce(req);
        return success(true);
    }

    /**
     * 向未进入开料阶段的贴皮排单新增贴皮记录。
     *
     * @param req 目标排单及待加入贴皮记录
     * @return 是否操作成功
     */
    @PostMapping("/plate/add")
    @Operation(summary = "向贴皮排单新增贴皮")
    @PreAuthorize("@ss.hasPermission('manage:order-plan:insert-plate')")
    public CommonResult<Boolean> addVeneerPlanPlate(
            @Valid @RequestBody VeneerPlanAddPlateReqVO req) {
        veneerPlanService.addPlanPlate(req);
        return success(true);
    }

    /**
     * 从未进入开料阶段的贴皮排单移除贴皮记录。
     *
     * @param req 目标排单及待移除贴皮记录
     * @return 是否操作成功
     */
    @PostMapping("/plate/remove")
    @Operation(summary = "从贴皮排单移除贴皮")
    @PreAuthorize("@ss.hasPermission('manage:order-plan:remove-plate')")
    public CommonResult<Boolean> removeVeneerPlanPlate(
            @Valid @RequestBody VeneerPlanRemovePlateReqVO req) {
        veneerPlanService.removePlanPlate(req);
        return success(true);
    }

    /**
     * 批量删除未进入开料阶段的贴皮排单，并将排单内贴皮恢复为待排单状态。
     *
     * @param ids 贴皮排单ID列表
     * @return 是否删除成功
     */
    @DeleteMapping("/delete")
    @Operation(summary = "批量删除贴皮排单")
    @Parameter(name = "ids", description = "贴皮排单ID列表", required = true)
    @PreAuthorize("@ss.hasPermission('placeorder:delete')")
    public CommonResult<Boolean> deleteVeneerPlan(
            @RequestParam("ids") @Size(max = 20, message = "最多可同时删除20个贴皮排单")
            List<Long> ids) {
        veneerPlanService.deletePlan(ids);
        return success(true);
    }

    /**
     * 分页查询已创建的贴皮排单。
     *
     * @param req 分页及筛选条件
     * @return 贴皮排单分页列表
     */
    @PostMapping("/page")
    @Operation(summary = "获取已排贴皮排单分页列表")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','placeorder:priority','producePlan:query')")
    public CommonResult<PageResult<VeneerPlanPageRespVO>> getVeneerPlanPage(
            @Valid @RequestBody VeneerPlanPageReqVO req) {
        return success(veneerPlanService.getPlanPage(req));
    }

    /**
     * 查询指定贴皮排单下的全部贴皮明细。
     *
     * @param planId 贴皮排单ID
     * @return 排单贴皮明细列表
     */
    @GetMapping("/plate/list")
    @Operation(summary = "获取贴皮排单明细列表")
    @Parameter(name = "planId", description = "贴皮排单ID", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','producePlan:query')")
    public CommonResult<List<VeneerPlanPlateRespVO>> getVeneerPlanPlateList(
            @RequestParam("planId") Long planId) {
        return success(veneerPlanService.getPlanPlateList(planId));
    }

    /**
     * 查询未完成开料的贴皮排单优先级列表。
     *
     * @return 贴皮排单优先级列表
     */
    @GetMapping("/sort/page")
    @Operation(summary = "获取贴皮排单优先级列表")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:query','placeorder:priority','producePlan:query')")
    public CommonResult<List<VeneerPlanSortRespVO>> getVeneerPlanSortPage() {
        return success(veneerPlanService.getPlanSortPage());
    }

    /**
     * 批量设置未完成开料贴皮排单的优先级。
     *
     * @param req 排单ID及排序值列表
     * @return 是否设置成功
     */
    @PutMapping("/setSort")
    @Operation(summary = "设置贴皮排单优先级")
    @PreAuthorize("@ss.hasPermission('placeorder:priority')")
    public CommonResult<Boolean> setVeneerPlanSort(
            @Valid @RequestBody List<VeneerPlanSortReqVO> req) {
        veneerPlanService.setPlanSort(req);
        return success(true);
    }
}
