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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package com.shlanbao.tzsc.pms.equ.lubricate.controller;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.shlanbao.tzsc.base.mapping.EqmLubricant;
import com.shlanbao.tzsc.base.mapping.EqmLubricantPlan;
import com.shlanbao.tzsc.base.mapping.EqmLubricantPlanParam;
import com.shlanbao.tzsc.base.mapping.MdEquipment;
import com.shlanbao.tzsc.base.mapping.SysEqpType;
import com.shlanbao.tzsc.base.model.DataGrid;
import com.shlanbao.tzsc.base.model.Json;
import com.shlanbao.tzsc.base.model.PageParams;
import com.shlanbao.tzsc.pms.equ.lubricate.beans.EqmBubricantfBean;
import com.shlanbao.tzsc.pms.equ.lubricate.beans.EqmLubricantPlanBean;
import com.shlanbao.tzsc.pms.equ.lubricate.service.EqmLubricantPlanParamServiceI;
import com.shlanbao.tzsc.pms.equ.lubricate.service.EqmLubricantPlanServiceI;
import com.shlanbao.tzsc.pms.equ.lubricate.service.EqmLubricantServiceI;
import com.shlanbao.tzsc.pms.sys.datadict.beans.SysEqpTypeBean;
import com.shlanbao.tzsc.pms.sys.datadict.service.SysEqpTypeServiceI;
import com.shlanbao.tzsc.utils.params.SysEqpTypeBase;
import com.shlanbao.tzsc.utils.tools.DateUtil;
import com.shlanbao.tzsc.utils.tools.StringUtil;
/**
 * 
* @ClassName: EqmLubricantController 
* @Description: 设备润滑计划 
* @author luo
* @date 2015年7月8日 下午3:58:32 
*
 */
@Controller
@RequestMapping("/pms/lubrplan")
public class EqmLubricantPlanController {
 
    protected Logger log = Logger.getLogger(this.getClass());
    
    @Autowired
    public EqmLubricantPlanServiceI eqmLubricantPlanServiceI;
    
    
    @Autowired
    public EqmLubricantServiceI lubricantServiceI;
    
    @Autowired
    public EqmLubricantPlanServiceI lubricantPlanService;
    
    @Autowired
    public EqmLubricantPlanParamServiceI lubricantPlanParamService;
    
    @Autowired
    public SysEqpTypeServiceI sysEqpTypeServiceImpl;
    
    
    @RequestMapping("/goToAddLubiPlan")
    public String goToAddLubiPlan()throws Exception{
        return "/pms/equ/lubriplan/addLubriPlan";
    }
    @RequestMapping("/goToEditLubiPlan")
    public String goToEditLubiPlan(String id,HttpServletRequest request)throws Exception{
        if(StringUtil.notNull(id)){
            EqmLubricantPlanBean bean=eqmLubricantPlanServiceI.getBeanByIds(id);
            request.setAttribute("bean", bean);
            return "/pms/equ/lubriplan/editLubriPlan";
        }else{
            return "";
        }
    }
    
