#
18516761980
2021-06-07 3002486dad95464e124b2aee02ff880d3003c903
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
package com.slcf.util;
 
import java.util.HashMap;
import java.util.Map;
 
import javax.sql.DataSource;
 
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
 
import com.slcf.dao.UserDao;
import com.slcf.pojo.CrnBean;
import com.slcf.pojo.LocationBean;
import com.slcf.pojo.RowNoBean;
import com.slcf.pojo.StaDescBean;
import com.slcf.pojo.StationBean;
import com.slcf.pojo.WorkMastBean;
import com.slcf.pojo.WorkNoBean;
import com.slcf.service.CrnService;
import com.slcf.service.LocationService;
import com.slcf.service.RowNoService;
import com.slcf.service.StaDescService;
import com.slcf.service.StationService;
import com.slcf.service.WorkFileService;
import com.slcf.service.WorkNoService;
 
/**
 * 公共方法类
 * @author admin
 * @date 2018年11月6日
 */
public class CommonMethod {
    private WorkFileService workFileService;
    private WorkNoService workNoService;
    private CrnService crnService;
    private StationService stationService;
    private LocationService locationService;
    private RowNoService rowNoService;
    private StaDescService staDescService;
    
    public CommonMethod(WorkFileService workFileService,WorkNoService workNoService,
            CrnService crnService,StationService stationService,LocationService locationService,
            RowNoService rowNoService,StaDescService staDescService) {
        this.workFileService = workFileService;
        this.workNoService = workNoService;
        this.crnService = crnService;
        this.stationService = stationService;
        this.locationService = locationService;
        this.rowNoService = rowNoService;
        this.staDescService = staDescService;
    }
    
    public SqlSession getSession(){
        SqlSession session = null;
        try {
            String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=AccessData";
            String username="sa";
            String password="sa4761516";
            
            DataSource dataSource =new PooledDataSource(driver,url,username,password);
            
            //创建事务        
            TransactionFactory transactionFactory =  new JdbcTransactionFactory();
            
            Environment environment = new Environment("development", transactionFactory, dataSource);
            
            Configuration configuration = new Configuration(environment);
            
            configuration.addMapper(UserDao.class);
            SqlSessionFactory sqlSessionFactory = new  SqlSessionFactoryBuilder().build(configuration);
            System.out.println(sqlSessionFactory);
            
            session =  sqlSessionFactory.openSession();
        }catch(Exception e) {
            System.out.println(e);
        }
        return session;
    }
    
    /**
     * 判断站点状态是否正常
     * @param station
     * @return
     */
    public Map<String,Object> checkStationStatus(StationBean station) {
        Map<String,Object> mapStation=new HashMap<String, Object>();
        try {
            if(station!=null) {
                if(station.getAutoing()==null || !station.getAutoing().equals("Y")) {
                    mapStation.put("code", 1);
                    mapStation.put("msg", "入库站点不是自动状态");
                    return mapStation;
                }
                if(station.getLoading()==null || !station.getLoading().equals("Y")) {
                    mapStation.put("code", 1);
                    mapStation.put("msg", "入库站点无物");
                    return mapStation;
                }
                if(station.getWrk_no()>0) {
                    mapStation.put("code", 1);
                    mapStation.put("msg", "入库站点已有工作号");
                    return mapStation;
                }
                
                int count = workFileService.getStoreWorkCount(station.getDev_no());
                if(count>0) {
                    mapStation.put("code", 1);
                    mapStation.put("msg", "同一站点不能同时生成两笔入库工作档");
                    return mapStation;
                }
                
                mapStation.put("code", 0);
            }else {
                mapStation.put("code", 1);
                mapStation.put("msg", "入库站点不存在");
                return mapStation;
            }
        }catch(Exception e) {
            System.out.println(e.getMessage());
            mapStation.put("code", 1);
            mapStation.put("msg", "站点异常!" + e.getMessage());
        }
        return mapStation;
    }
    
