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.fire.WarningAddDTO;
import com.yuchen.saas.park.api.dto.fire.WarningPageQueryDTO;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.park.PmgBatFireWarning;
import com.yuchen.saas.park.api.vo.park.fire.FireWarningVO;
import com.yuchen.saas.park.api.vo.park.fire.LatestWarningQueryVO;
import com.yuchen.saas.park.service.mapper.PmgBatFireWarningMapper;
import com.yuchen.saas.park.service.service.PmgBatFireWarningService;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.park.service.util.DateTimeUtils;
import com.yuchen.saas.park.service.wrapper.FireWarningWrapper;
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/27 15:51
 * @Description
 */
@Service
public class PmgBatFireWarningServiceImpl extends BaseServiceImpl<PmgBatFireWarningMapper, PmgBatFireWarning>
        implements PmgBatFireWarningService {

    @Autowired
    PmgBatSmartEquipmentService equipmentService;

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

    @Override
    public IPage<FireWarningVO> 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<PmgBatFireWarning> page = this.page(Condition.getPage(query), Wrappers.<PmgBatFireWarning>lambdaQuery()
                .eq(PmgBatFireWarning::getParkId, warningPageQueryDTO.getParkId())
                .in(!queryEquipmentList.isEmpty(), PmgBatFireWarning::getProductKey,
                        queryEquipmentList.stream().map(w -> w.getProductKey()).collect(Collectors.toList()))
                .in(!queryEquipmentList.isEmpty(), PmgBatFireWarning::getDeviceName,
                        queryEquipmentList.stream().map(w -> w.getDeviceName()).collect(Collectors.toList()))
                .like(warningPageQueryDTO.getWarningInfo() != null, PmgBatFireWarning::getWarningInfo,
                        warningPageQueryDTO.getWarningInfo())
                .ge(warningPageQueryDTO.getBeginTime() != null, PmgBatFireWarning::getTriggerTime,
                        DateTimeUtils.dateTimeToTimestamp(warningPageQueryDTO.getBeginTime()))
                .le(warningPageQueryDTO.getEndTime() != null, PmgBatFireWarning::getTriggerTime,
                        DateTimeUtils.dateTimeToTimestamp(warningPageQueryDTO.getEndTime()))
                .orderByDesc(PmgBatFireWarning::getTriggerTime));
        FireWarningWrapper fireWarningWrapper = new FireWarningWrapper();
        IPage<FireWarningVO> pageVO = fireWarningWrapper.pageVO(page);
        for (FireWarningVO fireWarningVO : pageVO.getRecords()) {
            PmgBatSmartEquipment equipment = equipmentService.getOne(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                    .select(PmgBatSmartEquipment::getName, PmgBatSmartEquipment::getSetPosition)
                    .eq(PmgBatSmartEquipment::getProductKey, fireWarningVO.getProductKey())
                    .eq(PmgBatSmartEquipment::getDeviceName, fireWarningVO.getDeviceName()));
            if (equipment != null) {
                fireWarningVO.setDeviceAliasName(equipment.getName());
                fireWarningVO.setSetPosition(equipment.getSetPosition());
            } else {
                fireWarningVO.setDeviceAliasName("枪机监控点-艾德双目人脸识别相机");
                fireWarningVO.setSetPosition("广场东侧");
            }
        }
        return pageVO;
    }

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