    /**
     * 设备润滑计划新增
     * @param object
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/addLubrplan")
    public Json addLubrplan(EqmLubricantPlanBean bean,HttpServletRequest request,HttpSession session)throws Exception{
        Json json = new Json();
        try {
            
            json.setMsg("操作成功!");
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("操作失败!");
            json.setSuccess(false);
        }
        return json;
    }
    
    /**
     * 设备润滑计划修改
     * @param object
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/editLubrplan")
    public Json editLubrplan(EqmLubricantPlanBean bean,HttpServletRequest request)throws Exception{
        Json json = new Json();
        try {
            eqmLubricantPlanServiceI.updateBean(bean);
            json.setMsg("操作成功!");
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("操作失败!");
            json.setSuccess(false);
        }
        return json;
    }
    
    @ResponseBody
    @RequestMapping("/queryLubrplan")
    public DataGrid queryLubrplan(EqmLubricantPlanBean bean,PageParams pageParams){
        try {
            return eqmLubricantPlanServiceI.queryDataGrid(bean,pageParams);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    /**
     * 功能说明:设备润滑计划管理-添加
     * @author wanchanghuang
     * @time 2015年8月21日16:35:42
     * 
     * */
    @ResponseBody
    @RequestMapping("/addEqmLubricatPlan")
    public Json addEqmLubricatPlan(EqmBubricantfBean bean){
        Json json = new Json();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            //日集合
            List<Date> dayDt=findDates(sdf.parse(bean.getDate_plan1()),sdf.parse( bean.getDate_plan2()));
            //月集合
            List<Date> periodDt=getMouthDay(bean.getDate_plan1(),bean.getDate_plan2());
            /*for(Date d: periodDt){
                System.out.println(sdf.format(d)+"  =================================");
            }*/
            //日润滑计划生成    天
            if(!"".equals(bean.getRuleId())){
                addDayLubricantPan(bean,dayDt); 
            }
            //周期性润滑-生成    周,月,年
            if(!"".equals(bean.getRuleIdf())){
                addPeriodLubricantPan(bean,dayDt,periodDt,sdf); //周期性润滑
            }   
            json.setMsg("操作成功!");
            json.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("操作失败!");
            json.setSuccess(false);
        }
        return json;
    }
    
    //周期性润滑-早,中,晚
    public void addPeriodLubricantPan(EqmBubricantfBean bean,List<Date> dayDt,List<Date> periodDt,SimpleDateFormat sdf) throws ParseException{
        SysEqpTypeBean sysEqpType=new SysEqpTypeBean();
        sysEqpType.setCategoryId(bean.getRuleIdf());
        //查询具体项周期值
        //List<SysEqpType> lists=sysEqpTypeServiceImpl.queryBean(sysEqpType);
        List<SysEqpTypeBean> lists=sysEqpTypeServiceImpl.querySysEqmTypeByCid(sysEqpType);
        if(lists.size()>0){
            for(SysEqpTypeBean s:lists){
                String unit_id=s.getUnit_id();
                if(SysEqpTypeBase.period_3.equals(unit_id)){ //月
                        int lt=periodDt.size(); //日期数
                        int f=2; //默认倍数
                        int period=s.getPeriod(); //周期
                        if(period==0){
                            continue;
                        }
                        int dx=period;
                        while(lt>=dx){
                            Date d=periodDt.get(dx-1);
                            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d)+"  ===月===需要添加的值");
                            if(d!=null){
                                String shift_id=bean.getShift_id();
                                if(shift_id.equals(SysEqpTypeBase.shift_0)){
                                    //早
                                    bean.setShift_id(SysEqpTypeBase.shift_1);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                    //中
                                    bean.setShift_id(SysEqpTypeBase.shift_2);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                    //晚
                                    bean.setShift_id(SysEqpTypeBase.shift_3);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                }else{
                                   saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                }
                            }
                            dx=f*period;
                            f++;
                        }
                }else if(SysEqpTypeBase.period_4.equals(unit_id)){ //年
                        int lt=periodDt.size(); //日期数
                        int f=2; //默认倍数
                        int period=s.getPeriod()*12; //周期  12
                        int dx=period;
                        while(lt>=dx){
                            Date d=periodDt.get(dx-1);
                            //System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d)+"  ===年===需要添加的值");
                            if(d!=null){
                                String shift_id=bean.getShift_id();
                                if(shift_id.equals(SysEqpTypeBase.shift_0)){
                                    //早
                                    bean.setShift_id(SysEqpTypeBase.shift_1);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                    //中
                                    bean.setShift_id(SysEqpTypeBase.shift_2);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                    //晚
                                    bean.setShift_id(SysEqpTypeBase.shift_3);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                }else{
                                   saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                }
                            }
                            dx=f*period;
                            f++;
                        }
                }else if(SysEqpTypeBase.period_2.equals(unit_id)){ //周
                        int lt=dayDt.size(); //日期数
                        int f=2; //默认倍数
                        int period=s.getPeriod()*7; //周期  7
                        int dx=period;
                        while(lt>=dx){
                            Date d=dayDt.get(dx-1);
                            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d)+"  ===周===需要添加的值");
                            if(d!=null){
                                String shift_id=bean.getShift_id();
                                if(shift_id.equals(SysEqpTypeBase.shift_0)){
                                    //早
                                    bean.setShift_id(SysEqpTypeBase.shift_1);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                    //中
                                    bean.setShift_id(SysEqpTypeBase.shift_2);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                    //晚
                                    bean.setShift_id(SysEqpTypeBase.shift_3);
                                    saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                }else{
                                   saveEqmLubricantPlan(bean,s,sdf.format(d),unit_id);
                                }
                            }
                            dx=f*period;
                            f++;
                        }
                }
            }
        }
    }
    
    //周期性润滑- 插入EQM_LUBRICAT_PLAN表  {d-日期   unit_id周期表示} 
    public void saveEqmLubricantPlan(EqmBubricantfBean bean,SysEqpTypeBean s,String date,String unit_id) throws ParseException{
        String shift_id=bean.getShift_id();
        EqmLubricantPlanParam param=new EqmLubricantPlanParam();
        EqmLubricantPlan epl=new EqmLubricantPlan();
        /**
         * 日期,时间,班次,设备,周期类型,查询该条数据是否已经生成,
         *    如果没有生成,插入后返回Id,再插入 EQM_LUBRICAT_PLAN_PARAM 表
         *    如果已生成,直接获取ID,插入 EQM_LUBRICAT_PLAN_PARAM 表
         * 
         */
        List<?> listMdEquipMent=eqmLubricantPlanServiceI.addEqmLubricatPlan(bean);
        for(Object o:listMdEquipMent){
            Object[] temp=(Object[]) o;
            String eqp_id= temp[0].toString();
            EqmBubricantfBean ebf=sysEqpTypeServiceImpl.queryEqmLubricantPlanByPar(bean,date,unit_id,shift_id,eqp_id);
            if(ebf!=null){
                //直接插入子表
                param.setCode(DateUtil.formatDateToString(new Date(), "ss")+Math.random());
                param.setName(s.getName());
                param.setDes(s.getDes());
                param.setOiltd(s.getOilId());
                param.setFill_quantity(s.getFillQuantity());
                param.setFill_unit(s.getFillUnitName());
                param.setPlan_id(ebf.getId());
                param.setMethods(s.getFashionName());
                SysEqpType setf=new SysEqpType();
                setf.setId(s.getId());
                param.setSysEqpType(setf);
                lubricantPlanParamService.savePlanParams(param);
                
            }else{
                MdEquipment met=new MdEquipment();
                met.setId(eqp_id);
                epl.setEqp(met);
                epl.setDate_p(new SimpleDateFormat("yyyy-MM-dd").parse(date));
                epl.setLub_id(unit_id);
                epl.setCode(DateUtil.formatDateToString(new Date(), "ss")+Math.random());
                epl.setShift_id(shift_id);
                epl.setRule_id(bean.getRuleIdf());
                
                EqmLubricantPlan plan=lubricantPlanService.saveBean(epl);
                param.setCode(DateUtil.formatDateToString(new Date(), "ss")+Math.random());
                param.setName(s.getName());
                param.setDes(s.getDes());
                param.setOiltd(s.getOilId());
                param.setFill_quantity(s.getFillQuantity());
                param.setFill_unit(s.getFillUnitName());
                param.setPlan_id(plan.getId());
                param.setMethods(s.getFashionName());
                SysEqpType setf=new SysEqpType();
                setf.setId(s.getId());
                param.setSysEqpType(setf);
                lubricantPlanParamService.savePlanParams(param);
            }
        }
        
        
        
    }
    
    //日润滑
    public void addDayLubricantPan(EqmBubricantfBean bean,List<Date> dateLists) throws ParseException{
        //获得选中的所有设备
        /**
         * id:4028998449c0f9650149c0fc428b0005    
         * eqp_type_id:4028998449bcb4b10149bcb558780002    
         * equipment_cde:31    
         * equipment_name:1号包装机
         * 
         * */
        List<?> listMdEquipMent=eqmLubricantPlanServiceI.addEqmLubricatPlan(bean);
        for(Date d :dateLists){
            for(Object o:listMdEquipMent){
                Object[] temp=(Object[]) o;
                EqmLubricantPlan plan=new EqmLubricantPlan();
                plan.setEqp(new MdEquipment(temp[0].toString()));
                plan.setDate_p(d);//计划开始日期
                plan.setCode(DateUtil.formatDateToString(new Date(), "yyyyMMdd")+"E"+temp[2].toString());
                plan.setLub_id("1");//表示日润滑,周润滑,月润滑,年润滑
                plan.setRule_id(bean.getRuleId());
                plan.setStatus(0);
                String shift_id=bean.getShift_id();
                if(shift_id.equals(SysEqpTypeBase.shift_0)){ // 0表示 全部   
                    //早
                    plan.setShift_id(SysEqpTypeBase.shift_1);
                    plan=lubricantPlanService.saveBean(plan);
                    addLubricantPlanParam(bean,plan);
                    //中
                    plan.setShift_id(SysEqpTypeBase.shift_2);
                    plan=lubricantPlanService.saveBean(plan);
                    addLubricantPlanParam(bean,plan);
                    //晚
                    plan.setShift_id(SysEqpTypeBase.shift_3);
                    plan=lubricantPlanService.saveBean(plan);
                    addLubricantPlanParam(bean,plan);
                }else{
                    plan.setShift_id(shift_id);
                    //保存至设备润滑计划
                    plan=lubricantPlanService.saveBean(plan);
                    addLubricantPlanParam(bean,plan);
                }
                
            }
        }
    }
    
    //日润滑-插入计划子表
    public void addLubricantPlanParam(EqmBubricantfBean bean,EqmLubricantPlan plan){
        //获取设备润滑规则详细项
        SysEqpTypeBean sysEqpType=new SysEqpTypeBean();
        sysEqpType.setCategoryId(bean.getRuleId());
        //根据润滑规则查找到具体的项
        List<SysEqpType> lists=sysEqpTypeServiceImpl.queryBean(sysEqpType);
        //获取刚创建的设备润滑计划ID
        String plan_id=plan.getId();
        for(SysEqpType sys:lists){
            EqmLubricantPlanParam param=new EqmLubricantPlanParam();
            param.setCode(sys.getCode());
            param.setDes(sys.getDes());
            //润滑剂单位
            if(sys.getFillUnitBean()!=null){
                param.setFill_unit(sys.getFillUnitBean().getName());
            }
            param.setFill_quantity(sys.getFillQuantity());
            //润滑剂名称
            if(sys.getOilIdBean()!=null){
                param.setOiltd(sys.getOilIdBean().getLubricantName());
            }
            param.setName(sys.getName());
            param.setPlan_id(plan_id);
            //润滑方式
            if(sys.getFashionBean()!=null){
                param.setMethods(sys.getFashionBean().getName());
            }
            param.setSysEqpType(new SysEqpType(sys.getId()));
            lubricantPlanParamService.savePlanParams(param);
        }
    }
        
    public void createLubriPlan(String lubri_id,String eqp_id,String eqp_code){
        //查询设备润滑周期表
        EqmLubricant lubri=lubricantServiceI.getBeanById(lubri_id);
        EqmLubricantPlan plan=new EqmLubricantPlan();
        plan.setEqp(new MdEquipment(eqp_id));
        //plan.setDate_p(DateUtil.dateCals(new Date(),lubri.getCycle()));
        plan.setCode(DateUtil.formatDateToString(new Date(), "yyyyMMdd")+"E"+eqp_code);
        plan.setLub_id(lubri_id);
        //plan.setRule_id(lubri.getRule_id());
        plan.setStatus(0);
        //保存至设备润滑计划
        plan=lubricantPlanService.saveBean(plan);
        //获取设备润滑规则详细项
        SysEqpTypeBean sysEqpType=new SysEqpTypeBean();
        //sysEqpType.setCategoryId(lubri.getRule_id());
        //根据润滑规则查找到具体的项
        List<SysEqpType> lists=sysEqpTypeServiceImpl.queryBean(sysEqpType);
        //获取刚创建的设备润滑计划ID
        String plan_id=plan.getId();
        for(SysEqpType sys:lists){
            EqmLubricantPlanParam param=new EqmLubricantPlanParam();
            param.setCode(sys.getCode());
            param.setDes(sys.getDes());
            //润滑剂单位
            if(sys.getFillUnitBean()!=null){
                param.setFill_unit(sys.getFillUnitBean().getName());
            }
            param.setFill_quantity(sys.getFillQuantity());
            //润滑剂名称
            if(sys.getOilIdBean()!=null){
                param.setOiltd(sys.getOilIdBean().getLubricantName());
            }
            param.setName(sys.getName());
            param.setPlan_id(plan_id);
            //润滑方式
            if(sys.getFashionBean()!=null){
                param.setMethods(sys.getFashionBean().getName());
            }
            param.setSysEqpType(new SysEqpType(sys.getId()));
            lubricantPlanParamService.savePlanParams(param);
        }
    }
    
    //获得两时间段所有日期集合
    public static List<Date> findDates(Date dBegin, Date dEnd) {  
        System.out.println(dBegin+"     "+dEnd);
        List<Date> lDate = new ArrayList<Date>();  
        lDate.add(dBegin);  
        Calendar calBegin = Calendar.getInstance();  
        // 使用给定的 Date 设置此 Calendar 的时间    
        calBegin.setTime(dBegin);  
        Calendar calEnd = Calendar.getInstance();  
        // 使用给定的 Date 设置此 Calendar 的时间    
        calEnd.setTime(dEnd);  
        // 测试此日期是否在指定日期之后    
        while (dEnd.after(calBegin.getTime())) {  
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量    
            calBegin.add(Calendar.DAY_OF_MONTH, 1);  
            lDate.add(calBegin.getTime());  
        }  
        return lDate;  
    }  
    
    public static List<Date> getMouthDay(String date1,String date2){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Date> list= new ArrayList<Date>();
        try {
            Date d1= sdf.parse(date1);
            Date d2= sdf.parse(date2);
            Calendar dd= Calendar.getInstance();
            dd.setTime(d1);
            while(dd.getTime().before(d2)){
                Date str=dd.getTime();
                list.add(str);
                dd.add(Calendar.MONTH, 1);
            }
            Date str=dd.getTime();
            list.add(str);
            dd.add(Calendar.MONTH, 1);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }
    
    
    /**
     * 批量删除用户
     */
    @ResponseBody
    @RequestMapping("/deleteCycle")
    public Json deleteCycle(String ids){
        Json json=new Json();
        try {
            lubricantPlanService.deleteCycle(ids);
            json.setMsg("批量删除数据成功!");
            json.setSuccess(true);
        } catch (Exception e) {
            json.setMsg("批量删除数据失败!");
            json.setSuccess(false);
        }
        return json;
    }
    
    /**
     * 返回日期列表
     * @param date1
     * @param date2
     * @return
     * @throws Exception
     */
    public static List<Date> getMonthSpace(String date1, String date2,int d)
            throws Exception {
            List<Date> dates=new ArrayList<Date>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));
            int result = (c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR))*12+(c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH));
            if(result==0){
                result=1;
            }
            for (int i = 0; i < result; i++) {
                c1.add(Calendar.MONTH, d);
                dates.add(c1.getTime());
            }
            return dates;
        }
    }