package jnpf.cultivate.v2.mq;

import jnpf.constants.MessageTopicConstants;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.cultivate.v2.service.V2CultivatePostStudyService;
import jnpf.cultivate.v2.service.V2CultivateTaskStudyService;
import jnpf.model.cultivate.v2.enums.PositionBusinessSourceEnum;
import jnpf.model.cultivate.v2.mq.CultivateMqDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        topic = MessageTopicConstants.CULTIVATE_TOPIC,
        consumerGroup = MessageTopicConstants.CULTIVATE_CONSUMER_GROUP_SELF,
        replyTimeout = 600000,
        nameServer = "${spring.cloud.stream.rocketmq.binder.name-server}"
)
@Slf4j
public class CultivateConsumerMQListener implements RocketMQListener<CultivateMqDTO> {

    @Autowired
    private V2CultivatePostStudyService v2CultivatePostStudyService;

    @Autowired
    private V2CultivateTaskStudyService v2CultivateTaskStudyService;

    @Autowired
    private UserApiV2Util userApiV2Util;

    @Override
    public void onMessage(CultivateMqDTO message) {
        if (message == null) {
            log.warn("接收到的培训信息为null");
            return;
        }

        log.info("开始处理培训信息: type={}, businessSource={}, businessId={}, tenantId={}",
                message.getType(), message.getBusinessSource(), message.getBusinessId(), message.getTenantId());

        userApiV2Util.checkOutTenant(message.getTenantId());

        try {
            switch (message.getType()) {
                case COURSE:
                    dealCourse(message);
                    break;
                case EXAM:
                    dealExam(message);
                    break;
                case IDENTITY:
                    dealIdentity(message);
                    break;
                case PRACTICE:
                    dealPractice(message);
                    break;
                default:
                    log.warn("不支持的培训类型: {}", message.getType());
            }

            log.info("培训信息处理成功: type={}, businessId={}", message.getType(), message.getBusinessId());

        } catch (Exception exception) {
            log.error("处理培训信息时发生异常: type={}, businessId={}, message={}",
                    message.getType(), message.getBusinessId(), message, exception);
        }
    }

    private void dealPractice(CultivateMqDTO message) {
        if (message == null) {
            log.warn("dealPractice方法接收到的message为null");
            return;
        }

        try {
            v2CultivatePostStudyService.dealPositionPracticeStudy(message);
        } catch (Exception e) {
            log.error("处理岗位学习信息练习时发生异常: message={}", message, e);
        }

        try {
            v2CultivateTaskStudyService.dealTaskPracticeStudy(message);
        } catch (Exception e) {
            log.error("处理任务学习信息练习时发生异常: message={}", message, e);
        }
    }

    private void dealIdentity(CultivateMqDTO message) {
        if (message == null) {
            log.warn("dealIdentity方法接收到的message为null");
            return;
        }
        // 根据业务来源判断处理哪个服务
        if (message.getBusinessSource() == PositionBusinessSourceEnum.POST_LEARNING) {
            try {
                v2CultivatePostStudyService.dealPositionIdentityStudy(message);
            } catch (Exception e) {
                log.error("处理岗位学习鉴定信息时发生异常: message={}", message, e);
            }
        } else if (message.getBusinessSource() == PositionBusinessSourceEnum.TASK_LEARNING) {
            try {
                v2CultivateTaskStudyService.dealTaskIdentityStudy(message);
            } catch (Exception e) {
                log.error("处理任务学习鉴定信息时发生异常: message={}", message, e);
            }
        } else {
            log.warn("dealIdentity方法不支持的培训来源: {}", message.getBusinessSource());
        }
    }

    private void dealExam(CultivateMqDTO message) {
        if (message == null) {
            log.warn("dealExam方法接收到的message为null");
            return;
        }

        try {
            v2CultivatePostStudyService.dealPositionExamStudy(message);
        } catch (Exception e) {
            log.error("处理岗位考试信息时发生异常: message={}", message, e);
        }

        try {
            v2CultivateTaskStudyService.dealTaskExamStudy(message);
        } catch (Exception e) {
            log.error("处理任务考试信息时发生异常: message={}", message, e);
        }
    }

    private void dealCourse(CultivateMqDTO message) {
        if (message == null) {
            log.warn("dealCourse方法接收到的message为null");
            return;
        }

        switch (message.getBusinessSource()) {
            case POST_LEARNING:
                v2CultivatePostStudyService.dealPositionCourseStudy(message);
                break;
            case TASK_LEARNING:
                v2CultivateTaskStudyService.dealTaskCourseStudy(message);
                break;
            case GENERAL_COURSE:
            case OFFLINE_CLASS:
                v2CultivatePostStudyService.dealCommonCourseStudy(message);
                break;
            default:
                log.warn("dealCourse方法不支持的培训来源: {}", message.getBusinessSource());
        }
    }
}