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)