package com.usky.sas.service.impl;



import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.usky.common.core.bean.CommonPage;

import com.usky.sas.common.enums.SystemTypeCodeEnum;

import com.usky.sas.domain.*;

import com.usky.sas.mapper.*;

import com.usky.sas.service.SasIntelligentService;

import com.usky.sas.service.vo.IntelligentEventItem;

import com.usky.sas.service.vo.IntelligentEventPageRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.stream.Collectors;



@Slf4j

@Service

public class SasIntelligentServiceImpl implements SasIntelligentService {



    @Autowired

    private SasSnapEventMapper sasSnapEventMapper;



    @Autowired

    private SasAlarsasEventMapper sasAlarsasEventMapper;



    @Autowired

    private SasEntranceEventMapper sasEntranceEventMapper;



    @Autowired

    private SasParkingEventMapper sasParkingEventMapper;



    @Autowired

    private SasPatrolEventMapper sasPatrolEventMapper;



    @Autowired

    private SasUsbEventMapper sasUsbEventMapper;



    @Autowired

    private SasPerceptionEventMapper sasPerceptionEventMapper;



    @Autowired

    private SasAcquisitionEventMapper sasAcquisitionEventMapper;



    @Autowired

    private SasCollectionEventMapper sasCollectionEventMapper;



    @Autowired

    private SasRoadblockEventMapper sasRoadblockEventMapper;



    @Autowired

    private SasDeviceMapper sasDeviceMapper;



    @Autowired

    private SasPicMapper sasPicMapper;



    @Autowired

    private SasSnapTypeCodeMapper sasSnapTypeCodeMapper;



    @Autowired

    private SasUsbEventCodeMapper sasUsbEventCodeMapper;



    @Autowired

    private SasEntranceEventCodeMapper sasEntranceEventCodeMapper;



    @Autowired

    private SasParkingEventCodeMapper sasParkingEventCodeMapper;



    @Autowired

    private SasAlarsasEventCodeMapper sasAlarsasEventCodeMapper;



    @Autowired

    private SasPatrolEventCodeMapper sasPatrolEventCodeMapper;



    @Autowired

    private SasPerceptionEventCodeMapper sasPerceptionEventCodeMapper;



    @Autowired

    private SasAcquisitionEventCodeMapper sasAcquisitionEventCodeMapper;



    @Autowired

    private SasCollectionEventCodeMapper sasCollectionEventCodeMapper;



    @Autowired

    private SasRoadblockEventCodeMapper sasRoadblockEventCodeMapper;



    @Autowired

    private SasPersonTypeCodeMapper sasPersonTypeCodeMapper;



    @Override

    public CommonPage<IntelligentEventItem> queryEvents(IntelligentEventPageRequest request) {

        log.info("查询事件列表，参数：{}", request);

        

        if (StrUtil.isBlank(request.getDeviceType())) {

            return new CommonPage<>(Collections.emptyList(), 0L, 

                    request.getCurrent().longValue(), request.getSize().longValue());

        }



        // 按 eventType 分发到不同的查询方法

        switch (request.getDeviceType().toLowerCase()) {

            case "1001":

                return querySnap(request);

            case "1003":

                return queryUsb(request);

            case "1005":

                return queryEntrance(request);

            case "1006":

                return queryParking(request);

            case "1007":

                return queryAlarm(request);

            case "1008":

                return queryPatrol(request);

            case "1009":

                return queryPerception(request);

            case "1010":

                return queryAcquisition(request);

            case "1011":

                return queryCollection(request);

            case "1012":

                return queryRoadblock(request);

            default:

                log.warn("未知的事件类型：{}", request.getDeviceType());

                return new CommonPage<>(Collections.emptyList(), 0L, 

                        request.getCurrent().longValue(), request.getSize().longValue());

        }

    }



    @Override

    public CommonPage<IntelligentEventItem> querySnap(IntelligentEventPageRequest request) {

        log.info("查询Snap事件列表，参数：page={}, pageSize={}, eventCode={}, deviceId={}, beginTime={}, endTime={}",

                request.getCurrent(), request.getSize(), request.getEventCode(), 

                request.getDeviceId(), request.getStartTime(), request.getEndTime());

        

        IPage<SasSnapEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasSnapEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasSnapEvent::getDeviceId, SasSnapEvent::getTriggerTime, 

                SasSnapEvent::getEventCode, SasSnapEvent::getTriggerTime);

        

