|
|
|
@ -1,31 +1,39 @@ |
|
|
|
package com.spring.modules.cdc.service.impl; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.spring.modules.cdc.entity.Collect; |
|
|
|
import com.spring.modules.cdc.entity.CollectRecord; |
|
|
|
import com.spring.modules.cdc.entity.Device; |
|
|
|
import com.spring.modules.cdc.entity.*; |
|
|
|
import com.spring.modules.cdc.mapper.CollectRecordMapper; |
|
|
|
import com.spring.modules.cdc.service.ICollectRecordService; |
|
|
|
import com.spring.modules.cdc.service.ICollectService; |
|
|
|
import com.spring.modules.cdc.service.*; |
|
|
|
import com.spring.modules.cdc.vo.CollectRecordVo; |
|
|
|
import com.spring.modules.cdc.vo.DeviceVo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
import javax.mail.MessagingException; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.text.DecimalFormat; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
public class CollectRecordServiceImpl extends ServiceImpl<CollectRecordMapper,CollectRecord> implements ICollectRecordService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ICollectService collectService; |
|
|
|
@Autowired |
|
|
|
private ICDCBaseDataService baseDataService; |
|
|
|
@Autowired |
|
|
|
private ICollectDetailService collectDetailService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IMailConfigService mailConfigService; |
|
|
|
@Autowired |
|
|
|
private IMobileService mobileService; |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<CollectRecord> selectCollectRecordList(CollectRecord collectRecord) { |
|
|
|
@ -77,20 +85,73 @@ public class CollectRecordServiceImpl extends ServiceImpl<CollectRecordMapper,Co |
|
|
|
Map<String, List<DeviceVo>> map = deviceVoList.stream().collect(Collectors.groupingBy(DeviceVo::getCollectId)); |
|
|
|
|
|
|
|
Map<String, List<Collect>> collect = collectService.lambdaQuery().in(Collect::getCollectId, map.keySet()).list().stream().collect(Collectors.groupingBy(Collect::getCollectId)); |
|
|
|
// 获取base日期 |
|
|
|
CdcBaseData baseData = baseDataService.lambdaQuery().eq(CdcBaseData::getBaseNo, "last_send_time").one(); |
|
|
|
BigDecimal time = baseData.getNumValue();// 默认为秒 |
|
|
|
for (String collectId : map.keySet()) { |
|
|
|
List<DeviceVo> deviceVos = map.get(collectId); |
|
|
|
List<Collect> collects = collect.get(collectId); |
|
|
|
// 设备信息集合 |
|
|
|
List<CollectRecordVo> list = new ArrayList<>(); |
|
|
|
List<DeviceVo> list = new ArrayList<>(); |
|
|
|
Date lastSendTime = collects.get(0).getLastSendTime(); |
|
|
|
for (DeviceVo deviceVo : deviceVos) { |
|
|
|
CollectRecordVo collectRecord = getCollectRecord(deviceVo, collects, deviceVo.getValue()); |
|
|
|
list.add(collectRecord); |
|
|
|
updateCollectCurrentValue(collectRecord); |
|
|
|
save(collectRecord); |
|
|
|
// 如果没有发送过数据 或者 距离上次发送时间大于设定的时间间隔 |
|
|
|
if (Objects.isNull(lastSendTime) || lastSendTime.before(new Date(System.currentTimeMillis() - time.multiply(new BigDecimal(1000)).longValue()))){ |
|
|
|
// 可以发送短信 |
|
|
|
// 数据校验 需要发送短信的设备 |
|
|
|
// if (deviceVo.getValue().compareTo(collectRecord.getMaxValue()) >= 0 || deviceVo.getValue().compareTo(collectRecord.getMinValue()) <= 0){ |
|
|
|
if (deviceVo.getValue().compareTo(collectRecord.getMaxValue()) >= 0){ |
|
|
|
list.add(deviceVo); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 处理 短信 |
|
|
|
sendMessage(list, collects.get(0)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 处理 邮件 |
|
|
|
private void sendMessage(List<DeviceVo> list, Collect collects) { |
|
|
|
if (!list.isEmpty()){ |
|
|
|
// 发送短信和邮件 |
|
|
|
StringBuilder mobileStr = new StringBuilder("【数据采集】"); |
|
|
|
StringBuilder mailStr = new StringBuilder(); |
|
|
|
mobileStr.append("\n"); |
|
|
|
List<CollectDetail> detailList = collectDetailService.lambdaQuery().eq(CollectDetail::getCollectId, collects.getCollectId()).eq(CollectDetail::getSite, collects.getSite()).eq(CollectDetail::getActive, "Y").list(); |
|
|
|
DecimalFormat df = new DecimalFormat("#.######"); |
|
|
|
mobileStr.append(collects.getCollectDesc()).append(":").append("上限:").append(df.format(collects.getMaxValue().doubleValue())).append("\n"); |
|
|
|
mailStr.append(collects.getCollectDesc()).append(":").append("上限:").append(df.format(collects.getMaxValue().doubleValue())).append("\n"); |
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
DeviceVo deviceVo = list.get(i); |
|
|
|
mobileStr.append(i+1).append("、").append(deviceVo.getDeviceDesc()).append("当前采集值为:").append(deviceVo.getValue()).append("\n"); |
|
|
|
mailStr.append(i+1).append("、").append(deviceVo.getDeviceDesc()).append("当前采集值为:").append(deviceVo.getValue()).append("\n"); |
|
|
|
} |
|
|
|
Map<String, List<CollectDetail>> listMap = detailList.stream().collect(Collectors.groupingBy(CollectDetail::getType)); |
|
|
|
List<CollectDetail> mobileList = listMap.get("phone"); |
|
|
|
List<CollectDetail> mailList = listMap.get("mail"); |
|
|
|
if (Objects.nonNull(mailList) && !mailList.isEmpty() && "Y".equals(collects.getMailInfoFlag())){ |
|
|
|
Set<String> mail = mailList.stream().map(CollectDetail::getItemValue).collect(Collectors.toSet()); |
|
|
|
try { |
|
|
|
mailConfigService.send(mail.toArray(new String[mail.size()]),"数据采集",mailStr.toString(),1); |
|
|
|
} catch (MessagingException | UnsupportedEncodingException e) { |
|
|
|
log.info("发送短信错误!"+e.getMessage()); |
|
|
|
// throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
if (Objects.nonNull(mobileList) && !mobileList.isEmpty() && "Y".equals(collects.getPhoneInfoFlag())){ |
|
|
|
Set<String> mobile = mobileList.stream().map(CollectDetail::getItemValue).collect(Collectors.toSet()); |
|
|
|
for (String s : mobile) { |
|
|
|
mobileService.send(s,mobileStr.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!detailList.isEmpty()){ |
|
|
|
collectService.lambdaUpdate() |
|
|
|
.set(Collect::getLastSendTime, new Date()) |
|
|
|
.eq(Collect::getCollectId, collects.getCollectId()) |
|
|
|
.eq(Collect::getSite, collects.getSite()) |
|
|
|
.update(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|