package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.util.StringUtils;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.DataRtMapper;
import com.usky.fire.service.BaseDeviceService;
import com.usky.fire.service.BaseDeviceStatusService;
import com.usky.fire.service.BaseUserCompanyService;
import com.usky.fire.service.BaseCompanyService;
import com.usky.fire.service.DataRtService;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.AlarmFireVo;
import com.usky.fire.service.vo.DataRtVo;
import com.usky.fire.service.vo.RealTimeStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实时数据表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-24
 */
@Service
public class DataRtServiceImpl extends AbstractCrudService<DataRtMapper, DataRt> implements DataRtService {

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Autowired
    private BaseCompanyService baseCompanyService;

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Autowired
    private BaseDeviceStatusService baseDeviceStatusService;

    @Override
    public CommonPage<DataRt> dataRtList(String deviceCode, Integer systemType, Integer pageNum, Integer pageSize) {
        List<String> companyIdList = baseUserCompanyService.companyIdList();
        IPage<DataRt> page = new Page<>(pageNum, pageSize);
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            LambdaQueryWrapper<DataRt> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.like(StringUtils.isNotBlank(deviceCode), DataRt::getDeviceCode, deviceCode)
                    .eq(DataRt::getSystemType, systemType)
                    .in(DataRt::getCompanyId, companyIdList);
            page = this.page(page, queryWrapper);
            for (int i = 0; i < page.getRecords().size(); i++) {
                page.getRecords().get(i).setDeviceTypeName(OnlineMethod.getDeviceTypeName(page.getRecords().get(i).getDeviceType()));
            }
        }
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public CommonPage<DataRtVo> deviceDataRtList(String deviceCode, Integer systemType, Integer pageNum, Integer pageSize){
        LambdaQueryWrapper<BaseUserCompany> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.select(BaseUserCompany::getCompanyId)
                .eq(BaseUserCompany::getUserId, SecurityUtils.getUserId());
        List<BaseUserCompany> comoanyList = baseUserCompanyService.list(QueryWrapper);
        List<DataRtVo> list = new ArrayList<>();
        CommonPage<DataRt> DataRtList = new CommonPage<>();
        int count=0;
        if (CollectionUtils.isNotEmpty(comoanyList)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < comoanyList.size(); i++) {
                companyIdList.add(comoanyList.get(i).getCompanyId());
            }
            DataRtList = this.dataValue(companyIdList, deviceCode, systemType, pageNum, pageSize);
            if((systemType == 2)||(systemType == 3)){
                List<Map<String, Object>> lt = new ArrayList<>();
                for (int i = 0; i < DataRtList.getRecords().size(); i++) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("portCode",DataRtList.getRecords().get(i).getPortCode());
                    map.put("portName",DataRtList.getRecords().get(i).getPortName());
                    map.put("portData",DataRtList.getRecords().get(i).getPortData());
                    lt.add(map);
                    if((i+1)%4==0){
                        DataRtVo dataRtVo = new DataRtVo();
                        List<BaseCompany> baseCompany = baseCompanyService.companySelect(companyIdList);

                        if (CollectionUtils.isNotEmpty(baseCompany)){
                            List<BaseDevice> baseDevice1 = baseDeviceService.deviceData(DataRtList.getRecords().get(i).getDeviceCode());
                            List<BaseDeviceStatus> baseDeviceStatus1 = baseDeviceStatusService.deviceStatusData(DataRtList.getRecords().get(i).getDeviceCode());
                            dataRtVo.setDeviceCode(DataRtList.getRecords().get(i).getDeviceCode());
                            dataRtVo.setDeviceStatus(baseDeviceStatus1.get(0).getDeviceStatus());
                            dataRtVo.setInstallAddress(baseDevice1.get(0).getInstallAddress());
                            dataRtVo.setCompanyName(baseCompany.get(0).getCompanyName());
                            dataRtVo.setLinkPerson(baseCompany.get(0).getLinkPerson());
                            dataRtVo.setLinkPhone(baseCompany.get(0).getLinkPhone());
                            List<Map<String, Object>> nlt= new ArrayList<>();
                            nlt.addAll(lt);
                            dataRtVo.setDataList(nlt);
                            list.add(dataRtVo);
                            lt.clear();
                        }
                        count++;
                    }

                }
            } else if(systemType == 4){
                List<Map<String, Object>> lt = new ArrayList<>();
                for (int i = 0; i < DataRtList.getRecords().size(); i++) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("portCode",DataRtList.getRecords().get(i).getPortCode());
                    map.put("portName",DataRtList.getRecords().get(i).getPortName());
                    map.put("portData",DataRtList.getRecords().get(i).getPortData());
                    lt.add(map);
                    if((i+1)%5==0){
                        DataRtVo dataRtVo = new DataRtVo();
                        List<BaseCompany> baseCompany = baseCompanyService.companySelect(companyIdList);
                        if (CollectionUtils.isNotEmpty(baseCompany)){
                            List<BaseDevice> baseDevice1 = baseDeviceService.deviceData(DataRtList.getRecords().get(i).getDeviceCode());
                            List<BaseDeviceStatus> baseDeviceStatus1 = baseDeviceStatusService.deviceStatusData(DataRtList.getRecords().get(i).getDeviceCode());
                            dataRtVo.setDeviceCode(DataRtList.getRecords().get(i).getDeviceCode());
                            dataRtVo.setDeviceStatus(baseDeviceStatus1.get(0).getDeviceStatus());
                            dataRtVo.setInstallAddress(baseDevice1.get(0).getInstallAddress());
                            dataRtVo.setCompanyName(baseCompany.get(0).getCompanyName());
                            dataRtVo.setLinkPerson(baseCompany.get(0).getLinkPerson());
                            dataRtVo.setLinkPhone(baseCompany.get(0).getLinkPhone());
                            List<Map<String, Object>> nlt= new ArrayList<>();
                            nlt.addAll(lt);
                            dataRtVo.setDataList(nlt);
                            list.add(dataRtVo);
                            lt.clear();
                        }
                        count++;
                        lt.clear();
                    }

                }

            } else if(systemType == 5){
                List<Map<String, Object>> lt = new ArrayList<>();
                for (int i = 0; i < DataRtList.getRecords().size(); i++) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("portCode",DataRtList.getRecords().get(i).getPortCode());
                    map.put("portName",DataRtList.getRecords().get(i).getPortName());
                    map.put("portData",DataRtList.getRecords().get(i).getPortData());
                    lt.add(map);
                    if((i+1)%16==0){
                        DataRtVo dataRtVo = new DataRtVo();
                        List<BaseCompany> baseCompany = baseCompanyService.companySelect(companyIdList);
                        if (CollectionUtils.isNotEmpty(baseCompany)){
                            List<BaseDevice> baseDevice1 = baseDeviceService.deviceData(DataRtList.getRecords().get(i).getDeviceCode());
                            List<BaseDeviceStatus> baseDeviceStatus1 = baseDeviceStatusService.deviceStatusData(DataRtList.getRecords().get(i).getDeviceCode());
                            dataRtVo.setDeviceCode(DataRtList.getRecords().get(i).getDeviceCode());
                            dataRtVo.setDeviceStatus(baseDeviceStatus1.get(0).getDeviceStatus());
                            dataRtVo.setInstallAddress(baseDevice1.get(0).getInstallAddress());
                            dataRtVo.setCompanyName(baseCompany.get(0).getCompanyName());
                            dataRtVo.setLinkPerson(baseCompany.get(0).getLinkPerson());
                            dataRtVo.setLinkPhone(baseCompany.get(0).getLinkPhone());
                            List<Map<String, Object>> nlt= new ArrayList<>();
                            nlt.addAll(lt);
                            dataRtVo.setDataList(nlt);
                            list.add(dataRtVo);
                            lt.clear();
                        }
                        count++;
                        lt.clear();
                    }

                }

            }
        }
        return new CommonPage<>(list, count, pageSize, pageNum);
    }


    @Override
    public List<DataRt> dataRtListOne(String deviceCode, Integer systemType) {
        List<String> companyIdList = baseUserCompanyService.companyIdList();
        List<DataRt> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            LambdaQueryWrapper<DataRt> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.like(StringUtils.isNotBlank(deviceCode), DataRt::getDeviceCode, deviceCode)
                    .eq(DataRt::getSystemType, systemType)
                    .in(DataRt::getCompanyId, companyIdList);
            list = this.list(queryWrapper);
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setDeviceTypeName(OnlineMethod.getDeviceTypeName(list.get(i).getDeviceType()));
            }
        }
        return list;
    }

    @Override
    public List<DataRt> dataRt(String deviceCode){
        LambdaQueryWrapper<DataRt> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DataRt::getDeviceCode, DataRt::getPorts, DataRt::getPortName, DataRt::getPortData,
                DataRt::getDataTime, DataRt::getContent)
                .eq(DataRt::getDeviceCode, deviceCode);
        List<DataRt> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public CommonPage<DataRt> dataValue(List<String> companyIdList, String deviceCode, Integer systemType, Integer pageNum, Integer pageSize){
        LambdaQueryWrapper<DataRt> queryWrapper = Wrappers.lambdaQuery();
        if (companyIdList.size() > 0) {
            queryWrapper.in(DataRt::getCompanyId, companyIdList);
        }
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(deviceCode)) {
            queryWrapper.like(DataRt::getDeviceCode, deviceCode);
        }
        if (systemType != null) {
            queryWrapper.eq(DataRt::getSystemType, systemType);
        }

        int total = 0;
        if(systemType == 2){
            total = (this.count(queryWrapper))/4;
            queryWrapper.orderByDesc(DataRt::getDeviceCode);
            if (pageNum != null && pageNum != 0 && pageSize != null && pageSize != 0) {
                Integer startFate = OnlineMethod.getStartFate(pageNum, pageSize*4);
                queryWrapper.last("limit " + startFate + "," + pageSize*4);
            }
        } else if(systemType == 3){
            total = (this.count(queryWrapper))/4;
            queryWrapper.orderByDesc(DataRt::getDeviceCode);
            if (pageNum != null && pageNum != 0 && pageSize != null && pageSize != 0) {
                Integer startFate = OnlineMethod.getStartFate(pageNum, pageSize*4);
                queryWrapper.last("limit " + startFate + "," + pageSize*4);
            }
        } else if(systemType == 4){
            total = (this.count(queryWrapper))/5;
            queryWrapper.eq(DataRt::getDeviceType,3);//烟感设备
            queryWrapper.orderByDesc(DataRt::getDeviceCode);
            if (pageNum != null && pageNum != 0 && pageSize != null && pageSize != 0) {
                Integer startFate = OnlineMethod.getStartFate(pageNum, pageSize*5);
                queryWrapper.last("limit " + startFate + "," + pageSize*5);
            }
        } else if(systemType == 5){
            total = (this.count(queryWrapper))/16;
            queryWrapper.orderByDesc(DataRt::getDeviceCode);
            if (pageNum != null && pageNum != 0 && pageSize != null && pageSize != 0) {
                Integer startFate = OnlineMethod.getStartFate(pageNum, pageSize*16);
                queryWrapper.last("limit " + startFate + "," + pageSize*16);
            }
        }
        List<DataRt> list = this.list(queryWrapper);
        return new CommonPage<>(list, total, pageSize, pageNum);
    }

    @Override
    public RealTimeStatus deviceInfoData(String deviceCode){
        RealTimeStatus realTimeStatus = new RealTimeStatus();
        List<BaseDevice> baseDevices = baseDeviceService.deviceData(deviceCode);
        if (CollectionUtils.isNotEmpty(baseDevices)){
            List<DataRt> dataRtsList = this.dataRt(deviceCode);
            realTimeStatus.setDeviceName(baseDevices.get(0).getDeviceName());
            realTimeStatus.setInstallAddress(baseDevices.get(0).getInstallAddress());
            realTimeStatus.setDeviceType(baseDevices.get(0).getDeviceType());
            realTimeStatus.setStatusTime(dataRtsList.get(0).getDataTime());
            List<Object> list = new ArrayList<>();
            for (int i = 0; i < dataRtsList.size(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("ports",dataRtsList.get(i).getPorts());
                map.put("portName",dataRtsList.get(i).getPortName());
                map.put("portData",dataRtsList.get(i).getPortData());
                map.put("content",dataRtsList.get(i).getContent());
                list.add(map);
            }
            realTimeStatus.setDataList(list);
        }
        return realTimeStatus;
    }

}
