package cn.axzo.msg.center.message.service.todo;

import cn.axzo.msg.center.dal.TodoBusinessDao;
import cn.axzo.msg.center.dal.TodoDao;
import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.domain.entity.TodoBusiness;
import cn.axzo.msg.center.service.pending.request.AnalysisInfo;
import cn.axzo.msg.center.service.pending.request.PendingMessagePageRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author yanglin
 */
@Component
@RequiredArgsConstructor
class AnalysisHelper {

    private final TodoBusinessDao todoBusinessDao;
    private final TodoDao todoDao;

    TodoBusiness getAnalyzeBusiness(AnalysisInfo analysis) {
        if (analysis == null) return null;
        return todoBusinessDao.getById(analysis.getBusinessId());
    }

    Todo getAnalyzeTodo(PendingMessagePageRequest request, AnalysisInfo analysis) {
        if (analysis.getTodoId() != null)
            return todoDao.getById(analysis.getTodoId());
        if (analysis.getTodoCode() != null)
            return todoDao.lambdaQuery()
                    .eq(Todo::getIdentityCode, analysis.getTodoCode())
                    .last("LIMIT 1")
                    .one();
        if (analysis.getBizCode() != null) {
            return todoDao.lambdaQuery()
                    .eq(Todo::getBizCode, analysis.getBizCode())
                    .eq(Todo::getExecutorPersonId, request.getPersonId())
                    .last("LIMIT 1")
                    .one();
        }
        return null;
    }

}