package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.dto.security.WarningAddDTO;
import com.yuchen.saas.park.api.dto.security.WarningPageQueryDTO;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.park.PmgBatSecurityWarning;
import com.yuchen.saas.park.api.vo.park.security.LatestWarningQueryVO;
import com.yuchen.saas.park.api.vo.park.security.SecurityWarningVO;
import com.yuchen.saas.park.api.vo.park.security.WarningTrendVO;
import com.yuchen.saas.park.service.mapper.PmgBatSecurityWarningMapper;
import com.yuchen.saas.park.service.service.PmgBatSecurityWarningService;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.park.service.util.DateTimeUtils;
import com.yuchen.saas.park.service.util.EnergyDateUtils;
import com.yuchen.saas.park.service.wrapper.SecurityWarningWrapper;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 颜艺波
 * @date 2022/6/24 17:19
 * @Description
 */
@Service
public class PmgBatSecurityWarningServiceImpl extends BaseServiceImpl<PmgBatSecurityWarningMapper,
        PmgBatSecurityWarning> implements PmgBatSecurityWarningService {

    @Autowired
    PmgBatSmartEquipmentService equipmentService;

    @Override
    public List<WarningTrendVO> warningTrendToday(Long parkId) {
        //获取今日日期
        String dateToday = EnergyDateUtils.getDateToday();
        //获取今日行为预警趋势(小时-例折线图)
        List<WarningTrendVO> list = baseMapper.warningTrendToday(parkId, dateToday);

        //获取今日几点
        int nowHour = EnergyDateUtils.getHourOfToday();
        //没有的小时给0
        for (int i = 1; i <= nowHour + 1; i++) {
            String hourNum = String.valueOf(i);
            if (!list.stream().anyMatch(e -> hourNum.equals(String.valueOf(e.getKey())))) {
                WarningTrendVO info = new WarningTrendVO();
                info.setKey(i);
                info.setValue(0);
                list.add(info);
            }
        }
        //升序排序
        list.sort((a, b) -> a.getKey() - b.getKey());
        return list;
    }

    @Override
    public LatestWarningQueryVO latestWarningQuery(Long parkId) {
        LatestWarningQueryVO latestWarningQueryVO = new LatestWarningQueryVO();
        List<PmgBatSecurityWarning> list =
                this.list(Wrappers.<PmgBatSecurityWarning>lambdaQuery()
                        .eq(PmgBatSecurityWarning::getParkId, parkId)
                        .orderByDesc(PmgBatSecurityWarning::getTriggerTime)
                        .last("limit 8"));
        SecurityWarningWrapper securityWarningWrapper = new SecurityWarningWrapper();
        List<SecurityWarningVO> voList = securityWarningWrapper.listVO(list);
        for (SecurityWarningVO securityWarningVO : voList) {
            PmgBatSmartEquipment equipment = equipmentService.getOne(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                    .select(PmgBatSmartEquipment::getName, PmgBatSmartEquipment::getSetPosition)
                    .eq(PmgBatSmartEquipment::getProductKey, securityWarningVO.getProductKey())
                    .eq(PmgBatSmartEquipment::getDeviceName, securityWarningVO.getDeviceName()));
            if (equipment != null) {
                securityWarningVO.setDeviceAliasName(equipment.getName());
                securityWarningVO.setSetPosition(equipment.getSetPosition());
            } else {
                securityWarningVO.setDeviceAliasName("枪机监控点-艾德双目人脸识别相机");
                securityWarningVO.setSetPosition("广场东侧");
            }
        }
        latestWarningQueryVO.setSecurityWarningVOList(voList);
        latestWarningQueryVO.setTotal(this.count(Wrappers.<PmgBatSecurityWarning>lambdaQuery()
                .eq(PmgBatSecurityWarning::getParkId, parkId)));
        return latestWarningQueryVO;
    }

    @Override
    public IPage<SecurityWarningVO> warningPageQuery(WarningPageQueryDTO warningPageQueryDTO, Query query) {
        List<PmgBatSmartEquipment> queryEquipmentList = new ArrayList<>();
        if (warningPageQueryDTO.getDeviceAliasName() != null) {
            // 根据设备名称查询
            queryEquipmentList = equipmentService.list(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                    .select(PmgBatSmartEquipment::getProductKey, PmgBatSmartEquipment::getDeviceName)
                    .like(PmgBatSmartEquipment::getName, warningPageQueryDTO.getDeviceAliasName())
                    .eq(PmgBatSmartEquipment::getParkId, warningPageQueryDTO.getParkId()));
        }
        IPage<PmgBatSecurityWarning> page = this.page(Condition.getPage(query),
                Wrappers.<PmgBatSecurityWarning>lambdaQuery()
                        .eq(PmgBatSecurityWarning::getParkId, warningPageQueryDTO.getParkId())
                        .in(!queryEquipmentList.isEmpty(), PmgBatSecurityWarning::getProductKey,
                                queryEquipmentList.stream().map(w -> w.getProductKey()).collect(Collectors.toList()))
                        .in(!queryEquipmentList.isEmpty(), PmgBatSecurityWarning::getDeviceName,
                                queryEquipmentList.stream().map(w -> w.getDeviceName()).collect(Collectors.toList()))
                        .eq(warningPageQueryDTO.getType() != null, PmgBatSecurityWarning::getCode,
                                warningPageQueryDTO.getType())
                        .ge(warningPageQueryDTO.getBeginTime() != null, PmgBatSecurityWarning::getTriggerTime,
                                DateTimeUtils.dateTimeToTimestamp(warningPageQueryDTO.getBeginTime()))
                        .le(warningPageQueryDTO.getEndTime() != null, PmgBatSecurityWarning::getTriggerTime,
                                DateTimeUtils.dateTimeToTimestamp(warningPageQueryDTO.getEndTime())));
        SecurityWarningWrapper securityWarningWrapper = new SecurityWarningWrapper();
        IPage<SecurityWarningVO> pageVO = securityWarningWrapper.pageVO(page);
        for (SecurityWarningVO securityWarningVO : pageVO.getRecords()) {
            PmgBatSmartEquipment equipment = equipmentService.getOne(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                    .select(PmgBatSmartEquipment::getName, PmgBatSmartEquipment::getSetPosition)
                    .eq(PmgBatSmartEquipment::getProductKey, securityWarningVO.getProductKey())
                    .eq(PmgBatSmartEquipment::getDeviceName, securityWarningVO.getDeviceName()));
            if (equipment != null) {
                securityWarningVO.setDeviceAliasName(equipment.getName());
                securityWarningVO.setSetPosition(equipment.getSetPosition());
            } else {
                securityWarningVO.setDeviceAliasName("枪机监控点-艾德双目人脸识别相机");
                securityWarningVO.setSetPosition("广场东侧");
            }
        }
        return pageVO;
    }

    @Override
    public Boolean add(WarningAddDTO warningAddDTO) {
        PmgBatSecurityWarning securityWarning = new PmgBatSecurityWarning();
        BeanUtils.copyProperties(warningAddDTO, securityWarning);
        // 园区id通过设备查询数据库获取
        PmgBatSmartEquipment equipment = equipmentService.getOne(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                .select(PmgBatSmartEquipment::getParkId)
                .eq(PmgBatSmartEquipment::getProductKey, warningAddDTO.getProductKey())
                .eq(PmgBatSmartEquipment::getDeviceName, warningAddDTO.getDeviceName()));
        if (equipment != null) {
            securityWarning.setParkId(equipment.getParkId());
            securityWarning.setTenantId(equipment.getTenantId());
        } else {
            securityWarning.setParkId(34325345354L);
        }
        this.save(securityWarning);
        return true;
    }

}
