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.vo.AddRemainReqVO;
import com.cf.imes.module.executor.controller.admin.plan.vo.OptimizeParamResVO;
import com.cf.imes.module.executor.controller.admin.plan.vo.PlateOptimize;
import com.cf.imes.module.executor.controller.admin.plan.vo.SavePlanPlateResult;
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;

/**
 * @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));
    }

}
