def monitor_click(task): #监控点击情况 mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() # items =mongodb.transform_rate.find()#检测发送超过一天的统计记录 条件通过createtime format = "%Y-%m-%d" todaystr = datetime.now().strftime("%Y-%m-%d") print todaystr today = datetime.strptime(todaystr, format) yesterday = today - timedelta(days=1) todayStamp = time.mktime(today.timetuple()) yesterdayStamp = time.mktime(yesterday.timetuple()) print todayStamp print yesterdayStamp ret = mongodb.push_record.find({ "createtime": { "$gt": int(yesterdayStamp), "$lt": int(todayStamp) }, "type": 1 }) for item in ret: if item["click"] == 0: sqldb.execute( "update supplier set maxpush=maxpush+1 where mobile=%s", item["sendid"])
def storeClick(rdd): if rdd.isEmpty() is False: resultList = rdd.collect() for item in resultList: mongodb = PymongoDateBase.instance().get_db() uuid = item["uuid"] colleciton = mongodb.push_record record = colleciton.find_one({"uuid": uuid}) if record: click = record["click"] type = record["type"] sendid = record["sendid"] if click == 0: sqldb = database.instance().get_session() if type == 1: #短信推送 supplier = sqldb.query( "select id,pushstatus from supplier where mobile=%s ", sendid) print supplier if supplier and supplier[0]["pushstatus"] != 2: sqldb.execute( "update supplier set pushscore=pushscore+1 where mobile=%s", sendid) else: sqldb.execute( "update users set pushscore=pushscore+1 where phone=%s", sendid) elif type == 2: #微信推送 sqldb.execute( "update users set pushscore=pushscore+1 where openid=%s", sendid) click = click + 1 colleciton.update({'uuid': uuid}, {'$set': {'click': click}})
def analysis_notify():#每天九点报价回复情况,生成提醒 print "start analysi_notify" sqldb = database.instance().get_session() ret=sqldb.query("select id,purchaseid from purchase_info where status!=0") for item in ret: purchaseinfoid=item["id"] purchaseid=item["purchaseid"] purchase=sqldb.get("select term, createtime from purchase where id=%s",purchaseid) if purchase: now=time.time() exprie=int(purchase["createtime"])+(int(purchase["term"])*24*60*60) if now>exprie: print "purchaseinfoid=%s,has exprie"%purchaseinfoid continue ret=sqldb.query("select id from quote where purchaseinfoid =%s" ,purchaseinfoid) quote_num=len(ret) if quote_num!=0: ret=sqldb.query("select id from quote where purchaseinfoid =%s and state!=0" , purchaseinfoid) reply_num=len(ret) ret=sqldb.query("select id,createtime from quote where purchaseinfoid =%s order by createtime" , purchaseinfoid) latest_time=ret[0]["createtime"] if (int(time.time())-int(latest_time))>notify_days*24*60*60: print reply_num/(quote_num*1.0) if reply_num/(quote_num*1.0)<reply_rate: task = {"purchaseinfoid": str(purchaseinfoid), "tasktype": 2,"channel":1} print task task_generate.apply_async(args=[task]) task = {"purchaseinfoid": str(purchaseinfoid), "tasktype": 2,"channel":2} print task task_generate.apply_async(args=[task])
def writeDB(rdd): if rdd.isEmpty() is False: resultList=rdd.collect() for item in resultList: mongodb = PymongoDateBase.instance().get_db() colleciton = mongodb.push_record uuid=item["uuid"] quoteid=item["quoteid"] sqldb = database.instance().get_session() ret=sqldb.get("select purchaseinfoid from quote where id=%s",quoteid) if ret: qinfoid=ret["purchaseinfoid"] record = colleciton.find_one({"uuid": uuid}) if record: pushid=record["pushid"] transform=mongodb.transform_rate.find_one({"_id":pushid}) if transform: purchaseinfoid=transform["purchaseinfoid"] quote=[] if transform["quote"]!="": quote=transform["quote"].split(",") if str(qinfoid)==str(purchaseinfoid):#如果是推送的批次采购则进入统计 if str(quoteid) not in quote: quote.append(str(quoteid)) mongodb.transform_rate.update({"_id":pushid}, {'$set':{'quote':','.join(quote)}})
def storeClick(rdd): if rdd.isEmpty() is False: resultList = rdd.collect() for item in resultList: mongodb = PymongoDateBase.instance().get_db() uuid = item["uuid"] colleciton = mongodb.push_record record=colleciton.find_one({"uuid":uuid}) if record: click=record["click"] type=record["type"] sendid=record["sendid"] if click==0: sqldb = database.instance().get_session() if type==1:#短信推送 supplier=sqldb.query("select id,pushstatus from supplier where mobile=%s ",sendid) print supplier if supplier and supplier[0]["pushstatus"]!=2: sqldb.execute("update supplier set pushscore=pushscore+1 where mobile=%s",sendid) else: sqldb.execute("update users set pushscore=pushscore+1 where phone=%s", sendid) elif type==2:#微信推送 sqldb.execute("update users set pushscore=pushscore+1 where openid=%s", sendid) click=click+1 colleciton.update({'uuid':uuid}, {'$set':{'click':click}})
def writeDB(rdd): if rdd.isEmpty() is False: resultList = rdd.collect() for item in resultList: mongodb = PymongoDateBase.instance().get_db() colleciton = mongodb.push_record uuid = item["uuid"] quoteid = item["quoteid"] sqldb = database.instance().get_session() ret = sqldb.get("select purchaseinfoid from quote where id=%s", quoteid) if ret: qinfoid = ret["purchaseinfoid"] record = colleciton.find_one({"uuid": uuid}) if record: pushid = record["pushid"] transform = mongodb.transform_rate.find_one( {"_id": pushid}) if transform: purchaseinfoid = transform["purchaseinfoid"] quote = [] if transform["quote"] != "": quote = transform["quote"].split(",") if str(qinfoid) == str( purchaseinfoid): #如果是推送的批次采购则进入统计 if str(quoteid) not in quote: quote.append(str(quoteid)) mongodb.transform_rate.update( {"_id": pushid}, {'$set': { 'quote': ','.join(quote) }})
def analysis_record():#每天九点定时检测 print "start analysis_record start" mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() #items =mongodb.transform_rate.find()#检测发送超过一天的统计记录 条件通过createtime format="%Y-%m-%d" todaystr=datetime.now().strftime("%Y-%m-%d") today=datetime.strptime(todaystr, format) yesterday=today- timedelta(days=1) todayStamp = time.mktime(today.timetuple()) yesterdayStamp =time.mktime(yesterday.timetuple()) print todayStamp print yesterdayStamp func = ''' function(obj,prev){ if (obj.order>prev.latest_order){ prev.latest_order=obj.order prev.latest_id=obj._id } } ''' ret = mongodb.transform_rate.group(['purchaseinfoid','type'], {"createtime":{"$gt":int(yesterdayStamp),"$lt":int(todayStamp)}}, {"latest_order": 0, "latest_id": ""}, func) for item in ret : print item id=item["latest_id"] transform=mongodb.transform_rate.find_one({"_id":id}) type=transform["type"] order=transform["order"] if order > max_push_time: continue purchaseinfoid=transform["purchaseinfoid"] purchaseinfo=sqldb.get("select status from purchase_info where id=%s",purchaseinfoid) if purchaseinfo["status"]==0: print "purchaseinfo status==0" continue accept = sqldb.query("select id from quote where purchaseinfoid=%s and state=1" % purchaseinfoid) if len(accept)>0: print "purchaseinfo accept=%s"%len(accept) continue quote_count =0 if transform["quote"]!="": quote_count=len(transform["quote"].split(",")) push_count=mongodb.push_record.find({"pushid":id}).count() if push_count!=0: click_count=mongodb.push_record.find({"pushid":id,"click":{'$gt':0}}).count() reject_rate=1 if quote_count!=0: aject = sqldb.query("select id from quote where id in(%s) and state=2" % transform["quote"]) quote_aject_count = len(aject) reject_rate=quote_aject_count/(quote_count*1.0) if click_count/(push_count*1.0)<click_rate or quote_count/(push_count*1.0)<quote_rate or reject_rate>reject_quote_rate : task = {"purchaseinfoid": purchaseinfoid, "tasktype": 1,"channel":type} print "task_generate task=%s"%task task_generate.apply_async(args=[task])
def analysis_notify(): #每天九点报价回复情况,生成提醒 print "start analysi_notify" sqldb = database.instance().get_session() ret = sqldb.query( "select id,purchaseid from purchase_info where status!=0") for item in ret: purchaseinfoid = item["id"] purchaseid = item["purchaseid"] purchase = sqldb.get( "select term, createtime from purchase where id=%s", purchaseid) if purchase: now = time.time() exprie = int(purchase["createtime"]) + (int(purchase["term"]) * 24 * 60 * 60) if now > exprie: print "purchaseinfoid=%s,has exprie" % purchaseinfoid continue ret = sqldb.query("select id from quote where purchaseinfoid =%s", purchaseinfoid) quote_num = len(ret) if quote_num != 0: ret = sqldb.query( "select id from quote where purchaseinfoid =%s and state!=0", purchaseinfoid) reply_num = len(ret) ret = sqldb.query( "select id,createtime from quote where purchaseinfoid =%s order by createtime", purchaseinfoid) latest_time = ret[0]["createtime"] if (int(time.time()) - int(latest_time)) > notify_days * 24 * 60 * 60: print reply_num / (quote_num * 1.0) if reply_num / (quote_num * 1.0) < reply_rate: task = { "purchaseinfoid": str(purchaseinfoid), "tasktype": 2, "channel": 1 } print task task_generate.apply_async(args=[task]) task = { "purchaseinfoid": str(purchaseinfoid), "tasktype": 2, "channel": 2 } print task task_generate.apply_async(args=[task])
def monitor_click(task): #监控点击情况 mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() # items =mongodb.transform_rate.find()#检测发送超过一天的统计记录 条件通过createtime format = "%Y-%m-%d" todaystr = datetime.now().strftime("%Y-%m-%d") print todaystr today = datetime.strptime(todaystr, format) yesterday = today - timedelta(days=1) todayStamp = time.mktime(today.timetuple()) yesterdayStamp = time.mktime(yesterday.timetuple()) print todayStamp print yesterdayStamp ret = mongodb.push_record.find({"createtime": {"$gt": int(yesterdayStamp), "$lt": int(todayStamp)}, "type": 1}) for item in ret: if item["click"] == 0: sqldb.execute("update supplier set maxpush=maxpush+1 where mobile=%s", item["sendid"])
def analysis_record(): #每天九点定时检测 print "start analysis_record start" mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() #items =mongodb.transform_rate.find()#检测发送超过一天的统计记录 条件通过createtime format = "%Y-%m-%d" todaystr = datetime.now().strftime("%Y-%m-%d") today = datetime.strptime(todaystr, format) yesterday = today - timedelta(days=1) todayStamp = time.mktime(today.timetuple()) yesterdayStamp = time.mktime(yesterday.timetuple()) print todayStamp print yesterdayStamp func = ''' function(obj,prev){ if (obj.order>prev.latest_order){ prev.latest_order=obj.order prev.latest_id=obj._id } } ''' ret = mongodb.transform_rate.group( ['purchaseinfoid', 'type'], {"createtime": { "$gt": int(yesterdayStamp), "$lt": int(todayStamp) }}, { "latest_order": 0, "latest_id": "" }, func) for item in ret: print item id = item["latest_id"] transform = mongodb.transform_rate.find_one({"_id": id}) type = transform["type"] order = transform["order"] if order > max_push_time: continue purchaseinfoid = transform["purchaseinfoid"] purchaseinfo = sqldb.get( "select status from purchase_info where id=%s", purchaseinfoid) if purchaseinfo["status"] == 0: print "purchaseinfo status==0" continue accept = sqldb.query( "select id from quote where purchaseinfoid=%s and state=1" % purchaseinfoid) if len(accept) > 0: print "purchaseinfo accept=%s" % len(accept) continue quote_count = 0 if transform["quote"] != "": quote_count = len(transform["quote"].split(",")) push_count = mongodb.push_record.find({"pushid": id}).count() if push_count != 0: click_count = mongodb.push_record.find({ "pushid": id, "click": { '$gt': 0 } }).count() reject_rate = 1 if quote_count != 0: aject = sqldb.query( "select id from quote where id in(%s) and state=2" % transform["quote"]) quote_aject_count = len(aject) reject_rate = quote_aject_count / (quote_count * 1.0) if click_count / (push_count * 1.0) < click_rate or quote_count / ( push_count * 1.0) < quote_rate or reject_rate > reject_quote_rate: task = { "purchaseinfoid": purchaseinfoid, "tasktype": 1, "channel": type } print "task_generate task=%s" % task task_generate.apply_async(args=[task])
def task_generate(task): #生成发送任务 mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() tasktype = task["tasktype"] purchaseinfoid = str(task["purchaseinfoid"]) channel = task["channel"] record = mongodb.celery_task.find({ "purchaseinfoid": purchaseinfoid, "channel": channel, "tasktype": tasktype }).sort("order", pymongo.DESCENDING) if record.count() == 0: order = 1 else: order = record[0]["order"] + 1 print "start order %s" % order purchaseinfo = sqldb.get( "select pi.id purchaseinfoid,pi.varietyid,pi.status,p.userid uid from purchase_info pi left join purchase p on pi.purchaseid = p.id where pi.id = %s", purchaseinfoid) if purchaseinfo == None: return elif purchaseinfo["status"] == 0: return if tasktype == 1: sendids = set() filtersend = [] if order > max_push_time: return if order != 1: #不是第一次发送,过滤发送过的 records = mongodb.transform_rate.find({ "purchaseinfoid": purchaseinfoid, "type": channel }) for item in records: pushrecord = mongodb.push_record.find({"pushid": item["_id"]}) for item in pushrecord: filtersend.append(item["sendid"]) if channel == 1: #短信渠道 print "channel=%s" % channel phonecondition = "" filtersend = [str(i) for i in filtersend] if filtersend != []: phonecondition = " and phone not in(%s)" % ",".join(filtersend) task = { "purchaseinfoid": purchaseinfoid, "tasktype": tasktype, "channel": 1, "order": order, "status": 0, "createtime": int(time.time()) } userphones = sqldb.query( "select phone from users where find_in_set(%s,varietyids) and maxpush<3 " + phonecondition + " order by pushscore limit 0,%s", purchaseinfo["varietyid"], max_wx_num) if filtersend != []: phonecondition = " and mobile not in(%s)" % ",".join( filtersend) yt = sqldb.query( "select mobile from supplier where find_in_set(%s,variety) and mobile != '' and pushstatus=1 and maxpush<3 " + phonecondition + " order by pushscore limit 0, %s", purchaseinfo["varietyid"], max_phone_num) for i in userphones: sendids.add(str(i["phone"])) for j in yt: sendids.add(str(j["mobile"])) sendids = list(set(sendids)) print "sendids= %s" % sendids elif channel == 2: #微信渠道 print "channel=%s" % channel wxcondition = "" filtersend = ["'" + str(i) + "'" for i in filtersend] if filtersend != []: wxcondition = " and openid not in(%s)" % ",".join(filtersend) task = { "purchaseinfoid": purchaseinfoid, "tasktype": tasktype, "channel": 2, "order": order, "status": 0, "createtime": int(time.time()) } userwxs = sqldb.query( "select openid from users where find_in_set(%s,varietyids) and openid!=''" + wxcondition + " order by pushscore", purchaseinfo["varietyid"]) for i in userwxs: sendids.add(str(i["openid"])) sendids = list(set(sendids)) print "sendids=%s" % sendids if len(sendids) != 0: taskid = mongodb.celery_task.insert(task) taskinfo = {"taskid": taskid, "sendlist": ",".join(sendids)} collection = mongodb.celery_task_info collection.insert(taskinfo) sendkafka.apply_async(args=[taskid]) else: #提醒采购商 if order != 1: if order > max_notify_time or (int(time.time()) - int( record[0]["createtime"])) < notify_days * 24 * 60 * 60: return sendids = [] if channel == 1: userphone = sqldb.get("select id,phone from users where id=%s", purchaseinfo["uid"]) if userphone: sendids.append(userphone["phone"]) elif channel == 2: useropenid = sqldb.get("select id,openid from users where id=%s", purchaseinfo["uid"]) if useropenid: sendids.append(useropenid["openid"]) useropenid2 = sqldb.get("select id,openid2 from users where id=%s", purchaseinfo["uid"]) if useropenid: sendids.append(useropenid2["openid2"]) print "channel=%s,sendid=%s" % (channel, sendids) if sendids != []: for sendid in sendids: task = { "purchaseinfoid": purchaseinfoid, "tasktype": tasktype, "channel": channel, "order": order, "status": 0, "createtime": int(time.time()) } taskid = mongodb.celery_task.insert(task) taskinfo = {"taskid": taskid, "sendlist": sendid} collection = mongodb.celery_task_info collection.insert(taskinfo) sendkafka.apply_async(args=[taskid])
def sendPush(rdd): if rdd.isEmpty() is False: resultList = rdd.collect() for item in resultList: taskid=item["taskid"] mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() taskid=ObjectId(taskid) task= mongodb.celery_task.find_one({"_id":taskid}) if task: mongodb.celery_task.update({"_id":taskid}, {'$set':{'status':1}}) purchaseinfoid=task["purchaseinfoid"] count=task["order"] channel=task["channel"] tasktype=task["tasktype"] taskinfo=mongodb.celery_task_info.find_one({"taskid":taskid}) if taskinfo: sendlist=taskinfo["sendlist"].split(",") if tasktype==1: purchaseinfo = sqldb.get("select pi.id purchaseinfoid,pi.varietyid,pi.name variety,pi.specification,pi.quantity,pi.unit,pi.quality,pi.origin,pi.pushcount,p.userid,p.createtime from purchase_info pi left join purchase p on pi.purchaseid = p.id where pi.id = %s", purchaseinfoid) u = sqldb.get("select name,nickname from users where id = %s", purchaseinfo["userid"]) purchaseinfo["name"] = u["name"] purchaseinfo["nickname"] = u["nickname"] push_user_infos = [] uuidmap={} sendstatus = 0 # 0,未发送,1:发送成功,2:失败 colleciton = mongodb.transform_rate createtime = int(time.time()) push_id=colleciton.insert({"purchaseinfoid":purchaseinfoid ,"varietyname":purchaseinfo["variety"],"order":count,"quote":"","type":channel,"createtime":createtime}) for send in sendlist: uuid = md5(str(time.time())+ str(send))[8:-8] sendid = send createtime = int(time.time()) push_user = {"pushid":push_id ,"uuid":uuid,"createtime":createtime,"click":0,"sendid":sendid,"sendstatus":sendstatus,"type":channel} push_user_infos.append(push_user) uuidmap[sendid]=uuid colleciton = mongodb.push_record colleciton.insert_many(push_user_infos) if channel==1: print sendlist, purchaseinfo, uuidmap #pushPurchase(sendlist, purchaseinfo, uuidmap) attentions=[]#关注用户 notattentions=[]#非关注用户 for phone in sendlist: userinfo = None userinfo=sqldb.get("select id,maxpush,openid from users where phone=%s",phone) print userinfo if userinfo and userinfo["openid"]!="": attentions.append(phone) else: if userinfo: notattentions.append(phone) sqldb.execute("update users set maxpush=maxpush+1 where phone=%s", phone) else: #supplierb表里面的 attentions.append(phone) if len(notattentions)!=0: print notattentions, purchaseinfo thread.start_new_thread(pushPurchase, (notattentions, purchaseinfo, uuidmap,2)) if len(attentions) != 0: print attentions, purchaseinfo thread.start_new_thread(pushPurchase, (attentions, purchaseinfo, uuidmap)) else: #print sendlist, purchaseinfo,uuidmap #pushPurchaseWx(sendlist, purchaseinfo,uuidmap) thread.start_new_thread(pushPurchaseWx, (sendlist, purchaseinfo, uuidmap)) elif tasktype==2: sendid=taskinfo["sendlist"] print purchaseinfoid ret = sqldb.query("select id from quote where purchaseinfoid =%s and state=0", purchaseinfoid)#未回复的报价个数 num=len(ret) if num!=0: ret= sqldb.query("select id from quote where purchaseinfoid =%s and state=0 order by price", purchaseinfoid) qid=ret[0]["id"] purchaseinfo = sqldb.get("select pi.name,pi.purchaseid,pi.unit,q.price from quote q left join purchase_info pi on q.purchaseinfoid=pi.id where q.id=%s",qid) if channel==1: if sendid!="": uuid = md5(str(time.time()) + str(sendid))[8:-8] createtime = int(time.time()) push_user = {"pushid": "", "uuid": uuid, "createtime": createtime, "click": 0, "sendid": sendid, "sendstatus": 0, "type": channel} colleciton = mongodb.push_record record_id=colleciton.insert(push_user) colleciton = mongodb.notify_record notify_user = {"createtime": int(time.time()), "sendid": sendid, "type": channel,"purchaseinfoid":purchaseinfoid,"recordid":record_id} colleciton.insert(notify_user) #print sendid, str(num),purchaseinfo["name"].encode("utf8"),purchaseinfo["price"].encode("utf8"),purchaseinfo["unit"].encode("utf8"),str(purchaseinfo["purchaseid"]) #reply_quote_notify(sendid, str(num), purchaseinfo["name"],purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid)) thread.start_new_thread(reply_quote_notify,(sendid, str(num), purchaseinfo["name"],purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid),uuid)) elif channel==2: print sendid if sendid!="": user=sqldb.query("select id from users where openid=%s", sendid) if user: sendtype=1 else: sendtype=2 uuid = md5(str(time.time()) + str(sendid))[8:-8] createtime = int(time.time()) push_user = {"pushid": "", "uuid": uuid, "createtime": createtime, "click": 0, "sendid": sendid, "sendstatus": 0, "type": channel} colleciton = mongodb.push_record record_id = colleciton.insert(push_user) colleciton = mongodb.notify_record notify_user = {"createtime": int(time.time()), "sendid": sendid, "type": channel,"purchaseinfoid":purchaseinfoid,"recordid":record_id} colleciton.insert(notify_user) #print sendid, str(num),purchaseinfo["name"].encode("utf8"),purchaseinfo["price"].encode("utf8"),purchaseinfo["unit"].encode("utf8"),str(purchaseinfo["purchaseid"]),sendtype #reply_wx_notify(sendid, str(num), purchaseinfo["name"],purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid),str(purchaseinfo["purchaseid"])) thread.start_new_thread(reply_wx_notify, (sendid, str(num), purchaseinfo["name"],purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid),str(purchaseinfo["purchaseid"]),uuid,sendtype)) pass
user={"phone":item,"notclick":0} notclick.append(user) ret = mongodb.push_record.find({"createtime": {"$gt": int(yesterdayStamp), "$lt": int(todayStamp)},"type":1}) for item in ret: if item["click"]==0: update(item["sendid"],notclick) notnum=0 for item in notclick: if item["notclick"]>3: notnum+=1 print totalcount print notnum ''' mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() # items =mongodb.transform_rate.find()#检测发送超过一天的统计记录 条件通过createtime format = "%Y-%m-%d" todaystr = datetime.now().strftime("%Y-%m-%d") print todaystr todaystr="2016-09-28" today = datetime.strptime(todaystr, format) yesterday = today - timedelta(days=4) todayStamp = time.mktime(today.timetuple()) yesterdayStamp = time.mktime(yesterday.timetuple()) print todayStamp print yesterdayStamp ret = mongodb.push_record.find({"createtime": {"$gt": int(yesterdayStamp), "$lt": int(todayStamp)}, "type": 1}) for item in ret:
def initialize(self): self.session = session.Session(self.application.session_manager, self) self.db = database.instance().get_session() self.log = logging.getLogger()
def __init__(self, dom): self.db = database.instance().domains self.domain = dom
def sendPush(rdd): if rdd.isEmpty() is False: resultList = rdd.collect() for item in resultList: taskid = item["taskid"] mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() taskid = ObjectId(taskid) task = mongodb.celery_task.find_one({"_id": taskid}) if task: mongodb.celery_task.update({"_id": taskid}, {'$set': { 'status': 1 }}) purchaseinfoid = task["purchaseinfoid"] count = task["order"] channel = task["channel"] tasktype = task["tasktype"] taskinfo = mongodb.celery_task_info.find_one( {"taskid": taskid}) if taskinfo: sendlist = taskinfo["sendlist"].split(",") if tasktype == 1: purchaseinfo = sqldb.get( "select pi.id purchaseinfoid,pi.varietyid,pi.name variety,pi.specification,pi.quantity,pi.unit,pi.quality,pi.origin,pi.pushcount,p.userid,p.createtime from purchase_info pi left join purchase p on pi.purchaseid = p.id where pi.id = %s", purchaseinfoid) u = sqldb.get( "select name,nickname from users where id = %s", purchaseinfo["userid"]) purchaseinfo["name"] = u["name"] purchaseinfo["nickname"] = u["nickname"] push_user_infos = [] uuidmap = {} sendstatus = 0 # 0,未发送,1:发送成功,2:失败 colleciton = mongodb.transform_rate createtime = int(time.time()) push_id = colleciton.insert({ "purchaseinfoid": purchaseinfoid, "varietyname": purchaseinfo["variety"], "order": count, "quote": "", "type": channel, "createtime": createtime }) for send in sendlist: uuid = md5(str(time.time()) + str(send))[8:-8] sendid = send createtime = int(time.time()) push_user = { "pushid": push_id, "uuid": uuid, "createtime": createtime, "click": 0, "sendid": sendid, "sendstatus": sendstatus, "type": channel } push_user_infos.append(push_user) uuidmap[sendid] = uuid colleciton = mongodb.push_record colleciton.insert_many(push_user_infos) if channel == 1: print sendlist, purchaseinfo, uuidmap #pushPurchase(sendlist, purchaseinfo, uuidmap) attentions = [] #关注用户 notattentions = [] #非关注用户 for phone in sendlist: userinfo = None userinfo = sqldb.get( "select id,maxpush,openid from users where phone=%s", phone) print userinfo if userinfo and userinfo["openid"] != "": attentions.append(phone) else: if userinfo: notattentions.append(phone) sqldb.execute( "update users set maxpush=maxpush+1 where phone=%s", phone) else: #supplierb表里面的 attentions.append(phone) if len(notattentions) != 0: print notattentions, purchaseinfo thread.start_new_thread( pushPurchase, (notattentions, purchaseinfo, uuidmap, 2)) if len(attentions) != 0: print attentions, purchaseinfo thread.start_new_thread( pushPurchase, (attentions, purchaseinfo, uuidmap)) else: #print sendlist, purchaseinfo,uuidmap #pushPurchaseWx(sendlist, purchaseinfo,uuidmap) thread.start_new_thread( pushPurchaseWx, (sendlist, purchaseinfo, uuidmap)) elif tasktype == 2: sendid = taskinfo["sendlist"] print purchaseinfoid ret = sqldb.query( "select id from quote where purchaseinfoid =%s and state=0", purchaseinfoid) #未回复的报价个数 num = len(ret) if num != 0: ret = sqldb.query( "select id from quote where purchaseinfoid =%s and state=0 order by price", purchaseinfoid) qid = ret[0]["id"] purchaseinfo = sqldb.get( "select pi.name,pi.purchaseid,pi.unit,q.price from quote q left join purchase_info pi on q.purchaseinfoid=pi.id where q.id=%s", qid) if channel == 1: if sendid != "": uuid = md5(str(time.time()) + str(sendid))[8:-8] createtime = int(time.time()) push_user = { "pushid": "", "uuid": uuid, "createtime": createtime, "click": 0, "sendid": sendid, "sendstatus": 0, "type": channel } colleciton = mongodb.push_record record_id = colleciton.insert(push_user) colleciton = mongodb.notify_record notify_user = { "createtime": int(time.time()), "sendid": sendid, "type": channel, "purchaseinfoid": purchaseinfoid, "recordid": record_id } colleciton.insert(notify_user) #print sendid, str(num),purchaseinfo["name"].encode("utf8"),purchaseinfo["price"].encode("utf8"),purchaseinfo["unit"].encode("utf8"),str(purchaseinfo["purchaseid"]) #reply_quote_notify(sendid, str(num), purchaseinfo["name"],purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid)) thread.start_new_thread( reply_quote_notify, (sendid, str(num), purchaseinfo["name"], purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid), uuid)) elif channel == 2: print sendid if sendid != "": user = sqldb.query( "select id from users where openid=%s", sendid) if user: sendtype = 1 else: sendtype = 2 uuid = md5(str(time.time()) + str(sendid))[8:-8] createtime = int(time.time()) push_user = { "pushid": "", "uuid": uuid, "createtime": createtime, "click": 0, "sendid": sendid, "sendstatus": 0, "type": channel } colleciton = mongodb.push_record record_id = colleciton.insert(push_user) colleciton = mongodb.notify_record notify_user = { "createtime": int(time.time()), "sendid": sendid, "type": channel, "purchaseinfoid": purchaseinfoid, "recordid": record_id } colleciton.insert(notify_user) #print sendid, str(num),purchaseinfo["name"].encode("utf8"),purchaseinfo["price"].encode("utf8"),purchaseinfo["unit"].encode("utf8"),str(purchaseinfo["purchaseid"]),sendtype #reply_wx_notify(sendid, str(num), purchaseinfo["name"],purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid),str(purchaseinfo["purchaseid"])) thread.start_new_thread( reply_wx_notify, (sendid, str(num), purchaseinfo["name"], purchaseinfo["price"], purchaseinfo["unit"], str(purchaseinfoid), str(purchaseinfo["purchaseid"]), uuid, sendtype)) pass
def task_generate(task):#生成发送任务 mongodb = PymongoDateBase.instance().get_db() sqldb = database.instance().get_session() tasktype=task["tasktype"] purchaseinfoid = str(task["purchaseinfoid"]) channel = task["channel"] record = mongodb.celery_task.find( {"purchaseinfoid": purchaseinfoid, "channel": channel, "tasktype": tasktype}).sort("order", pymongo.DESCENDING) if record.count() == 0: order = 1 else: order = record[0]["order"] + 1 print "start order %s"%order purchaseinfo = sqldb.get( "select pi.id purchaseinfoid,pi.varietyid,pi.status,p.userid uid from purchase_info pi left join purchase p on pi.purchaseid = p.id where pi.id = %s", purchaseinfoid) if purchaseinfo==None: return elif purchaseinfo["status"]==0: return if tasktype==1: sendids = set() filtersend = [] if order>max_push_time: return if order!=1:#不是第一次发送,过滤发送过的 records = mongodb.transform_rate.find({"purchaseinfoid": purchaseinfoid, "type": channel}) for item in records: pushrecord= mongodb.push_record.find({"pushid": item["_id"]}) for item in pushrecord: filtersend.append(item["sendid"]) if channel == 1: #短信渠道 print "channel=%s"%channel phonecondition = "" filtersend = [str(i) for i in filtersend] if filtersend != []: phonecondition = " and phone not in(%s)" % ",".join(filtersend) task = {"purchaseinfoid": purchaseinfoid, "tasktype": tasktype, "channel": 1, "order": order, "status": 0,"createtime":int(time.time())} userphones = sqldb.query( "select phone from users where find_in_set(%s,varietyids) and maxpush<3 " + phonecondition + " order by pushscore limit 0,%s", purchaseinfo["varietyid"], max_wx_num) if filtersend != []: phonecondition = " and mobile not in(%s)" % ",".join(filtersend) yt = sqldb.query( "select mobile from supplier where find_in_set(%s,variety) and mobile != '' and pushstatus=1 and maxpush<3 " + phonecondition + " order by pushscore limit 0, %s", purchaseinfo["varietyid"], max_phone_num) for i in userphones: sendids.add(str(i["phone"])) for j in yt: sendids.add(str(j["mobile"])) sendids = list(set(sendids)) print "sendids= %s"%sendids elif channel==2: #微信渠道 print "channel=%s"%channel wxcondition="" filtersend = ["'"+str(i)+"'" for i in filtersend] if filtersend!=[]: wxcondition = " and openid not in(%s)" % ",".join(filtersend) task = {"purchaseinfoid": purchaseinfoid, "tasktype": tasktype, "channel": 2, "order": order, "status": 0,"createtime":int(time.time())} userwxs = sqldb.query( "select openid from users where find_in_set(%s,varietyids) and openid!=''" + wxcondition + " order by pushscore", purchaseinfo["varietyid"]) for i in userwxs: sendids.add(str(i["openid"])) sendids = list(set(sendids)) print "sendids=%s"%sendids if len(sendids) != 0: taskid = mongodb.celery_task.insert(task) taskinfo = {"taskid": taskid, "sendlist": ",".join(sendids)} collection = mongodb.celery_task_info collection.insert(taskinfo) sendkafka.apply_async(args=[taskid]) else: #提醒采购商 if order!=1: if order>max_notify_time or (int(time.time())-int(record[0]["createtime"]))<notify_days*24*60*60: return sendids=[] if channel==1: userphone=sqldb.get("select id,phone from users where id=%s",purchaseinfo["uid"]) if userphone: sendids.append(userphone["phone"]) elif channel==2: useropenid=sqldb.get("select id,openid from users where id=%s",purchaseinfo["uid"]) if useropenid: sendids.append(useropenid["openid"]) useropenid2 = sqldb.get("select id,openid2 from users where id=%s", purchaseinfo["uid"]) if useropenid: sendids.append(useropenid2["openid2"]) print "channel=%s,sendid=%s"%(channel,sendids) if sendids!=[]: for sendid in sendids: task = {"purchaseinfoid": purchaseinfoid, "tasktype": tasktype, "channel": channel, "order": order, "status": 0, "createtime": int(time.time())} taskid = mongodb.celery_task.insert(task) taskinfo = {"taskid": taskid, "sendlist": sendid} collection = mongodb.celery_task_info collection.insert(taskinfo) sendkafka.apply_async(args=[taskid])