2021与蓝度共同重构项目,服务端
liuhaonan
2022-08-04 681d31e1de290eebd5f5ecb0a0e07a2a66cd01ae
ximon-admin/src/main/java/com/sandu/ximon/admin/service/PlayPlanNvService.java
@@ -1,31 +1,33 @@
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.*;
import com.sandu.ximon.admin.param.PlayPlanParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.LogUtils;
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.request.PlayerProgram;
import com.sandu.ximon.admin.utils.response.VnnoxResult;
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.domain.*;
import com.sandu.ximon.dao.mapper.PlayPlanNvMapper;
import lombok.AllArgsConstructor;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 播放计划
@@ -40,6 +42,7 @@
    private VnnoxAPIUtil vnnoxAPIUtil;
    private final LEDProgramService ledProgramService;
    private final ClientService clientService;
    private final LEDProgramFileService fileService;
    public boolean addPlan(PlayPlanParam playPlanParam) {
@@ -62,13 +65,80 @@
        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.getSchedules()
                + "}," + " 推送结果:" + save
                + " }";
        if (playPlanParam.getId() == null) {
            StoreOperationRecordsUtils.storeOperationData(null, null, "诺瓦定时任务/播放计划新增", content);
        } else {
            StoreOperationRecordsUtils.storeOperationData(null, null, "诺瓦定时任务/播放计划修改", content);
        }
        /**
         * 诺瓦定时任务/播放计划新增日志记录结束
         */
        return save(playPlan);
        return save;
    }
@@ -77,21 +147,12 @@
        if (byId == null) {
            throw new BusinessException("操作对象不存在");
        }
        PlayPlanNv playPlan = new PlayPlanNv();
        playPlan.setId(id);
        playPlan.setName(playPlanParam.getName());
        playPlan.setLedProgramName(playPlanParam.getLedProgramName());
        playPlan.setLedProgramId(playPlanParam.getLedProgramId());
        playPlan.setVolume(playPlanParam.getVolume());
        playPlan.setApplySeries(playPlanParam.getApplySeries());
        playPlan.setStartDate(playPlanParam.getStartDate());
        playPlan.setEndDate(playPlanParam.getEndDate());
        playPlan.setWeekDays(playPlanParam.getWeekDays());
        playPlan.setSchedule(JSON.toJSONString(playPlanParam.getSchedule()));
        playPlan.setStartTime(playPlanParam.getStartTime());
        playPlan.setEndTime(playPlanParam.getEndTime());
        if (removeById(id)) {
            playPlanParam.setId(id);
            return addPlan(playPlanParam);
        }
        return updateById(playPlan);
        return false;
    }
