2021与蓝度共同重构项目,服务端
liuhaonan
2022-05-18 bd8da139decc60e8c7765e375ffda037f5209e3a
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.config.VnnoxConstant;
import com.sandu.ximon.admin.dto.PlanDto;
import com.sandu.ximon.admin.dto.SchedulesDTO;
import com.sandu.ximon.admin.dto.nova.PlayerProgram;
import com.sandu.ximon.admin.dto.nova.ProgramSchedule;
import com.sandu.ximon.admin.entity.Plans;
import com.sandu.ximon.admin.param.PlayPlanParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.VnnoxAPIUtil;
import com.sandu.ximon.admin.utils.VnnoxProgramAPIUtil;
import com.sandu.ximon.admin.utils.response.VnnoxResultResponse;
import com.sandu.ximon.admin.vo.NovaOpenVO;
import com.sandu.ximon.admin.vo.NovaPushResultVO;
import com.sandu.ximon.admin.vo.PlansVO;
import com.sandu.ximon.dao.domain.LEDProgram;
import com.sandu.ximon.dao.domain.LedPlayerEntity;
import com.sandu.ximon.dao.domain.PlayPlanNv;
import com.sandu.ximon.dao.domain.PushToLed;
import com.sandu.ximon.dao.mapper.PlayPlanNvMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 播放计划
 */
@Service
@AllArgsConstructor
public class PlayPlanNvService extends BaseServiceImpl<PlayPlanNvMapper, PlayPlanNv> {
 
    private final PlayPlanNvMapper playPlanMapper;
    private final VnnoxProgramAPIUtil vnnoxProgramAPIUtil;
    private final LedPlayerEntityService ledPlayerEntityService;
    private VnnoxAPIUtil vnnoxAPIUtil;
    private final LEDProgramService ledProgramService;
    private final ClientService clientService;
 
    public boolean addPlan(PlayPlanParam playPlanParam) {
 
        PlayPlanNv playPlan = new PlayPlanNv();
        if (SecurityUtils.getClientId() != null) {
            playPlan.setUserId(SecurityUtils.getUserId());
            if (clientService.findClientId()) {
                playPlan.setClientId(clientService.getClientId());
            }
        }
        playPlan.setName(playPlanParam.getName());
        playPlan.setLedProgramName(playPlanParam.getLedProgramName());
        LEDProgram byId = ledProgramService.getById(playPlanParam.getLedProgramId());
        if (byId == null) {
            throw new BusinessException("ledProgramId有误,未找到该节目");
        }
        playPlan.setLedProgramId(playPlanParam.getLedProgramId());
        playPlan.setVolume(playPlanParam.getVolume());
        playPlan.setApplySeries(playPlanParam.getApplySeries());
        playPlan.setStartDate(playPlanParam.getStartDate());
        playPlan.setEndDate(playPlanParam.getEndDate());
        playPlan.setWeekDays(playPlanParam.getWeekDays());
 
 
        Map schedule = playPlanParam.getSchedule();
 
        String s = JSON.toJSONString(schedule.get("plans"));
        List<PlanDto> plans = JSON.parseArray(s, PlanDto.class);
        System.out.println(plans.toString() + "plans");
 
//        List<PlanDto> plans = (List<PlanDto>) schedule.get("plans");
 
        List<SchedulesDTO> schedulesDTOS = new ArrayList<>();
        for (int i = 0; i < plans.size(); i++) {
            SchedulesDTO openDto = new SchedulesDTO();
 
 
            SchedulesDTO closeDto = new SchedulesDTO();
 
            openDto.setStartDate(schedule.get("startDate").toString());
            openDto.setEndDate(schedule.get("endDate").toString());
            openDto.setExecTime(plans.get(i).getStartTime());
            openDto.setStatus("OPEN");
 
            /**
             * 判断时间的先后
             */
            String startTime = openDto.getStartDate() + " " + plans.get(i).getStartTime();
            String endTime = openDto.getEndDate() + " " + plans.get(i).getEndTime();
 
            //转换成时间戳
            long startTimeStamp = DateUtil.parse(startTime, DatePattern.NORM_DATETIME_PATTERN).getTime();
            long endTimeStamp = DateUtil.parse(endTime, DatePattern.NORM_DATETIME_PATTERN).getTime();
            if (startTimeStamp > endTimeStamp) {
                throw new BusinessException("开始时间不能大于结束时间");
            }
 
            closeDto.setStartDate(schedule.get("startDate").toString());
            closeDto.setEndDate(schedule.get("endDate").toString());
            closeDto.setWeekDays(plans.get(i).getWeekDays());
            closeDto.setExecTime(plans.get(i).getEndTime());
            closeDto.setStatus("CLOSE");
 
            schedulesDTOS.add(closeDto);
            schedulesDTOS.add(openDto);
        }
 
        playPlan.setSchedule(JSON.toJSONString(playPlanParam.getSchedule()));
        playPlan.setSchedules(JSON.toJSONString(schedulesDTOS));
        playPlan.setStartTime(playPlanParam.getStartTime());
        playPlan.setEndTime(playPlanParam.getEndTime());
        //  BeanUtils.copyProperties(playPlanParam, playPlan);
 
        /**
         * 诺瓦定时任务/播放计划新增日志记录开始
         */
        boolean save = save(playPlan);
        String content = "{播放计划:" + playPlan.getId()
                + ", 节目名称:" + byId.getName()
                + ", 播放计划计划名称:"
                + playPlan.getName()
                + ", 播放计划计划内容:"
                + JSON.toJSONString(playPlan)
                + ", 定时任务内容:" + playPlan.getSchedule()
                + "}," + " 推送结果:" + save
                + " }";
        if (playPlanParam.getId() == null) {
            StoreOperationRecordsUtils.storeOperationData(null, null, "诺瓦定时任务/播放计划新增", content);
        } else {
            StoreOperationRecordsUtils.storeOperationData(null, null, "诺瓦定时任务/播放计划修改", content);
        }
        /**
         * 诺瓦定时任务/播放计划新增日志记录结束
         */
 
        return save;
 
    }
 
