package com.moral.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.AlarmInfo;
import com.moral.api.entity.Device;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.AlarmInfoMapper;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.service.AlarmInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.OrganizationService;
import com.moral.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 
 *  服务实现类
 * 
 *
 * @author moral
 * @since 2021-11-10
 */
@Service
public class AlarmInfoServiceImpl extends ServiceImpl implements AlarmInfoService {
    @Autowired
    private OrganizationService organizationService;
    @Autowired(required = false)
    private DeviceMapper deviceMapper;
    @Autowired(required = false)
    private AlarmInfoMapper alarmInfoMapper;
    @Override
    public Map getDataByCondition(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,存放设备id
        List deviceIdList = 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) {
                    int deviceId = device.getId();
                    deviceIdList.add(deviceId);
                }
            }else {
                continue;
            }
        }
        if (deviceIdList.size()>0){
            Map map = new HashMap<>();
            QueryWrapper alarmInfoQueryWrapper = new QueryWrapper<>();
            map.put("deviceIds",deviceIdList);
            alarmInfoQueryWrapper.in("device_id",deviceIdList);
            int size = Integer.parseInt(parameters.get("size").toString());
            int current = Integer.parseInt(parameters.get("current").toString());
            int start = (current-1)*size;
            map.put("size",size);
            map.put("start",start);
            String startTime = parameters.get("startTime")+" 00:00:00";
            String endTime = parameters.get("endTime")+" 23:59:59";
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            alarmInfoQueryWrapper.between("alarm_time", startTime, endTime);
            String index = parameters.get("index").toString();
            if (!index.equals("all")){
                map.put("index",index);
                alarmInfoQueryWrapper.eq("`index`", index);
            }
            String alarmType = parameters.get("alarmType").toString();
            switch (alarmType){
                case "overrun" : map.put("alarmType","超限");
                    alarmInfoQueryWrapper.eq("alarm_type","超限");
                    break;
                case "sudden" : map.put("alarmType","突然高");
                    alarmInfoQueryWrapper.eq("alarm_type","突然高");
                    break;
                case "state100" : map.put("alarmType","超过国控站点100%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过国控站点100%");
                    break;
                case "state150" : map.put("alarmType","超过国控站点150%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过国控站点150%");
                    break;
                case "state250" : map.put("alarmType","超过国控站点250%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过国控站点250%");
                    break;
                case "city150" : map.put("alarmType","超过市区均值150%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过市区均值150%");
                    break;
                case "city250" : map.put("alarmType","超过市区均值250%");
                    alarmInfoQueryWrapper.eq("alarm_type","超过市区均值250%");
                    break;
                default:break;
            }
            List