package com.tashow.cloud.app.mq.handler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tashow.cloud.app.mapper.BuriedPointFailRecordMapper;
import com.tashow.cloud.app.model.MqMessageRecord;
import com.tashow.cloud.app.service.feishu.BuriedPointMonitorService;
import com.tashow.cloud.mq.handler.FailRecordHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
 * MQ消息记录处理器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class BuriedPointFailRecordHandler implements FailRecordHandler {
    private final BuriedPointFailRecordMapper buriedPointFailRecordMapper;
    private final BuriedPointMonitorService buriedPointMonitorService;
    /**
     * 保存消息记录=
     */
    @Override
    public void saveMessageRecord(Integer id, String exchange, String routingKey, String cause, String messageContent, int status) {
        try {
            MqMessageRecord existingRecord = findExistingRecord(id);
            if (existingRecord != null) {
                existingRecord.setRetryCount(existingRecord.getRetryCount() + 1);
                existingRecord.setMessageContent(messageContent);
                existingRecord.setStatus(status);
                existingRecord.setCause(cause);
                existingRecord.setUpdateTime(new Date());
                buriedPointFailRecordMapper.updateById(existingRecord);
            } else {
                MqMessageRecord record = new MqMessageRecord();
                record.setId(id);
                record.setExchange(exchange);
                record.setRoutingKey(routingKey);
                record.setCause(cause);
                record.setMessageContent(messageContent);
                record.setRetryCount(0);
                record.setStatus(status);
                record.setCreateTime(new Date());
                record.setUpdateTime(new Date());
                buriedPointFailRecordMapper.insert(record);
                if (status == MqMessageRecord.STATUS_FAILED) {
                    buriedPointMonitorService.checkFailRecordsAndAlert(cause);
                }
            }
        } catch (Exception e) {
            log.error("[MQ消息处理器] 保存消息记录异常", e);
        }
    }

    /**
     * 更新消息状态
     */
    @Override
    public void updateMessageStatus(Integer id) {
        try {
            MqMessageRecord record = findExistingRecord(id);
            if (record != null) {
                record.setStatus(MqMessageRecord.STATUS_SUCCESS);
                record.setUpdateTime(new Date());
                buriedPointFailRecordMapper.updateById(record);
            }
        } catch (Exception e) {
            log.error("[MQ消息处理器] 更新消息状态异常: {}", id, e);
        }
    }

    /**
     * 更新消息状态并设置失败原因
     */
    @Override
    public void updateMessageStatusWithCause(Integer id,  String cause) {
        try {
            MqMessageRecord record = findExistingRecord(id);
            if (record != null) {
                record.setStatus(MqMessageRecord.STATUS_FAILED);
                record.setCause(cause);
                record.setUpdateTime(new Date());
                buriedPointFailRecordMapper.updateById(record);
                buriedPointMonitorService.checkFailRecordsAndAlert(cause);
            }
        } catch (Exception e) {
            log.error("[MQ消息处理器] 更新消息状态和原因异常: {}", id, e);
        }
    }

    /**
     * 查找已存在的失败记录
     */
    private MqMessageRecord findExistingRecord(Integer id) {
        LambdaQueryWrapper<MqMessageRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MqMessageRecord::getId, id);
        return buriedPointFailRecordMapper.selectOne(queryWrapper);
    }

} 