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

import cn.axzo.basics.common.BeanMapper;
import cn.axzo.msg.center.api.mq.TodoInfo;
import cn.axzo.msg.center.api.mq.TodoUpdateMessage;
import cn.axzo.msg.center.dal.TodoBusinessDao;
import cn.axzo.msg.center.dal.TodoBusinesses;
import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.domain.entity.TodoBusiness;
import cn.axzo.msg.center.message.service.todo.manage.TodoExt;
import cn.axzo.msg.center.mq.MqMessageRecord;
import cn.axzo.msg.center.mq.MqProducer;
import cn.axzo.msg.center.service.enums.MqMessageType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author yanglin
 */
@Component
@RequiredArgsConstructor
public class TodoMqBroadcaster {

    private final MqProducer mqProducer;
    private final TodoBusinessDao todoBusinessDao;

    public void fireTodoUpdated(String operation, List<Todo> todos) {
        TodoBusinesses businesses = todoBusinessDao.getBusinesses(todos);
        for (Todo todo : todos)
            fireTodoUpdated(operation, businesses, todo);
    }

    private void fireTodoUpdated(String operation, TodoBusinesses businesses, Todo todo) {
        TodoUpdateMessage message = new TodoUpdateMessage();
        message.setOperation(operation);
        message.setUpdatedTodo(createTodoInfo(businesses, todo));
        mqProducer.send(MqMessageRecord
                .builder(MqMessageType.TODO_STATE_UPDATE, message)
                .messageKey(todo.getId())
                .shardingKey(todo.getTemplateCode())
                .build());
    }

    public TodoInfo createTodoInfo(TodoBusinesses businesses, Todo todo) {
        TodoInfo todoInfo = BeanMapper.copyBean(todo, TodoInfo.class);
        todoInfo.setBizExtParam(TodoExt.copyBizExtParam(todo, true));
        todoInfo.setRouterParams(TodoExt.copyRouterExtParam(todo, true));
        todoInfo.setTodoId(todo.getId());
        todoInfo.setTodoBusinessId(todo.getTodoBusinessId());
        todoInfo.setExecutorOuId(todo.getOuId());
        todoInfo.setExecutorWorkspaceId(todo.getOrgId());
        todoInfo.setExecutorWorkspaceName(todo.getOrgName());
        TodoBusiness business = businesses.findBusiness(todo).orElse(null);
        if (business != null) {
            todoInfo.setBizFinalState(business.getBizFinalState());
            todoInfo.setBizFlag(business.getBizFlag());
            todoInfo.setPromoterType(business.getPromoterType());
            todoInfo.setPromoterPersonId(business.getPromoterPersonId());
            todoInfo.setPromoterName(business.getPromoterName());
            todoInfo.setPromoterId(business.getPromoterId());
            todoInfo.setPromoterOuId(business.getOuId());
            todoInfo.setPromoterWorkspaceId(business.getOrgId());
            todoInfo.setPromoterWorkspaceName(business.getOrgName());
            todoInfo.setBizCategory(business.getBizCategory());
            todoInfo.setDeadline(business.getDeadline());
        }
        return todoInfo;
    }

}