12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # -*- coding: utf-8 -*-
- #!/usr/bin/env python
- import os
- import simplejson as json
- import sys
- PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
- sys.path.insert(0, PROJECT_ROOT)
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.production')
- from script.base import init_env
- init_env(interactive = False)
- from apps.web.device.models import Device
- from apps.web.dealer.models import Dealer
- from apps.web.agent.models import Agent
- from bson.objectid import ObjectId
- from apps.web.core.messages.sms import SMSSender
- import datetime
- # 将所有未配置的100台设备以内经销商的设备,配置为自动充值
- dealers = Dealer.get_collection().find({},{'username': 1, 'agentId': 1, 'smsVendor': 1})
- dealer_map = {}
- for dealer in dealers[10000:50000]:
- try:
- if not dealer['agentId']:
- continue
- b = ObjectId(dealer['agentId'])
- dealer_map[str(dealer['_id'])] = dealer
- print dealer['_id']
- except :
- continue
- agent_product_map = {
- agent['_id']: agent.get('productName', '').encode('utf-8')
- for agent in Agent.get_collection()
- .find({'_id': {'$in': [ObjectId(_['agentId']) for _ in dealer_map.values()]}},
- {'productName': 1})
- }
- for dealerId in dealer_map.keys():
- try:
- devNos = [_.devNo for _ in Device.objects.filter(ownerId = dealerId).only('devNo')]
- if len(devNos) == 0:
- continue
- if len(devNos) > 200:
- print 'dev count > 200',dealerId,devNos
- continue
- unsetCount = Device.get_collection().find({'simExpireDate':{'$gte':datetime.datetime.now() - datetime.timedelta(days = 365)},'devNo':{'$in':devNos},'$and':[{'simChargeAuto':{'$ne':False}},{'simChargeAuto':{'$ne':True}}]}).count()
- if unsetCount == 0:
- continue
-
- Device.get_collection().update({'devNo':{'$in':devNos},'simChargeAuto':{'$ne':False}},{'$set':{'simChargeAuto':True}},multi = True)
- Device.invalid_many_device_cache(devNos)
- print 'change to auto charge',len(devNos)
-
- # 通知客户
- sms_sender = SMSSender()
- dealer = dealer_map[dealerId]
- response = sms_sender.send(phoneNumber = dealer['username'],
- templateName="SMS_NOTIFY_EXPIRED_DEVICE_TEMPLATEID",
- msg = u'请您务必重视,2021年部分客户总是忘记充值流量卡,导致停机换卡,为了防止运营商假期处理不及时,影响您的正常运营,2022年为了避免此类事宜,年初我们将您设备置为自动续费流量卡,如果希望取消自动续费,请登录系统直接更改配置。'.encode(
- 'utf-8'), productName = agent_product_map[ObjectId(dealer['agentId'])])
- print('sending sim expired alert to dealer(phone=%s) result=%s' % (dealer['username'], json.dumps(response),))
- if not response['result']:
- print('send sms to dealer failed, error=%s' % json.dumps(response))
-
- except Exception,e :
- continue
- # devices = Device.get_sim_expire_notify_devices()
- #
- #
- # from cytoolz import groupby
- # devMap = groupby('ownerId', devices)
- #
- # dealerMap = {} # type: Dict[ObjectId, dict]
- # for id_,devs in devMap.items():
- # try:
- # print len(devs)
- # if len(devs) <= 1000:
- # for dev in devs:
- # try:
- # devObj = Device.objects.get(logicalCode = dev['logicalCode'])
- # devObj.simChargeAuto = True
- # devObj.save()
- # Device.invalid_device_cache(devObj.devNo)
- # print devObj.devNo
- # except Exception,e:
- # print e
- # continue
- # except:
- # continue
|