package com.moral.api.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.entity.*;
import com.moral.api.mapper.HistorySecondUavMapper;
import com.moral.api.pojo.dto.uav.UAVQueryTimeSlotDTO;
import com.moral.api.pojo.enums.SysDictTypeEnum;
import com.moral.api.pojo.form.uav.UAVQueryTimeSlotForm;
import com.moral.api.service.HistorySecondUavService;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SpecialDeviceService;
import com.moral.api.service.SysDictTypeService;
import com.moral.api.utils.UnitConvertUtils;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
import com.moral.util.GeodesyUtils;
import com.moral.util.MathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
/**
*
* 无人机秒数据表 服务实现类
*
*
* @author moral
* @since 2021-08-31
*/
@Service
public class HistorySecondUavServiceImpl extends ServiceImpl implements HistorySecondUavService {
/*
* 筛选数据举例
* */
private Double filterDistance = 2d;
@Autowired
HistorySecondUavMapper historySecondUavMapper;
@Autowired
OrganizationService organizationService;
@Autowired
SpecialDeviceService specialDeviceService;
@Autowired
private SysDictTypeService sysDictTypeService;
@Autowired
RedisTemplate redisTemplate;
@Override
public List queryDate(Integer organizationId) {
//构造查询条件
QueryWrapper queryWrapper = new QueryWrapper<>();
//获取子组织id
List children = organizationService.getChildrenOrganizationsById(organizationId);
List childrenId = new ArrayList<>();
for (Organization child : children) {
childrenId.add(child.getId());
}
childrenId.add(organizationId);
queryWrapper.in("organization_id", childrenId);
//设置查询时间范围为180天
Date endDate = new Date();
Date startDate = DateUtils.addDays(endDate, -180);
queryWrapper.between("batch", startDate, endDate);
//设置查询字段
queryWrapper.select("DISTINCT batch");
queryWrapper.orderByDesc("batch");
//查询结果
List historySecondUavs = historySecondUavMapper.selectList(queryWrapper);
//结果转为Date集合
List result = new ArrayList<>();
for (HistorySecondUav historySecondUav : historySecondUavs) {
result.add(historySecondUav.getBatch());
}
return result;
}
@Override
public List queryTimeSlot(UAVQueryTimeSlotForm form) {
//取参
Integer organizationId = form.getOrganizationId();
List integerList = sysDictTypeService.dateValueList(SysDictTypeEnum.SYS_SECOND_UAV_RANGE.getValue());
Date startDate = form.getStartDate();
Date endDate = form.getEndDate();
QueryWrapper wrapper = new QueryWrapper<>();
//获取子组织id
List children = organizationService.getChildrenOrganizationsById(organizationId);
List childrenId = new ArrayList<>();
for (Organization child : children) {
childrenId.add(child.getId());
}
childrenId.add(organizationId);
childrenId.addAll(integerList);
wrapper.in("organization_id", childrenId);
//查询根据batch查,因为可能会有跨天飞行的情况。
wrapper.between("batch", startDate, endDate);
//设置查询字段
wrapper.select("mac,time,batch");
//查询结果
List historySecondUavs = historySecondUavMapper.selectList(wrapper);
//根据batch进行分批
Map> batchMap = new LinkedHashMap<>();//key为batch的string
for (HistorySecondUav historySecondUav : historySecondUavs) {
//获取batch对应的数据集合
List list = batchMap.get(DateUtils.dateToDateString(historySecondUav.getBatch(), "yyyy-MM-dd HH:mm:ss"));
if (list != null) {
list.add(historySecondUav);
} else {
ArrayList newList = new ArrayList<>();
newList.add(historySecondUav);
batchMap.put(DateUtils.dateToDateString(historySecondUav.getBatch(), "yyyy-MM-dd HH:mm:ss"), newList);
}
}
//去除少于30条数据的集合
batchMap.values().removeIf(new Predicate>() {
@Override
public boolean test(List historySecondUavs) {
if (historySecondUavs.size() <= 30)
return true;
return false;
}
});
//根据mac进行分类
Map>>> macBatchMap = new LinkedHashMap<>();//key为mac
//遍历batchMap将mac提取出来,作为macBatchMap的key,batch和数据map的数据集合放入value
batchMap.forEach((key, value) -> {
String mac = value.get(0).getMac();
List