def addPayCallbackData(self,pay_order_id = 0,type = 'pay',data = ''): model_callback = PayOrderCallbackData() model_callback.pay_order_id = pay_order_id if type == "pay": model_callback.pay_data = data model_callback.refund_data = '' else: model_callback.refund_data = data model_callback.pay_data = '' model_callback.created_time = model_callback.updated_time = getCurrentDate() db.session.add( model_callback ) db.session.commit() return True
def addPayCallbackData(self, pay_order_id=0, type='pay', data=''): try: app.logger.info("enter 1") if int(pay_order_id) < 1: return False app.logger.info("enter 11") pay_order_info = PayOrder.query.filter_by( id=pay_order_id, express_status=-7).first() if not pay_order_info: return False pay_order_items = PayOrderItem.query.filter_by( pay_order_id=pay_order_id).all() if not pay_order_items: return False model_callback = PayOrderCallbackData() model_callback.pay_order_id = pay_order_id if type == "pay": app.logger.info("enter 2") # `1` # 新增 售卖数量改变sale # pay_order_items = PayOrderItem.query.filter_by(pay_order_id=pay_order_id).all() for order_item in pay_order_items: tmp_model_sale_log = FoodSaleChangeLog() tmp_model_sale_log.food_id = order_item.food_id tmp_model_sale_log.quantity = order_item.quantity tmp_model_sale_log.price = order_item.price tmp_model_sale_log.member_id = order_item.member_id tmp_model_sale_log.created_time = getCurrentDate() db.session.add(tmp_model_sale_log) db.session.commit() # `2` # 每个月的月底进行统计或者更新 # 更新销售总量 notice_content = [] if pay_order_items: date_from = datetime.datetime.now().strftime( "%Y-%m-01 00:00:00") date_to = datetime.datetime.now().strftime( "%Y-%m-31 23:59:59") for item in pay_order_items: tmp_food_info = Food.query.filter_by( id=item.food_id).first() if not tmp_food_info: continue notice_content.append( "%s %s份" % (tmp_food_info.name, item.quantity)) # 当月数量 # query( 0, 1) 当中指针为0 和 1的两个对象。 tmp_stat_info = db.session.query(FoodSaleChangeLog, func.sum(FoodSaleChangeLog.quantity).label("total")) \ .filter(FoodSaleChangeLog.food_id == item.food_id)\ .filter(FoodSaleChangeLog.created_time >= date_from, FoodSaleChangeLog.created_time <= date_to).first() app.logger.info( "当月数量:tmp_stat_info:{0}".format(tmp_stat_info)) tmp_month_count = tmp_stat_info[1] if tmp_stat_info[ 1] else 0 tmp_food_info.total_count += item.quantity # 错误修改 tmp_food_info.month_count = tmp_month_count db.session.add(tmp_food_info) app.logger.info("enter 3") # `3` # 设置为取餐号 pay_order_info.express_status = -6 # -6 pay_order_info.updated_time = getCurrentDate() db.session.add(pay_order_info) model_callback.pay_data = data model_callback.refund_data = '' elif type == "cancel": # `1` 归还库存 if pay_order_items: # 需要归还库存 for item in pay_order_items: tmp_food_info = Food.query.filter_by( id=item.food_id).first() if tmp_food_info: tmp_food_info.stock = tmp_food_info.stock + item.quantity tmp_food_info.updated_time = getCurrentDate() db.session.add(tmp_food_info) db.session.commit() FoodService.setStockChangeLog( item.food_id, item.quantity, "订单取消") # `2` 变更状态 pay_order_info.status = 0 pay_order_info.express_status = 0 pay_order_info.updated_time = getCurrentDate() db.session.add(pay_order_info) db.session.commit() model_callback.refund_data = data model_callback.pay_data = '' else: db.session.rollback() return False model_callback.created_time = model_callback.updated_time = getCurrentDate( ) db.session.add(model_callback) db.session.commit() except Exception as e: db.session.rollback() print("exception is :%s" % e) app.logger.info("exception") return False # 统计end return True