2021与蓝度共同重构项目,服务端
liuhaonan
2022-05-07 14ce780fb9977acad2b6dabf9033635b076f590b
ximon-admin/src/main/java/com/sandu/ximon/admin/service/PlayPlanNvService.java
@@ -5,21 +5,29 @@
import com.sandu.common.execption.BusinessException;
import com.sandu.common.service.impl.BaseServiceImpl;
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.param.PlayPlanParam;
import com.sandu.ximon.admin.security.SecurityUtils;
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.dto.nova.PlayerProgram;
import com.sandu.ximon.admin.utils.response.VnnoxResult;
import com.sandu.ximon.admin.utils.response.VnnoxResultResponse;
import com.sandu.ximon.dao.domain.*;
import com.sandu.ximon.dao.enums.AdministratorEnums;
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.PlayPlanNv;
import com.sandu.ximon.dao.domain.PushToLed;
import com.sandu.ximon.dao.mapper.PlayPlanNvMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 播放计划
@@ -33,17 +41,21 @@
    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 (AdministratorEnums.CUSTOMER.getCode().equals(SecurityUtils.getAdministratorIdentity())) {
        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){
        if (byId == null) {
            throw new BusinessException("ledProgramId有误,未找到该节目");
        }
        playPlan.setLedProgramId(playPlanParam.getLedProgramId());
@@ -52,10 +64,50 @@
        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();
//            if (i % 2 == 0) {
//                schedulesDTO.setExecTime(plans.get(i).getStartTime());
//                schedulesDTO.setStatus("OPEN");
//            } else {
//                schedulesDTO.setWeekDays(plans.get(i).getWeekDays());
//                schedulesDTO.setExecTime(plans.get(i).getEndTime());
//                schedulesDTO.setStatus("CLOSE");
//            }
            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");
            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);
        //  BeanUtils.copyProperties(playPlanParam, playPlan);
        return save(playPlan);
@@ -67,26 +119,16 @@
        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)){
            return addPlan(playPlanParam);
        }
        return updateById(playPlan);
        return false;
    }
    public boolean deletePlan(List<Long> ids) {
        for(Long id:ids){
        for (Long id : ids) {
            PlayPlanNv byId = getById(id);
            if (byId == null) {
                throw new BusinessException("部分操作对象不存在");
@@ -95,69 +137,120 @@
        return removeByIds(ids);
    }
    public Map<String, Object> pushToLed(Long planId,List<String> playerIds){
    public Map<String, Object> pushToLed(Long planId, List<NovaPushResultVO> nova) {
//    public PlayerProgram pushToLed(Long planId){
        PushToLed pushToLed = playPlanMapper.pushToLed(planId);
        PlayPlanNv one =getOne(Wrappers.lambdaQuery(PlayPlanNv.class).eq(PlayPlanNv::getId, planId));
        if(pushToLed==null){
        PlayPlanNv one = getOne(Wrappers.lambdaQuery(PlayPlanNv.class).eq(PlayPlanNv::getId, 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);
        PlayerProgram program=new PlayerProgram();
        List<String> playerIds=new ArrayList<>();
        nova.forEach(
                n->{
                    playerIds.add(n.getPlayerId());
                }
        );
        PlayerProgram program = new PlayerProgram();
        program.setPlayerIds(playerIds);
        System.out.println(pushToLed.getPages());
        System.out.println(JSON.parseArray(pushToLed.getPages(), Map.class));
        //获取节目实体
        program.setPages(JSON.parseObject(pushToLed.getPages(), List.class));
        program.setSchedule(JSON.parseObject(pushToLed.getSchedule(), Map.class));
        //获取节目定时实体
        program.setSchedules((JSON.parseObject(pushToLed.getSchedules(), Map.class)));
        //获取定时开关屏幕
//        program.setSchedules((JSON.parseArray(pushToLed.getSchedule(), SchedulesDTO.class)));
        // Map map = JSON.parseObject(pushToLed.getSchedule(), Map.class);
        program.setNoticeUrl(VnnoxConstant.NOTIFY_URL);
      /*  Map schedule = JSON.parseObject(pushToLed.getSchedule(), Map.class);
        Map<String,Object> push=new HashMap<>();
        push.put("playerIds",playerIds);
        push.put("pages",pages);
        push.put("schedule",schedule);
        push.put("noticeUrl", VnnoxConstant.SCREEN_SHOT_NOTIFY_URL);
*/
//        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.timeProgram(program);
        VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.normalProgram(program);
        VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange(playerIds, Integer.valueOf(one.getVolume()).intValue());
        // VnnoxResultResponse vnnoxResultResponse = vnnoxProgramAPIUtil.normalProgram(push);
        List<String> success=new ArrayList<>();
        List<String> fail= new ArrayList<>();
//        List<String> success=new ArrayList<>();
//        if(vnnoxResultResponse.getData().getSuccess()!=null&&vnnoxResultResponse.getData()!=null){
//            success = vnnoxResultResponse.getData().getSuccess();
//        }
//        //success.addAll(vnnoxResult.getSuccess());
//        if(vnnoxResultResponse.getData().getFail()!=null&vnnoxResultResponse.getData()!=null){
//            fail= vnnoxResultResponse.getData().getFail();
//        }
        if(vnnoxResultResponse.getData()!=null){
        List<String> success = new ArrayList<>();
        List<String> fail = new ArrayList<>();
        if (vnnoxResultResponse.getData() != null) {
            success = vnnoxResultResponse.getData().getSuccess();
            fail= vnnoxResultResponse.getData().getFail();
            fail = vnnoxResultResponse.getData().getFail();
        }
        // fail.addAll(vnnoxResult.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);
                    }
                }
       // 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));
        }
        );
        result.put("success",successList);
        result.put("fail",faileList);
        result.put("success", successList);
        result.put("fail", faileList);
        return result;
       // return program;
        // return program;
    }
    /**
     * 解析定时
     *
     * @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;
    }
    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.getSchedule(), List.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);
        return plan;
    }
}