package com.aps.service.mp;

import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;

/**
 * 作者：佟礼
 * 时间：2026-07-29
 */
public class InitVariablesProduct {
    /**
     * 创建单层产品的决策变量
     * @param solver 求解器
     * @param config 层配置
     * @param numDays 天数
     * @param isPurchaseLayer 是否为采购层（原材料层，无生产变量和开关变量）
     * @return 层变量
     */
    static ProductLayerVariables createVariables(MPSolver solver, ProductLayerConfig config,
                                                      int numDays, boolean isPurchaseLayer) {
        ProductLayerVariables vars = new ProductLayerVariables();
        int n = config.size();

        if (isPurchaseLayer) {
            // 原材料层：只有采购量和库存变量
            vars.purchase = new MPVariable[n][numDays];
            vars.inventory = new MPVariable[n][numDays];
            for (int i = 0; i < n; i++) {
                for (int t = 0; t < numDays; t++) {
                    vars.purchase[i][t] = solver.makeNumVar(0, Double.POSITIVE_INFINITY,
                            "pr_" + config.names[i] + "_d" + (t + 1));
                    vars.inventory[i][t] = solver.makeNumVar(0, Double.POSITIVE_INFINITY,
                            "ir_" + config.names[i] + "_d" + (t + 1));
                }
            }
        } else {
            // 生产层：产量、库存、开关变量
            vars.production = new MPVariable[n][numDays];
            vars.inventory = new MPVariable[n][numDays];
            vars.switchVar = new MPVariable[n][numDays];
            String prefix = config.names[0].startsWith("P") ? "xp" : "xs";
            String invPrefix = config.names[0].startsWith("P") ? "ip" : "is";
            String swPrefix = config.names[0].startsWith("P") ? "yp" : "ys";

            for (int i = 0; i < n; i++) {
                for (int t = 0; t < numDays; t++) {
                    vars.production[i][t] = solver.makeNumVar(0, Double.POSITIVE_INFINITY,
                            prefix + "_" + config.names[i] + "_d" + (t + 1));
                    vars.inventory[i][t] = solver.makeNumVar(0, Double.POSITIVE_INFINITY,
                            invPrefix + "_" + config.names[i] + "_d" + (t + 1));
                    vars.switchVar[i][t] = solver.makeBoolVar(
                            swPrefix + "_" + config.names[i] + "_d" + (t + 1));
                }
            }
        }
        return vars;
    }

}
