package com.usky.issue.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.issue.domain.YtDeviceStatus;
import com.usky.issue.mapper.YtDeviceStatusMapper;
import com.usky.issue.service.SpHj2017Service;
import com.usky.issue.service.SpRtu2017Service;
import com.usky.issue.service.SpSj2017Service;
import com.usky.issue.service.YtDeviceStatusService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.issue.service.vo.YtDeviceStatusRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 离线设备VIEW 服务实现类
 * </p>
 *
 * @author fu
 * @since 2023-11-23
 */
@Service
public class YtDeviceStatusServiceImpl extends AbstractCrudService<YtDeviceStatusMapper, YtDeviceStatus> implements YtDeviceStatusService {
    @Autowired
    private YtDeviceStatusMapper ytDeviceStatusMapper;
    @Autowired
    private SpRtu2017Service spRtu2017Service;
    @Autowired
    private SpSj2017Service spSj2017Service;
    @Autowired
    private SpHj2017Service spHj2017Service;

    /*
    分页查询
    */
    @Override
    public CommonPage<YtDeviceStatus> page(YtDeviceStatusRequestVO requestVO) {

        String deviceType = requestVO.getDeviceType();
        int pageCurrent = requestVO.getCurrent();
        int pageSize = requestVO.getSize();

        List<YtDeviceStatus> list = ytDeviceStatusMapper.getDeviceStatusType(deviceType, pageCurrent, pageSize);
        // 计算总记录数
        int total = list.size();

        // 计算总页数
        int current = (total + pageSize - 1) / pageSize;

        // 计算当前页的起始索引
        int startIndex = (pageCurrent - 1) * pageSize;

        //对传入类型进行判断筛选
        List<YtDeviceStatus> filteredList = list.stream()
                .filter(device -> device.getDeviceType().equals(deviceType))
                .collect(Collectors.toList());

        // 对记录进行分页操作
        List<YtDeviceStatus> records = filteredList.stream()
                .skip(startIndex)//跳过startIndex个元素，从startIndex+1开始
                .limit(pageSize)//限制显示大小，只处理pageSize个元素
                .collect(Collectors.toList());//将流中的元素收集到List<YtDeviceStatus> == 获取当前页记录

        return new CommonPage<>(records, total, pageSize, pageCurrent);
    }


    /*
    导出离线数据查询结果
     */
    @Override
    public List<YtDeviceStatus> export() {
        return ytDeviceStatusMapper.exportData();
    }


    /*
    告警设备数量统计
    */

    /*
    根据传入单位编号查询所有设备信息
    */
    @Override
    public List<Map<String, Object>> deviceList(String requestBody) {
        JSONObject requestVO = JSONObject.parseObject(requestBody);
        String companyCode = requestVO.get("companyCode").toString();
        QueryWrapper<YtDeviceStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("device_type AS deviceType","COUNT(*) AS deviceTypeCounts",
                    "COUNT(difference < 48 OR NULL) AS onlineCount",
                    "COUNT(difference >= 48 OR NULL) AS offlineCount")
                    .eq("company_code",companyCode)
                    .groupBy("device_type");
        List<Map<String, Object>> deviceStatusList = this.listMaps(queryWrapper);
        if (deviceStatusList.size() == 0) {
            throw new BusinessException("暂无设备");
        }
        for (Map<String, Object> type : deviceStatusList) {
            String dwtype = String.valueOf(type.get("deviceType"));
            type.put("deviceType", dwtype);
            switch (dwtype) {
                case "火系统":
                    // 1火系统
                    Long countHj = (spHj2017Service.countDevice(requestBody)!= null)? spHj2017Service.countDevice(requestBody) : 0L;
                    type.put("alarmDeviceSum", countHj);
                    break;
                case "水系统":
                    // 2水系统
                    Long countSj = (spSj2017Service.countDevice(requestBody) != null) ? spSj2017Service.countDevice(requestBody) : 0L;
                    type.put("alarmDeviceSum", countSj);
                case "液位":
                    // 5液位
                    Long countSjYw = (spSj2017Service.countDevice(requestBody) != null) ? spSj2017Service.countDevice(requestBody) : 0L;
                    type.put("alarmDeviceSum", countSjYw);
                    break;
                case "烟感系统":
                    // 3烟感系统
                    break;
                case "消防栓":
                    // 4消防栓
                    break;
                case "rtu":
                    // 6rtu
                    Long countRtu = (spRtu2017Service.countDevice(requestBody) != null) ? spRtu2017Service.countDevice(requestBody) : 0L;
                    type.put("alarmDeviceSum", countRtu);
                    break;
                case "电气火灾":
                    // 7电气火灾
                    break;
                case "防火门":
                    // 8防火门
                    break;
                case "气体灭火":
                    // 9气体灭火
                    break;
                case "人脸识别":
                    // 10人脸识别
                    break;
                case "视频监控":
                    // 11视频监控
                    break;
                case "井盖":
                    // 128井盖
                    break;
                default:
                    // 处理未知类型
                    break;
            }
        }
        return deviceStatusList;
    }

}