# coding=utf-8
# create by oldman at 17/2/16
from django.contrib.auth.models import User
from django.core.management import BaseCommand
from gm_types.msg import CONVERSATION_TYPE, MESSAGE_TYPE

from api.tool.user_tool import get_user_by_id
from gaia import settings
from message.utils.message import send_one_message


class Command(BaseCommand):
    """
    python manage.py exe_20170216_send_privatemessage
    """

    def handle(self, *args, **options):
        file = open(u'/tmp/user.txt', 'r')
        message = u"""你被选中做用户电话回访啦，有美券相送！

如果你期待所长能为你带来更多优惠、帮你找到更多高水平医生，就回复所长你的【姓名+电话】；近期小秘书会与你电话沟通，回访完毕后就有美券啦！据说，回复私信的人会变美哦~"""
        for line in file:
            try:
                user_id = int(line)
                send_user = get_user_by_id(settings.BOSS)
                user = User.objects.get(id=user_id)
                target_user = user
                send_one_message(send_user, target_user=target_user, conversation=None,
                                 conversation_type=CONVERSATION_TYPE.MESSAGE,
                                 msg_type=MESSAGE_TYPE.TEXT,
                                 content={'text': message})
                print ('{} is finshed'.format(user_id))
            except Exception:
                print ('{} is error'.format(user_id))
                continue
