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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuchen.saas.park.api.dto.park.IoTEvent;
import com.yuchen.saas.park.api.entity.security.SaasParkElectronicFenceAlarmEvent;
import com.yuchen.saas.park.service.iot.NetParkProductEnum;
import com.yuchen.saas.park.service.mapper.security.SaasParkElectronicFenceAlarmEventMapper;
import com.yuchen.saas.park.service.service.security.ISaasParkElectronicFenceAlarmEventService;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电子围栏报警事件表 服务实现类
 * </p>
 *
 * @author 洪铭铭
 * @since 2024-09-06
 */
@Slf4j
@Service
public class SaasParkElectronicFenceAlarmEventServiceImpl extends BaseServiceImpl<SaasParkElectronicFenceAlarmEventMapper, SaasParkElectronicFenceAlarmEvent> implements ISaasParkElectronicFenceAlarmEventService {
    private final String alarmEvent = "alarmEvent";

    private final String deviceStatusEvent = "deviceStatusEvent";

    private final String defenseAreaStatusEvent = "defenseAreaStatusEvent";
    /**
     * @description: 
     * @param
     * @return: java.util.List<com.yuchen.saas.park.api.entity.security.SaasParkElectronicFenceAlarmEvent>
     * @author: 洪铭铭
     * @time: 2024/9/6 14:58
     */
    public List<SaasParkElectronicFenceAlarmEvent> getAll() {

        LambdaQueryWrapper<SaasParkElectronicFenceAlarmEvent> qw = new LambdaQueryWrapper<>();
        List<SaasParkElectronicFenceAlarmEvent> saasParkElectronicFenceAlarmEventList = baseMapper.selectList(qw);

        return saasParkElectronicFenceAlarmEventList;
    }

    /**
     * @description:
     * @param jsonObjectContent
     * @return: boolean
     * @author: 洪铭铭
     * @time: 2024/9/9 10:00
     */
    @Override
    public boolean receiveIotEvent(String jsonObjectContent) throws Exception{
        // 处理接收到的iot事件
        log.info("====== 接收到的iot上报设备事件:{}", jsonObjectContent);
        if(Func.isEmpty(jsonObjectContent)){
            log.error("====== 接收到的iot上报事件为空");
            return false;
        }
        IoTEvent ioTEvent = JSON.parseObject(jsonObjectContent, IoTEvent.class);

        if(Func.isNotEmpty(ioTEvent) || Func.isNotEmpty(ioTEvent.getIdentifier())){
            throw new RuntimeException("====== 接收到的iot上报事件为空");
        }
        String deviceName = ioTEvent.getDeviceName();
        String productKey = ioTEvent.getProductKey();
        log.info("设备信息:productKey=="+productKey+",deviceName=="+deviceName+":该电子围栏设备上报信息"+ioTEvent.getName());
        if(ioTEvent.getProductKey().equals(NetParkProductEnum.ELECTRONIC_FENCE.getKey())) {

            String identifier = ioTEvent.getIdentifier();
            if (identifier.equals(alarmEvent)) {
                //报警事件
                saveAlarmEvent(ioTEvent);
            } else if (identifier.equals(deviceStatusEvent)) {
                // TODO
            } else if (identifier.equals(defenseAreaStatusEvent)) {
                // TODO
            }
        }
        return true;
    }

    public void saveAlarmEvent(IoTEvent ioTEvent) {
        if(Func.isNotEmpty(ioTEvent) && Func.isNotEmpty(ioTEvent.getOut())){
            Map<String, Object> out = ioTEvent.getOut();
//            List<Map<String, Object>> list = (List<Map<String, Object>>) out.get("list");
//            List<SaasParkElectronicFenceAlarmEvent> saasParkElectronicFenceAlarmEventList = new ArrayList<>();
//            for (Map<String, Object> outMap : list) {
//                SaasParkElectronicFenceAlarmEvent saasParkElectronicFenceAlarmEvent = new SaasParkElectronicFenceAlarmEvent();
//                saasParkElectronicFenceAlarmEvent = BeanUtil.copyProperties(outMap,SaasParkElectronicFenceAlarmEvent.class);
//                saasParkElectronicFenceAlarmEventList.add(saasParkElectronicFenceAlarmEvent);
//            }
            SaasParkElectronicFenceAlarmEvent saasParkElectronicFenceAlarmEvent = new SaasParkElectronicFenceAlarmEvent();
            saasParkElectronicFenceAlarmEvent = BeanUtil.copyProperties(out,SaasParkElectronicFenceAlarmEvent.class);
            save(saasParkElectronicFenceAlarmEvent);
        }
    }

}
