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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newfiber.api.core.utils.NumberUtils;
import com.newfiber.api.pc.dao.DeviceAlarmMapper;
import com.newfiber.api.pc.dao.PondingDeviceMapper;
import com.newfiber.api.pc.dao.newmonitoring.StationBaseDao;
import com.newfiber.api.pc.model.entity.*;
import com.newfiber.api.pc.model.entity.DeviceAlarm;
import com.newfiber.api.pc.model.monitor.StationBase;
import com.newfiber.api.pc.model.zhz.*;
import com.newfiber.api.pc.service.DeviceAlarmService;
import com.newfiber.api.core.utils.DateUtil;
import com.newfiber.api.core.utils.DateUtils;
import com.newfiber.api.core.utils.StringUtil;
import com.newfiber.api.pc.model.monitor.Sl6512014;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DeviceAlarmServiceImpl extends ServiceImpl<DeviceAlarmMapper, DeviceAlarm> implements DeviceAlarmService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DeviceAlarmMapper deviceAlarmMapper;

    @Autowired
    private PondingDeviceMapper pondingDeviceMapper;

    @Autowired
    private StationBaseDao stationBaseDao;



    @Override
    public PageInfo<DeviceAlarm> selectList(String alarmType,String address,Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        EntityWrapper<DeviceAlarm> wrapper = new EntityWrapper<>();
        if(!StringUtil.isEmpty(alarmType)){
            wrapper.in("alarm_type",alarmType);
        }
        if(!StringUtil.isEmpty(address)){
            wrapper.like("address",address);
        }
        wrapper.eq("status",1);
        wrapper.orderBy("record_time",false);
        Date startTime = DateUtils.getDayBegin();
        Date endTime = DateUtils.getDayEnd();
        /*wrapper.gt("create_time",startTime);
        wrapper.lt("create_time",endTime);*/
        List<DeviceAlarm> result =  deviceAlarmMapper.selectList(wrapper);
        PageInfo<DeviceAlarm> pageInfo = new PageInfo<>();
        if(!result.isEmpty()){
            pageInfo = new PageInfo<DeviceAlarm>(result);
        }
        return pageInfo;
    }

    @Override
    public List<DeviceAlarmCountDto> selectCount() {
        Date startTime = DateUtils.getDayBegin();
        Date endTime = DateUtils.getDayEnd();
        return deviceAlarmMapper.selectAlarmCount(startTime,endTime);
    }

    @Override
    public PageInfo<DeviceAlarm> selectAlarmList(String alarmType, String address, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        EntityWrapper<DeviceAlarm> wrapper = new EntityWrapper<>();
        if(!StringUtil.isEmpty(alarmType)){
            wrapper.in("alarm_type",alarmType);
        }
        if(!StringUtil.isEmpty(address)){
            wrapper.like("address",address);
        }
        wrapper.orderBy("record_time",false);
        List<DeviceAlarm> result =  deviceAlarmMapper.selectList(wrapper);
        PageInfo<DeviceAlarm> pageInfo = new PageInfo<>();
        if(!result.isEmpty()){
            pageInfo = new PageInfo<DeviceAlarm>(result);
        }
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int udpdate(String id) {
        boolean b = deviceAlarmMapper.updateDeviceById(id);
        if(b){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public List<PondingDeviceVo> selectDeviceList() {
        List<PondingDevice> pondingDevices = pondingDeviceMapper.selectAll();
        List<PondingDeviceVo> result= new ArrayList<>();
        for (PondingDevice deviceId : pondingDevices) {

            Query query = new Query();
            query.with(Sort.by(Sort.Direction.DESC,"tt"));
            Sl6512014 dto = mongoTemplate.findOne(query, Sl6512014.class,deviceId.getDeviceId());
            PondingDeviceVo vo = new PondingDeviceVo();
            BeanUtils.copyProperties(deviceId,vo);
            if(dto!=null){
                BeanUtils.copyProperties(dto,vo);
            }
            vo.setDevice_id(vo.getDeviceId());
            if(!"正常".equalsIgnoreCase(vo.getWater_level_off())
                    || !"正常".equalsIgnoreCase(vo.getWater_level1_warn()) || !"正常".equalsIgnoreCase(vo.getWater_level2_warn())
                    || !"正常".equalsIgnoreCase(vo.getWater_level3_warn()) || !"正常".equalsIgnoreCase(vo.getPower_warn())
                    || !"正常通行".equalsIgnoreCase(vo.getCurrent_state())){
                vo.setPondingState(2);
                if(!"全部正常".equalsIgnoreCase(vo.getDevice_state())){
                    vo.setPondingState(1);
                }
            }else{
                vo.setPondingState(0);
                if(!"全部正常".equalsIgnoreCase(vo.getDevice_state())){
                    vo.setPondingState(1);
                }
            }
            if("报警".equalsIgnoreCase(vo.getWater_level3_warn())){
                vo.setLevel("III");
            }else if("报警".equalsIgnoreCase(vo.getWater_level2_warn())){
                vo.setLevel("II");
            }else if("报警".equalsIgnoreCase(vo.getWater_level1_warn())){
                vo.setLevel("I");
            }else{
                vo.setLevel("无");
            }
            vo.setWater_depth(NumberUtils.keepPrecision(dto.getZ().doubleValue()*100.0,2));
            vo.setRecord_time(dto.getTt());
            StationBase site= stationBaseDao.selectByStCode(deviceId.getDeviceId());
            if(null!=site){
                vo.setPondingState(site.getStatus());
            }
            result.add(vo);
        }
        return result;
    }

    @Override
    public Map<String, Object> getHistoryData(String deviceId, String startTime, String endTime) {
        String deviceTable = deviceId;
        //组装query条件
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Query query = new Query();
        if(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
            query = new Query(Criteria.where("tt").gte(cn.hutool.core.date.DateUtil.parse(startTime)).lt(cn.hutool.core.date.DateUtil.parse(endTime))).with(Sort.by(Sort.Direction.DESC,"tt"));
        }else{
            Calendar instance = Calendar.getInstance();
            instance.set(instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DATE),0,0,0);
            Date startTimew = instance.getTime();
            instance.set(instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DATE),23,59,59);
            Date endTimew = instance.getTime();
            query = new Query(Criteria.where("tt").gte(startTimew).lt(endTimew)).with(Sort.by(Sort.Direction.DESC,"tt"));
        }
        List<Sl6512014> iotObjects = mongoTemplate.find(query, Sl6512014.class,deviceTable);
        Map<String,Object> map = new HashMap<>();
        List<String> datas = new ArrayList<>();
        List<Double> values = new ArrayList<>();
        for (Sl6512014 iotObject : iotObjects) {
            datas.add(format.format(iotObject.getTt()));
            values.add(NumberUtils.keepPrecision(iotObject.getZ().doubleValue()*100,2));
        }
        Collections.reverse(datas);
        Collections.reverse(values);

        PondingDevice pondingDevices = pondingDeviceMapper.selectOne(deviceId);
        if(pondingDevices!=null){
            map.put("name",pondingDevices.getDeviceName());
        }
        map.put("time",datas);
        map.put("value",values);
        return map;
    }

    /**
     * 获取查询表
     * @param deviceId
     * @param year
     * @return
     */
    public String getDeviceTable(String deviceId,String year){
        if(StringUtil.isEmpty(deviceId) || StringUtil.isEmpty(year)){
            return "";
        }
        StringBuffer sb = new StringBuffer();
        sb.append("device_000").append(deviceId).append("_").append(year);
        return sb.toString();
    }
}