2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-09-09 5e2fe6ce6e80dea99f6eda304dba6fafbb30ad63
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.config.RealtimeServerBean;
import com.sandu.ximon.admin.param.LEDScheduleParam_xixun;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.JsonUtil;
import com.sandu.ximon.admin.utils.LightemitUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.request.Schedules;
import com.sandu.ximon.admin.utils.request.TaskSchedules;
import com.sandu.ximon.admin.utils.request.requestbody.GetSchedules;
import com.sandu.ximon.admin.utils.request.requestbody.Task;
import com.sandu.ximon.admin.vo.LedScheduleVO;
import com.sandu.ximon.admin.vo.XiXunResultVO;
import com.sandu.ximon.dao.domain.LedScheduleEntity;
import com.sandu.ximon.dao.domain.PoleLightemitEntity;
import com.sandu.ximon.dao.enums.OrderByEnums;
import com.sandu.ximon.dao.mapper.LedScheduleEntityMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * @Author liuhaonan
 * @Date 2022/2/28 13:45
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class LedScheduleService extends BaseServiceImpl<LedScheduleEntityMapper, LedScheduleEntity> {
 
    private final LedScheduleEntityMapper ledScheduleEntityMapper;
    private final ClientService clientService;
    private final PoleLightemitService poleLightemitService;
    private final LightemitUtils lightemitUtils;
    private final RealtimeServerBean realtimeServerBean;
 
 
    public boolean insert(LEDScheduleParam_xixun ledEntity) {
        LedScheduleEntity ledScheduleEntity = new LedScheduleEntity();
        BeanUtil.copyProperties(ledEntity, ledScheduleEntity);
        ledScheduleEntity.setUserId(SecurityUtils.getClientId());
        Schedules schedules = new Schedules();
        Task task = new Task();
        TaskSchedules taskSchedules = new TaskSchedules();
        if (ledEntity.getStartTime() == "" || ledEntity.getEndTime() == "" || ledEntity.getStartTime() == null || ledEntity.getEndTime() == null) {
            schedules.setTimeType("All");
        } else {
            schedules.setTimeType("Range");
            schedules.setStartTime(ledEntity.getStartTime());
            schedules.setEndTime(ledEntity.getEndTime());
        }
        if (ledEntity.getStartDate() == "" || ledEntity.getEndDate() == "" || ledEntity.getStartDate() == null || ledEntity.getEndDate() == null) {
            schedules.setDateType("All");
        } else {
            schedules.setDateType("Range");
            schedules.setStartDate(ledEntity.getStartDate());
            schedules.setEndDate(ledEntity.getEndDate());
        }
        if (ledEntity.getWeek().length != 0) {
            schedules.setFilterType("Week");
            schedules.setWeekFilter(ledEntity.getWeek());
        } else {
            schedules.setFilterType("None");
        }
        List list = new ArrayList();
        list.add(schedules);
        task.setSchedules(list);
        task.setCreateDate(ledEntity.getCreateDate());
        task.setDateCreated(ledEntity.getCreateDate());
        String id = UUID.randomUUID().toString();
        task.set_id(id);
        task.setId(id);
        task.set_departmentd(UUID.randomUUID().toString());
        task.set_role(UUID.randomUUID().toString());
        task.setName(ledEntity.getName());
        taskSchedules.setTask(task);
 
        String json = JSON.toJSONString(taskSchedules, SerializerFeature.WriteMapNullValue);
//         String json = new Gson().toJson(taskSchedules);
        ledScheduleEntity.setSchedule(json);
        if (SecurityUtils.getClientId() != null) {
            ledScheduleEntity.setUserId(SecurityUtils.getUserId());
            if (clientService.findClientId()) {
                ledScheduleEntity.setClientId(clientService.getClientId());
            }
        }
 
        /**
         * 熙汛定时更改 日志记录开始
         */
        String content = "{熙汛定时id:" + ledScheduleEntity.getId() + ",熙汛定时名称:" + ledScheduleEntity.getName() + "熙汛定时内容:" + JSON.toJSONString(ledScheduleEntity.getSchedule()) + " }";
        StoreOperationRecordsUtils.storeOperationData(null, null, "熙汛定时任务更改", content);
        /**
         * 熙汛定时更改 日志记录结束
         */
        return this.save(ledScheduleEntity);
    }
 
 
    /**
     * 定时推送
     *
     * @param scheduleId
     * @param lightemitIds
     * @return
     */
    public Map<String, List<XiXunResultVO>> ledschedulepush(Integer scheduleId, Long[] lightemitIds) {
 
//        LedScheduleEntity ledScheduleEntity = baseMapper.selectById(scheduleId);
        LedScheduleEntity ledScheduleEntity = getById(scheduleId);
        if (ledScheduleEntity == null) {
            throw new BusinessException("该定时任务不存在");
        }
        Collection<PoleLightemitEntity> poleLightemitEntities = poleLightemitService.listByIds(Arrays.asList(lightemitIds));
        Map<String, List<XiXunResultVO>> result=new HashMap<>();
        List<XiXunResultVO> success =new ArrayList<>();
        List<XiXunResultVO> fail =new ArrayList<>();
 
 
        if (poleLightemitEntities != null) {
            for (PoleLightemitEntity entity : poleLightemitEntities) {
                String post = lightemitUtils.post(realtimeServerBean.getCommand() + entity.getLightemitControlCode(), ledScheduleEntity.getSchedule());
                XiXunResultVO vo=new XiXunResultVO();
                vo.setLightemitId(entity.getLightemitId());
                vo.setLightemitName(entity.getLightemitName());
                if(post.contains("{\"_type\":\"success\",\"_id\":")){
                    success.add(vo);
                }else {
                    fail.add(vo);
                }
            }
        }
 
 
 
 
        /**
         * 熙汛定时推送 日志记录开始
         */
        String message = "";
        List<String> listCode = new ArrayList<>();
 
        for (PoleLightemitEntity entity : poleLightemitEntities) {
 
            listCode.add(entity.getLightemitControlCode());
            message += "[屏幕Code:" + entity.getLightemitControlCode() + "屏幕名称:" + entity.getLightemitName() + "],";
        }
        String content = "{熙汛定时id:" + ledScheduleEntity.getId() + ",熙汛定时名称:" + ledScheduleEntity.getName() + "熙汛屏幕信息:" + message + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "推送熙汛定时任务", content);
        /**
         * 熙汛定时任务推送 日志记录结束
         */
 
        result.put("success",success);
        result.put("fail",fail);
        return result;
    }
 
    /**
     * 清空定时
     *
     * @param
     * @param lightemitIds
     */
    public Map<String, List<XiXunResultVO>> deleteSchedulePush( Long[] lightemitIds) {
 
        Collection<PoleLightemitEntity> poleLightemitEntities = poleLightemitService.listByIds(Arrays.asList(lightemitIds));
 
        Map<String, List<XiXunResultVO>> result=new HashMap<>();
        List<XiXunResultVO> success =new ArrayList<>();
        List<XiXunResultVO> fail =new ArrayList<>();
 
        if (poleLightemitEntities != null) {
            for (PoleLightemitEntity entity : poleLightemitEntities) {
//                lightemitUtils.post(realtimeServerBean.getCommand() + entity.getLightemitControlCode(), ledScheduleEntity.getSchedule());
                String post = lightemitUtils.post(realtimeServerBean.getCommand() + entity.getLightemitControlCode(), "{\"type\": \"timedScreening\",\"task\":{}}");
 
                XiXunResultVO vo=new XiXunResultVO();
                vo.setLightemitId(entity.getLightemitId());
                vo.setLightemitName(entity.getLightemitName());
                if(post.contains("{\"_type\":\"success\",\"_id\":")){
                   success.add(vo);
                }else {
                    fail.add(vo);
                }
 
                System.out.println(post+"---------");
            }
        }
 
 
        /**
         * 清空熙汛定时任务 日志记录开始
         */
        String message = "";
        List<String> listCode = new ArrayList<>();
 
        for (PoleLightemitEntity entity : poleLightemitEntities) {
 
            listCode.add(entity.getLightemitControlCode());
            message += "[屏幕Code:" + entity.getLightemitControlCode() + "屏幕名称:" + entity.getLightemitName() + "],";
        }
        String content = "{熙汛屏幕信息:" + message + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "清空熙汛定时任务", content);
        /**
         * 清空熙汛定时任务 日志记录结束
         */
        result.put("success",success);
        result.put("fail",fail);
        return result;
    }
 
    public boolean updateSchedule(LEDScheduleParam_xixun paramXixun) {
        if (paramXixun.getId() == null) {
            throw new BusinessException("参数错误");
        }
        LedScheduleEntity byId = getById(paramXixun.getId());
        if (byId == null) {
            throw new BusinessException("未找到该定时数据");
        }
        boolean result = false;
        if (removeById(paramXixun.getId())) {
            result = insert(paramXixun);
        }
        return result;
    }
 
    public LedScheduleEntity getSchedule(Integer id) {
        if (id == null) {
            throw new BusinessException("参数错误");
        }
        LedScheduleEntity byId = getById(id);
        if (byId == null) {
            throw new BusinessException("未找到该定时数据");
        }
        //List<Map> schedule = JSON.parseArray(byId.getSchedule(), List.class);
        return byId;
    }
 
 
    /**
     * 播放计划列表(熙讯)
     *
     * @param baseConditionVO
     * @param keyword
     * @return
     */
    public List listSchedule(BaseConditionVO baseConditionVO,Integer order,Integer seq, String keyword) {
        LambdaQueryWrapper<LedScheduleEntity> eq = Wrappers.lambdaQuery(LedScheduleEntity.class);
        //用户类型判断
        if (SecurityUtils.getClientId() != null) {
            eq = eq.eq(LedScheduleEntity::getUserId, SecurityUtils.getUserId()).or(w -> {
                w.eq(LedScheduleEntity::getClientId, SecurityUtils.getUserId());
            });
        }
        //模糊查询关键字判断
        if (keyword != null && !keyword.isEmpty()) {
            eq = eq.like(LedScheduleEntity::getName, keyword);
        }
 
        //排序字段
        String orderByResult = "id";
        //正序、倒叙
        String orderBySeq = OrderByEnums.ASC.getCode();
        if (order != null) {
            switch (order) {
                case 1:
                    orderByResult = OrderByEnums.LED_S_PLAY_PLAN_CREATE_TIME.getCode();
                    break;
                default:
            }
        }
        if (seq != null) {
            switch (seq) {
                case 1:
                    orderBySeq = " ASC";
                    break;
                case 2:
                    orderBySeq = " DESC";
                    break;
                default:
                    break;
            }
        }
        //排序方式
        String orderBy = orderByResult + " " + orderBySeq;
 
        PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize(),orderBy);
        return list(eq);
    }
 
 
    /**
     * 定时查询
     */
    public LedScheduleVO getledschedules(Long id) {
 
        PoleLightemitEntity poleLightemitEntity = poleLightemitService.getById(id);
 
        String result1 = lightemitUtils.getTimeSchedule(poleLightemitEntity.getLightemitControlCode());
        if (result1.indexOf("not open") != -1) {
            LedScheduleVO notOpen = new LedScheduleVO();
            notOpen.setLedName(poleLightemitEntity.getLightemitName());
            notOpen.setScheduleName("设备未开启");
            return notOpen;
        }
        boolean flag = result1.contains("exist");
        if (flag != true) {
            String result = lightemitUtils.getTimeSchedule(poleLightemitEntity.getLightemitControlCode());
//            if(result !=null){
            GetSchedules getSchedules = new GetSchedules();
            try {
                getSchedules = JsonUtil.convertJsonStringToObject(result, GetSchedules.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            LedScheduleVO ledScheduleVO = new LedScheduleVO();
            ledScheduleVO.setLedName(poleLightemitEntity.getLightemitName());
            if (getSchedules.getTask().getName() != null) {
                ledScheduleVO.setScheduleName(getSchedules.getTask().getName());
                List<Schedules> list = new ArrayList();
                list = getSchedules.getTask().getSchedules();
                for (Schedules schedules : list) {
 
                    ledScheduleVO.setStartTime(schedules.getStartTime());
                    ledScheduleVO.setEndTime(schedules.getEndTime());
                    ledScheduleVO.setStartDate(schedules.getStartDate());
                    ledScheduleVO.setEndDate(schedules.getEndDate());
                    ledScheduleVO.setWeek(schedules.getWeekFilter());
                }
                ;
            } else {
                ledScheduleVO.setScheduleName("无定时");
            }
            return ledScheduleVO;
        } else {
            LedScheduleVO ledScheduleVO = new LedScheduleVO();
            ledScheduleVO.setLedName(poleLightemitEntity.getLightemitName());
            ledScheduleVO.setScheduleName("设备未开启");
            return ledScheduleVO;
        }
 
    }
 
}