package com.ssj.service.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import com.ssj.framework.core.rabbitmq.TopicRabbitConfig;
import com.ssj.service.sys.homework.service.HomeworkPictureService;
import com.ssj.service.sys.homework.service.HomeworkService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用于提交匹配作业
 *
 * @Author: chchen
 * @Description
 * @Date Create in 14:59 2019/11/5
 * @Modified By
 */
@Component
public class TopicRabbitSubmitHomeworkPictureConsumer {
    private static Logger logger = LoggerFactory.getLogger(TopicRabbitSubmitHomeworkPictureConsumer.class);
    @Autowired
    private HomeworkPictureService homeworkPictureService;
    @Autowired
    private HomeworkService homeworkService;

    @RabbitListener(queues = {TopicRabbitConfig.topic_submit_homework_picture_discern})
    @RabbitHandler
    public void handleMessage(Message message){
        try{
            HomeworkPicture homeworkPicture = JSONObject.parseObject(message.getBody(), HomeworkPicture.class);

            boolean flag = homeworkService.isCorrected(homeworkPicture.getHomeworkId()) == 1 ? true : false;
            //推送小塾
            if (StringUtils.isNotBlank(homeworkPicture.getDerver())){
                homeworkPictureService.singleSujectPushConche(homeworkPicture, flag, homeworkPicture.getCreateTime());
            }
            if (flag){
                homeworkPictureService.singleSujectPushPad(homeworkPicture, homeworkPicture.getCreateTime());
            }
            homeworkService.findWrongQuestionAndSetRedis(homeworkPicture.getHomeworkId());
            homeworkService.findWrongHowDetailAndSetRedis(homeworkPicture.getHomeworkId());
        } catch (Exception e){
            logger.error("提交作业消费队列消费异常" + e);
        }
    }
}
