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.moral.api.entity.*; 
 | 
import com.moral.api.mapper.*; 
 | 
import com.moral.api.service.*; 
 | 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
 | 
import com.moral.constant.Constants; 
 | 
import com.moral.util.*; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.util.ObjectUtils; 
 | 
  
 | 
import java.text.ParseException; 
 | 
import java.text.SimpleDateFormat; 
 | 
import java.util.*; 
 | 
import java.util.stream.Collectors; 
 | 
  
 | 
/** 
 | 
 * <p> 
 | 
 *  服务实现类 
 | 
 * </p> 
 | 
 * 
 | 
 * @author moral 
 | 
 * @since 2022-01-19 
 | 
 */ 
 | 
@Service 
 | 
public class ServicesScopeServiceImpl extends ServiceImpl<ServicesScopeMapper, ServicesScope> implements ServicesScopeService { 
 | 
  
 | 
    @Autowired 
 | 
    private OrganizationMapper organizationMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private ServicesScopeMapper servicesScopeMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private OrganizationService organizationService; 
 | 
  
 | 
    @Autowired 
 | 
    private ServicesScopeDeviceMapper servicesScopeDeviceMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private DeviceMapper deviceMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private HistoryMonthlyMapper historyMonthlyMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private HistoryDailyMapper historyDailyMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private HistoryHourlyMapper historyHourlyMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private HistoryHourlyService historyHourlyService; 
 | 
  
