zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
package com.shlanbao.tzsc.base.interceptor;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Element;
 
import com.shlanbao.tzsc.base.dao.SchCalendarDaoI;
import com.shlanbao.tzsc.base.mapping.MdShift;
import com.shlanbao.tzsc.base.mapping.MdTeam;
import com.shlanbao.tzsc.base.mapping.MdWorkshop;
import com.shlanbao.tzsc.base.mapping.SchCalendar;
import com.shlanbao.tzsc.utils.tools.ApplicationContextUtil;
import com.shlanbao.tzsc.utils.tools.DateUtil;
 
/**
 * 解析MES关于工厂日历xml数据
 * @author TRAVLER
 *
 */
public class AlalyCalenderXML {
    private static AlalyCalenderXML calender=null;
    
    private AlalyCalenderXML() {
        
    }
    
    public static AlalyCalenderXML getInstance(){
        if(calender==null){
            calender=new AlalyCalenderXML();
        }
         return calender;
    }
    
    /**
     * 解析保存xml
     * @param root
     * @param fileName
     * @return
     * TRAVLER
     * 20152015年12月24日上午10:02:38
     */
    public boolean handleQCXML(Element root,String fileName){
        boolean flag=false;
        
        return flag;
    }
    
    /**
     * 解析工厂日历xml,并封装成map
     * @param root
     * @param fileName 文件名
     * @return
     */
    @SuppressWarnings("rawtypes")
    public boolean alalyCalenderXML(Element root,String fileName) throws Exception{
        MdWorkshop workshop=new MdWorkshop();
        MdTeam team=null;
        MdShift shift=null;
        SchCalendar calender=null;
        
        Date sday=null;//开始排班日
        Date eday=null;//最后排班日
        
        String shiftId1=null;//早班
        String shiftName1=null;
        String shiftId2=null;//中班
        String shiftName2=null;
        String shiftId3=null;//晚班
        String shiftName3=null;
        
        String date=null;//工作日
        String shiftStime1=null;//早班开始时间
        String shiftEtime1=null;//早班结束时间
        
        String shiftStime2=null;//中班开始时间
        String shiftEtime2=null;//中班结束时间
        
        String shiftStime3=null;//晚班开始时间
        String shiftEtime3=null;//晚班结束时间
        
        Element ele=null;//中转引用
        String val=null;//中转引用
        Element teamId;//
        Element teamName;
        Element date0;
        List<SchCalendar> calenders=new ArrayList<SchCalendar>();
        //1.解析数据*********************start************************/
        try {
            for (Iterator i= root.elementIterator();i.hasNext();) {
                ele=(Element) i.next();
                val=ele.getName();
                if(val.equals("segmentItem")){
                    for (Iterator segmentItem= ele.elementIterator();segmentItem.hasNext();){
                        ele=(Element) segmentItem.next();
                        val=ele.getName();
                        if(val.equals("work_shop")){
                            workshop.setId(getNodeStringVal(ele));
                        }else if(val.equals("shiftId1")){
                            shiftId1=transShift(getNodeStringVal(ele));
                        }else if(val.equals("shiftName1")){
                            shiftName1=getNodeStringVal(ele);
                        }else if(val.equals("stime1")){
                            shiftStime1=getNodeStringVal(ele);
                        }else if(val.equals("etime1")){
                            shiftEtime1=getNodeStringVal(ele);
                        }else if(val.equals("shiftId2")){
                            shiftId2=transShift(getNodeStringVal(ele));
                        }else if(val.equals("shiftName2")){
                            shiftName2=getNodeStringVal(ele);
                        }else if(val.equals("stime2")){
                            shiftStime2=getNodeStringVal(ele);
                        }else if(val.equals("etime2")){
                            shiftEtime2=getNodeStringVal(ele);
                        }else if(val.equals("shiftId3")){
                            shiftId3=transShift(getNodeStringVal(ele));
                        }else if(val.equals("shiftName3")){
                            shiftName3=getNodeStringVal(ele);
                        }else if(val.equals("stime3")){
                            shiftStime3=getNodeStringVal(ele);
                        }else if(val.equals("etime3")){
                            shiftEtime3=getNodeStringVal(ele);
                        }else if(val.equals("bomInfo")){
                            List<?> ls=ele.elements("segmentInfo");
                            int size=ls.size();
                            //具体的排班
                            for (int j=0;j<size;j++){
                                ele=(Element) ls.get(j);
                                date0=ele.element("date");
                                date=getNodeStringVal(date0);
                                if(j==0){
                                    sday=DateUtil.strToDate(date+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
                                }
                                if(j==size-1){
                                    eday=DateUtil.strToDate(date+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
                                }
                                //夜班结束时间天数加一
                                /***************第一个*******************/
                                String shiftStime = date+" "+shiftStime1;
                                String shiftEtime=null;
                                if(shiftId1.equals("3")){
                                    shiftEtime = DateUtil.dateAdd("d",1,DateUtil.strToDate(date, "yyyy-MM-dd"),"yyyy-MM-dd")+" "+shiftEtime1;
                                }else{
                                    shiftEtime = date+" "+shiftEtime1;
                                }
                                //排班信息 第一个
                                teamId=ele.element("teamId1");
                                team=new MdTeam(getNodeStringVal(teamId));
                                teamName=ele.element("teamName1");
                                team.setName(getNodeStringVal(teamName));
                                shift=new MdShift(shiftId1);
                                shift.setName(shiftName1);
                                calender=new SchCalendar(team, shift, workshop, DateUtil.strToDate(date+" 00:00:00", "yyyy-MM-dd HH:mm:ss"), DateUtil.strToDate(shiftStime, "yyyy-MM-dd HH:mm"), DateUtil.strToDate(shiftEtime, "yyyy-MM-dd HH:mm"), 0L);
                                calenders.add(calender);
                                
                                /***************end*******************/
                                
                                
                                /***************第二个*******************/
                                shiftStime = date+" "+shiftStime2;
                                if(shiftId2.equals("3")){
                                    shiftEtime = DateUtil.dateAdd("d",1,DateUtil.strToDate(date, "yyyy-MM-dd"),"yyyy-MM-dd")+" "+shiftEtime2;
                                }else{
                                    shiftEtime = date+" "+shiftEtime2;
                                }
                                //第二个
                                teamId=ele.element("teamId2");
                                team=new MdTeam(getNodeStringVal(teamId));;
                                teamName=ele.element("teamName2");
                                team.setName(getNodeStringVal(teamName));
                                shift=new MdShift(shiftId2);;
                                shift.setName(shiftName2);
                                calender=new SchCalendar(team, shift, workshop, DateUtil.strToDate(date+" 00:00:00", "yyyy-MM-dd HH:mm:ss"), DateUtil.strToDate(shiftStime, "yyyy-MM-dd HH:mm"), DateUtil.strToDate(shiftEtime, "yyyy-MM-dd HH:mm"), 0L);
                                calenders.add(calender);
                                
                                /***************end*******************/
                                
                                /***************第三个*******************/
                                shiftStime = date+" "+shiftStime3;
                                if(shiftId3.equals("3")){
                                    shiftEtime = DateUtil.dateAdd("d",1,DateUtil.strToDate(date, "yyyy-MM-dd"),"yyyy-MM-dd")+" "+shiftEtime3;
                                }else{
                                    shiftEtime = date+" "+shiftEtime3;
                                }
                                //第三个
                                teamId=ele.element("teamId3");
                                team=new MdTeam(getNodeStringVal(teamId));
                                teamName=ele.element("teamName3");
                                team.setName(getNodeStringVal(teamName));
                                shift=new MdShift(shiftId3);;
                                shift.setName(shiftName3);
                                calender=new SchCalendar(team, shift, workshop, DateUtil.strToDate(date+" 00:00:00", "yyyy-MM-dd HH:mm:ss"), DateUtil.strToDate(shiftStime, "yyyy-MM-dd HH:mm"), DateUtil.strToDate(shiftEtime, "yyyy-MM-dd HH:mm"), 0L);
                                calenders.add(calender);
                                /***************end******************/
                            }
                        }
                    }
                }
            }    
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        /**********************end*******************/
        //2.先删除原有的日历
        
        StringBuffer sql = new StringBuffer();
        sql.append("DELETE SCH_CALENDAR where [DATE]>=? AND [DATE]<=? AND WORKSHOP=? ");
        List<Object> params = new ArrayList<Object>();
        params.add(sday);
        params.add(eday);
        params.add(workshop.getId());
        SchCalendarDaoI dao=ApplicationContextUtil.getBean(SchCalendarDaoI.class);
        dao.updateBySql(sql.toString(),params);
        //3.插入新的日历数据
        return dao.batchInsertAndReturn(calenders, SchCalendar.class);
    }
    
    
    
    /**
     * 获取节点的String 值
     *  TODO
     *  @param e
     *  @return
     *  TRAVLER
     *2015年12月14日下午3:54:36
     */
    private String getNodeStringVal(Element e){
        String r=null;
        if(null!=e){
            r=e.getTextTrim();
        }
        return r;
    }
    
    /**
     * 这个不是滕州班次,
     * 滕州班次与数据库一致
     * @param st
     * @return
     * TRAVLER
     * 20152015年12月24日上午9:17:50
     */
    private String transShift(String st){
        String shift="";
        if(st.equals("1")){
            shift="3";//夜班
        }else if(st.equals("2")){
            shift="1";//白班
        }else if(st.equals("3")){
            shift="2";//中班
        }
        return shift;
    }
    
}