廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package ng.Algorithm.Layouts;
 
 
import java.lang.reflect.Array;
import java.sql.*;
import java.util.*;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import ng.Algorithm.Layouts.Heuristic.RowResult;
import ng.Algorithm.Layouts.LayoutAlgorithm.AlgorithmParams;
 
public class AlgorithmDBInterface {
 
    private String connectionString;
    private int workId;
    public AlgorithmDBInterface(String connectionString,int WorkId){
        this.connectionString=connectionString;
        this.workId=WorkId;
        param_sql=String.format("SELECT `width`,`length`,`xinterval`,`yinterval` from gmms_furnace where id=%d", WorkId);
        this.peice_sql=String.format("select id,processcard,position,front,width,height,glass_idx,thickness,color,layout_mode,w,h  from  tempere_task_glass where work_id=%d", WorkId);
    }
    
    private String param_sql;
    private String peice_sql;
    
    public int Line;
    
    public boolean canr=true;
    
    
    private AlgorithmParams getParam(Connection con) throws SQLException{
        Statement sm= con.createStatement();
        ResultSet rs= sm.executeQuery(param_sql);
        rs.next();
        AlgorithmParams  p=new AlgorithmParams();
        p.width=rs.getInt(1);
        p.length=rs.getInt(2);
        p.h_interval=rs.getInt(3);
        p.v_interval=rs.getInt(4);
        p.method="Heuristic";
        rs.close();
        sm.close();
        return p;
    }
    
     class Info {
        public long id;
        public int thicknss;
        public String color;
        public int width;
        public int height;
        public Piece.LayoutMode layout_mode;
        public int FrameNumber;
        public boolean IsFront;
        public int GlassIndex;
        public String Processcard;
        public int GlassNumber;
        public double w,h;
        @Override
        public String toString(){
            return String.format("%d.%d[%d]",FrameNumber,GlassNumber,id);
        }
        
    }
    
     compare piececompare=new compare();
     
     
    class compare implements java.util.Comparator<Piece>{
 
        @Override
        public int compare(Piece arg0, Piece arg1) {
            // TODO Auto-generated method stub
            Info info1=(Info)arg0.Source;
            Info info2=(Info)arg1.Source;
            int a1=info1.IsFront?0:1;
            int a2=info2.IsFront?0:1;
            if(a1>a2)
                return -1;
            if(a1<a2)
                return 1;
            if(info1.GlassIndex<info2.GlassIndex)
                return 1;
            if(info1.GlassIndex>info2.GlassIndex)
                return -1;
            return 0;
        }
        
    }
         
     
     
         private Info[] toInfo(ResultSet set,boolean canRotate) throws SQLException{
            List<Info> list=new ArrayList<Info>();
            while(set.next()){
                Info info=new Info();
                info.id=set.getLong("id");
                info.thicknss= (int)set.getDouble("thickness");
                 info.color=set.getString("color");
                info.width=(int)set.getDouble("width");
                info.height=(int)set.getDouble("height");
                info.Processcard=set.getString("processcard");
                double w=set.getDouble("w");
                double h=set.getDouble("h");
                info.w=w;
                info.h=h;
                if(canRotate==true){
                    info.layout_mode=Piece.LayoutMode.Both;
                }
                else{
                    info.layout_mode=w<h?Piece.LayoutMode.Zhong:Piece.LayoutMode.Heng;
                }
                
                /*
                int mode=set.getInt("layout_mode");
                switch(mode){
                 case 1:
                     info.layout_mode= Piece.LayoutMode.Zhong;
                     break;
                 case 2:
                     info.layout_mode=Piece.LayoutMode.Heng;
                     break;
                    default:
                        info.layout_mode=Piece.LayoutMode.Both;
                        break;
                }
                */
                info.FrameNumber=set.getInt("position");
                info.IsFront=set.getInt("front")==1;
                info.GlassIndex=set.getInt("glass_idx");
                list.add(info);
;            }
            Info[] ret=new Info[list.size()];
            return list.toArray(ret);
        }
    
    
    
    private List<Piece>[] getPiece(Connection con,boolean canRotate) throws SQLException{
        Statement sm= con.createStatement();
        ResultSet rs= sm.executeQuery(this.peice_sql);
        ArrayList<ArrayList<Piece>> pss=new ArrayList<ArrayList<Piece>>();
        Info[] infos=toInfo(rs,canRotate);
        if(infos==null)
            return null;
        if(infos.length==0)
            return null;
        rs.close();
        sm.close();
        for(int i=0;i<infos.length;i++){
            Info cur=infos[i];
            Piece p=new Piece();
            p.width=cur.width;
            p.height=cur.height;
            p.Mode=cur.layout_mode;
            p.Source=cur;
            p.w=cur.w;
            p.h=cur.h;
            boolean ok=false;
            for(int j=0;j<pss.size();j++){
                Piece pp=pss.get(j).get(0);
                Info info=(Info)pp.Source;
                if(info.FrameNumber==cur.FrameNumber){
                    pss.get(j).add(p);
                    ok=true;
                    break;
                }
            }
            if(ok==false){
                ArrayList<Piece> pl=new ArrayList<Piece>();
                pl.add(p);
                pss.add(pl);
            }
        }
        for(int i=0;i<pss.size();i++){
            pss.get(i).sort(this.piececompare);
            List<Piece> list=pss.get(i);
            for(int j=0;j<list.size();j++){
                Info info= (Info)list.get(j).Source;
                info.GlassNumber=j+1;
            }
        }
         List<Piece> p=new ArrayList<Piece>();
        List<Piece>[] ret=(List<Piece>[])Array.newInstance(p.getClass(), pss.size());
        for(int i=0;i<ret.length;i++){
            ret[i]=pss.get(i);
        }
        return ret;
    }
    
 
 
    
    
    
    