@@ -105,29 +166,76 @@
        return removeByIds(ids);
    }
    public Map<String, Object> pushToLed(Long planId, List<String> playerIds) {
//    public PlayerProgram pushToLed(Long planId){
    /**
     * 推送定时节目
     *
     * @param planId
     * @param nova
     * @return
     */
    public Map<String, Object> pushToLed(Long planId, List<NovaPushResultVO> nova) {
        if (nova == null) {
            throw new BusinessException("请选择正确的LED屏");
        }
        PushToLed pushToLed = playPlanMapper.pushToLed(planId);
        PlayPlanNv one = getOne(Wrappers.lambdaQuery(PlayPlanNv.class).eq(PlayPlanNv::getId, planId));
        if (pushToLed.getPages() == null) {
            throw new BusinessException("节目内容丢失");
        }
        PlayPlanNv byId = getById(planId);
        if (pushToLed == null) {
            throw new BusinessException("未找到该播放计划");
        }
        //List<String> playerIds=new ArrayList<>();
        // playerIds.add("872bb51ae0f06e70c21e913cf3dc9e4d");
        //List<Map<String,Object>> pages = JSON.parseObject(pushToLed.getPages(), List.class);
        List<String> playerIds = new ArrayList<>();
        nova.forEach(n -> {
            playerIds.add(n.getPlayerId());
        });
        PlayerProgram program = new PlayerProgram();
        program.setPlayerIds(playerIds);
        System.out.println(pushToLed.getPages());
        program.setPages(JSON.parseObject(pushToLed.getPages(), List.class));
        program.setSchedules(JSON.parseObject(pushToLed.getSchedule(), List.class));
        Map map = JSON.parseObject(pushToLed.getSchedule(), Map.class);
        //获取节目实体
        List<PlayerPage> programDtos = JSON.parseArray(pushToLed.getPages(), PlayerPage.class);
        programDtos.forEach(
                programDto -> {
                    programDto.getWidgets().forEach(
                            widget -> {
                                if (!PlayerWidgetType.WEATHER.equals(widget.getType())) {
                                    String url = widget.getUrl();
                                    LEDProgramFile one = fileService.getOne(Wrappers.lambdaQuery(LEDProgramFile.class).eq(LEDProgramFile::getFileUrl, url));
                                    if (one == null) {
                                        throw new BusinessException("节目文件已失效");
                                    }
                                    widget.setMd5(one.getMd5());
                                    widget.setSize(Long.parseLong(one.getSize()));
                                }
                            }
                    );
                }
        );
        program.setPages(programDtos);
        //获取节目定时实体
        PlayerSchedule playerSchedule = JSON.parseObject(pushToLed.getSchedule(), PlayerSchedule.class);
        //删除时间的后三位
        List<PlayerSchedulePlans> plans = playerSchedule.getPlans();
        plans.forEach(
                plan -> {
                    plan.setStartTime(plan.getStartTime().substring(0, plan.getStartTime().length() - 3));
                    plan.setEndTime(plan.getEndTime().substring(0, plan.getEndTime().length() - 3));
                }
        );
        program.setSchedule(playerSchedule);
        program.setNoticeUrl(VnnoxConstant.NOTIFY_URL);
        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.timeProgram(program);
        VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange(playerIds, Integer.valueOf(one.getVolume()).intValue());
        LogUtils.error("节目内容+++++++++++" + JSON.toJSONString(program));
        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.normalProgram(program);
        vnnoxAPIUtil.volChange(playerIds, Integer.valueOf(pushToLed.getVolume()).intValue());
        // VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.normalProgram(push);
        List<String> success = new ArrayList<>();
        List<String> fail = new ArrayList<>();
        if (vnnoxResultResponse.getData() != null) {
@@ -135,26 +243,110 @@
            fail = vnnoxResultResponse.getData().getFail();
        }
        // fail.addAll(vnnoxResult.getFail());
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<LedPlayerEntity> successList = new ArrayList<>();
        List<LedPlayerEntity> faileList = new ArrayList<>();
        for (String playerId : success) {
            successList.add(ledPlayerEntityService.getByPlayerId(playerId));
        }
        for (String playerId : fail) {
            faileList.add(ledPlayerEntityService.getByPlayerId(playerId));
        }
        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);
        result.put("program", program);
        /**
         * 诺瓦推送节目日志记录开始
         */
        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;
        // return program;
    }
    //推送定时到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;
    }
    /**
@@ -185,6 +377,12 @@
        return null;
    }
    /**
     * 获取播放计划
     *
     * @param planId
     * @return
     */
    public Object getByPlanId(Long planId) {
        PlayPlanNv byId = getById(planId);
        if (byId == null) {
@@ -194,7 +392,8 @@
        if (byId1 == null) {
            throw new BusinessException("找不到该节目");
        }
        Map schedule = JSON.parseObject(byId.getSchedule(), Map.class);
        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());
@@ -205,6 +404,113 @@
        plan.put("preview", byId1.getPreview());
        plan.put("pages", pages);
        plan.put("schedule", schedule);
        plan.put("schedules", schedules);
        return plan;
    }
    /**
     * 推送实时节目
     *
     * @param pid
     * @param nova
     * @return
     */
    public Map<String, Object> pushProgram(Long pid, List<NovaPushResultVO> nova) {
        LEDProgram ledProgram = ledProgramService.getById(pid);
        if (ledProgram == null) {
            throw new BusinessException("未找到节目");
        }
        if (ledProgram.getPages() == null) {
            throw new BusinessException("节目内容丢失");
        }
        if (SecurityUtils.getClientId() != null && !Objects.equals(ledProgram.getClientId(), SecurityUtils.getUserId()) && !Objects.equals(ledProgram.getClientId(), SecurityUtils.getClientId())) {
            throw new BusinessException("请不要操作其他人的节目");
        }
        String pages = ledProgram.getPages();
        List<String> playerIds = new ArrayList<>();
        nova.forEach(n -> {
            playerIds.add(n.getPlayerId());
        });
        PlayerProgram program = new PlayerProgram();
        List<PlayerPage> programDtos = JSON.parseArray(pages, PlayerPage.class);
        programDtos.forEach(
                programDto -> {
                    programDto.getWidgets().forEach(
                            widget -> {
                                if (!PlayerWidgetType.WEATHER.equals(widget.getType())) {
                                    String url = widget.getUrl();
                                    LEDProgramFile one = fileService.getOne(Wrappers.lambdaQuery(LEDProgramFile.class).eq(LEDProgramFile::getFileUrl, url));
                                    if (one == null) {
                                        throw new BusinessException("节目文件已失效");
                                    }
                                    widget.setMd5(one.getMd5());
                                    widget.setSize(Long.parseLong(one.getSize()));
                                }else {
                                    //WratherPage
                                    System.out.println(widget+"widget");
                                }
                            }
                    );
                }
        );
        program.setPlayerIds(playerIds);
        //获取节目实体
        program.setPages(programDtos);
        program.setNoticeUrl(VnnoxConstant.NOTIFY_URL);
        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.pushProgram(program);
        if (vnnoxResultResponse == null || vnnoxResultResponse.getData() == null) {
            throw new BusinessException("推送失败");
        }
        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:" + pid
                + ", 节目名称:" + ledProgram.getName()
                + "}," + " 推送结果:" + result
                + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "诺瓦推送节目", content);
        /**
         * 诺瓦推送节目日志记录结束
         */
        return result;
    }
}