package com.moral.api.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.HistoryMonthlyMapper;
import com.moral.api.mapper.ServicesScopeDeviceMapper;
import com.moral.api.mapper.ServicesScopeMapper;
import com.moral.api.service.HistoryMonthlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.api.utils.GetCenterPointFromListOfCoordinates;
import com.moral.constant.Constants;
import com.moral.util.PollutantUtils;
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.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
/**
 * 
 * 月数据 服务实现类
 * 
 *
 * @author moral
 * @since 2021-07-20
 */
@Service
public class HistoryMonthlyServiceImpl extends ServiceImpl implements HistoryMonthlyService {
    @Autowired
    HistoryMonthlyMapper historyMonthlyMapper;
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private ServicesScopeMapper servicesScopeMapper;
    @Autowired
    private ServicesScopeDeviceMapper servicesScopeDeviceMapper;
    @Override
    public HistoryMonthly getHistoryMonthlyByMacAndDate(String mac, Date date) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("mac", mac);
        wrapper.eq("time", date);
        List historyMonthlies = historyMonthlyMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(historyMonthlies))
            return null;
        return historyMonthlies.get(0);
    }
    @Override
    public Map getHistoryMonthlyByMacsAndDate(List mac, Date date) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.in("mac", mac);
        wrapper.eq("time", date);
        List historyMonthlies = historyMonthlyMapper.selectList(wrapper);
        Map map = new HashMap<>();
        for (HistoryMonthly historyMonthly : historyMonthlies) {
            map.put(historyMonthly.getMac(), historyMonthly);
        }
        return map;
    }
    @Override
    public Map getThermodynamicDiagramDataByOrgIdSensorCodeTime(Map parameters) {
        Map resultMap = new HashMap<>();
        int orgId = Integer.parseInt(parameters.get("organization_id").toString());
        //定义一个集合,存放所有id
        List allOrgId = new ArrayList<>();
        allOrgId.add(orgId);
        //循环集合
        //所有子组织
        List allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId);
        if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1) {
            for (Organization organization : allChildrenOrganization) {
                allOrgId.add(organization.getId());
            }
        }
        //集合去重
        List allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList());
        //声明一个list,存放设备mac
        List deviceMacList = new ArrayList<>();
        //声明一个map,Mac作为key,device作为value
        Map deviceMap = new HashMap<>();
        List longitudeList = new ArrayList<>();
        List latitudeList = new ArrayList<>();
        for (Integer orgIdWithoutDuplicates : allOrgIdWithoutDuplicates) {
            //根据id查询所属设备
            QueryWrapper wrapper_device = new QueryWrapper<>();
            wrapper_device.eq("is_delete", Constants.NOT_DELETE).eq("organization_id", orgIdWithoutDuplicates);
            List devices = new ArrayList<>();
            devices = deviceMapper.selectList(wrapper_device);
            if (devices.size() > 0) {
                for (Device device : devices) {
                    String mac = device.getMac();
                    deviceMacList.add(mac);
                    deviceMap.put(mac, device);
                    double longitude = device.getLongitude();
                    double latitude = device.getLatitude();
                    longitudeList.add(longitude);
                    latitudeList.add(latitude);
                }
            } else {
                continue;
            }
        }
        //获取时间
        String time = parameters.get("time").toString().substring(0, 7) + "-01 00:00:00";
        resultMap.put("time", time);
        QueryWrapper historyMonthlyQueryWrapper = new QueryWrapper<>();
        historyMonthlyQueryWrapper.eq("time", time);
        historyMonthlyQueryWrapper.in("mac", deviceMacList);
        List historyDailies = historyMonthlyMapper.selectList(historyMonthlyQueryWrapper);
        List