# # coding=utf-8
# import datetime
# from django.core.management import BaseCommand
# from django.db.models import Count
# from api.models import CouponInfo
# from api.models import Coupon
# from api.models import Points
# from api.models import POINTS_TYPE
# from api.models import BUSINESS_CHANNEL
# from api.models import COUPON_TYPE
# from api.models import Person
# from api.models import User
# import requests
#
# start_time = datetime.datetime.strptime('2015-10-01 00:00:00', '%Y-%m-%d %H:%M:%S')
# end_time = datetime.datetime.strptime('2015-12-30 19:00:00', '%Y-%m-%d %H:%M:%S')
#
# def _phone_valid(phone):
#     if not phone:
#         return False
#     if phone.isdigit() and len(phone) == 11 and phone != '18618133328':
#         return True
#     else:
#         return False
#
# def _send_the_msg(phone_list):
#     if len(phone_list) <= 0:
#         return
#     msg = '【更美APP】剁所长手涨你颜值。送你50元无门槛红包，仅1天有效。新年前“更美”一下：http://t.cn/R4fsrUT 退订回复TD'
#     data = {'sn': u'SDK-BBX-010-19243', 'pwd': u'E5D3CF92329CA75E6B794ADE37154616', 'mobile': phone_list, 'content': msg.encode(u'gbk'), 'ext': u'1', 'stime': u'', 'rrid': u''}
#     response = requests.post('http://sdk.entinfo.cn:8060/webservice.asmx/mt', data)
#     print response
#
# class Command(BaseCommand):
#     def handle(self, *args, **options):
#         phone_list = []
#         the_count = 0
#         user_activate_points = Points.objects.values('user').annotate(user_count=Count('user_id')).filter(reason=POINTS_TYPE.LOGIN).filter(add_time__gt=start_time).filter(add_time__lt=end_time)
#         for user_activate_point in user_activate_points:
#             the_count += 1
#             print the_count
#             if len(phone_list) == 9000:
#                 print len(phone_list), '...............................'
#                 _send_the_msg(phone_list)
#                 phone_list = []
#
#             user_id = user_activate_point['user']
#             person = Person.objects.filter(user_id=user_id).first()
#
#             if person and _phone_valid(person.phone):
#                 phone_list.append(person.phone)
#                 continue
#
#             user = User.objects.get(pk=user_id)
#
#             if _phone_valid(user.username):
#                 phone_list.append(person.phone)
#                 continue
#
#             try:
#                 user_extra = user.userextra
#                 if _phone_valid(user_extra.phone):
#                     phone_list.append(user_extra.phone)
#                     continue
#             except User.DoesNotExist:
#                 continue
#
#         print len(phone_list)
#         _send_the_msg(phone_list)