 | 
    @Override 
 | 
    public List<Map<String, Object>> getDateByOrgIdAndCondition(Map map) { 
 | 
        int orgId = Integer.parseInt(map.get("organization_id").toString()); 
 | 
        //定义一个集合,存放所有id 
 | 
        List<Integer> allOrgId = new ArrayList<>(); 
 | 
        allOrgId.add(orgId); 
 | 
        //循环集合 
 | 
        //所有子组织 
 | 
        List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId); 
 | 
        if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){ 
 | 
            for (Organization organization:allChildrenOrganization) { 
 | 
                allOrgId.add(organization.getId()); 
 | 
            } 
 | 
        } 
 | 
        //集合去重 
 | 
        List<Integer> allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList()); 
 | 
        List<Map<String,Object>> resultList = new ArrayList<>(); 
 | 
        for (int organizationId:allOrgIdWithoutDuplicates) { 
 | 
            Map<String,Object> resultMap = new HashMap<>(); 
 | 
            QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>(); 
 | 
            organizationQueryWrapper.eq("is_delete",Constants.NOT_DELETE); 
 | 
            organizationQueryWrapper.eq("id",organizationId); 
 | 
            if (!ObjectUtils.isEmpty(map.get("regionCode"))){ 
 | 
                String region = RegionCodeUtils.regionCodeConvertToName(Integer.parseInt(map.get("regionCode").toString())); 
 | 
                organizationQueryWrapper.eq(region,Integer.parseInt(map.get("regionCode").toString())); 
 | 
            } 
 | 
            Organization organization = new Organization(); 
 | 
            organization = organizationMapper.selectOne(organizationQueryWrapper); 
 | 
            if (ObjectUtils.isEmpty(organization)){ 
 | 
                continue; 
 | 
            } 
 | 
            resultMap.put("id",organization.getId()); 
 | 
            resultMap.put("name",organization.getName()); 
 | 
            QueryWrapper<ServicesScope> servicesScopeQueryWrapper = new QueryWrapper<>(); 
 | 
            servicesScopeQueryWrapper.eq("is_delete",Constants.NOT_DELETE); 
 | 
            servicesScopeQueryWrapper.eq("organization_id",organizationId); 
 | 
            if (!ObjectUtils.isEmpty(map.get("name"))){ 
 | 
                servicesScopeQueryWrapper.like("name",map.get("name").toString()); 
 | 
            } 
 | 
            List<ServicesScope> servicesScopes = new ArrayList<>(); 
 | 
            servicesScopes = servicesScopeMapper.selectList(servicesScopeQueryWrapper); 
 | 
            if (servicesScopes.size()<1){ 
 | 
                continue; 
 | 
            } 
 | 
            List<Map<String,Object>> servicesScopeList= new ArrayList<>(); 
 | 
            for (ServicesScope servicesScope:servicesScopes) { 
 | 
                Map<String,Object> servicesScopeMap = new HashMap<>(); 
 | 
                servicesScopeMap.put("id",servicesScope.getId()); 
 | 
                servicesScopeMap.put("name",servicesScope.getName()); 
 | 
                servicesScopeMap.put("organizationId",servicesScope.getOrganizationId()); 
 | 
                servicesScopeMap.put("centerLongitude",servicesScope.getCenterLongitude()); 
 | 
                servicesScopeMap.put("centerLatitude",servicesScope.getCenterLatitude()); 
 | 
                servicesScopeList.add(servicesScopeMap); 
 | 
            } 
 | 
            resultMap.put("servicesScopes",servicesScopeList); 
 | 
            resultList.add(resultMap); 
 | 
        } 
 | 
        if (resultList.size()<1){ 
 | 
            return null; 
 | 
        } 
 | 
        return resultList; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public List<Map<String, Object>> honeycombDiagram(Integer serviceScopeId,Integer distance,String type,String time,String sensorCode) { 
 | 
        List<Map<String,Object>> resultList = new ArrayList<>(); 
 | 
        ServicesScope servicesScope = servicesScopeMapper.selectById(serviceScopeId); 
 | 
        QueryWrapper<ServicesScopeDevice> servicesScopeDeviceQueryWrapper = new QueryWrapper<>(); 
 | 
        servicesScopeDeviceQueryWrapper.eq("services_scope_id",servicesScope.getId()); 
 | 
        servicesScopeDeviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); 
 | 
        List<ServicesScopeDevice> servicesScopeDevices = servicesScopeDeviceMapper.selectList(servicesScopeDeviceQueryWrapper); 
 | 
        List<Integer> deviceIds = servicesScopeDevices.stream().map(d -> d.getDeviceId()).collect(Collectors.toList()); 
 | 
        QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>(); 
 | 
        deviceQueryWrapper.eq("is_delete",Constants.NOT_DELETE); 
 | 
        deviceQueryWrapper.in("id",deviceIds); 
 | 
        List<Device> devices = deviceMapper.selectList(deviceQueryWrapper); 
 | 
        List<String> deviceMacs = devices.stream().map(d -> d.getMac()).collect(Collectors.toList()); 
 | 
        Map<String,Device> devicesMap = new HashMap<>(); 
 | 
        for (Device device : devices) { 
 | 
            devicesMap.put(device.getMac(), device); 
 | 
        } 
 | 
        Map<String, Map<String,Object>> historyDataMap = new HashMap<>(); 
 | 
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.yyyy_MM_dd_HH_mm_ss_EN); 
 | 
        if (type.equals("monthly")){ 
 | 
            Date monthly_time = DateUtils.getDate(time+"-01 00:00:00",DateUtils.yyyy_MM_dd_HH_mm_ss_EN); 
 | 
            QueryWrapper<HistoryMonthly> wrapper = new QueryWrapper<>(); 
 | 
            wrapper.in("mac", deviceMacs); 
 | 
            wrapper.eq("time", monthly_time); 
 | 
            List<HistoryMonthly> historyMonthlies = historyMonthlyMapper.selectList(wrapper); 
 | 
            for (HistoryMonthly historyMonthly : historyMonthlies) { 
 | 
                historyDataMap.put(historyMonthly.getMac(), JSON.parseObject(JSON.toJSONString(historyMonthly), Map.class)); 
 | 
            } 
 | 
        } 
 | 
        if (type.equals("daily")){ 
 | 
            Date daily_time = DateUtils.getDate(time+" 00:00:00",DateUtils.yyyy_MM_dd_HH_mm_ss_EN); 
 | 
            QueryWrapper<HistoryDaily> wrapper = new QueryWrapper<>(); 
 | 
            wrapper.in("mac", deviceMacs); 
 | 
            wrapper.eq("time", daily_time); 
 | 
            List<HistoryDaily> historyDailies = historyDailyMapper.selectList(wrapper); 
 | 
            for (HistoryDaily historyDaily : historyDailies) { 
 | 
                historyDataMap.put(historyDaily.getMac(), JSON.parseObject(JSON.toJSONString(historyDaily), Map.class)); 
 | 
            } 
 | 
        } 
 | 
        if (type.equals("hourly")){ 
 | 
            Date hourly_time = DateUtils.getDate(time+":00:00",DateUtils.yyyy_MM_dd_HH_mm_ss_EN); 
 | 
            QueryWrapper<HistoryHourly> wrapper = new QueryWrapper<>(); 
 | 
            wrapper.in("mac", deviceMacs); 
 | 
            wrapper.eq("time", hourly_time); 
 | 
            List<HistoryHourly> historyHourlies = historyHourlyService.getHourlyDataByMacs(deviceMacs,time); 
 | 
            for (HistoryHourly historyHourly : historyHourlies) { 
 | 
                historyDataMap.put(historyHourly.getMac(), JSON.parseObject(JSON.toJSONString(historyHourly), Map.class)); 
 | 
            } 
 | 
  
 | 
        } 
 | 
        String boundary = servicesScope.getBoundary(); 
 | 
        String[] lon_lat_arrs = boundary.split(";"); 
 | 
        List<Map<String,Double>> areas=new ArrayList<Map<String,Double>>(); 
 | 
        List<Double> lonList = new ArrayList<>(); 
 | 
        List<Double> latList = new ArrayList<>(); 
 | 
        for (String lon_lat:lon_lat_arrs) { 
 | 
            String[] lonAndLat = lon_lat.split(","); 
 | 
            Map<String, Double> lon_lat_map = new HashMap<String,Double>();//左下 
 | 
            lon_lat_map.put("px", Double.parseDouble(lonAndLat[0])); 
 | 
            lon_lat_map.put("py", Double.parseDouble(lonAndLat[1])); 
 | 
            areas.add(lon_lat_map); 
 | 
            lonList.add(Double.parseDouble(lonAndLat[0])); 
 | 
            latList.add(Double.parseDouble(lonAndLat[1])); 
 | 
        } 
 | 
        Double lonMax = Collections.max(lonList); 
 | 
        Double lonMin = Collections.min(lonList); 
 | 
        Double latMax = Collections.max(latList); 
 | 
        Double latMin = Collections.min(latList); 
 | 
        double lon_getmeter = LongitudeAndLatitudeUtils.getmeter(lonMax, latMin, lonMin, latMin); 
 | 
        Double lon_numberOfCopies = DoubleUtils.div(lon_getmeter, distance.doubleValue(), 0); 
 | 
        double lat_getmeter = LongitudeAndLatitudeUtils.getmeter(lonMax, latMin, lonMax, latMax); 
 | 
        Double lat_numberOfCopies = DoubleUtils.div(lat_getmeter, distance.doubleValue(), 0); 
 | 
        double lon_sub = DoubleUtils.sub(lonMax, lonMin); 
 | 
        double lat_sub = DoubleUtils.sub(latMax, latMin); 
 | 
        Double lon_oneShare = DoubleUtils.div(lon_sub, lon_numberOfCopies,15); 
 | 
        Double lat_ontShare = DoubleUtils.div(lat_sub, lat_numberOfCopies,15); 
 | 
        Double point_lon = lonMin; 
 | 
        for (int i=0;i<lon_numberOfCopies.intValue();i++){ 
 | 
            Double point_lat = latMin; 
 | 
            for (int j=0;j<lat_numberOfCopies.intValue();j++){ 
 | 
                Boolean pointInPolygon = OtgUtils.isPointInPolygon(areas, point_lon, point_lat); 
 | 
                if (pointInPolygon){ 
 | 
                    Map<String,Object> resultMap = new HashMap<>(); 
 | 
                    Double getmeter = 100000000.0; 
 | 
                    String near_mac = ""; 
 | 
                    for(String key:devicesMap.keySet()){ 
 | 
                        Device device = devicesMap.get(key); 
 | 
                        double getmeter1 = LongitudeAndLatitudeUtils.getmeter(point_lon, point_lat, device.getLongitude(), device.getLatitude()); 
 | 
                        if (getmeter1<getmeter){ 
 | 
                            getmeter=getmeter1; 
 | 
                            near_mac = device.getMac(); 
 | 
                        } 
 | 
                    } 
 | 
                    Map<String, Object> map = historyDataMap.get(near_mac); 
 | 
                    if (map != null){ 
 | 
                        String value = map.get("value").toString(); 
 | 
                        JSONObject jsonObject = JSONObject.parseObject(value); 
 | 
                        Double sensorValue = Double.parseDouble(jsonObject.get(sensorCode).toString()); 
 | 
                        resultMap.put("lon",point_lon); 
 | 
                        resultMap.put("lat",point_lat); 
 | 
                        resultMap.put("value",sensorValue); 
 | 
                        resultList.add(resultMap); 
 | 
                    } 
 | 
                } 
 | 
                point_lat = DoubleUtils.add(point_lat,lat_ontShare); 
 | 
            } 
 | 
            point_lon = DoubleUtils.add(point_lon,lon_oneShare); 
 | 
        } 
 | 
        return resultList; 
 | 
    } 
 | 
  
 | 
    /*private OrganizationServicesScopeVO treeStructure(int orgId){ 
 | 
        OrganizationServicesScopeVO organizationServicesScopeVO = new OrganizationServicesScopeVO(); 
 | 
        Organization organization = organizationMapper.selectById(orgId); 
 | 
        organizationServicesScopeVO.setOrganization(organization); 
 | 
        QueryWrapper<Organization> chileOrganizationQueryWrapper = new QueryWrapper<>(); 
 | 
        chileOrganizationQueryWrapper.eq("is_delete",Constants.NOT_DELETE); 
 | 
        chileOrganizationQueryWrapper.eq("parent_id",orgId); 
 | 
        List<Organization> childOrganizations = new ArrayList<>(); 
 | 
        childOrganizations = organizationMapper.selectList(chileOrganizationQueryWrapper); 
 | 
        if (childOrganizations.size()>0){ 
 | 
            List<OrganizationServicesScopeVO> organizationServicesScopeVOS = new ArrayList<>(); 
 | 
            for (Organization childOrganization:childOrganizations) { 
 | 
                OrganizationServicesScopeVO organizationServicesScopeVO1 = treeStructure(childOrganization.getId()); 
 | 
                if (ObjectUtils.isEmpty(organizationServicesScopeVO1)){ 
 | 
                    organizationServicesScopeVOS.add(organizationServicesScopeVO1); 
 | 
                } 
 | 
            } 
 | 
            organizationServicesScopeVO.setOrganizationServicesScopeVOS(organizationServicesScopeVOS); 
 | 
        } 
 | 
        //servicesScopeMapper.selectList() 
 | 
        return null; 
 | 
    }*/ 
 | 
} 
 |