def get(self): caller = self.get_argument('caller').encode('utf-8') coupon_sn = self.get_argument('coupon').encode('utf-8') sign = self.get_argument('sign').encode('utf-8') timestamp = self.get_argument('timestamp').encode('utf-8') logging.info('telephone verify. caller: %s, coupon_sn: %s, sign: %s, timestamp: %s', caller, coupon_sn, sign, timestamp) if not caller: logging.error('telephone verify failed: empty caller.') self.write('1|主叫号码无效') return if not coupon_sn: logging.error('telephone verify failed: empty coupon.') self.write('2|券号为空') return if not sign: logging.error('telephone verify failed: empty sign.') self.write('3|签名无效') return if not timestamp: logging.error('telephone verify failed: empty timestamp.') self.write('3|时间戳无效') return if abs(int(time.time()) - int(timestamp)) > 300: logging.error('telephone verify failed: request timeout.') self.write('3|请求超时') return # 暂不检查签名 # todo # 查找电话 supplier_shop = self.db.get('select * from supplier_shop where deleted=0 and verify_phones like %s', '%'+caller+'%') #if not supplier_shop: # #部分商家使用分机,出口线很多,不能一一录入,这里可以使用电话号码的前面一部分进行识别。 # #尝试模糊查找验证电话机 # supplier_user_list = self.db.query('select * from supplier_user where type="ANDROID"') # for su in supplier_user_list: # if caller.startswith(su.loginname): # supplier_shop = su # break if not supplier_shop: logging.error('telehphone verify failed. can not found valid supplier_user %s.', caller) self.write('1|您的电话还未绑定') return coupon = self.db.get('select id, sn, mobile from item_coupon where sn=%s', coupon_sn) if not coupon: if len(coupon_sn) == 9: for i in range(10): coupon = self.db.get('select id, sn, mobile from item_coupon where sn=%s', '%s%s' % (i, coupon_sn)) if coupon: #我们自己的券 check_result = local_check(self.db, coupon.sn, supplier_shop.id) if not check_result.ok: ok, msg = (False, check_result.msg) else: # 合作伙伴API验证 api_result = yield partner_api_verify(self.db, coupon.sn) if not api_result.ok: ok, msg = (False, api_result.msg) else: # 本地验证 verify_result = local_verify(self.db, coupon.sn, supplier_shop.id, '电话验证') if verify_result.ok: ok, msg = (True, verify_result.msg) else: ok, msg = (False, verify_result.msg) else: results = yield partner_browser_verify(self.db, [coupon_sn], supplier_shop.id) # 记录外部订单 for result in results: if result.ok: goods_info = self.db.get('select g.* from goods g where id=%s', result.goods_id) verify_infos = {'goods_id': result.goods_id, 'shop_id': supplier_shop.id} #创建分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, NOW())', result.coupon_sn, json_dumps(verify_infos), result.distributor_shop_id) order_id, order_no = new_distributor_order(self.db, result.distributor_shop_id, goods_info.sales_price, goods_info.sales_price, '') #记录订单信息 new_order = new_distributor_item( db=self.db, order_id=order_id, order_no=order_no, sales_price=None, count=1, goods_info=goods_info, mobile='', distributor_shop_id=result.distributor_shop_id, distributor_goods_id='', distributor_coupons=[{'coupon_sn': result.coupon_sn, 'coupon_pwd': result.coupon_pwd or ''}], use_distributor_coupon=True ) if new_order.ok: #更新分销订单id self.db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) #更新订单id self.db.execute('update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) #更新该券验证的门店 self.db.execute('update item set sp_shop_id = %s where order_id=%s', supplier_shop.id, order_id) self.redis.lpush(options.queue_coupon_local_verify, json_dumps({'coupon': result.coupon_sn, 'shop_id': supplier_shop.id, 'retry': 0, 'used_at': datetime.now()})) ok, msg = (results[0].ok, results[0].msg) if not ok: logging.error('telephone verify failed. %s', msg) self.write('2|验证失败') else: logging.info('telephone verify ok. caller: %s, coupon_sn: %s, sign: %s, timestamp: %s', caller, coupon_sn, sign, timestamp) # 发送验证确认短信 CouponConsumedMessage(self.redis, [coupon.sn], supplier_shop.name, coupon.mobile).send() if coupon: face_value = self.db.get('select face_value from item i, item_coupon c where i.id=c.item_id ' 'and c.id=%s', coupon.id).face_value self.write('0|券尾号%s验证成功,面值%s' % (coupon_sn[-4:], upper(face_value).encode('utf-8'))) else: self.write('0|券尾号%s验证成功' % coupon_sn[-4:])
def post(self): """ 开始导入""" if not self.request.files: self.render('real/import_outer_order.html', message=u'请选择文件') #读取文件 order_file = self.request.files['order_file'][0] book = xlrd.open_workbook(file_contents=order_file['body']) try: sheet = book.sheet_by_index(0) except: self.render('real/import_outer_order.html', message=u'文件格式错误,请检查') return partner = self.get_argument('partner').lower() distributor_shop_id = { 'dp': 40, 'mt': 44, 'ls': '45', 'nm': 42, 'ww': 37 }.get(partner) #读取工作表的所有行数据内容 rows = [sheet.row_values(i) for i in range(1, sheet.nrows)] #转换数据 col_names = ('coupon_sn', 'goods_id') coupon_list = [ dict([(col_names[i], row[i]) for i in range(len(col_names))]) for row in rows ] existed_orders = [] need_convert_orders = [] goods_id = -1 for order in coupon_list: current_goods_id = order['goods_id'] if goods_id != current_goods_id: goods_id = current_goods_id goods_info = self.db.get( 'select g.* ,ss.id shop_id from goods g,supplier_shop ss ,supplier s ' 'where ss.deleted=0 and s.id=ss.supplier_id and s.id=g.supplier_id ' 'and g.id=%s limit 1', goods_id) verify_infos = { 'goods_id': goods_id, 'shop_id': goods_info.shop_id } if isinstance(order['coupon_sn'], int) or isinstance( order['coupon_sn'], float): need_convert_orders.append(str(order['coupon_sn'])) continue #判断是不是已经导入过的外部订单 distributor_order = self.db.get( 'select * from distributor_order where distributor_shop_id=%s and ' 'order_no=%s', distributor_shop_id, order['coupon_sn']) if distributor_order: existed_orders.append(str(order['coupon_sn'])) continue created_at = datetime.now() - timedelta(days=1) #创建分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, %s)', order['coupon_sn'], json_dumps(verify_infos), distributor_shop_id, created_at) order_id, order_no = new_distributor_order(self.db, distributor_shop_id, goods_info.sales_price, goods_info.sales_price, "") # 改时间 self.db.execute( 'update orders set created_at=%s, paid_at=%s where id=%s', created_at, created_at, order_id) self.db.execute( 'update journal set created_at=%s where type=1 and iid=%s', created_at, order_id) #记录订单信息 new_order = new_distributor_item( db=self.db, order_id=order_id, order_no=order_no, sales_price=None, count=1, goods_info=goods_info, mobile='', distributor_shop_id=distributor_shop_id, distributor_goods_id='', distributor_coupons=[{ 'coupon_sn': order['coupon_sn'], 'coupon_pwd': '' }], use_distributor_coupon=True) if new_order.ok: #更新分销订单id self.db.execute( 'update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) #更新订单id self.db.execute( 'update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) #更新该券验证的门店 self.db.execute( 'update item set sp_shop_id = %s where order_id=%s', goods_info.shop_id, order_id) #设定时间为前一天 self.db.execute( 'update item set created_at=%s where order_id=%s', created_at, order_id) #自动验证 self.redis.lpush( options.queue_coupon_local_verify, json_dumps({ 'coupon': order['coupon_sn'], 'shop_id': goods_info.shop_id, 'retry': 0, 'used_at': created_at })) self.render('order/import_outer_order.html', message='success', existed_orders=existed_orders, need_convert_orders=need_convert_orders)
def yihaodian_order(db, redis, distributor_order_id, message_raw): """ 一号店分销订单处理 :param db: :param redis: :param distributor_order_id: :param message_raw: :type db:torndb.Connection :type redis:redis.client.StrictRedis :type distributor_order_id: int """ distributor_order = db.get('select * from distributor_order where id=%s', distributor_order_id) distributor_order_no = distributor_order.order_no # 分销订单号不存在,结束 if not distributor_order: logging.error('yihaodian job consume cannot find distributor order: order_no=%s', distributor_order_no) return # 重新刷新订单信息 yihaodian = Yihaodian('yhd.order.detail.get') response = yihaodian.sync_fetch(orderCode=distributor_order_no) logging.info('yihaodian response for distributor_order_no %s : %s' % (distributor_order_no, response)) yihaodian.parse_response(response) # 一号店请求失败,结束 if not yihaodian.is_ok(): logging.error('yihaodian job consume fetch order failed, distributor order_no=%s', distributor_order_no) return order_status = yihaodian.message.findtext('./orderInfo/orderDetail/orderStatus').strip() # 一号店订单已取消,从队列移除,结束 if order_status == 'ORDER_CANCEL': # 队列移除 redis.lrem(options.queue_distributor_order_processing, 0, message_raw) logging.info(' yihaodian order_no %s cancelled ', distributor_order_no) return # 一号店订单状态为‘可以发货’, 则处理该订单 elif order_status == 'ORDER_TRUNED_TO_DO': # 更新分销订单message, 如果distributor_order表中message为空,则该分销订单已在分销商处取消 db.execute('update distributor_order set message=%s where order_no=%s', response, distributor_order_no) # 筛选电子券订单 order_items = yihaodian.message.findall('./orderInfo/orderItemList/orderItem') coupon_items = [] for item in order_items: goods_link_id = item.findtext('./outerId') if goods_link_id: goods = db.get('select g.* from goods as g, goods_distributor_shop as gds ' 'where g.id = gds.goods_id and gds.distributor_shop_id=%s and gds.goods_link_id=%s', options.shop_id_yihaodian, goods_link_id) if not goods and int(goods_link_id) < 20000: goods = db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id) # 当商品类型为电子券时,才处理 if goods and goods.type == options.goods_type_electronic: coupon_items.append((item, goods)) else: logging.warning('yihaodian job consume warning: goods_link_id=%s not found or goods type is real' % goods_link_id) if len(coupon_items) == 0: # 分销订单没有电子券,从处理队列移除 redis.lrem(options.queue_distributor_order_processing, 0, message_raw) logging.info('yihaodian job consume exits for no real goods, distributor order_no: %s' % distributor_order_no) return # 生成一百券订单 if distributor_order.order_id == 0 and check_stock(coupon_items): mobile = yihaodian.message.findtext('./orderInfo/orderDetail/goodReceiverMoblie') order_amount = yihaodian.message.findtext('./orderInfo/orderDetail/orderAmount') payment = Decimal(yihaodian.message.findtext('./orderInfo/orderDetail/realAmount')) order_id, order_no = new_distributor_order(db, options.shop_id_yihaodian, order_amount, payment, mobile) # 生成订单项 for item in coupon_items: order_item = item[0] goods_info = item[1] sales_price = Decimal(order_item.findtext('./orderItemPrice')) count = int(order_item.findtext('./orderItemNum')) distributor_goods_id = order_item.findtext('./productId') result = new_distributor_item(db, order_id, order_no, sales_price, count, goods_info, mobile, options.shop_id_yihaodian, distributor_goods_id, [], False) if not result.ok: logging.error('failed to generate order items for distributor_order_id=%s of goods_id=%s' % (distributor_order_id, goods_info.id)) return logging.info('create new yibaiquan order id=%s for yihaodian distributor order id=%s' % (order_id, distributor_order_id)) # 相互更新分销订单和订单 db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) db.execute('update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) # 按order_item发送券短信 all_order_items = db.query('select * from order_item where order_id=%s', order_id) for item in all_order_items: CouponSMSMessage(db, redis, order_item=item).remark('一号店订单发送券号短信').send() # 通知一号店,已发货 yihaodian = Yihaodian('yhd.logistics.order.shipments.update') response = yihaodian.sync_fetch(orderCode=distributor_order_no, deliverySupplierId=options.yhd_delivery, expressNbr=distributor_order_no ) yihaodian.parse_response(response) if yihaodian.is_ok(): # 分销订单处理完成,从处理队列移除 redis.lrem(options.queue_distributor_order_processing, 0, message_raw) logging.info('yihaodian distributor order (id=%s) finish' % distributor_order_id) else: logging.error('update yihaodian distributor order (id=%s) shipments failed' % distributor_order_id)
def post(self): self.set_header('Content-Type', 'application/xml; charset=UTF-8') # 解析京东请求 logging.info('jingdong api. send_order %s', self.request.body) request = JdToUs(self.request.body) distr_shop = self.db.get('select id, taobao_api_info from distributor_shop ' 'where distributor_id=%s and taobao_seller_id=%s', options.distributor_id_jingdong, request.vender_id) api_info = json.loads(distr_shop.taobao_api_info, object_hook=json_hook) request.set_key(api_info.vender_key, api_info.secret_key) request.parse() # 查找分销订单 jd_order_id = request.message.findtext('JdOrderId') order = self.db.get('select * from distributor_order where ' 'distributor_shop_id=%s and order_no=%s', distr_shop.id, jd_order_id) order_message = ElementTree.tostring(request.message, encoding='utf-8', method='xml') logging.info('jingdong api. send_order decrypted %s', order_message) if order: return self.write(request.response('send_order', 212, 'the order has been processed')) else: # 保存分销订单信息 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, NOW())', jd_order_id, order_message, distr_shop.id) count = int(request.message.findtext('Count')) goods_link_id = request.message.findtext('VenderTeamId') order_amount = Decimal(request.message.findtext('Origin'))/100 sales_price = Decimal(request.message.findtext('TeamPrice'))/100 jd_goods_id = request.message.findtext('JdTeamId') mobile = request.message.findtext('Mobile') jd_coupons = [{'coupon_sn': c.findtext('CouponId'), 'coupon_pwd': c.findtext('CouponPwd')} for c in request.message.findall('Coupons/Coupon')] # 找到关联的商品 goods_info = self.db.get('select g.* from goods g, goods_distributor_shop gds ' 'where g.id = gds.goods_id and distributor_shop_id=%s and goods_link_id=%s', distr_shop.id, goods_link_id) if not goods_info and int(goods_link_id) < 20000: goods_info = self.db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id) if not goods_info: self.write(request.response('send_order', 213, 'goods not found')) return order_id, order_no = new_distributor_order(self.db, distr_shop.id, order_amount, order_amount, mobile) result = new_distributor_item(self.db, order_id, order_no, sales_price, count, goods_info, mobile, distr_shop.id, jd_goods_id, jd_coupons, False) if not result.ok: self.write(request.response('send_order', 213, result.msg)) return else: self.db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, result.order_id) self.db.execute('update distributor_order set order_id=%s where id = %s', result.order_id, distributor_order_id) ## 按order_item发送券短信 #all_order_items = self.db.query('select * from order_item where order_id=%s', order_id) #for item in all_order_items: # CouponSMSMessage(self.db, self.redis, order_item=item).remark('京东订单短信发送').send() query_coupons = self.db.query('select ic.sn, ic.distr_sn from item_coupon ic, item i ' 'where ic.item_id=i.id and i.order_id=%s', order_id) result_coupon = [] for jd_coupon in jd_coupons: for query_coupon in query_coupons: if query_coupon.distr_sn == jd_coupon['coupon_sn']: result_coupon.append(query_coupon.sn) break self.write(request.response('send_order', 200, 'ok', jd_team_id=jd_goods_id, vender_team_id=goods_link_id, sell_count=count, vender_order_id=order_no, coupons=result_coupon))
def post(self): self.set_header('Content-Type', 'application/json; charset=UTF-8') coupon_list = [i.strip() for i in self.get_argument('coupons', '').split(',') if i.strip()] if len(coupon_list) == 0: self.write({'ok': False, 'msg': u'请输入券号'}) return shop_id = self.get_argument('shop_id', 0) #这一步是检查该操作员是否有权限验证券 all_shops = self.db.query('select ss.* from supplier_shop ss, supplier_user su where ss.supplier_id=%s and ' 'ss.deleted=0 and su.id=%s and su.supplier_id=ss.supplier_id and ' ' (su.shop_id=0 or ss.id=su.shop_id)', self.current_user.supplier_id, self.current_user.id) if int(shop_id) not in [i.id for i in all_shops]: self.write({'ok': False, 'msg': u'对不起,您无权执行此操作'}) return is_our_coupon = False coupon_length = 0 messages = PropDict() # 用来存不同的手机号对应的券号 #检测长度是否相同、是否是我们的券 for coupon_sn in coupon_list: coupon = self.db.get('select id, sn, mobile from item_coupon where sn=%s', coupon_sn) if coupon: is_our_coupon = True if coupon_length == 0: coupon_length = len(coupon_sn) elif coupon_length != len(coupon_sn): coupon_length = -1 if coupon_length == -1: self.write({'ok': False, 'msg': u'券号长度必须一致'}) return result_list = [] if is_our_coupon: # 我们自己的券 for coupon_sn in coupon_list: # 本地检测 check_result = local_check(self.db, coupon_sn, shop_id) if not check_result.ok: ok, msg = (False, check_result.msg) else: # 合作伙伴API验证 api_result = yield partner_api_verify(self.db, coupon_sn) if not api_result.ok: ok, msg = (False, api_result.msg) else: # 本地验证 verify_result = local_verify(self.db, coupon_sn, shop_id, self.current_user.name) ok, msg = (verify_result.ok, verify_result.msg) # 验证通过,记录需要发送确认短信的券 if ok: if messages.get(str(check_result.coupon.mobile)): # 手机号已存在,添加券号到对应手机号 messages.get(str(check_result.coupon.mobile)).append(str(coupon_sn)) else: # 手机号不存在,新建K-V对 messages.update({str(check_result.coupon.mobile): [str(coupon_sn)]}) result_list.append({'coupon_sn': coupon_sn, 'ok': ok, 'msg': msg}) else: mobile = self.get_argument('mobile', '') # 检查手机号合法性 mobile_ptn = re.compile('^1[\d+]{10}$') if not re.match(mobile_ptn, mobile): # 不合法手机号,不记录 mobile = '' # 尝试外部验证 results = yield partner_browser_verify(self.db, coupon_list, shop_id) for result in results: if result.ok: goods_info = self.db.get('select g.* from goods g where id=%s', result.goods_id) verify_infos = {'goods_id': result.goods_id, 'shop_id': shop_id} #创建分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, NOW())', result.coupon_sn, json_dumps(verify_infos), result.distributor_shop_id) order_id, order_no = new_distributor_order(self.db, result.distributor_shop_id, goods_info.sales_price, goods_info.sales_price, mobile) #记录订单信息 new_order = new_distributor_item( db=self.db, order_id=order_id, order_no=order_no, sales_price=None, count=1, goods_info=goods_info, mobile=mobile, distributor_shop_id=result.distributor_shop_id, distributor_goods_id='', distributor_coupons=[{'coupon_sn': result.coupon_sn, 'coupon_pwd': result.coupon_pwd or ''}], use_distributor_coupon=True ) if new_order.ok: #更新分销订单id self.db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) #更新订单id self.db.execute('update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) #更新该券验证的门店 self.db.execute('update item set sp_shop_id = %s where order_id=%s', shop_id, order_id) self.redis.lpush(options.queue_coupon_local_verify, json_dumps({'coupon': result.coupon_sn, 'shop_id': shop_id, 'retry': 0, 'used_at': datetime.now()})) result_list.append({'coupon_sn': result.coupon_sn, 'coupon_pwd': result.coupon_pwd or '', 'ok': result.ok, 'msg': result.msg}) # 如果有合法券号,发送确认短信 if messages: shop_name = self.db.get('select name from supplier_shop where id=%s', shop_id).name for mobile, coupon_sns in messages.items(): CouponConsumedMessage(self.redis, coupon_sns, shop_name, mobile).send() self.write({'ok': True, 'result': result_list})
def post(self): # 解密58请求参数 json_text = decrypt(self.get_argument('param'), options.wuba_secret_key) # '2a6f8f2c')# params = json.loads(json_text, object_hook=json_hook) logging.info('wuba new order request : %s', json_dumps(params)) # 获得参数 try: wuba_order_no = params.orderId # 58 订单号 sales_price = Decimal(params.prodPrice) # 商品售价 product_count = int(params.prodCount) # 购买数量 mobile = params.mobile # 订单券号接受手机号 goods_link_id = int(params.groupbuyIdThirdpart) # 伪商品ID except AttributeError: logging.info('wuba request failed: wrong params') return self.write('{"status":"10201", "msg": "参数解析错误"}') # 检查参数格式 if int(product_count) <= 0 or sales_price.compare(Decimal(0)) < 0 or not check_phone(mobile): logging.info('wuba request failed: invalid params') return self.write('{"status":"20210", "msg": "输入参数错误"}') # 查找分销订单 wuba_order = self.db.get('select * from distributor_order where ' 'distributor_shop_id=%s and order_no=%s', options.shop_id_wuba, wuba_order_no) if wuba_order: # 分销订单重复 logging.info('wuba request failed: duplicated wuba order no') return self.write('{"status":"10100", "msg": "订单已存在"}') else: # 分销订单不存在, 生成分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, NOW())', wuba_order_no, json_text, options.shop_id_wuba) #放入处理队列 self.redis.lpush(options.queue_distributor_failed_order, "%s-%s" % (wuba_order_no, goods_link_id)) # 根据伪商品找到关联的商品 goods_info = self.db.get('select g.* from goods g, goods_distributor_shop gds ' 'where g.id = gds.goods_id and distributor_shop_id=%s and goods_link_id=%s', options.shop_id_wuba, goods_link_id) # 如果商品不存在,根据真实商品ID查找 if not goods_info and goods_link_id < 20000: goods_info = self.db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id) # 商品不存在, 返回错误信息 if not goods_info: logging.info('wuba request failed: can not find goods') return self.write('{"status":"10100", "msg": "未找到商品"}') # 生成订单 order_id, order_no = new_distributor_order(self.db, options.shop_id_wuba, sales_price*product_count, sales_price*product_count, mobile) result = new_distributor_item(self.db, order_id, order_no, sales_price, product_count, goods_info, mobile, options.shop_id_wuba, "", [], False) if not result.ok: # 生成订单失败,返回 logging.info('wuba request failed: generate order failed msg %s' % result.msg) return self.write('{"status":"10100", "msg": %s}' % result.msg) else: # 生成订单成功,更新id self.db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, result.order_id) self.db.execute('update distributor_order set order_id=%s where id = %s', result.order_id, distributor_order_id) # 从处理队列移除 self.redis.lrem(options.queue_distributor_failed_order, 0, "%s-%s" % (wuba_order_no, goods_link_id)) # 发送短信 all_order_items = self.db.query('select * from order_item where order_id=%s', order_id) for item in all_order_items: CouponSMSMessage(self.db, self.redis, order_item=item).remark('58订单短信发送').send() tickets = [] # 添加券信息到需要返回的tickets for coupon in result.coupons: coupon_sn = coupon['coupon_sn'] created_at = self.db.get('select * from item i, item_coupon c where i.id=c.item_id and c.sn=%s', coupon_sn).created_at ticket = { "ticketId": int(coupon['id']), "ticketCode": str(coupon_sn), "ticketCount": 1, "createTime": str(created_at), "endTime": str(goods_info.expire_at), } tickets.append(ticket) # 生成合法的json返回 data = {'orderId': wuba_order_no, 'orderIdThirdpart': order_no, 'tickets': tickets} result_json = {'status': '10000', 'msg': 'ok', 'data': encrypt(str(data), options.wuba_secret_key)} # '2a6f8f2c') logging.info('wuba request success: %s tickets generated' % product_count) return self.write(json_dumps(result_json))
def yihaodian_order(db, redis, distributor_order_id, message_raw): """ 一号店分销订单处理 :param db: :param redis: :param distributor_order_id: :param message_raw: :type db:torndb.Connection :type redis:redis.client.StrictRedis :type distributor_order_id: int """ distributor_order = db.get('select * from distributor_order where id=%s', distributor_order_id) distributor_order_no = distributor_order.order_no # 分销订单号不存在,结束 if not distributor_order: logging.error( 'yihaodian job consume cannot find distributor order: order_no=%s', distributor_order_no) return # 重新刷新订单信息 yihaodian = Yihaodian('yhd.order.detail.get') response = yihaodian.sync_fetch(orderCode=distributor_order_no) logging.info('yihaodian response for distributor_order_no %s : %s' % (distributor_order_no, response)) yihaodian.parse_response(response) # 一号店请求失败,结束 if not yihaodian.is_ok(): logging.error( 'yihaodian job consume fetch order failed, distributor order_no=%s', distributor_order_no) return order_status = yihaodian.message.findtext( './orderInfo/orderDetail/orderStatus').strip() # 一号店订单已取消,从队列移除,结束 if order_status == 'ORDER_CANCEL': # 队列移除 redis.lrem(options.queue_distributor_order_processing, 0, message_raw) logging.info(' yihaodian order_no %s cancelled ', distributor_order_no) return # 一号店订单状态为‘可以发货’, 则处理该订单 elif order_status == 'ORDER_TRUNED_TO_DO': # 更新分销订单message, 如果distributor_order表中message为空,则该分销订单已在分销商处取消 db.execute('update distributor_order set message=%s where order_no=%s', response, distributor_order_no) # 筛选电子券订单 order_items = yihaodian.message.findall( './orderInfo/orderItemList/orderItem') coupon_items = [] for item in order_items: goods_link_id = item.findtext('./outerId') if goods_link_id: goods = db.get( 'select g.* from goods as g, goods_distributor_shop as gds ' 'where g.id = gds.goods_id and gds.distributor_shop_id=%s and gds.goods_link_id=%s', options.shop_id_yihaodian, goods_link_id) if not goods and int(goods_link_id) < 20000: goods = db.get( 'select g.* from goods g where g.type="E" and g.id=%s', goods_link_id) # 当商品类型为电子券时,才处理 if goods and goods.type == options.goods_type_electronic: coupon_items.append((item, goods)) else: logging.warning( 'yihaodian job consume warning: goods_link_id=%s not found or goods type is real' % goods_link_id) if len(coupon_items) == 0: # 分销订单没有电子券,从处理队列移除 redis.lrem(options.queue_distributor_order_processing, 0, message_raw) logging.info( 'yihaodian job consume exits for no real goods, distributor order_no: %s' % distributor_order_no) return # 生成一百券订单 if distributor_order.order_id == 0 and check_stock(coupon_items): mobile = yihaodian.message.findtext( './orderInfo/orderDetail/goodReceiverMoblie') order_amount = yihaodian.message.findtext( './orderInfo/orderDetail/orderAmount') payment = Decimal( yihaodian.message.findtext( './orderInfo/orderDetail/realAmount')) order_id, order_no = new_distributor_order( db, options.shop_id_yihaodian, order_amount, payment, mobile) # 生成订单项 for item in coupon_items: order_item = item[0] goods_info = item[1] sales_price = Decimal(order_item.findtext('./orderItemPrice')) count = int(order_item.findtext('./orderItemNum')) distributor_goods_id = order_item.findtext('./productId') result = new_distributor_item(db, order_id, order_no, sales_price, count, goods_info, mobile, options.shop_id_yihaodian, distributor_goods_id, [], False) if not result.ok: logging.error( 'failed to generate order items for distributor_order_id=%s of goods_id=%s' % (distributor_order_id, goods_info.id)) return logging.info( 'create new yibaiquan order id=%s for yihaodian distributor order id=%s' % (order_id, distributor_order_id)) # 相互更新分销订单和订单 db.execute( 'update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) db.execute('update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) # 按order_item发送券短信 all_order_items = db.query( 'select * from order_item where order_id=%s', order_id) for item in all_order_items: CouponSMSMessage(db, redis, order_item=item).remark('一号店订单发送券号短信').send() # 通知一号店,已发货 yihaodian = Yihaodian('yhd.logistics.order.shipments.update') response = yihaodian.sync_fetch( orderCode=distributor_order_no, deliverySupplierId=options.yhd_delivery, expressNbr=distributor_order_no) yihaodian.parse_response(response) if yihaodian.is_ok(): # 分销订单处理完成,从处理队列移除 redis.lrem(options.queue_distributor_order_processing, 0, message_raw) logging.info('yihaodian distributor order (id=%s) finish' % distributor_order_id) else: logging.error( 'update yihaodian distributor order (id=%s) shipments failed' % distributor_order_id)
def get(self): caller = self.get_argument('caller').encode('utf-8') coupon_sn = self.get_argument('coupon').encode('utf-8') sign = self.get_argument('sign').encode('utf-8') timestamp = self.get_argument('timestamp').encode('utf-8') logging.info( 'telephone verify. caller: %s, coupon_sn: %s, sign: %s, timestamp: %s', caller, coupon_sn, sign, timestamp) if not caller: logging.error('telephone verify failed: empty caller.') self.write('1|主叫号码无效') return if not coupon_sn: logging.error('telephone verify failed: empty coupon.') self.write('2|券号为空') return if not sign: logging.error('telephone verify failed: empty sign.') self.write('3|签名无效') return if not timestamp: logging.error('telephone verify failed: empty timestamp.') self.write('3|时间戳无效') return if abs(int(time.time()) - int(timestamp)) > 300: logging.error('telephone verify failed: request timeout.') self.write('3|请求超时') return # 暂不检查签名 # todo # 查找电话 supplier_shop = self.db.get( 'select * from supplier_shop where deleted=0 and verify_phones like %s', '%' + caller + '%') #if not supplier_shop: # #部分商家使用分机,出口线很多,不能一一录入,这里可以使用电话号码的前面一部分进行识别。 # #尝试模糊查找验证电话机 # supplier_user_list = self.db.query('select * from supplier_user where type="ANDROID"') # for su in supplier_user_list: # if caller.startswith(su.loginname): # supplier_shop = su # break if not supplier_shop: logging.error( 'telehphone verify failed. can not found valid supplier_user %s.', caller) self.write('1|您的电话还未绑定') return coupon = self.db.get( 'select id, sn, mobile from item_coupon where sn=%s', coupon_sn) if not coupon: if len(coupon_sn) == 9: for i in range(10): coupon = self.db.get( 'select id, sn, mobile from item_coupon where sn=%s', '%s%s' % (i, coupon_sn)) if coupon: #我们自己的券 check_result = local_check(self.db, coupon.sn, supplier_shop.id) if not check_result.ok: ok, msg = (False, check_result.msg) else: # 合作伙伴API验证 api_result = yield partner_api_verify(self.db, coupon.sn) if not api_result.ok: ok, msg = (False, api_result.msg) else: # 本地验证 verify_result = local_verify(self.db, coupon.sn, supplier_shop.id, '电话验证') if verify_result.ok: ok, msg = (True, verify_result.msg) else: ok, msg = (False, verify_result.msg) else: results = yield partner_browser_verify(self.db, [coupon_sn], supplier_shop.id) # 记录外部订单 for result in results: if result.ok: goods_info = self.db.get( 'select g.* from goods g where id=%s', result.goods_id) verify_infos = { 'goods_id': result.goods_id, 'shop_id': supplier_shop.id } #创建分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, NOW())', result.coupon_sn, json_dumps(verify_infos), result.distributor_shop_id) order_id, order_no = new_distributor_order( self.db, result.distributor_shop_id, goods_info.sales_price, goods_info.sales_price, '') #记录订单信息 new_order = new_distributor_item( db=self.db, order_id=order_id, order_no=order_no, sales_price=None, count=1, goods_info=goods_info, mobile='', distributor_shop_id=result.distributor_shop_id, distributor_goods_id='', distributor_coupons=[{ 'coupon_sn': result.coupon_sn, 'coupon_pwd': result.coupon_pwd or '' }], use_distributor_coupon=True) if new_order.ok: #更新分销订单id self.db.execute( 'update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) #更新订单id self.db.execute( 'update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) #更新该券验证的门店 self.db.execute( 'update item set sp_shop_id = %s where order_id=%s', supplier_shop.id, order_id) self.redis.lpush( options.queue_coupon_local_verify, json_dumps({ 'coupon': result.coupon_sn, 'shop_id': supplier_shop.id, 'retry': 0, 'used_at': datetime.now() })) ok, msg = (results[0].ok, results[0].msg) if not ok: logging.error('telephone verify failed. %s', msg) self.write('2|验证失败') else: logging.info( 'telephone verify ok. caller: %s, coupon_sn: %s, sign: %s, timestamp: %s', caller, coupon_sn, sign, timestamp) # 发送验证确认短信 CouponConsumedMessage(self.redis, [coupon.sn], supplier_shop.name, coupon.mobile).send() if coupon: face_value = self.db.get( 'select face_value from item i, item_coupon c where i.id=c.item_id ' 'and c.id=%s', coupon.id).face_value self.write('0|券尾号%s验证成功,面值%s' % (coupon_sn[-4:], upper(face_value).encode('utf-8'))) else: self.write('0|券尾号%s验证成功' % coupon_sn[-4:])
def post(self): distributor_shop_id = self.get_argument('distributor_id').encode( 'utf-8') goods_id = self.get_argument('goods_id').encode('utf-8') num = int(self.get_argument('num').encode('utf-8')) # 参数错误返回 if not distributor_shop_id or not goods_id or not num: return self.render('coupon/export_result.html', result="出错了!!参数错误, 请重新操作", coupons=[], goods_name='', order_id='') # 没有商品,返回 goods_info = self.db.get( 'select * from goods where id=%s and deleted=0', goods_id) if not goods_info: return self.render('coupon/export_result.html', result="出错了!!找不到商品", coupons=[], goods_name='', order_id='') # 检查库存,如果库存不足,返回 if int(goods_info.stock) < num: return self.render('coupon/export_result.html', result="出错了!!库存不足", coupons=[], goods_name='', order_id='') # 生成订单 order_amount = goods_info.sales_price * num order_id, order_no = new_distributor_order(self.db, distributor_shop_id, order_amount, order_amount, "") result = new_distributor_item(self.db, order_id, order_no, goods_info.sales_price, num, goods_info, "", distributor_shop_id, "", [], False) # 生成分销订单 self.db.execute( 'insert into distributor_order (order_no, created_at, message, distributor_shop_id, order_id) ' 'values (%s, NOW(), %s, %s, %s)', 'EXPORT%s' % int(time.time()), '批量导出电子券', distributor_shop_id, order_id) if not result.ok: return self.render('coupon/export_result.html', result=result.msg, goods_name=goods_info.short_name, coupons=[], order_id='') else: # 写日志,订单记一个,商品记一个 operator = self.get_current_user().name message = "批量导出电子券, 分销商店铺id:%s, 数量:%s, 商品:%s" % ( distributor_shop_id, num, goods_info.short_name) self.db.execute( 'insert into journal (created_at, type, created_by, message, iid) values ' '(NOW(), 1, %s, %s, %s)', operator, message, order_id) self.db.execute( 'insert into journal (created_at, type, created_by, message, iid) values ' '(NOW(), 3, %s, %s, %s)', operator, message, goods_id) coupons = [c['coupon_sn'] for c in result.coupons] self.render('coupon/export_result.html', result=result.msg, coupons=coupons, goods_name=goods_info.short_name, order_id=order_id)
def post(self): """ 开始导入""" if not self.request.files: self.render('real/import_outer_order.html', message=u'请选择文件') #读取文件 order_file = self.request.files['order_file'][0] book = xlrd.open_workbook(file_contents=order_file['body']) try: sheet = book.sheet_by_index(0) except: self.render('real/import_outer_order.html', message=u'文件格式错误,请检查') return partner = self.get_argument('partner').lower() distributor_shop_id = {'dp': 40, 'mt': 44, 'ls': '45', 'nm': 42, 'ww': 37}.get(partner) #读取工作表的所有行数据内容 rows = [sheet.row_values(i) for i in range(1, sheet.nrows)] #转换数据 col_names = ('coupon_sn', 'goods_id') coupon_list = [dict([(col_names[i], row[i]) for i in range(len(col_names))]) for row in rows] existed_orders = [] need_convert_orders = [] goods_id = -1 for order in coupon_list: current_goods_id = order['goods_id'] if goods_id != current_goods_id: goods_id = current_goods_id goods_info = self.db.get('select g.* ,ss.id shop_id from goods g,supplier_shop ss ,supplier s ' 'where ss.deleted=0 and s.id=ss.supplier_id and s.id=g.supplier_id ' 'and g.id=%s limit 1', goods_id) verify_infos = {'goods_id': goods_id, 'shop_id': goods_info.shop_id} if isinstance(order['coupon_sn'], int) or isinstance(order['coupon_sn'], float): need_convert_orders.append(str(order['coupon_sn'])) continue #判断是不是已经导入过的外部订单 distributor_order = self.db.get('select * from distributor_order where distributor_shop_id=%s and ' 'order_no=%s', distributor_shop_id, order['coupon_sn']) if distributor_order: existed_orders.append(str(order['coupon_sn'])) continue created_at = datetime.now() - timedelta(days=1) #创建分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, %s)', order['coupon_sn'], json_dumps(verify_infos), distributor_shop_id, created_at) order_id, order_no = new_distributor_order(self.db, distributor_shop_id, goods_info.sales_price, goods_info.sales_price, "") # 改时间 self.db.execute('update orders set created_at=%s, paid_at=%s where id=%s', created_at, created_at, order_id) self.db.execute('update journal set created_at=%s where type=1 and iid=%s', created_at, order_id) #记录订单信息 new_order = new_distributor_item( db=self.db, order_id=order_id, order_no=order_no, sales_price=None, count=1, goods_info=goods_info, mobile='', distributor_shop_id=distributor_shop_id, distributor_goods_id='', distributor_coupons=[{'coupon_sn': order['coupon_sn'], 'coupon_pwd': ''}], use_distributor_coupon=True ) if new_order.ok: #更新分销订单id self.db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) #更新订单id self.db.execute('update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) #更新该券验证的门店 self.db.execute('update item set sp_shop_id = %s where order_id=%s', goods_info.shop_id, order_id) #设定时间为前一天 self.db.execute('update item set created_at=%s where order_id=%s', created_at, order_id) #自动验证 self.redis.lpush(options.queue_coupon_local_verify, json_dumps({'coupon': order['coupon_sn'], 'shop_id': goods_info.shop_id, 'retry': 0, 'used_at': created_at})) self.render('order/import_outer_order.html', message='success', existed_orders=existed_orders, need_convert_orders=need_convert_orders)
def post(self): self.set_header('Content-Type', 'application/json; charset=UTF-8') coupon_list = [ i.strip() for i in self.get_argument('coupons', '').split(',') if i.strip() ] if len(coupon_list) == 0: self.write({'ok': False, 'msg': u'请输入券号'}) return shop_id = self.get_argument('shop_id', 0) #这一步是检查该操作员是否有权限验证券 all_shops = self.db.query( 'select ss.* from supplier_shop ss, supplier_user su where ss.supplier_id=%s and ' 'ss.deleted=0 and su.id=%s and su.supplier_id=ss.supplier_id and ' ' (su.shop_id=0 or ss.id=su.shop_id)', self.current_user.supplier_id, self.current_user.id) if int(shop_id) not in [i.id for i in all_shops]: self.write({'ok': False, 'msg': u'对不起,您无权执行此操作'}) return is_our_coupon = False coupon_length = 0 messages = PropDict() # 用来存不同的手机号对应的券号 #检测长度是否相同、是否是我们的券 for coupon_sn in coupon_list: coupon = self.db.get( 'select id, sn, mobile from item_coupon where sn=%s', coupon_sn) if coupon: is_our_coupon = True if coupon_length == 0: coupon_length = len(coupon_sn) elif coupon_length != len(coupon_sn): coupon_length = -1 if coupon_length == -1: self.write({'ok': False, 'msg': u'券号长度必须一致'}) return result_list = [] if is_our_coupon: # 我们自己的券 for coupon_sn in coupon_list: # 本地检测 check_result = local_check(self.db, coupon_sn, shop_id) if not check_result.ok: ok, msg = (False, check_result.msg) else: # 合作伙伴API验证 api_result = yield partner_api_verify(self.db, coupon_sn) if not api_result.ok: ok, msg = (False, api_result.msg) else: # 本地验证 verify_result = local_verify(self.db, coupon_sn, shop_id, self.current_user.name) ok, msg = (verify_result.ok, verify_result.msg) # 验证通过,记录需要发送确认短信的券 if ok: if messages.get(str(check_result.coupon.mobile)): # 手机号已存在,添加券号到对应手机号 messages.get(str( check_result.coupon.mobile)).append( str(coupon_sn)) else: # 手机号不存在,新建K-V对 messages.update({ str(check_result.coupon.mobile): [str(coupon_sn)] }) result_list.append({ 'coupon_sn': coupon_sn, 'ok': ok, 'msg': msg }) else: mobile = self.get_argument('mobile', '') # 检查手机号合法性 mobile_ptn = re.compile('^1[\d+]{10}$') if not re.match(mobile_ptn, mobile): # 不合法手机号,不记录 mobile = '' # 尝试外部验证 results = yield partner_browser_verify(self.db, coupon_list, shop_id) for result in results: if result.ok: goods_info = self.db.get( 'select g.* from goods g where id=%s', result.goods_id) verify_infos = { 'goods_id': result.goods_id, 'shop_id': shop_id } #创建分销订单 distributor_order_id = self.db.execute( 'insert into distributor_order(order_no, message, distributor_shop_id, created_at) ' 'values (%s, %s, %s, NOW())', result.coupon_sn, json_dumps(verify_infos), result.distributor_shop_id) order_id, order_no = new_distributor_order( self.db, result.distributor_shop_id, goods_info.sales_price, goods_info.sales_price, mobile) #记录订单信息 new_order = new_distributor_item( db=self.db, order_id=order_id, order_no=order_no, sales_price=None, count=1, goods_info=goods_info, mobile=mobile, distributor_shop_id=result.distributor_shop_id, distributor_goods_id='', distributor_coupons=[{ 'coupon_sn': result.coupon_sn, 'coupon_pwd': result.coupon_pwd or '' }], use_distributor_coupon=True) if new_order.ok: #更新分销订单id self.db.execute( 'update orders set distributor_order_id=%s where id = %s', distributor_order_id, order_id) #更新订单id self.db.execute( 'update distributor_order set order_id=%s where id=%s', order_id, distributor_order_id) #更新该券验证的门店 self.db.execute( 'update item set sp_shop_id = %s where order_id=%s', shop_id, order_id) self.redis.lpush( options.queue_coupon_local_verify, json_dumps({ 'coupon': result.coupon_sn, 'shop_id': shop_id, 'retry': 0, 'used_at': datetime.now() })) result_list.append({ 'coupon_sn': result.coupon_sn, 'coupon_pwd': result.coupon_pwd or '', 'ok': result.ok, 'msg': result.msg }) # 如果有合法券号,发送确认短信 if messages: shop_name = self.db.get( 'select name from supplier_shop where id=%s', shop_id).name for mobile, coupon_sns in messages.items(): CouponConsumedMessage(self.redis, coupon_sns, shop_name, mobile).send() self.write({'ok': True, 'result': result_list})
def taobao_order(db, redis, distributor_order_id, message_raw): """ 处理淘宝的分销订单队列 :param db :param redis :param distributor_order_id: 分销订单id :param message_raw: redis 队列元素值 :type db:torndb.Connection :type redis:redis.client.StrictRedis :type distributor_order_id: int """ distributor_order = db.get('select * from distributor_order where id=%s', distributor_order_id) params = json.loads(distributor_order.message, object_hook=json_hook) shop = db.get('select * from distributor_shop where taobao_seller_id=%s', params.taobao_sid) api_info = json.loads(shop.taobao_api_info, object_hook=json_hook) if not distributor_order.order_id: # 如果还没生成一百券订单 goods_link_id = int(params.outer_iid) # 找到关联的商品 goods_info = db.get('select g.* from goods g, goods_distributor_shop gds ' 'where g.id = gds.goods_id and distributor_shop_id=%s and goods_link_id=%s', shop.id, goods_link_id) if not goods_info and goods_link_id < 20000: goods_info = db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id) if not goods_info: logging.error('taobao order consume failed: goods not found. link_id: %s', goods_link_id) return taobao = Taobao('taobao.trade.get') taobao.set_app_info(api_info.app_key, api_info.app_secret_key) taobao.set_session(api_info.session) response = taobao.sync_fetch(tid=distributor_order.order_no, fields='total_fee,payment,orders.payment,orders.num,' 'orders.sku_properties_name,orders.price') taobao.parse_response(response) order_payment = Decimal(taobao.message.trade.payment) # 创建订单 order_id, order_no = new_distributor_order(db, shop.id, Decimal(taobao.message.trade.total_fee), order_payment, params.mobile) result = new_distributor_item(db, order_id, order_no, order_payment / int(params.num), int(params.num), goods_info, params.mobile, shop.id, params.num_iid, None, False) if not result.ok: logging.error('taobao order consume failed. %s', result.msg) return else: db.execute('update orders set distributor_order_id=%s where id = %s', distributor_order_id, result.order_id) db.execute('update distributor_order set order_id=%s where id=%s', result.order_id, distributor_order_id) ktv_order_result = create_ktv_order(db, order_id, params) else: order_id = distributor_order.order_id ktv_order_result = [] coupons = db.query('select c.sn as coupon_sn from item i, item_coupon c where i.id=c.item_id and i.order_id=%s', order_id) # 告诉淘宝我们已发货 taobao = Taobao('taobao.vmarket.eticket.send') taobao.set_app_info(api_info.app_key, api_info.app_secret_key) if api_info.app_key == options.taobao_kunran_app_key: # 如果是码商,要加上码商的信息 taobao.add_field('codemerchant_id', options.taobao_kunran_id) taobao.set_session(api_info.merchant_session) else: taobao.set_session(api_info.session) response = taobao.sync_fetch(order_id=params.order_id, token=params.token, verify_codes=','.join(['%s:1' % item.coupon_sn for item in coupons])) logging.info('tell taobao coupon send response: %s', response) taobao.parse_response(response) if taobao.is_ok(): logging.info('taobao order complete. distributor_order_id: %s', distributor_order_id) all_order_items = db.query('select * from order_item where order_id=%s', order_id) for item in all_order_items: CouponSMSMessage(db, redis, order_item=item).remark('淘宝订单短信发送').send() redis.lrem(options.queue_distributor_order_processing, 0, message_raw) #只要ktv预订时间有,就给门店经理发送ktv预订的包厢信息 for ktv_info in ktv_order_result: sku = ktv_info['sku'] phone_numbers = ktv_info['manager_mobile'] for phone in (phone_numbers.split(',') if phone_numbers else []): content = str(sku.date) + ktv_info['shop_name'] + '预订【' + str(params.mobile) + \ sku.room_name + " (" + str(params.num) + "间)" + sku.human_time_range + "】" #给经理发送短信告知预订信息 logging.info('send message to manager,phone:%s,content:%s', phone, content) SMSMessage(content, phone).send(redis) else: logging.error('tell taobao coupon send failed: %s', taobao.error)