    public boolean updatePlan(Long id, PlayPlanParam playPlanParam) {
        PlayPlanNv byId = getById(id);
        if (byId == null) {
            throw new BusinessException("操作对象不存在");
        }
        if (removeById(id)) {
            playPlanParam.setId(id);
            return addPlan(playPlanParam);
        }
 
        return false;
 
    }
 
    public boolean deletePlan(List<Long> ids) {
        for (Long id : ids) {
            PlayPlanNv byId = getById(id);
            if (byId == null) {
                throw new BusinessException("部分操作对象不存在");
            }
        }
        return removeByIds(ids);
    }
 
    public Map<String, Object> pushToLed(Long planId, List<NovaPushResultVO> nova) {
        PushToLed pushToLed = playPlanMapper.pushToLed(planId);
        PlayPlanNv byId = getById(planId);
        if (pushToLed == null) {
            throw new BusinessException("未找到该播放计划");
        }
        List<String> playerIds = new ArrayList<>();
        nova.forEach(n -> {
            playerIds.add(n.getPlayerId());
        });
 
        PlayerProgram program = new PlayerProgram();
        program.setPlayerIds(playerIds);
 
        //获取节目实体
        program.setPages(JSON.parseObject(pushToLed.getPages(), List.class));
 
 
        //获取节目定时实体
        program.setSchedule((JSON.parseObject(pushToLed.getSchedule(), Map.class)));
        Map map = JSON.parseObject(pushToLed.getSchedule(), Map.class);
        map.get("plans");
        List<Plans> plans = JSON.parseArray(map.get("plans").toString(), Plans.class);
        plans.forEach(p -> {
            //删除后两位字符串
            p.setStartTime(p.getStartTime().substring(0, p.getStartTime().length() - 3));
            p.setEndTime(p.getEndTime().substring(0, p.getEndTime().length() - 3));
        });
        System.out.println(plans + "plans11111");
        map.put("plans", plans);
        program.setSchedule(map);
 
        program.setNoticeUrl(VnnoxConstant.NOTIFY_URL);
        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.normalProgram(program);
        vnnoxAPIUtil.volChange(playerIds, Integer.valueOf(pushToLed.getVolume()).intValue());
 
        List<String> success = new ArrayList<>();
        List<String> fail = new ArrayList<>();
        if (vnnoxResultResponse.getData() != null) {
            success = vnnoxResultResponse.getData().getSuccess();
            fail = vnnoxResultResponse.getData().getFail();
        }
 
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        nova.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                successList.add(n);
            } else if (finalFail.contains(n.getPlayerId())) {
                faileList.add(n);
            }
        });
 
        result.put("success", successList);
        result.put("fail", faileList);
 
        /**
         * 诺瓦推送节目日志记录开始
         */
        List<LedPlayerEntity> list = SpringContextHolder.getBean(LedPlayerEntityService.class)
                .list(Wrappers.lambdaQuery(LedPlayerEntity.class).in(LedPlayerEntity::getId, nova.stream().map(NovaPushResultVO::getPlayerId).toArray()));
        List<String> listCode = new ArrayList<>();
        for (LedPlayerEntity temp : list) {
            listCode.add(temp.getSn());
        }
 
        String content = "{节目ID:" + planId
                + ", 节目名称:" + byId.getName()
                + "}," + " 推送结果:" + result
                + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "诺瓦推送播放计划", content);
        /**
         * 诺瓦推送节目日志记录结束
         */
 
        return result;
    }
 
 
    //推送定时到LED
    public Map<String, Object> pushSchedule(Long planId, List<NovaPushResultVO> nova) {
        PlayPlanNv one = getOne(Wrappers.lambdaQuery(PlayPlanNv.class).eq(PlayPlanNv::getId, planId));
        if (one == null) {
            throw new BusinessException("未找到节目");
        }
        List<SchedulesDTO> schedulesDTOS = JSON.parseArray(one.getSchedules(), SchedulesDTO.class);
        ProgramSchedule programSchedule = new ProgramSchedule();
        programSchedule.setSchedules(schedulesDTOS);
        List<String> playerIds = new ArrayList<>();
        nova.forEach(
                n -> {
                    playerIds.add(n.getPlayerId());
                }
        );
        //设置
        programSchedule.setPlayerIds(playerIds);
        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.timeProgram(programSchedule);
        List<String> success = new ArrayList<>();
        List<String> fail = new ArrayList<>();
        if (vnnoxResultResponse.getData() != null) {
            success = vnnoxResultResponse.getData().getSuccess();
            fail = vnnoxResultResponse.getData().getFail();
        }
 
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        nova.forEach(
                n -> {
                    if (finalSuccess.contains(n.getPlayerId())) {
                        successList.add(n);
                    } else if (finalFail.contains(n.getPlayerId())) {
                        faileList.add(n);
                    }
                }
 
        );
 
        result.put("success", successList);
        result.put("fail", faileList);
 
        /**
         * 诺瓦推送定时日志记录开始
         */
        List<LedPlayerEntity> list = SpringContextHolder.getBean(LedPlayerEntityService.class)
                .list(Wrappers.lambdaQuery(LedPlayerEntity.class).in(LedPlayerEntity::getId, nova.stream().map(NovaPushResultVO::getPlayerId).toArray()));
        List<String> listCode = new ArrayList<>();
        for (LedPlayerEntity temp : list) {
            listCode.add(temp.getSn());
        }
 
        String content = "{节目ID:" + one.getId()
                + ", 节目名称:" + one.getName()
                + "}," + " 推送结果:" + result
                + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "诺瓦推送定时开关屏", content);
        /**
         * 诺瓦推送定时日志记录结束
         */
        return result;
    }
 
    /**
     * 解析定时
     *
     * @param
     * @return
     */
    public List<Map<String, Object>> parseSchedule(Map map) {
        map.get("startDate");
        map.get("endDate");
        List<PlansVO> plans = (List<PlansVO>) map.get("plans");
 
 
        plans.forEach(
                plan -> {
                    NovaOpenVO Open = new NovaOpenVO();
                    Open.setStartDate(map.get("startDate").toString());
                    Open.setEndDate(map.get("endDate").toString());
                    Open.setExecTime(plan.getStartTime());
                }
        );
        NovaOpenVO Open1 = new NovaOpenVO();
        Open1.setStartDate(map.get("startDate").toString());
        Open1.setEndDate(map.get("endDate").toString());
 
 
        return null;
    }
 
    /**
     * 获取播放计划
     *
     * @param planId
     * @return
     */
    public Object getByPlanId(Long planId) {
        PlayPlanNv byId = getById(planId);
        if (byId == null) {
            throw new BusinessException("找不到该计划");
        }
        LEDProgram byId1 = ledProgramService.getById(byId.getLedProgramId());
        if (byId1 == null) {
            throw new BusinessException("找不到该节目");
        }
        List schedule = JSON.parseObject(byId.getSchedules(), List.class);
        Map schedules = JSON.parseObject(byId.getSchedule(), Map.class);
        List pages = JSON.parseObject(byId1.getPages(), List.class);
        Map plan = new HashMap();
        plan.put("planId", byId.getId());
        plan.put("planName", byId.getName());
        plan.put("programId", byId1.getId());
        plan.put("programName", byId1.getName());
        plan.put("planVolume", byId.getVolume());
        plan.put("preview", byId1.getPreview());
        plan.put("pages", pages);
        plan.put("schedule", schedule);
        plan.put("schedules", schedules);
        return plan;
    }
}