Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / AutomaticRealtimeDataDisplayServiceImpl.java
package com.newfiber.api.pc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.pc.dao.AutomaticRealtimeDataDisplayMapper;
import com.newfiber.api.pc.model.vo.InstantaneousFlowVo;
import com.newfiber.api.pc.model.vo.PumpFiveSecDataFatherVo;
import com.newfiber.api.pc.model.vo.PumpFiveSecDataVo;
import com.newfiber.api.pc.model.zhz.AutomaticRealtimeDataDisplay;
import com.newfiber.api.pc.model.zhz.PumpStationInfo;
import com.newfiber.api.pc.service.AutomaticRealtimeDataDisplayService;
import com.newfiber.api.pc.service.PumpStationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 自控实时数据展示
 * @description: TODO
 * @author: 张鸿志
 * @date: 2020/12/21 10:51
 * @version: v1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AutomaticRealtimeDataDisplayServiceImpl extends ServiceImpl<AutomaticRealtimeDataDisplayMapper,AutomaticRealtimeDataDisplay> implements AutomaticRealtimeDataDisplayService {

    private static final DecimalFormat format = new DecimalFormat("0.00");
    @Autowired
    private AutomaticRealtimeDataDisplayMapper automaticRealtimeDataDisplayMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private PumpStationInfoService pumpStationInfoService;

    private  Random random = new Random();
    @Override
    public InstantaneousFlowVo queryAutomaticRealtimeDataDisplay(String pumpStationNo) {
        if(StringUtils.isEmpty(pumpStationNo)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        //获取当前泵站当天的所有数据
        List<AutomaticRealtimeDataDisplay> list =  automaticRealtimeDataDisplayMapper.queryAutomaticRealtimeDataDisplay(pumpStationNo);
        List<String> timeList = new ArrayList<>();
        List<BigDecimal> waterList = new ArrayList<>();
        List<BigDecimal> flowList = new ArrayList<>();
        Calendar instance = Calendar.getInstance();
        for(AutomaticRealtimeDataDisplay display : list){
            Date updateTime = display.getUpdateTime();
            instance.setTime(updateTime);
            timeList.add(instance.get(Calendar.HOUR_OF_DAY)+":"+instance.get(Calendar.MINUTE));
            waterList.add(display.getFrontPoolLiquid());
            flowList.add(display.getInstantaneousFlow());
        }

        Collections.reverse(timeList);
        Collections.reverse(waterList);
        Collections.reverse(flowList);
        return new InstantaneousFlowVo(timeList,waterList,flowList);
    }


    @Override
    public PumpFiveSecDataFatherVo findPumpDataSimlate(String pumpStationNo) {
        //首先删除key(模拟操作是,不需要记录之前的电压电流等)
        redisTemplate.delete(pumpStationNo);
        BoundListOperations<String, Object>  listOps = redisTemplate.boundListOps(pumpStationNo);
        List<PumpFiveSecDataVo> list = new ArrayList<>();
        //1、判断传入进来的泵站编号Redis中是否已经有缓存了
        //2、如果Redis中没有,就将他底下的所有泵机查出
        EntityWrapper<PumpStationInfo> wrapper = new EntityWrapper<>();
        wrapper.eq("station_code",pumpStationNo);
        List<PumpStationInfo> stationInfos = pumpStationInfoService.selectList(wrapper);
        for(PumpStationInfo info : stationInfos){
            PumpFiveSecDataVo pumpFiveSecDataVo = new PumpFiveSecDataVo();
            pumpFiveSecDataVo.setPumpNo(info.getPumpCode());
            pumpFiveSecDataVo.setPumpName(info.getPumpName());
            pumpFiveSecDataVo.setModel(1);
            pumpFiveSecDataVo.setState(1);
            //设置电流
            pumpFiveSecDataVo.setElectric(returnElectric(null));
            //设置电压
            pumpFiveSecDataVo.setVoltage(returnVoltage(null));
            //设置操作时间
            pumpFiveSecDataVo.setMonitoringTime(new Date());
            //将数据循环写入Redis中
            listOps.leftPush(pumpFiveSecDataVo);
            list.add(pumpFiveSecDataVo);
        }

        PumpFiveSecDataFatherVo vo = new PumpFiveSecDataFatherVo();
        vo.setVos(list);
        vo.setPoolLevel(get().add(new BigDecimal(45)));
        return vo;
    }

    /**
     * @Description: 方法是:返回电流数据(按一定的规则,增加多少或减少多少)
     * @param:
     * @return:
     * @author: 张鸿志
     * @data: 2020/12/24 14:04
     */
    private BigDecimal returnElectric(BigDecimal electric){
        //如果为空,就设定电流为100-105之间的数据
        if(StringUtils.isEmpty(electric)){
            return new BigDecimal(format.format(Math.random()+ 100));
        }
        if(random.nextInt(10) % 2 == 0){
            return electric.add(get());
        }else{
            return electric.subtract(get());
        }
    }

    /**
     * @Description: 方法是:获取一个随机数生成的bigDecimal;
     * @param:
     * @return:
     * @author: 张鸿志
     * @data: 2020/12/24 14:18
     */
    private BigDecimal get(){
        return new BigDecimal(format.format(Math.random()));
    }

    /**
     * @Description: 方法是:返回电压数据,如果为空,就设置215左右的数据
     * @param:
     * @return:
     * @author: 张鸿志
     * @data: 2020/12/25 11:20
     */
    private BigDecimal returnVoltage(BigDecimal voltage){
        //如果为空,就设定电流为100-105之间的数据
        if(StringUtils.isEmpty(voltage)){
            return new BigDecimal(format.format(Math.random()+ 215));
        }
        if(random.nextInt(10) % 2 == 0){
            return voltage.add(get());
        }else{
            return voltage.subtract(get());
        }
    }





}