jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.timer.service;
 
import java.util.*;
 
import com.ld.igds.models.Device;
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.bdf2.job.model.JobCalendarRelation;
import com.bstek.bdf2.job.model.JobDefinition;
import com.bstek.bdf2.job.model.JobHistory;
import com.bstek.bdf2.job.model.JobParameter;
import com.bstek.bdf2.job.model.JobState;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.Constant;
import com.ld.igds.models.Timing;
import com.ld.igds.models.TimingDepot;
import com.ld.igds.models.TimingDevice;
import com.ld.igds.timer.BdfJobService;
import com.ld.igds.timer.service.ITimerService;
import com.ld.igds.util.ContextUtil;
 
@Slf4j
@Repository(ITimerService.BEAN_ID)
public class TimperServiceImpl extends HibernateDao implements ITimerService {
 
    @Autowired
    private BdfJobService bdfJobService;
 
    @Override
    public List<Timing> getTiming(Map<String, Object> param) throws Exception {
        StringBuilder hql = new StringBuilder();
        hql.append(" from " + Timing.class.getName()
                + " where companyId = :companyId ");
        Map<String, Object> arg = new HashMap<String, Object>();
        if (null != param) {
            bulidHql(hql, param, arg);
        }
        arg.put("companyId", ContextUtil.getCompanyId());
 
        hql.append(" order by updateTime ");
 
        List<Timing> list = this.query(hql.toString(), arg);
 
        if (null == list || list.isEmpty())
            return list;
 
        // 添加job信息
        for (Timing tim : list) {
            tim.setJob(bdfJobService.getJobById(tim.getId()));
        }
 
        return list;
    }
 
    @Override
    public List<TimingDevice> getDevieByTimerId(String timId) {
        if (StringUtils.isEmpty(timId))
            return null;
 
        Map<String, Object> p = new HashMap<String, Object>();
        String hql = " from "
                + TimingDevice.class.getName()
                + " where companyId = :companyId and timId =:timId order by depotId";
        p.put("companyId", ContextUtil.getCompanyId());
        p.put("timId", timId);
 
        return this.query(hql, p);
    }
 
    private void bulidHql(StringBuilder hql, Map<String, Object> param,
            Map<String, Object> arg) {
        String depotId = (String) param.get("depotId");
        if (StringUtils.isNotBlank(depotId)) {
            hql.append(" and depotId = :depotId ");
            arg.put("depotId", depotId);
        }
 
        String type = (String) param.get("type");
        if (StringUtils.isNotBlank(type)) {
            hql.append(" and type = :type ");
            arg.put("type", type);
        }
 
    }
 
