package com.sohu.tv.mq.cloud.service;

import com.sohu.tv.mq.cloud.bo.*;
import com.sohu.tv.mq.cloud.bo.Audit.StatusEnum;
import com.sohu.tv.mq.cloud.bo.Audit.TypeEnum;
import com.sohu.tv.mq.cloud.dao.AuditDao;
import com.sohu.tv.mq.cloud.dao.UserConsumerDao;
import com.sohu.tv.mq.cloud.dao.UserProducerDao;
import com.sohu.tv.mq.cloud.util.Result;
import com.sohu.tv.mq.cloud.util.Status;
import com.sohu.tv.mq.cloud.web.vo.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.remoting.protocol.body.ConsumerConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;
import java.util.stream.Collectors;

/**
 * audit服务
 * 
 * @Description:
 * @author yumeiwang
 * @date 2018年7月16日
 */
@Service
public class AuditService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AuditDao auditDao;

    @Autowired
    private AuditTopicService auditTopicService;

    @Autowired
    private AssociateProducerService associateProducerService;

    @Autowired
    private AssociateConsumerService associateConsumerService;

    @Autowired
    private AuditConsumerService auditConsumerService;

    @Autowired
    private AuditResendMessageService auditResendMessageService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private UserProducerService userProducerService;

    @Autowired
    private AuditTopicDeleteService auditTopicDeleteService;

    @Autowired
    private UserService userService;

    @Autowired
    private AuditTopicUpdateService auditTopicUpdateService;

    @Autowired
    private AuditConsumerDeleteService auditConsumerDeleteService;

    @Autowired
    private AuditResetOffsetService auditResetOffsetService;

    @Autowired
    private AuditUserProducerDeleteService auditUserProducerDeleteService;

    @Autowired
    private AuditUserConsumerDeleteService auditUserConsumerDeleteService;

    @Autowired
    private UserConsumerService userConsumerService;

    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private AuditTopicTraceService auditTopicTraceService;

    @Autowired
    private AuditBatchAssociateService auditBatchAssociateService;

    @Autowired
    private UserProducerDao userProducerDao;

    @Autowired
    private UserConsumerDao userConsumerDao;

    @Autowired
    private AuditConsumerConfigService auditConsumerConfigService;

    @Autowired
    private AuditTopicTrafficWarnService auditTopicTrafficWarnService;

    @Autowired
    private AuditTimespanMessageConsumeService auditTimespanMessageConsumeService;

    @Autowired
    private AuditWheelMessageCancelService auditWheelMessageCancelService;

    @Autowired
    private CancelUniqIdService cancelUniqIdService;

    @Autowired
    private ClusterService clusterService;

    @Autowired
    private AuditTimespanMessageExportService auditTimespanMessageExportService;

    @Autowired
    private AuditHttpConsumerConfigService auditHttpConsumerConfigService;

    /**
     * 查询列表
     * 
     * @param type
     * @return status
     */
    public Result<List<Audit>> queryAuditList(Audit audit) {
        List<Audit> auditList = null;
        try {
            auditList = auditDao.select(audit);
        } catch (Exception e) {
            logger.error("queryAuditList err, type:{}", audit.getType(), e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(auditList);
    }

    /**
     * 查询列表
     * 
     * @param type
     * @return status
     */
    public Result<Integer> queryCount(Audit audit) {
        Integer count = null;
        try {
            count = auditDao.selectByPageCount(audit);
        } catch (Exception e) {
            logger.error("selectByPageCount err, type:{}", audit.getType(), e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(count);
    }

    /**
     * 查询列表
     * 
     * @param type
     * @return status
     */
    public Result<List<Audit>> queryAuditListByPage(Audit audit, int offset, int size) {
        List<Audit> auditList = null;
        try {
            auditList = auditDao.selectByPage(audit, offset, size);
        } catch (Exception e) {
            logger.error("queryAuditListByPage err, type:{}", audit.getType(), e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(auditList);
    }

    /**
     * 保存审核admin信息
     * 
     * @param audit
     * @return
     */
    public Result<?> saveAuditAdmin(Audit audit) {
        try {
            auditDao.insert(audit);
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及topic信息
     * 
     * @param audit
     * @param topicParam
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndTopic(Audit audit, AuditTopic auditTopic) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditTopic
            if (count != null && count > 0) {
                auditTopic.setAid(audit.getId());
                auditTopicService.saveAuditTopic(auditTopic);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及consumer信息
     * 
     * @param audit
     * @param topicParam
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndConsumer(Audit audit, AuditConsumer auditConsumer) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditConsumer.setAid(audit.getId());
                auditConsumerService.saveAuditConsumer(auditConsumer);
            }
        } catch (DuplicateKeyException e) {
            logger.warn("duplicate key:{}", audit);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getResult(Status.DB_DUPLICATE_KEY).setMessage(auditConsumer.getConsumer() + "已存在");
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存用户关联的生产者
     * 
     * @param audit
     * @param auditAssociateProducer
     * @return
     */
    @Transactional
    public Result<Audit> saveAuditAndAssociateProducer(Audit audit, AuditAssociateProducer auditAssociateProducer) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditAssociateProducer
            if (count != null && count > 0) {
                auditAssociateProducer.setAid(audit.getId());
                associateProducerService.save(auditAssociateProducer);
            }
        } catch (DuplicateKeyException e) {
            logger.warn("duplicate key:{}", audit);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getResult(Status.DB_DUPLICATE_KEY);
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存用户关联的消费者
     * 
     * @param audit
     * @param AuditAssociateConsumer
     * @return
     */
    @Transactional
    public Result<Audit> saveAuditAndAssociateConsumer(Audit audit, AuditAssociateConsumer auditAssociateConsumer) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditAssociateProducer
            if (count != null && count > 0) {
                auditAssociateConsumer.setAid(audit.getId());
                associateConsumerService.save(auditAssociateConsumer);
            }
        } catch (DuplicateKeyException e) {
            logger.warn("duplicate key:{}", audit);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getResult(Status.DB_DUPLICATE_KEY);
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存批量关联数据
     * 
     * @param audit
     * @return
     */
    @Transactional
    public Result<Audit> saveAuditAndAssociateBatch(Audit audit, AuditBatchAssociate auditBatchAssociate) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存其他的
            if (count != null && count > 0) {
                auditBatchAssociate.setAid(audit.getId());
                auditBatchAssociateService.save(auditBatchAssociate);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及topic删除信息
     * 
     * @param audit
     * @param topicParam
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndTopicDelete(Audit audit, long tid, String topic) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditTopic
            if (count != null && count > 0) {
                auditTopicDeleteService.save(audit.getId(), tid, topic);
            }
        } catch (DuplicateKeyException e) {
            logger.warn("duplicate key:{}", audit);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getResult(Status.DB_DUPLICATE_KEY).setMessage("删除申请已存在");
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及topic trace信息
     * 
     * @param audit
     * @param topicParam
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndTopicTrace(Audit audit, long tid, int traceEnabled) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditTopic
            if (count != null && count > 0) {
                auditTopicTraceService.save(audit.getId(), tid, traceEnabled);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及topic流量突增预警信息
     *
     * @param audit
     * @param tid
     * @param trafficWarnEnabled
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndTopicTrafficWarn(Audit audit, long tid, int trafficWarnEnabled) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditTopic
            if (count != null && count > 0) {
                auditTopicTrafficWarnService.save(audit.getId(), tid, trafficWarnEnabled);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及topic更新信息
     * 
     * @param audit
     * @param auditTopicUpdate
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndTopicUpdate(Audit audit, AuditTopicUpdate auditTopicUpdate) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditTopicUpdate
            if (count != null && count > 0) {
                auditTopicUpdate.setAid(audit.getId());
                auditTopicUpdateService.save(auditTopicUpdate);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及consumer删除信息
     * 
     * @param audit
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndConsumerDelete(Audit audit, long cid, String consumer, String topic) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditConsumerDeleteService.save(audit.getId(), cid, consumer, topic);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及跳过堆积
     * 
     * @param audit
     * @param topicParam
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndSkipAccumulation(Audit audit, AuditResetOffset auditResetOffset) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditResetOffset.setAid(audit.getId());
                auditResetOffsetService.save(auditResetOffset);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及时间段消息消费
     * 
     * @param audit
     * @param topicParam
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndAuditTimespanMessageConsume(Audit audit,
            AuditTimespanMessageConsume auditTimespanMessageConsume) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditTimespanMessageConsume.setAid(audit.getId());
                auditTimespanMessageConsumeService.save(auditTimespanMessageConsume);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getOKResult();
    }

    /**
     * 保存审核以及时间段消息导出
     */
    @Transactional
    public Result<?> saveAuditAndAuditTimespanMessageExport(Audit audit, AuditTimespanMessageExport auditTimespanMessageExport) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditTimespanMessageExport.setAid(audit.getId());
                auditTimespanMessageExportService.save(auditTimespanMessageExport);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getOKResult();
    }

    /**
     * 保存审核以及定时消息取消申请
     *
     * @param audit
     * @param auditWheelMessageCancels
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndAuditWheelMessageCancels(Audit audit, List<AuditWheelMessageCancel> auditWheelMessageCancels){
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                Long auditId = audit.getId();
                auditWheelMessageCancels.forEach(auditWheelMessageCancel -> auditWheelMessageCancel.setAid(auditId));
                auditWheelMessageCancelService.saveBatch(auditWheelMessageCancels);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getOKResult();
    }

    /**
     * 查询取消类消息是否重复申请
     * 检查是否cancelUniqId表记录 || 检查申请记录（待审核）
     * @param uniqIds
     * @param tid
     * @return status
     */
    public Result<List<String>> queryWheelCancelByUniqIdsAndTid(List<String> uniqIds, Long tid) {
        // 第一步 校验该uniqId是否已经成功取消
        Result<List<CancelUniqId>> cancelUniqIdResult = cancelUniqIdService.queryByUniqIds(uniqIds, tid);
        if (cancelUniqIdResult.isNotOK()) {
            return Result.getDBErrorResult(cancelUniqIdResult.getException());
        }
        if (CollectionUtils.isNotEmpty(cancelUniqIdResult.getResult())) {
            List<String> existUniqIds = cancelUniqIdResult.getResult().stream()
                    .map(CancelUniqId::getUniqueId).collect(Collectors.toList());
            return Result.getResult(existUniqIds);
        }
        // 到了这一步说明还没成功取消，还需校验是否存在处于正在审核中的取消申请
        Result<List<String>> auditWheelCheckResult = auditWheelMessageCancelService.checkExistsByUniqIdAndTopic(tid, uniqIds);
        if (auditWheelCheckResult.isNotOK()) {
            return Result.getDBErrorResult(auditWheelCheckResult.getException());
        }
        if (CollectionUtils.isNotEmpty(auditWheelCheckResult.getResult())) {
            return Result.getResult(auditWheelCheckResult.getResult());
        }
        return Result.getOKResult();
    }

    /**
     * 查询
     * 
     * @param type
     * @return status
     */
    public Result<Audit> queryAudit(long id) {
        Audit audit = null;
        try {
            audit = auditDao.selectById(id);
        } catch (Exception e) {
            logger.error("queryAudit err, id:{}", id, e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 更新
     * 
     * @param type
     * @return status
     */
    public Result<Integer> updateAudit(Audit audit, int oldStatus) {
        Integer result = null;
        try {
            result = auditDao.update(audit, oldStatus);
            if (result == 0) {
                return Result.getResult(Status.AUDITED);
            }
        } catch (Exception e) {
            logger.error("updateAudit err, audit:{}", audit, e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(result);
    }

    /**
     * 更新
     * 
     * @param type
     * @return status
     */
    public Result<Integer> updateAuditStatus(long aid, int newStatus, int oldStatus) {
        Integer result = null;
        try {
            result = auditDao.updateStatus(aid, newStatus, oldStatus);
        } catch (Exception e) {
            logger.error("updateAuditStatus err, aid:{} newStatus:{} oldStatus:{}", aid, newStatus, oldStatus, e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(result);
    }

    /**
     * 保存审核以及userProducer删除信息
     * 
     * @param audit
     * @param pid
     * @param producer
     * @param topic
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndUserProducerDelete(Audit audit, long pid, String producer, String topic, long uid) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditUserProducerDeleteService.save(audit.getId(), pid, producer, topic, uid);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    @Transactional
    public Result<?> saveAuditAndUserConsumerDelete(Audit audit, long ucid, String consumer, String topic, long uid) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            if (count != null && count > 0) {
                auditUserConsumerDeleteService.save(audit.getId(), ucid, consumer, topic, uid);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 保存审核以及重发消息信息
     * 
     * @param audit
     * @param auditResendMessageList
     * @return
     */
    @Transactional
    public Result<?> saveAuditAndAuditResendMessage(Audit audit, List<AuditResendMessage> auditResendMessageList,
            AuditResendMessageConsumer auditResendMessageConsumer) {
        Long count = null;
        try {
            count = auditDao.insert(audit);
            // 如果保存成功，保存auditResendMessageList
            if (count != null && count > 0) {
                for (AuditResendMessage msg : auditResendMessageList) {
                    msg.setAid(audit.getId());
                }
                auditResendMessageConsumer.setAid(audit.getId());
                auditResendMessageService.save(auditResendMessageList, auditResendMessageConsumer);
            }
        } catch (Exception e) {
            logger.error("insert err, audit:{}", audit, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(audit);
    }

    /**
     * 根据uid查询列表
     * 
     * @param type
     * @return status
     */
    public Result<List<Audit>> queryAuditList(long uid, int offset, int size) {
        List<Audit> auditList = null;
        try {
            auditList = auditDao.selectByUid(uid, offset, size);
        } catch (Exception e) {
            logger.error("queryAuditList err, uid:{}", uid, e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(auditList);
    }
    
    /**
     * 查询用户审核量
     * 
     * @param uid
     * @return
     */
    public Result<Integer> queryAuditCount(long uid) {
        Integer count = null;
        try {
            count = auditDao.selectCountByUid(uid);
        } catch (Exception e) {
            logger.error("queryAuditCount err, uid:{}", uid, e);
            return Result.getDBErrorResult(e);
        }
        return Result.getResult(count);
    }

    /**
     * 申请详情
     * 
     * @param aid
     * @param map
     * @return
     */
    public Result<?> detail(TypeEnum typeEnum, long aid) {
        switch (typeEnum) {
            case NEW_TOPIC:
                return auditTopicService.queryAuditTopic(aid);
            case UPDATE_TOPIC:
                return getUpdateTopicResult(aid);
            case DELETE_TOPIC:
                return getDeleteTopicResult(aid);
            case NEW_CONSUMER:
                return getAuditConumerResult(aid);
            case DELETE_CONSUMER:
                return getDeleteConsumerResult(aid);
            case RESET_OFFSET:
            case RESET_OFFSET_TO_MAX:
            case RESET_RETRY_OFFSET:
                return getResetOffsetResult(aid);
            case ASSOCIATE_PRODUCER:
            case NEW_PRODUCER:
                return getAuditAssociateProducerResult(aid, typeEnum.getType());
            case ASSOCIATE_CONSUMER:
                return getAuditAssociateConsumerResult(aid);
            case BECOME_ADMIN:
                return getBecomAdminResult(aid);
            case DELETE_USERPRODUCER:
                return getDeleteUserProducerResult(aid);
            case DELETE_USERCONSUMER:
                return getDeleteUserConsumerResult(aid);
            case RESEND_MESSAGE:
                return getResendMessageResult(aid);
            case UPDATE_TOPIC_TRACE:
                return getUpdateTopicTraceResult(aid);
            case BATCH_ASSOCIATE:
                return getAuditBatchAssociateResult(aid);
            case PAUSE_CONSUME:
            case RESUME_CONSUME:
                return getConsumerConfig(aid);
            case LIMIT_CONSUME:
                return getConsumerConfig(aid);
            case UPDATE_TOPIC_TRAFFIC_WARN:
                return getUpdateTopicTrafficWarnResult(aid);
            case TIMESPAN_MESSAGE_CONSUME:
                return getTimespanMessageConsumeResult(aid);
            case CANCEL_WHEEL_MSG:
                return getCancelWheelMsgResult(aid);
            case TIMESPAN_MESSAGE_EXPORT:
                return getTimespanMessageExportResult(aid);
            case UPDATE_HTTP_CONSUMER_CONFIG:
                return getUpdateHttpConsumerConfigResult(aid);
        }
        return null;
    }

    /**
     * 获取 topic申请删除 信息
     * 
     * @param aid
     * @return Result
     */
    private Result<?> getDeleteTopicResult(long aid) {
        Result<Audit> auditResult = queryAudit(aid);
        if (auditResult.isNotOK()) {
            return auditResult;
        }
        // 查询 topic删除审核记录
        Result<AuditTopicDelete> auditTopicDeleteResult = auditTopicDeleteService.queryAuditTopicDelete(aid);
        if (auditTopicDeleteResult.isNotOK()) {
            return auditTopicDeleteResult;
        }
        // 已经同意过的，数据已经删除了
        if (StatusEnum.AGREE.getStatus() == auditResult.getResult().getStatus()) {
            AuditTopicDeleteVO auditTopicDeleteVO = new AuditTopicDeleteVO();
            Topic t = new Topic();
            t.setName(auditTopicDeleteResult.getResult().getTopic());
            auditTopicDeleteVO.setTopic(t);
            auditTopicDeleteVO.setAid(aid);
            return Result.getResult(auditTopicDeleteVO);
        }
        AuditTopicDelete auditTopicDelete = auditTopicDeleteResult.getResult();
        // 查询topic记录
        Result<Topic> topicResult = topicService.queryTopic(auditTopicDelete.getTid());
        if (topicResult.isNotOK()) {
            return topicResult;
        }
        // 查询该topic生产者记录
        Result<List<UserProducer>> userProducerListResult = userProducerService
                .queryUserProducerByTid(auditTopicDelete.getTid());
        List<UserProducer> userProducerList = null;
        if (userProducerListResult.isNotEmpty()) {
            userProducerList = userProducerListResult.getResult();
            // 查询用户
            List<Long> uidList = new ArrayList<Long>();
            for (UserProducer up : userProducerList) {
                uidList.add(up.getUid());
            }
            Result<List<User>> userListResult = userService.query(uidList);
            if (userListResult.isEmpty()) {
                return userListResult;
            }

            // 赋值用户
            for (UserProducer up : userProducerList) {
                for (User u : userListResult.getResult()) {
                    if (up.getUid() == u.getId()) {
                        if (StringUtils.isBlank(u.getName())) {
                            up.setUsername(u.getEmail());
                        } else {
                            up.setUsername(u.getName());
                        }
                        break;
                    }
                }
            }
        }

        // 组装vo
        AuditTopicDeleteVO auditTopicDeleteVO = new AuditTopicDeleteVO();
        auditTopicDeleteVO.setTopic(topicResult.getResult());
        auditTopicDeleteVO.setUserProducerList(userProducerList);
        auditTopicDeleteVO.setAid(aid);
        return Result.getResult(auditTopicDeleteVO);
    }

    /**
     * 获取审核消费者
     * 
     * @param aid
     * @return Result
     */
    private Result<?> getAuditConumerResult(long aid) {
        Result<AuditConsumer> auditConsumerResult = auditConsumerService.queryAuditConsumer(aid);
        if (auditConsumerResult.isNotOK()) {
            return auditConsumerResult;
        }
        AuditConsumer auditConsumer = auditConsumerResult.getResult();
        Result<Topic> topicResult = topicService.queryTopic(auditConsumer.getTid());
        if (topicResult.isNotOK()) {
            return topicResult;
        }
        AuditConsumerVO auditConsumerVO = new AuditConsumerVO();
        BeanUtils.copyProperties(auditConsumer, auditConsumerVO);
        auditConsumerVO.setTopic(topicResult.getResult().getName());
        return Result.getResult(auditConsumerVO);
    }

    /**
     * 获取 topic更新 信息
     * 
     * @param aid
     * @return Result
     */
    private Result<?> getUpdateTopicResult(long aid) {
        Result<AuditTopicUpdate> auditTopicUpdateResult = auditTopicUpdateService.queryAuditTopicUpdate(aid);
        if (auditTopicUpdateResult.isNotOK()) {
            return auditTopicUpdateResult;
        }
        AuditTopicUpdate auditTopicUpdate = auditTopicUpdateResult.getResult();
        // 查询topic记录
        Result<Topic> topicResult = topicService.queryTopic(auditTopicUpdate.getTid());
        if (topicResult.isNotOK()) {
            return topicResult;
        }

        // 组装vo
        AuditTopicUpdateVO auditTopicUpdateVO = new AuditTopicUpdateVO();
        BeanUtils.copyProperties(auditTopicUpdate, auditTopicUpdateVO);
        auditTopicUpdateVO.setTopic(topicResult.getResult());
        return Result.getResult(auditTopicUpdateVO);
    }

    /**
     * 获取 topic更新trace信息
     * 
     * @param aid
     * @return Result
     */
    private Result<?> getUpdateTopicTraceResult(long aid) {
        Result<AuditTopicTrace> auditTopicTraceResult = auditTopicTraceService.queryAuditTopicTrace(aid);
        if (auditTopicTraceResult.isNotOK()) {
            return auditTopicTraceResult;
        }
        AuditTopicTrace auditTopicTrace = auditTopicTraceResult.getResult();
        // 查询topic记录
        Result<Topic> topicResult = topicService.queryTopic(auditTopicTrace.getTid());
        if (topicResult.isNotOK()) {
            return topicResult;
        }

        // 组装vo
        AuditTopicTraceVO auditTopicTraceVO = new AuditTopicTraceVO();
        BeanUtils.copyProperties(auditTopicTrace, auditTopicTraceVO);
        auditTopicTraceVO.setTopic(topicResult.getResult());
        return Result.getResult(auditTopicTraceVO);
    }

    /**
     * 获取重发消息详情
     * 
     * @param aid
     * @return
     */
    private Result<?> getResendMessageResult(long aid) {
        // 查询audit
        Result<Audit> auditResult = queryAudit(aid);
        if (auditResult.isNotOK()) {
            return auditResult;
        }
        // 查询AuditResendMessage
        Audit audit = auditResult.getResult();
