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

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.executor.controller.admin.plan.bo.OrderSource;
import com.cf.imes.module.executor.controller.admin.plan.saveOptimize.SavePlanPlateResultReqVO;
import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.service.optimizeplan.OptimizePlanService;
import io.swagger.v3.oas.annotations.Operation;
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 javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

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

    @Resource
    private OptimizePlanService optimizePlanService;

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


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

    @PostMapping("savePlanPlateResult")
    @Operation(summary = "提交保存优化结果文件")
    @OperateLog(logArgs = false)
    @PreAuthorize("@ss.hasPermission('executor:optimize-plate:create')")
    public CommonResult<Boolean> savePlanPlateResult(SavePlanPlateResult result) {
        return CommonResult.success(optimizePlanService.savePlanPlateResult(result));
    }

    @GetMapping("commit")
    @Operation(summary = "开始开料")
    @PreAuthorize("@ss.hasPermission('executor:optimize-plate:create')")
    public CommonResult<Boolean> commit(@RequestParam @Valid @NotNull(message = "排单id不能空") Long planId) {
        return CommonResult.success(optimizePlanService.commit(planId));
    }

    @GetMapping("/getOptimizeParam")
    @Operation(summary = "获取优化算法参数")
    @PreAuthorize("@ss.hasPermission('executor:optimize-plate:create')")
    public CommonResult<OptimizeParamResVO> getOptimizeParam(@RequestParam @Valid @NotNull(message = "排单id不能空") Long planId) {
        return CommonResult.success(optimizePlanService.getOptimizeParam(planId));
    }

    @GetMapping("/getCfData")
    @Operation(summary = "获取cfData")
    @PreAuthorize("@ss.hasPermission('executor:optimize-plate:create')")
    public CommonResult<OptimizeParamResVO> getCfData(@RequestParam @Valid @NotNull(message = "排单id不能空") Long planId) {
        //todo
        return CommonResult.success(null);
    }

/*    @GetMapping("/getOptimizePlanParam")
    @Operation(summary = "获取优化排单参数 (mock)")
    @PreAuthorize("@ss.hasPermission('executor:optimize-plate:create')")
    @Parameter(name = "planId", description = "排单id", required = false)
    public CommonResult<OptimizeParamRespVO> getOptimizePlanParam (@RequestParam (required = false) Long planId) {
       *//* OptimizeParamRespVO data = RandomUtils.randomPojo(OptimizeParamRespVO.class);
        return CommonResult.success(data);*//*
        //todo
        OptimizeParamRespVO optimizePlanParam = optimizePlanService.getOptimizePlanParam(planId);
        return CommonResult.success(optimizePlanParam);
    }*/

    @GetMapping("/getOrderSource")
    @Operation(summary = "获取生产单源数据")
    @PreAuthorize("@ss.hasPermission('production:manager-list:calculate')")
    public CommonResult<OrderSource> getOrderSource(@Schema(description = "生产单id") @RequestParam(required = false) Long orderId,
                                                    @Schema(description = "排单id") @RequestParam(required = false) Long planId,
                                                    @Schema(description = "机台Id") @RequestParam("machineId") Long machineId
    ) {
        return CommonResult.success(optimizePlanService.getOrderSource(orderId, planId, machineId));
        //return CommonResult.success(RandomUtils.randomPojo(OrderSource.class));
    }


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

    }


    @PostMapping("/saveOptimizationResults")
    @Operation(summary = "保存优化后的数据")
    @PreAuthorize("@ss.hasPermission('executor:optimize-plate:create')")
    public CommonResult<Boolean> saveOptimizationResults(@Valid @RequestBody SavePlanPlateResultReqVO req ) {

        optimizePlanService.saveOptimizationResults(req);

        return CommonResult.success(true);

    }

}
