package com.ag.algroithm;

import com.ag.algroithm.data.CntrProerty;
import com.ag.algroithm.data.ContantDataTwoChant;
import com.ag.algroithm.instance.HungarianAlgorithm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YardCntrPlanAlgorithm {


    /**
     * select * from port_cntr where substring(yard_position,1,2)='F1' and E_F_ID='F' and CNTR_OPERATOR_COD='XWWL' and good_notes='磷酸一铵'
     *
     * 呗位 3-15
     *
     * 3 5 79 11 13 15 从这7个呗位找14个箱子
     * @param args
     */

    public static void main(String args[]) {
        //Aver表示平均每个拖卡都能分到对应拖卡呗位的箱子
        // ContantDataAver contantData = new ContantDataAver();

        //Aver表示其中有一个拖卡的呗位没有箱子
        ContantDataTwoChant contantData = new ContantDataTwoChant();

        List arrlist = contantData.data();
        Map datamap=new HashMap();
        double[][] matrix = new double[7][200];
        for (int i = 1; i < 6; i++) {
            String[][][][] namelist = new String[5][5][6][3];
            for (Object cntrProerty1 : arrlist) {
                CntrProerty cntrProerty = (CntrProerty) cntrProerty1;
                namelist[cntrProerty.getRow()][cntrProerty.getFloor()][cntrProerty.getChant()][cntrProerty.getDirect()] = cntrProerty.getCntrno();
            }
            System.out.println("===================T" + i + "=================");

            computeOcclusion(namelist, 3, 0, i,matrix,datamap);



        }

        for(int x=0; x<matrix.length; x++)//循环输出二维数组的for循环嵌套
        {
            for(int y=0; y<matrix[x].length; y++)
            {
                if(y==19) {
                  //  if(matrix[x][y]!=0)
                    System.out.print(x+"@"+y+":"+matrix[x][y] + ","+"\n");
                }else{
                  //  if(matrix[x][y]!=0)
                    System.out.print(x+"@"+y+":"+matrix[x][y] + ",");
                }
                if(matrix[x][y]==0.0){

                    matrix[x][y]=10000.0;
                }
            }
        }
        HungarianAlgorithm b = new HungarianAlgorithm(matrix);
        int[] match = b.execute();
      //  System.out.println(computeCost(matrix,match));

      // System.out.println(Arrays.toString(matrix));

        for (int i = 0; i < matrix.length; i++) {


            System.out.println("matrix["+i+"]["+match[i]+"]"+"=代价：=="+matrix[i][match[i]]+"==箱号：=="+datamap.get("matrix["+i+"]["+match[i]+"]"));

        }
    }


    /**
     * 计算出箱子路径长度和是否有遮挡，计算匈牙利算法的代价
     * floors 总层数
     * 目标位置，排数 rows， 呗数 chant
     *
     * @param namelist
     */
    public static void computeOcclusion(String[][][][] namelist, int floors, int rows, int chant, double[][] matrix, Map datamap) {
        //i表示排数 j表示层数 k 表示呗 m表示方向，排*50（权重），呗*10权重， 遮挡*80权重
        for (int i = 1; i < namelist.length; i++) {
            for (int j = 1; j < namelist[i].length; j++) {
                for (int k = 1; k < namelist[i][j].length; k++) {
                    for (int m = 0; m <= 1; m++) {
                        if (j < 4) {
                            if (namelist[i][j][k][m] != null) {
                                if (namelist[i][j][k][m].contains("@pre")) {
                                    namelist[i][j][k][m] = namelist[i][j][k][m].substring(0, namelist[i][j][k][m].indexOf("@pre"));
                                }
                            }
                            boolean flag = false;


                            //如果上面存在箱子标记一下
                            if (namelist[i][j + 1][k][m] != null) {
                                if (k == chant) {
                                    if (namelist[i][j][k][m] != null) {

                                        //1排+1呗+（3总层数-2层）+方向0,呗移动*30

                                        //排*呗位

                                        matrix[chant][i+k*11+(floors-j)+m]=(i + (Math.abs(k - chant)*30 + 1) + (floors - j) - 1);

                                        namelist[i][j][k][m] = (i + (Math.abs(k - chant) + 1) + (floors - j) - 1) + "@" + namelist[i][j][k][m] + "@pre" + "@chant";
                                    }
                                } else if (namelist[i][j][k][m] != null) {
                                    matrix[chant][i+k*11+(floors-j)+m]=(i + (Math.abs(k - chant)*30 + 1) + (floors - j) - 1);
                                    namelist[i][j][k][m] = (i + (Math.abs(k - chant) + 1) + (floors - j) - 1) + "@" + namelist[i][j][k][m] + "@pre";
                                }
                                flag = true;
                            }
                            if (namelist[i][j][k][m] != null) {
                                if (!flag) {

                                    if (k == chant) {
                                        matrix[chant][i+k*11+(floors-j)+m]=(i + (Math.abs(k - chant)*30 + 1) + (floors - j) - 1);
                                        namelist[i][j][k][m] = (i + (Math.abs(k - chant) + 1) + (floors - j) - 1) + "@" + namelist[i][j][k][m] + "@chant";
                                    } else {
                                        matrix[chant][i+k*11+(floors-j)+m]=(i + (Math.abs(k - chant)*30 + 1) + (floors - j) - 1);
                                        namelist[i][j][k][m] = (i + (Math.abs(k - chant) + 1) + (floors - j) - 1) + "@" + namelist[i][j][k][m];
                                    }
                                }
                                int s=i+k*11+(floors-j)+m;
                                System.out.println( "matrix["+chant+"]["+s+"]最终====namelist[" + i + "][" + j + "][" + k + "][" + m + "]=" + namelist[i][j][k][m]);

                                datamap.put("matrix["+chant+"]["+s+"]",namelist[i][j][k][m]);
                            }
                        }
                    }
                }
            }
        }

    }

    /**
     * @param i      排号
     * @param j      层
     * @param k      呗位
     * @param m      方向
     * @param cntrno
     */
    public void computefinalCntrPosition(int i, int j, int k, int m, String cntrno) {


    }

}

