package com.jeesharp.business.modules.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.setting.entity.Feedback;
import com.jeesharp.business.modules.setting.mapper.FeedbackMapper;
import com.jeesharp.business.modules.setting.service.FeedbackService;
import com.jeesharp.business.modules.setting.vo.FeedbackQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 意见反馈服务实现类
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@Service
@Transactional(readOnly = true)
public class FeedbackServiceImpl extends CrudService<FeedbackMapper, Feedback, FeedbackQueryVO> implements FeedbackService {

    private static final int STATUS_HANDLING = 1;
    private static final int STATUS_COMPLETED = 2;
    private static final int STATUS_CLOSED = 3;

    @Override
    public List<Feedback> listByMemberId(String memberId) {
        if (memberId == null || memberId.trim().isEmpty()) {
            return List.of();
        }
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getMemberId, memberId);
        wrapper.orderByDesc(Feedback::getCreateDate);
        return this.list(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public String submitFeedback(String memberId, Feedback feedback) {
        feedback.setMemberId(memberId);
        save(feedback);
        return feedback.getId();
    }

    @Override
    @Transactional(readOnly = false)
    public void handleFeedback(String id, String handleResult, String handlerId, String handlerName) {
        Feedback feedback = get(id);
        if (feedback != null) {
            feedback.setStatus(STATUS_HANDLING);
            feedback.setHandlerId(handlerId);
            feedback.setHandlerName(handlerName);
            feedback.setHandleDate(LocalDateTime.now());
            feedback.setHandleResult(handleResult);
            update(feedback);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void completeFeedback(String id, String handleResult, String handlerId, String handlerName) {
        Feedback feedback = get(id);
        if (feedback != null) {
            feedback.setStatus(STATUS_COMPLETED);
            if (feedback.getHandlerId() == null || feedback.getHandlerId().isBlank()) {
                feedback.setHandlerId(handlerId);
                feedback.setHandlerName(handlerName);
                feedback.setHandleDate(LocalDateTime.now());
            }
            if (handleResult != null && !handleResult.isBlank()) {
                feedback.setHandleResult(handleResult);
            }
            update(feedback);
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void closeFeedback(String id, String handlerId, String handlerName) {
        Feedback feedback = get(id);
        if (feedback != null) {
            feedback.setStatus(STATUS_CLOSED);
            if (feedback.getHandlerId() == null || feedback.getHandlerId().isBlank()) {
                feedback.setHandlerId(handlerId);
                feedback.setHandlerName(handlerName);
                feedback.setHandleDate(LocalDateTime.now());
            }
            update(feedback);
        }
    }

}
