package com.zhentao.service.impl;

import com.zhentao.entity.Feedback;
import com.zhentao.mapper.FeedbackMapper;
import com.zhentao.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户反馈服务实现类
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {
    
    @Autowired
    private FeedbackMapper feedbackMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitFeedback(Feedback feedback) {
        System.out.println("=== FeedbackServiceImpl.submitFeedback 接收请求 ===");
        
        // 数据验证
        if (feedback == null) {
            throw new RuntimeException("反馈信息不能为空");
        }
        
        if (feedback.getUserId() == null || feedback.getUserId().isEmpty()) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        if (feedback.getFeedbackType() == null || feedback.getFeedbackType() < 1 || feedback.getFeedbackType() > 3) {
            throw new RuntimeException("反馈类型无效");
        }
        
        if (feedback.getContent() == null || feedback.getContent().trim().isEmpty()) {
            throw new RuntimeException("反馈内容不能为空");
        }
        
        if (feedback.getContent().length() > 500) {
            throw new RuntimeException("反馈内容不能超过500字");
        }
        
        // 设置默认值
        if (feedback.getImageUrls() == null) {
            feedback.setImageUrls("");
        }
        
        feedback.setCreateTime(LocalDateTime.now());
        feedback.setStatus(0); // 0=未处理
        
        // 保存反馈
        int result = feedbackMapper.insert(feedback);
        
        System.out.println("✅ 反馈提交成功，反馈ID: " + feedback.getId());
        
        return result > 0;
    }
}
