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

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.executor.controller.admin.plan.bo.OrderSource;
import com.cf.imes.module.executor.controller.admin.plan.bo.PlanProcessSchemeList;
import com.cf.imes.module.executor.controller.admin.plan.saveOptimize.*;
import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.dal.dataobject.ordermodel.OrderModelDO;
import com.cf.imes.module.executor.service.optimizeplan.OptimizePlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
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 jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

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

/**
 * @author there
 */
@RestController
@RequestMapping("/executor/optimize-plan")
@Tag(name= "优化排单")
@Validated
public class OptimizePlanController {

    @Resource
    private OptimizePlanService optimizePlanService;

    @GetMapping("/getPlateListByPlanId")
    @Operation(summary = "根据排单id获取板材列表")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','producePlan:optimize')")
    public CommonResult<List<PlateOptimize>> getPlateListByPlanId(@Schema(description = "排单id") @RequestParam("planId") Long planId) {
        return CommonResult.success(optimizePlanService.getPlateListByPlanId(planId));
    }


    @PostMapping("/addRemain")
    @Operation(summary = "添加余料板")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','producePlan:optimize')")
    public CommonResult<Boolean> addRemain(@RequestBody @Valid AddRemainReqVO vo){
        return CommonResult.success(optimizePlanService.addRemain(vo));
    }




    @PutMapping("/updateCuttingStatus")
    @Operation(summary = "修改开料状态")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize:updateCuttingStatus','placeorder:optimize:cutting-materials','producePlan:optimize:cutting-materials','producePlan:optimize:updateCuttingStatus')")
    public CommonResult<Boolean> updateCuttingStatus(@RequestBody CuttingStatusRespVO respVO) {
        optimizePlanService.updateCuttingStatus(respVO);
        return success(Boolean.TRUE);
    }



    @GetMapping("/getOrderSource")
    @Operation(summary = "获取生产单源数据")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','production:manager-list:calculate','manage:order-plan:process-scheme','producePlan:order-plan:process-scheme','producePlan:optimize')")
    public CommonResult<OrderSource> getOrderSource(@Schema(description = "生产单id") @RequestParam(required = false) Long orderId,
                                                    @Schema(description = "排单id") @RequestParam(required = false) Long planId,
                                                    @Schema(description = "机台Id")  @RequestParam(required = false) Long machineId
    ) {
        return CommonResult.success(optimizePlanService.getOrderSource(orderId, planId, machineId));

    }


    @GetMapping("/getLabelDataSourceValue")
    @Operation(summary = "获取标签数据源value")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','producePlan:optimize')")
    public CommonResult<Map<String,Object>> getLabelDataSourceValue(@Valid GetSourceDataReq req ) {
        return CommonResult.success( optimizePlanService.getLabelDataSourceValue(req));

    }


    @PostMapping("/saveOptimizationResults")
    @Operation(summary = "保存优化生产后的数据")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize:save-optimize','placeorder:optimize:re-optimize','producePlan:optimize:re-optimize','producePlan:optimize:save-optimize')")
    public CommonResult<Boolean> saveOptimizationResults(@Valid @RequestBody SavePlanPlateResultReqVO req ) {

        optimizePlanService.saveOptimizationResults(req);

        return CommonResult.success(true);

    }


    @PostMapping("/saveProcessSchemeResults")
    @Operation(summary = "保存方案组的优化信息")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','manage:order-plan:process-scheme','producePlan:order-plan:process-scheme','producePlan:optimize')")
    public CommonResult<Boolean> saveProcessSchemeResults(@RequestBody SaveProcessSchemeReqVO saveProcessSchemeReqVO) {

        optimizePlanService.saveProcessSchemeResults(saveProcessSchemeReqVO);

        return CommonResult.success(true);

    }



    @GetMapping("/getProcessSchemeConfig")
    @Operation(summary = "查询加工方案组对应的配置信息")
    @Parameter(name = "planId", description = "排单ID", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('manage:order-plan:process-scheme','producePlan:order-plan:process-scheme')")
    public CommonResult<PlanProcessSchemeList> getProcessSchemeConfig(@Schema(description = "方案组ID") @NotNull(message = "方案组的ID不能为空")  Long processId,
                                                                      @Schema(description = "排单id") @RequestParam Long planId) {

        return success(optimizePlanService.getProcessSchemeConfig(processId,planId));

    }





    @PutMapping("/updateProcessScheme")
    @Operation(summary = "修改排单对应的方案组的是否锁单")
    @PreAuthorize("@ss.hasAnyPermissions('placeorder:optimize','producePlan:optimize')")
    public CommonResult<Boolean> updateProcessScheme(@RequestBody PlanProcessSchemeReqVO reqVO) {
        optimizePlanService.updateProcessScheme(reqVO);
        return success(true);
    }






    @GetMapping("/getPlateModel")
    @Operation(summary = "查询当前生产单的板材造型信息")
    @Parameter(name = "orderId", description = "生产单Id", required = true)
//    @PreAuthorize("@ss.hasAnyPermissions('manage:pack:query','manage:pack:pre-pack')")
    public CommonResult<List<OrderModelDO>> getPlateModel (@Valid @RequestParam("orderId") Long orderId) {

        return success(optimizePlanService.getPlateModel(orderId));

    }


    @GetMapping("/getPlanPlateData")
    @Operation(summary = "未排单获取板材的所有数据")
//    @PreAuthorize("@ss.hasPermission('manage:un-order-olan:create')")
    public CommonResult<OrderSource> getNoPlanPlateData (@Valid NoPlanFilterPlateReqVO reqVO) {

        return success(optimizePlanService.getNoPlanPlateData(reqVO));

    }



    @GetMapping("/getOrderDataFilter")
    @Operation(summary = "未排单高级筛选-订单分组")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<OrderRespVOCopy>> getOrderPlateDataFilter (@Valid FilterDataPageReqVO pageReqVO) {

        return success(optimizePlanService.getOrderPlateDataFilter(pageReqVO));

    }



    @GetMapping("/newPlanPlate")
    @Operation(summary = "获取排单是否有新加板材")
    @Parameter(name = "planId", description = "排单ID", required = true)
    public CommonResult<Boolean> getNewPlanPlate(@Valid @RequestParam("planId") Long planId) {

        return success(optimizePlanService.getNewPlanPlate(planId));

    }



    @PutMapping("/updateConfig")
    @Operation(summary = "修改排单对应的加工方案组的配置信息")
    public CommonResult<Boolean> updateProcessSchemeConfig(@RequestBody PlanSchemeConfigReqVO reqVO) {

        optimizePlanService.updateProcessSchemeConfig(reqVO);
        return success(Boolean.TRUE);

    }





    @GetMapping("/getGoodsDataFilter")
    @Operation(summary = "未排单高级筛选-板材分组")
    @PreAuthorize("@ss.hasPermission('manage:pack:query')")
    public CommonResult<PageResult<OrderGoodsResp>> getGoodsPlateDataFilter (@Valid FilterDataPageReqVO pageReqVO) {

        return success(optimizePlanService.getGoodsPlateDataFilter(pageReqVO));

    }


}
