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

import cn.axzo.basics.common.exception.ServiceException;
import cn.axzo.msg.center.common.utils.BizAssertions;
import cn.axzo.msg.center.dal.TodoDao;
import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.service.enums.TodoType;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

    private final TodoDao todoDao;

    void exposeSetCopiedToMeReadFailReason(String identityCode) {
        if (StringUtils.isNotBlank(identityCode)) {
            Todo todo = todoDao.findTodoByCode(identityCode).orElse(null);
            BizAssertions.assertNotNull(todo, "找不到对应的待办");
            //noinspection DataFlowIssue
            BizAssertions.assertTrue(todo.getType() == TodoType.COPIED_TO_ME, "只有抄送待办可以设置为已读");
            throw new ServiceException("指定的抄送已经为已读");
        } else {
            throw new ServiceException("没有未读的待办");
        }
    }

}