
from common.accounts.models import UserRoles, Roles
from crm.plan.models import Message, UserMessage


def send_message(**kwargs):
    message = Message.objects.create(
        title=kwargs.get("title"),

        content=kwargs.get("content"),
        creater=kwargs.get("user"),
        customer_id=kwargs.get("customer_id"),
        limit_time=kwargs.get("limit_time"),
        plan_id=kwargs.get("plan_id"),
        type=kwargs.get("type"),
        task_id=kwargs.get("task_id")
    )
    role = Roles.objects.filter(name__icontains="销售主管").first()
    user_id = UserRoles.objects.filter(role=role).values_list("user_id", flat=True)
    for item in user_id:
        UserMessage.objects.create(
            message=message,
            user_id=item
        )