    private void saveLayout(Connection con,LayoutResult result,AlgorithmParams param) throws SQLException {
        int step=0;
        try {
            con.setAutoCommit(false);
            int number=result.Number;
            step=1;
             CallableStatement call= con.prepareCall("{call insert_tempere_layout(?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
            boolean ok=true;
             for(int i=0;i<result.layout.length;i++){
                Rect r=result.layout[i];
                Info info=(Info)r.getSource().Source;
                int move=0;
                int pendulum=1;
                 if(i==result.layout.length-1){
                     pendulum=3;
                 }
                 else{
                     if(result.layout[i].Row!=result.layout[i+1].Row)
                         pendulum=2;
                 }
                
                
                if(pendulum!=1){
                     move=param.width-result.layout[i].x;
                 }
                 else{
                     move=result.layout[i+1].x-result.layout[i].x;
                 }
                
                 call.setInt(1,this.workId);
                 call.setInt(2,number);
                 call.setDouble(3,result.rate);
                 call.setInt(4,i+1);
                 call.setInt(5,r.x);
                 call.setInt(6,r.y);
                 if(r.getLength()==r.getWidth()){
                     call.setInt(7,0);
                 }else{
                     call.setInt(7,r.isHeng?0:1);
                 }
                 
                 call.setInt(8, pendulum);
                 call.setInt(9,move);
                 call.setInt(10, r.Row);
                 call.setInt(11,r.Col);
                 call.setLong(12,info.id);
                 call.setString(13, null);
                 call.registerOutParameter(14,java.sql.Types.INTEGER);
                 call.execute();
                 if(call.getInt(14)!=1){
                     ok=false;
                     break;
                 }
                
            }
             if(ok){
                 con.commit();
             }
             else{
                 con.rollback();
             }
             con.close();
            
        } catch (SQLException e) {
            if(step>=1)
            con.rollback();
            con.close();
            
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public LayoutResult lastResult;
    
    public class ComputeResult{
        public Exception Error;
        public boolean Success;
        public int Number;
        public boolean isNothing;
        public LayoutResult Result;
        public String toJson(){
            
            try{
                JSONArray arr=new JSONArray();
                for(int i=0;i<Result.layout.length;i++){
                    Rect r=Result.layout[i];
                    JSONObject obj=new JSONObject();
                    obj.put("x",r.x);
                    obj.put("y",r.y);
                    obj.put("w",r.getXSize());
                    obj.put("h",r.getYSize());
                    obj.put("size",""+r.getSource().w+"*"+r.getSource().h);
                    int k=0;
                    if(r.canHeng()){
                        k=2;
                    }
                    if(r.canZhong()){
                        k+=1;
                    }
                    obj.put("mode",k);
                     arr.put(obj);
                }
                return arr.toString();
            }
            catch(Exception e){
                return "{}";
            }
        }
    }
    
    
    
    int getNextLayoutNumber(Connection con) throws SQLException{
        CallableStatement sm= con.prepareCall("{call get_next_tempere_number(?,?)}");
        sm.setInt(1,this.workId);
        sm.registerOutParameter(2,java.sql.Types.INTEGER);
        
        int ret=0;
        sm.execute();
        ret=sm.getInt(2);
        sm.close();
        return ret;
    }
    
    
    public ComputeResult ComputeOnce(boolean canRotate){
        Connection con=null;
        lastResult=null;
        try {
            con = java.sql.DriverManager.getConnection(this.connectionString);
            AlgorithmParams param=getParam(con);
            List<Piece>[] ps=getPiece(con,canRotate);
            if(ps==null){
                ComputeResult ret= new ComputeResult();
                ret.Success=false;
                ret.Number=0;
                ret.Error=null;
                ret.isNothing=true;
                con.close();
                return ret;
            }
            LayoutResult result= LayoutAlgorithm.Compute(param,ps);
            int number= getNextLayoutNumber(con);
            result.WorkId=this.workId;
            result.Number=number;
            if(result!=null){
                this.saveLayout(con, result,param);
                this.lastResult=result;
                ComputeResult ret= new ComputeResult();
                ret.Success=true;
                ret.Number=number;
                ret.Error=null;
                ret.isNothing=false;
                ret.Result=result;
                return ret;
            }
            ComputeResult ret1= new ComputeResult();
            ret1.Success=false;
            ret1.Number=0;
            ret1.Error=null;
            ret1.isNothing=true;
            con.close();
            return ret1;
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ComputeResult ret= new ComputeResult();
            ret.Success=false;
            ret.Number=0;
            ret.Error=e;
            ret.isNothing=false;
            return ret;
        }
        finally{
            try {
                con.close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        
        
    }
    private static AlgorithmDBInterface[] als;
    public static void initAlgorithm(String connectionString,int[] lines){
          als=new AlgorithmDBInterface[lines.length];
          for(int i=0;i<lines.length;i++){
              als[i]=new AlgorithmDBInterface(connectionString,lines[i]);
              als[i].Line=lines[i];
          }
    }
 
    public static int getCanRotate(int line){
        for(int i=0;i<als.length;i++){
              if(als[i].Line==line)
                  return als[i].canr?1:0;
          }
        return -1;
    }
    
    public static boolean setCanRotate(int line,boolean rotate){
        for(int i=0;i<als.length;i++){
              if(als[i].Line==line){
                  als[i].canr=rotate;
                return true;  
              }
          }
        return false;
    }
 
         
    public static ComputeResult ComputeOnce(int line){
        
         for(int i=0;i<als.length;i++){
              if(als[i].Line==line)
                  return als[i].ComputeOnce(als[i].canr);
          }
          return null;
         }     
}