package com.usky.sas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.sas.domain.SasAcquisitionEvent;
import com.usky.sas.domain.SasCollectionEvent;
import com.usky.sas.mapper.SasAcquisitionEventMapper;
import com.usky.sas.mapper.SasCollectionEventMapper;
import com.usky.sas.service.SasVideoPatrolService;
import com.usky.sas.service.vo.PatrolConfigResponse;
import com.usky.sas.service.vo.VideoProtectionConfigResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SasVideoPatrolServiceImpl implements SasVideoPatrolService {

    @Autowired
    private SasCollectionEventMapper collectionEventMapper;

    @Autowired
    private SasAcquisitionEventMapper acquisitionEventMapper;

    @Override
    public VideoProtectionConfigResponse getVideoProtectionConfig() {
        LambdaQueryWrapper<SasCollectionEvent> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(SasCollectionEvent::getTriggerTime).last("limit 20");
        List<SasCollectionEvent> list = collectionEventMapper.selectList(wrapper);

        VideoProtectionConfigResponse resp = new VideoProtectionConfigResponse();
        resp.setEnabled(true);
        resp.setEvents(list.stream().map(e -> {
            VideoProtectionConfigResponse.EventItem item = new VideoProtectionConfigResponse.EventItem();
            item.setEventId(e.getEventId());
            item.setDeviceId(e.getDeviceId());
            item.setTriggerTime(e.getTriggerTime());
            item.setEventCode(e.getEventCode());
            return item;
        }).collect(Collectors.toList()));
        return resp;
    }

    @Override
    public PatrolConfigResponse getPatrolConfig() {
        LambdaQueryWrapper<SasAcquisitionEvent> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(SasAcquisitionEvent::getTriggerTime).last("limit 20");
        List<SasAcquisitionEvent> list = acquisitionEventMapper.selectList(wrapper);

        PatrolConfigResponse resp = new PatrolConfigResponse();
        resp.setEnabled(true);
        resp.setEvents(list.stream().map(e -> {
            PatrolConfigResponse.EventItem item = new PatrolConfigResponse.EventItem();
            item.setEventId(e.getEventId());
            item.setDeviceId(e.getDeviceId());
            // 名称暂无法从库直接获取，这里留空
            item.setName(null);
            item.setTriggerTime(e.getTriggerTime());
            item.setEventCode(e.getEventCode());
            return item;
        }).collect(Collectors.toList()));
        return resp;
    }
}