    @Override
    public void addTimer(Timing tim) throws Exception {
        Session session = this.getSessionFactory().openSession();
        try {
            //更新定时规则
            String cron = Constant.CRON_DAY;
            if (tim.getTimType().equals(Constant.TIM_TYPE_DAY)) {
                cron = cron.replace("H", tim.getHour() + "");
                cron = cron.replace("M", tim.getMinute() + "");
            }
 
            if (tim.getTimType().equals(Constant.TIM_TYPE_WEEK)) {
                cron = Constant.CRON_WEEK;
                cron = cron.replace("H", tim.getHour() + "");
                cron = cron.replace("M", tim.getMinute() + "");
                cron = cron.replace("W", tim.getWeek());
            }
 
            if (tim.getTimType().equals(Constant.TIM_TYPE_HOUR)) {
                cron = Constant.CRON_HOUR;
                Calendar cal = Calendar.getInstance();
                cal.setTime(tim.getAppointTime());
                cron = cron.replace("S", cal.get(Calendar.SECOND) + "");
                cron = cron.replace("M", cal.get(Calendar.MINUTE) + "");
                cron = cron.replace("H", cal.get(Calendar.HOUR) + "");
                cron = cron.replace("D", cal.get(Calendar.DATE) + "");
                cron = cron.replace("&", cal.get(Calendar.MONTH) + 1 + "");
            }
            tim.setCron(cron);
 
            tim.setUpdateTime(new Date());
            tim.setUpdateUser(ContextUtil.getLoginUserCName());
 
            // 新增数据
            if (null == tim.getId()) {
                tim.setId(ContextUtil.getCurTimeMillis());
                tim.setCompanyId(ContextUtil.getCompanyId());
                //新增job
                tim.setJob(this.saveJob(tim, true));
                session.save(tim);
                log.info("==========" + tim.getUpdateUser() + "添加仓库定时配置……" + "==========");
            } else { //更新数据
                //更新job
                tim.setJob(this.saveJob(tim, false));
                session.update(tim);
                log.info("==========" + tim.getUpdateUser() + "更新仓库定时配置……" + "==========");
            }
 
            this.updateTimDevice(tim, session);
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    // 处理定时任务中的设备信息
    private void updateTimDevice(Timing tim, Session session) {
        if (null == tim.getDevices())
            return;
        // 删除现有的设备信息
        String hql = " delete " + TimingDevice.class.getName()
                + " where timId=:timId";
        session.createQuery(hql).setString("timId", tim.getId())
                .executeUpdate();
 
        for (TimingDevice device : tim.getDevices()) {
            device.setId(ContextUtil.getUUID());
            device.setTimId(tim.getId());
            device.setCompanyId(tim.getCompanyId());
            device.setDepotId(tim.getDepotId());
            session.save(device);
        }
    }
 
    private JobDefinition saveJob(Timing tim, boolean flag) throws Exception {
        String type = tim.getType();
        if (StringUtils.isEmpty(type)) {
            throw new Exception("没有获获取到检测类型,请重新刷新页面添加……");
        }
        JobDefinition job = new JobDefinition();
        job.setId(tim.getId());
        job.setCompanyId(tim.getCompanyId());
        job.setCronExpression(tim.getCron());
        job.setState(JobState.ready);
        job.setDesc("业务人员配置,请勿操作!");
 
        if (type.equals(BizType.GRAIN.getCode())) {
            job.setBeanId(Constant.JOB_BEAN_GRAIN);
            job.setName(tim.getName() + "-粮情定时检测");
        }
        
        if (type.equals(BizType.GAS.getCode())) {
            job.setBeanId(Constant.JOB_BEAN_GAS);
            job.setName(tim.getName() + "-气体定时检测");
        }
        
        if (type.equals(BizType.PEST.getCode())) {
            job.setBeanId(Constant.JOB_BEAN_PEST);
            job.setName(tim.getName() + "-虫害定时检测");
        }
 
        if ("lamp".equals(type)) {
            job.setBeanId(Constant.JOB_BEAN_LAMP);
            job.setName(tim.getName() + "-照明定时控制");
        }
 
        if(flag){ //新增
            bdfJobService.saveJob(job);
        }else {   //更新
            bdfJobService.updateJob(job);
        }
        return job;
    }
 
    @Override
    public void delTimer(String id) {
        Assert.notNull(id, "定时配置ID为空");
        Session session = this.getSessionFactory().openSession();
        try {
            String hql = " delete " + Timing.class.getName() + " where id=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
            hql = "delete " + JobDefinition.class.getName() + " where id=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
            hql = "delete " + JobHistory.class.getName() + " where jobId=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
            hql = "delete " + JobCalendarRelation.class.getName()
                    + " where jobId=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
            hql = "delete " + JobParameter.class.getName() + " where jobId=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
            hql = " delete " + TimingDevice.class.getName()
                    + " where timId=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
            hql = " delete " + TimingDepot.class.getName() + " where timId=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    @Override
    public List<TimingDepot> getDepotByTimerId(String timId) {
        if (null == timId)
            return null;
        String hql = " from " + TimingDepot.class.getName()
                + " where timId = :timId";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("timId", timId);
        return this.query(hql, args);
    }
 
    @Override
    public void saveDepot(List<TimingDepot> items) {
        if (null == items)
            return;
        Session session = this.getSessionFactory().openSession();
        try {
            for (TimingDepot item : items) {
                if (null == item.getId()) {
                    item.setId(ContextUtil.getUUID());
                    session.save(item);
                } else {
                    session.update(item);
                }
            }
        } finally {
            session.flush();
            session.close();
        }
    }
 
    @Override
    public String delDepotById(String id) {
        if (null == id)
            return "没有获取到ID无法删除";
        Session session = this.getSessionFactory().openSession();
        try {
            String hql = " delete " + TimingDepot.class.getName()
                    + " where id=:id";
            session.createQuery(hql).setString("id", id).executeUpdate();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    @Override
    public void delDeviceByTimerId(String timId, String depotId) {
        Session session = null;
        try {
            session = this.getSessionFactory().openSession();
            String hql = " delete from " + TimingDevice.class.getName()
                    + " where timId=:timId and depotId=:depotId ";
            session.createQuery(hql).setString("timId", timId)
                    .setString("depotId", depotId).executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
 
    }
 
    @Override
    public void addTimingDevice(TimingDevice timingDevice) {
        Session session = null;
        try {
            session = this.getSessionFactory().openSession();
            session.save(timingDevice);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
 
    }
    @Override
    public String delSettings(List<TimingDevice> list) {
        for (TimingDevice device : list) {
            Session session = null;
            try {
                session = this.getSessionFactory().openSession();
                session.delete(device);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                session.flush();
                session.close();
            }
        }
        return null;
    }
    @Override
    public String addSettings(List<Device> list, String timId) {
        List<TimingDevice> timingDevices = new ArrayList<>();
        String depotId = list.get(0).getDepotId();
        // 首先过滤掉未操作数据
        /*
         * for (Device device : list) {
         * if(device.getTargetStatus().equals("ZERO")){ list.remove(device); } }
         */
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTargetStatus() == null || list.get(i).getTargetStatus().equals("ZERO") || list.get(i).getTargetStatus().equals("")) {
                //list.remove(list.get(i));
            } else {
                TimingDevice timingDevice = new TimingDevice();
                timingDevice.setId(UUID.randomUUID().toString());
                timingDevice.setDepotId(list.get(i).getDepotId());
                timingDevice.setDeviceId(list.get(i).getId());
                timingDevice.setTimId(timId);
                timingDevice.setCompanyId(ContextUtil.getCompanyId());
                timingDevice.setTargetStatus(list.get(i).getTargetStatus());
                timingDevices.add(timingDevice);
            }
        }
        //System.out.println(timingDevices.toString());
 
        // 其次新增配置
        for (TimingDevice timingDevice : timingDevices) {
            //先删除已经有的,避免重复
            delDeviceByTimerId(timId,timingDevice.getDeviceId());
            addTimingDevice(timingDevice);
        }
        return null;
    }
}