        page = sasSnapEventMapper.selectPage(page, wrapper);

        

        // 转换并填充图片URL和设备信息

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::enrichSnapEventItem)

                .collect(Collectors.toList());

        

        log.info("查询Snap事件列表完成，共{}条记录", list.size());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryUsb(IntelligentEventPageRequest request) {

        IPage<SasUsbEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasUsbEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasUsbEvent::getDeviceId, SasUsbEvent::getTriggerTime,

                SasUsbEvent::getEventCode, SasUsbEvent::getTriggerTime);

        

        page = sasUsbEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromUsb).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryAlarm(IntelligentEventPageRequest request) {

        IPage<SasAlarsasEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasAlarsasEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasAlarsasEvent::getDeviceId, SasAlarsasEvent::getTriggerTime,

                SasAlarsasEvent::getEventCode, SasAlarsasEvent::getTriggerTime);

        

        page = sasAlarsasEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromAlarm).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryEntrance(IntelligentEventPageRequest request) {

        log.info("查询Entrance事件列表，参数：{}", request);

        

        // 处理 eventCode，支持人员类型的"进"和"出"

        List<Integer> codes = new ArrayList<>();

        if (StrUtil.isNotBlank(request.getEventCode())) {

            List<Integer> inputCodes = Arrays.stream(request.getEventCode().split(","))

                    .map(String::trim)

                    .filter(StrUtil::isNotBlank)

                    .map(Integer::valueOf)

                    .collect(Collectors.toList());

            

            for (Integer code : inputCodes) {

                SasEntranceEventCode eventCode = sasEntranceEventCodeMapper.selectById(code);

                if (eventCode != null) {

                    codes.add(code * 10);

                    String eventName = eventCode.getName();

                    String personTypeName = eventName.substring(0, eventName.length() - 1);

                    

                    // 根据人员类型名称查询人员类型编码

                    LambdaQueryWrapper<SasPersonTypeCode> wrapper = new LambdaQueryWrapper<>();

                    wrapper.eq(SasPersonTypeCode::getName, personTypeName);

                    SasPersonTypeCode personTypeCode = sasPersonTypeCodeMapper.selectOne(wrapper);

                    

                    if (personTypeCode != null) {

                        if (eventName.endsWith("进")) {

                            codes.add(personTypeCode.getCode() * 10 + 1);

                        }

                        if (eventName.endsWith("出")) {

                            codes.add(personTypeCode.getCode() * 10 + 2);

                        }

                    }

                }

            }

        }

        

        // 构建查询条件

        IPage<SasEntranceEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasEntranceEvent> wrapper = new LambdaQueryWrapper<>();

        

        // eventCode 条件

        if (CollUtil.isNotEmpty(codes)) {

            wrapper.in(SasEntranceEvent::getEventCode, codes);

        }

        

        // deviceId 条件

        if (StrUtil.isNotBlank(request.getDeviceId())) {

            wrapper.eq(SasEntranceEvent::getDeviceId, request.getDeviceId());

        }

        

        // 时间范围条件

        if (request.getStartTime() != null) {

            wrapper.ge(SasEntranceEvent::getTriggerTime, request.getStartTime());

        }

        if (request.getEndTime() != null) {

            wrapper.le(SasEntranceEvent::getTriggerTime, request.getEndTime());

        }

        

        // 按创建时间倒序

        wrapper.orderByDesc(SasEntranceEvent::getCreateTime);

        

        page = sasEntranceEventMapper.selectPage(page, wrapper);

        

        // 转换并填充完整信息

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::enrichEntranceEventItem)

                .collect(Collectors.toList());

        

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }

    // ==================== enrichEntranceEventItem 方法 ====================
    /**
     * 填充Entrance事件的完整信息
     */
    private IntelligentEventItem enrichEntranceEventItem(SasEntranceEvent event) {
        IntelligentEventItem item = fromEntrance(event);

        // 1. 查询并设置图片URL
        if (StrUtil.isNotBlank(event.getPicId())) {
            String picUrl = getPicUrl(event.getPicId());
            if (StrUtil.isNotBlank(picUrl)) {
                item.setPicUrl(picUrl);
            }
        }

        // 2. 查询并设置事件类型名称（根据 eventCode 的规则）
        if (event.getEventCode() != null) {
            if (event.getEventCode() % 10 == 0) {
                // eventCode 是 10 的倍数，直接查询 EntranceEventCode
                SasEntranceEventCode code = sasEntranceEventCodeMapper.selectById(event.getEventCode());
                if (code != null) {
                    item.setEventName(code.getName());
                }
            } else if (event.getEventCode() % 10 == 1) {
                // eventCode 余数为 1，表示"进"
                Integer personTypeCodeId = (event.getEventCode() - 1) / 10;
                SasPersonTypeCode personTypeCode = sasPersonTypeCodeMapper.selectById(personTypeCodeId);
                if (personTypeCode != null) {
                    item.setEventName(personTypeCode.getName() + "进");
                }
            } else if (event.getEventCode() % 10 == 2) {
                // eventCode 余数为 2，表示"出"
                Integer personTypeCodeId = (event.getEventCode() - 2) / 10;
                SasPersonTypeCode personTypeCode = sasPersonTypeCodeMapper.selectById(personTypeCodeId);
                if (personTypeCode != null) {
                    item.setEventName(personTypeCode.getName() + "出");
                }
            }
        }

        // 3. 查询并设置设备信息
        if (StrUtil.isNotBlank(event.getDeviceId()) && event.getChannel() != null) {
            SasDevice device = getDevice(event.getDeviceId(), event.getChannel(), 3); // 3 = entrance设备类型
            if (device != null) {
                if (StrUtil.isBlank(event.getNote())) {
                    item.setDeviceName(device.getNote());
                }
                item.setAddress(device.getAddress());
            }
        }

        return item;
    }



    @Override

    public CommonPage<IntelligentEventItem> queryParking(IntelligentEventPageRequest request) {

        IPage<SasParkingEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasParkingEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasParkingEvent::getDeviceId, SasParkingEvent::getTriggerTime,

                SasParkingEvent::getEventCode, SasParkingEvent::getTriggerTime);

        

        page = sasParkingEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromParking).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryPatrol(IntelligentEventPageRequest request) {

        IPage<SasPatrolEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasPatrolEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasPatrolEvent::getDeviceId, SasPatrolEvent::getTriggerTime,

                SasPatrolEvent::getEventCode, SasPatrolEvent::getTriggerTime);

        

        page = sasPatrolEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromPatrol).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryPerception(IntelligentEventPageRequest request) {

        IPage<SasPerceptionEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasPerceptionEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasPerceptionEvent::getDeviceId, SasPerceptionEvent::getTriggerTime,

                SasPerceptionEvent::getEventCode, SasPerceptionEvent::getTriggerTime);

        

        page = sasPerceptionEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromPerception).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryAcquisition(IntelligentEventPageRequest request) {

        IPage<SasAcquisitionEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasAcquisitionEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasAcquisitionEvent::getDeviceId, SasAcquisitionEvent::getTriggerTime,

                SasAcquisitionEvent::getEventCode, SasAcquisitionEvent::getTriggerTime);

        

        page = sasAcquisitionEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromAcquisition).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryCollection(IntelligentEventPageRequest request) {

        IPage<SasCollectionEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasCollectionEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasCollectionEvent::getDeviceId, SasCollectionEvent::getTriggerTime,

                SasCollectionEvent::getEventCode, SasCollectionEvent::getTriggerTime);

        

        page = sasCollectionEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromCollection).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    @Override

    public CommonPage<IntelligentEventItem> queryRoadblock(IntelligentEventPageRequest request) {

        IPage<SasRoadblockEvent> page = new Page<>(request.getCurrent(), request.getSize());

        LambdaQueryWrapper<SasRoadblockEvent> wrapper = buildCommonWrapper(

                new LambdaQueryWrapper<>(), request,

                SasRoadblockEvent::getDeviceId, SasRoadblockEvent::getTriggerTime,

                SasRoadblockEvent::getEventCode, SasRoadblockEvent::getTriggerTime);

        

        page = sasRoadblockEventMapper.selectPage(page, wrapper);

        List<IntelligentEventItem> list = page.getRecords().stream()

                .map(this::fromRoadblock).collect(Collectors.toList());

        return new CommonPage<>(list, page.getTotal(), page.getCurrent(), page.getSize());

    }



    /**

     * 构建通用查询条件

     */

    private <T, R> LambdaQueryWrapper<T> buildCommonWrapper(

            LambdaQueryWrapper<T> wrapper,

            IntelligentEventPageRequest request,

            com.baomidou.mybatisplus.core.toolkit.support.SFunction<T, R> deviceIdFunc,

            com.baomidou.mybatisplus.core.toolkit.support.SFunction<T, R> triggerTimeFunc,

            com.baomidou.mybatisplus.core.toolkit.support.SFunction<T, R> eventCodeFunc,

            com.baomidou.mybatisplus.core.toolkit.support.SFunction<T, R> orderByFunc) {

        

        // 设备ID条件

        if (StrUtil.isNotBlank(request.getDeviceId())) {

            wrapper.eq(deviceIdFunc, request.getDeviceId());

        }

        

        // 事件代码条件（支持多个，逗号分隔）

        if (StrUtil.isNotBlank(request.getEventCode())) {

            List<Integer> eventCodes = Arrays.stream(request.getEventCode().split(","))

                    .map(String::trim)

                    .filter(StrUtil::isNotBlank)

                    .map(Integer::valueOf)

                    .collect(Collectors.toList());

            if (CollUtil.isNotEmpty(eventCodes)) {

                wrapper.in(eventCodeFunc, eventCodes);

            }

        }

        

        // 时间范围条件

        if (request.getStartTime() != null) {

            wrapper.ge(triggerTimeFunc, request.getStartTime());

        }

        if (request.getEndTime() != null) {

            wrapper.le(triggerTimeFunc, request.getEndTime());

        }

        

        // 按触发时间倒序

        wrapper.orderByDesc(orderByFunc);

        

        return wrapper;

    }



    /**

     * 填充Snap事件的完整信息（图片URL、设备信息等）

     */

    private IntelligentEventItem enrichSnapEventItem(SasSnapEvent event) {

        IntelligentEventItem item = fromSnap(event);

        

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(event.getEventPicId())) {

            String picUrl = getPicUrl(event.getEventPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }

        

        // 查询并设置事件类型名称

        if (event.getEventCode() != null) {

            SasSnapTypeCode code = sasSnapTypeCodeMapper.selectById(event.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }

        

        // 查询并设置设备信息

        if (StrUtil.isNotBlank(event.getDeviceId()) && event.getChannel() != null) {

            SasDevice device = getDevice(event.getDeviceId(), event.getChannel(), SystemTypeCodeEnum.snap.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(event.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }



        SasSnapTypeCode code = sasSnapTypeCodeMapper.selectById(event.getEventCode());

        if (code != null) {

            item.setEventName(code.getName());

        }

        

        return item;

    }



    /**

     * 根据图片ID获取图片完整URL

     */

    private String getPicUrl(String picId) {

        if (StrUtil.isBlank(picId)) {

            return null;

        }

        

        SasPic pic = sasPicMapper.selectById(picId);

        if (pic != null && StrUtil.isNotBlank(pic.getUrl()) && StrUtil.isNotBlank(pic.getPath())) {

            return pic.getUrl() + pic.getPath();

        }

        return null;

    }



    /**

     * 根据设备ID、通道号和设备类型查询设备信息

     */

    private SasDevice getDevice(String deviceId, Integer channel, Integer deviceType) {

        if (StrUtil.isBlank(deviceId) || channel == null || deviceType == null) {

            return null;

        }

        

        LambdaQueryWrapper<SasDevice> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(SasDevice::getDeviceId, deviceId)

               .eq(SasDevice::getChannel, channel)

               .eq(SasDevice::getDeviceType, deviceType);

        

        return sasDeviceMapper.selectOne(wrapper);

    }



    private IntelligentEventItem fromSnap(SasSnapEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setScenePicId(e.getScenePicId());

        item.setEventPicId(e.getEventPicId());

        item.setSimilarity(e.getSimilarity());

        item.setPersonCode(e.getPersonCode());

        item.setNotify(e.getNotify());

        item.setScene(e.getScene());

        return item;

    }



    private IntelligentEventItem fromUsb(SasUsbEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        if (e.getEventCode() != null) {

            SasUsbEventCode code = sasUsbEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }

        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.usb.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }



        return item;

    }



    private IntelligentEventItem fromAlarm(SasAlarsasEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setPicId(e.getPicId());

        item.setRelPerson(e.getRelPerson());

        item.setDealPerson(e.getDealPerson());

        item.setEventSystem(e.getEventSystem());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getPicId())) {

            String picUrl = getPicUrl(e.getPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasAlarsasEventCode code = sasAlarsasEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.alarm.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }


        return item;

    }



    private IntelligentEventItem fromEntrance(SasEntranceEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setCertifiedType(e.getCertifiedType());

        item.setCertifiedNo(e.getCertifiedNo());

        item.setPersonName(e.getName());

        item.setPhone(e.getPhone());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setCardId(e.getCardId());

        item.setSimilarity(e.getSimilarity());

        item.setPicId(e.getPicId());

        return item;

    }



    private IntelligentEventItem fromParking(SasParkingEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setEntranceCode(e.getEntranceCode());

        item.setPlateNo(e.getPlateNo());

        item.setPlateType(e.getPlateType());

        item.setPlateColor(e.getPlateColor());

        item.setCarType(e.getCarType());

        item.setAccessType(e.getAccessType());

        item.setSimilarity(e.getSimilarity());

        item.setPicId(e.getPlatePicId());

        item.setEventPicId(e.getEventPicId());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getEventPicId())) {

            String picUrl = getPicUrl(e.getEventPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasParkingEventCode code = sasParkingEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.parking.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }


        return item;

    }



    private IntelligentEventItem fromPatrol(SasPatrolEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setName(e.getName());

        item.setCredentialType(e.getCredentialType());

        item.setCredentialNo(e.getCredentialNo());

        item.setTypeName(e.getTypeName());

        item.setPicId(e.getPicId());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getPicId())) {

            String picUrl = getPicUrl(e.getPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasPatrolEventCode code = sasPatrolEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.patrol.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }



        return item;

    }



    private IntelligentEventItem fromPerception(SasPerceptionEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setPicId(e.getPicId());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getPicId())) {

            String picUrl = getPicUrl(e.getPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasPerceptionEventCode code = sasPerceptionEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.perception.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }

        return item;

    }



    private IntelligentEventItem fromAcquisition(SasAcquisitionEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setPicId(e.getPicId());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getPicId())) {

            String picUrl = getPicUrl(e.getPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasAcquisitionEventCode code = sasAcquisitionEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.acquisition.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }

        return item;

    }



    private IntelligentEventItem fromCollection(SasCollectionEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setPicId(e.getPicId());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getPicId())) {

            String picUrl = getPicUrl(e.getPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasCollectionEventCode code = sasCollectionEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.collection.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }

        return item;

    }



    private IntelligentEventItem fromRoadblock(SasRoadblockEvent e) {

        IntelligentEventItem item = new IntelligentEventItem();

        item.setEventId(e.getEventId());

        item.setDeviceId(e.getDeviceId());

        item.setChannel(e.getChannel());

        item.setTriggerTime(e.getTriggerTime());

        item.setEventCode(e.getEventCode());

        item.setPicId(e.getPicId());

        // 查询并设置事件图片URL

        if (StrUtil.isNotBlank(e.getPicId())) {

            String picUrl = getPicUrl(e.getPicId());

            if (StrUtil.isNotBlank(picUrl)) {

                item.setPicUrl(picUrl);

            }

        }



        // 查询并设置事件类型名称

        if (e.getEventCode() != null) {

            SasRoadblockEventCode code = sasRoadblockEventCodeMapper.selectById(e.getEventCode());

            if (code != null) {

                item.setEventName(code.getName());

            }

        }



        // 查询并设置设备信息

        if (StrUtil.isNotBlank(e.getDeviceId()) && e.getChannel() != null) {

            SasDevice device = getDevice(e.getDeviceId(), e.getChannel(), SystemTypeCodeEnum.roadblock.getCode()); // 1 = snap设备类型

            if (device != null) {

                if(StringUtils.isBlank(e.getNote())) {

                    item.setNote(device.getNote());

                }



                item.setAddress(device.getAddress());

            }

        }

        return item;

    }

}

