package com.imdroid.beidou.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imdroid.beidou.entity.User;
import com.imdroid.beidou.entity.UserMapper;
import com.imdroid.secapi.dto.WarningCfg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class NotificationService {
    @Autowired
    UserMapper userMapper;

    public void onWarning(String deviceIds, String content, String msgTime, short warningLevel) throws ExecutionException, InterruptedException {
        // 严重告警推送短信
        if(warningLevel == WarningCfg.LEVEL_2){
            List<String> fwdPhoneList = getFwdPhones();
            if(fwdPhoneList!=null) SmsService.send(deviceIds, content,
                    msgTime, fwdPhoneList);
        }
    }

    List<String> getFwdPhones(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warning_enabled",1);
        queryWrapper.eq("locked",0);
        queryWrapper.isNotNull("mobile");
        List<User> userList = userMapper.selectList(queryWrapper);
        if(userList.size() == 0) return null;

        List<String> phoneList = new ArrayList<>();
        for(User user:userList){
            phoneList.add(user.getMobile());
        }
        return phoneList;
    }
}