    /**
     * 入出库作业生成新的工作号
     * @return
     */
    public int getNewWorkNo(WorkNoBean workNoBean) {
        int wrk_no=0;
        try {
            if(workNoBean!=null) {
                wrk_no = workNoBean.getWrk_no();
                int s_no = workNoBean.getS_no();
                int e_no = workNoBean.getE_no();
                
                if(wrk_no>=e_no) {
                    wrk_no = s_no;
                }else {
                    wrk_no++;
                }
                
                while(true) {
                    WorkMastBean workFile = workFileService.queryWorkMastById(wrk_no);
                    if(workFile!=null) {
                        wrk_no++;
                        if(wrk_no>=e_no) {
                            wrk_no = s_no;
                        }else {
                            wrk_no++;
                        }
                    }else {
                        break;
                    }
                }
                
                if(wrk_no>0) {
                    workNoBean.setWrk_no(wrk_no);
                    workNoService.upWorkNo(workNoBean);
                }
            }
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return wrk_no;
    }
    
    /**
     * 入库作业寻找空库位号
     * @return
     */
    public String getLocNo(int type, RowNoBean rowNoBean, int stn_no, int loc_type) {
        String loc_no = "";
        try {
            if(rowNoBean!=null) {
                int cur_row = rowNoBean.getCurrent_row();
                int min_row = rowNoBean.getS_row();
                int max_row = rowNoBean.getE_row();
                int crn_qty = rowNoBean.getCrn_qty();
                
                int iii=1;
                int max_row_1 = max_row - 1;
                
                while(loc_no=="") {
                    if(cur_row==max_row) {
                        cur_row = min_row;
                    }else if(cur_row==max_row_1) {
                        cur_row = min_row + 1;
                    }else {
                        cur_row = cur_row + 2;
                    }
                    
                    int crn_no = (cur_row+1)/2;
                    if("Y".equals(getCrnEnable(crn_no,"I"))) {
                        int e_staNo = getIoStaNo(type,crn_no,stn_no);
                        if(e_staNo==0) {
                            continue;
                        }
                        StationBean station = stationService.queryStationById(e_staNo);
                        if(station!=null) {
                            if("Y".equals(station.getIn_enable())
                                && "Y".equals(station.getAutoing())
                                && station.getIn_qty()<2) {
                                //查找库位
                                LocationBean location = locationService.getLocNoByRow(cur_row,loc_type);
                                if(location!=null) {
                                    loc_no = location.getLoc_no();
                                    break;
                                }
                            }
                        }
                    }
                    iii=iii+1;
                    if(iii>(crn_qty*2+1)) {
                        return loc_no;
                    }
                }
                
                if(loc_no!=null && !loc_no.equals("")) {
                    rowNoBean.setCurrent_row(cur_row);
                    rowNoService.upRowNo(rowNoBean);
                }
            }
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return loc_no;
    }
    
    /**
     * 判断堆垛机入出库是否禁用
     * @param crn_no
     * @param type
     * @return
     */
    public String getCrnEnable(int crn_no,String type) {
        String result="";
        try {
            CrnBean crn = crnService.queryCrnById(crn_no);
            if(crn!=null) {
                if(type.equals("I")) {
                    result = crn.getIn_enable();
                }else {
                    result = crn.getOut_enable();
                }
            }
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据堆垛机编号,入库站得到堆垛机入库站
     * @param type
     * @param crn_no
     * @param sta_no
     * @return
     */
    public int getIoStaNo(int type, int crn_no, int sta_no) {
        int result=0;
        try {
            StationBean station = stationService.queryStationById(sta_no);
            if(station!=null) {
                StaDescBean staDesc = new StaDescBean();
                staDesc.setType_no(type);
                staDesc.setStn_no(sta_no);
                staDesc.setCrn_no(crn_no);
                result = staDescService.queryStnByTypeStnCrn(staDesc);
            }
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
    
}