package com.common.service;

import com.common.entity.WecomNotification;
import com.common.mapper.WecomNotificationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 微信通知服务类
 */
@Service
public class WecomNotificationService {

    private static final Logger logger = LoggerFactory.getLogger(WecomNotificationService.class);

    @Autowired
    private WecomNotificationMapper wecomNotificationMapper;

    /**
     * 插入微信通知
     * 
     * @param notification 通知实体
     * @return 影响的行数
     */
    public int insert(WecomNotification notification) {
        try {
            if (notification.getCreateTime() == null) {
                notification.setCreateTime(LocalDateTime.now());
            }
            if (notification.getWecomNotificationTime() == null) {
                notification.setWecomNotificationTime(LocalDateTime.now());
            }
            if (notification.getWecomNotificationStatus() == null) {
                notification.setWecomNotificationStatus("1"); // 默认未发送
            }
            if (notification.getTenantId() == null) {
                notification.setTenantId("000000");
            }
            int rows = wecomNotificationMapper.insert(notification);
            logger.info("插入微信通知成功, ID: {}, 类型: {}, 内容: {}", 
                    notification.getWecomNotificationId(),
                    notification.getWecomNotificationType(),
                    notification.getWecomNotificationName());
            return rows;
        } catch (Exception e) {
            logger.error("插入微信通知失败: {}", e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 根据ID查询
     */
    public WecomNotification selectById(Long id) {
        return wecomNotificationMapper.selectById(id);
    }

    /**
     * 查询未发送的通知列表
     */
    public java.util.List<WecomNotification> selectUnsentNotifications() {
        return wecomNotificationMapper.selectUnsentNotifications();
    }

    /**
     * 更新通知状态
     * 
     * @param id 通知ID
     * @param status 状态（0:已发送, 1:未发送）
     * @return 影响的行数
     */
    public int updateStatus(Long id, String status) {
        return wecomNotificationMapper.updateStatus(id, status);
    }

    /**
     * 发送微信通知告警
     * 提供给其他模块在运行异常时调用
     * 
     * @param name 通知名称
     * @param type 通知类型（如：alarm_health_check）
     * @param level 通知等级（1:低, 2:中, 3:高, 4:紧急）
     * @param content 通知内容
     * @param ip 关联IP（可选）
     * @return 通知实体
     */
    public WecomNotification sendAlert(String name, String type, String level, String content, String ip) {
        WecomNotification notification = new WecomNotification();
        notification.setWecomNotificationName(name);
        notification.setWecomNotificationType(type);
        notification.setWecomNotificationLevel(level);
        notification.setWecomNotificationContent(content);
        notification.setWecomNotificationIp(ip);
        notification.setWecomNotificationTime(LocalDateTime.now());
        notification.setWecomNotificationStatus("1"); // 未发送
        notification.setTenantId("000000");
        notification.setCreateTime(LocalDateTime.now());
        
        insert(notification);
        
        logger.warn("发送微信告警通知 - 名称: {}, 类型: {}, 等级: {}, 内容: {}", 
                name, type, level, content);
        
        return notification;
    }

    /**
     * 发送告警通知（简化版本）
     * 
     * @param alertType 告警类型
     * @param alertContent 告警内容
     * @return 通知实体
     */
    public WecomNotification sendAlert(String alertType, String alertContent) {
        return sendAlert(alertType, alertType, "3", alertContent, null);
    }
}