def add_user_recharge(user_id, user_name, total_fee, out_trade_no, trade_status, recharge_way): """method to update user money """ # 用户冲值记录, 只要调用支付宝接口就生成。根据状态判断是否成功 # check user is it exist user = User.query.filter(User.user_id == user_id).first() if not user: em = "can not found user. user id: <{0}>".format(user_id) LOG.exception(em) # return {"error": [{"code": 400, "msg": "{0}".format(em)}]} return 0 uid = str(uuid.uuid1()) log_time = int(time.time()) try: # 记录冲值记录,默认状态为`failed` recharge_log = RechargeLog(uid, user_id, user_name, total_fee, log_time, out_trade_no, trade_status, recharge_way=recharge_way) db_session.add(recharge_log) db_session.commit() return {"success": [{"code": 200, "msg": ""}]} except Exception as e: db_session.rollback() em = "recharge for user <{0}> error. user id : <{1}>. msg: {2}".format( user.user_name, user_id, e) LOG.exception(em) return {"error": [{"code": 500, "msg": "{0}".format(em)}]}
def add_user(data): project_id = data.get("tenant_id") project_name = data.get("_context_project_name") user_id = data.get("user_id") user_name = data.get('_context_user_name') id = str(uuid.uuid1()) exist_user = User.query.filter(User.user_id == user_id).first() try: if not exist_user: db_users = User(id=id, user_id=user_id, user_name=user_name, project_id=project_id, project_name=project_name, register_time=int(time.time())) db_session.add(db_users) db_session.commit() em = "add user: <{0}> success".format(user_name) LOG.debug(em) return { "success": [{ "code": 200, "msg": "{0}".format(em), "data": db_users }] } except Exception as e: db_session.rollback() em = "add user: <{0}> failed msg: {1}".format(user_name, e) LOG.exception(em) return {"error": [{"code": 400, "msg": "{0}".format(em)}]}
def update_user_money(money, order): """ this method to update user's money(order's money) :param order: db object with order info :return: """ try: if float(money) <= 0: return {"code": 0, "message": ""} user = User.query.filter(User.user_id == order.user_id).first() if not user: em = "can not found user id: <{0}>".format(order.user_id) LOG.exception(em) return False user.money = (float(user.money) if float(user.money) else 0) new_money = user.money - money if new_money < 0 and order.order_type == options.order_immed: em = "user have no enough money. user id: <{0}> resource id: <{1}> " \ "resource name: <{2}>".format(order.user_id, order.resource_id, order.resource_name) LOG.exception(em) return {"code": 1, "message": em} User.query.filter(User.user_id == order.user_id).update( {User.money: new_money}) db_session.flush() msg = "update user <{0}> money from <{1}> to <{2}>".format( user.user_id, user.money, new_money) LOG.debug(msg) # add log consume_log = ConsumeLog(str(uuid.uuid1()), order.user_id, order.user_name, order.project_id, order.resource_name, order.resource_type, order.resource_id, order.start_time, order.end_time, money, int(time.time()), resource_from=order.resource_from, order_uid=order.uid, details=order.resource) db_session.add(consume_log) db_session.commit() # 实时同步数据到cmdb CMDBHandle.syncdata2cmdb("consume", consume_log.uid) return {"code": 0, "message": ""} except Exception as e: db_session.rollback() em = "update user money failed. user id: <{0}> " \ "resource id: <{1}> resource name: <{2}> msg: <{3}>".format(order.user_id, order.resource_id, order.resource_name, e) LOG.exception(em) return {"code": 1, "message": em}
def cycle_update_user(): "" try: now_time = int(time.time()) User.query.filter(User.exceed_time < now_time).update( {User.is_exceed: True}) db_session.commit() except Exception as e: db_session.rollback() em = "cycle update user error. msg: <{0}>".format(e) LOG.exception(em)
def change_user_resource_status(user_id, status): """status: is a boolean value. True or False""" try: User.query.filter(User.user_id == user_id).update( {User.is_destroy: status}) db_session.commit() return True, 200 except Exception as e: db_session.rollback() em = "update user's status error msg: <{0}>".format(e) LOG.exception(em) return False, 500
def update_user_recharge(async_data): """根据号in_trade_no(唯一订单号)来更新用户冲值记录的状态""" out_trade_no = async_data.get("out_trade_no") trade_status = async_data.get("trade_status") money = float(async_data.get("price")) try: # 更新用户余额(如果是成功的消息) if trade_status == "success": # 根据订单号从recharge_log表找用户user_id recharg = RechargeLog.query.filter( RechargeLog.out_trade_no == out_trade_no).first() if not recharg: em = "can not fond user's user_id from table recharge_log" return {"error": [{"code": 500, "msg": "{0}".format(em)}]} user_id = recharg.user_id user = User.query.filter(User.user_id == user_id).first() if not user: em = "can not fond user user_id : <{0}>".format(user_id) LOG.exception(em) return 0 user_money = user.money new_money = money + (float(user_money) if float(user_money) else 0) is_exceed = (True if new_money <= 0 else False) # up date user's money User.query.filter(User.user_id == user_id).update({ User.money: new_money, User.is_exceed: is_exceed }) db_session.commit() ExprOrders.sync_user_exceed_time(user_id=user_id) # 更新日志记录 RechargeLog.query.filter( RechargeLog.out_trade_no == out_trade_no).update( {RechargeLog.trade_status: trade_status}) msg = "user <{0}> recharge money <{1}>".format(user_id, money) LOG.debug(msg) db_session.commit() # 同步数据到cmdb result = RechargeLog.query.filter( RechargeLog.out_trade_no == out_trade_no).first() CMDBHandle.syncdata2cmdb("recharge", result.uid) return {"success": [{"code": 200, "msg": ""}]} except Exception as e: db_session.rollback() em = "update user recharge failed with in_trade_no: <{0}> msg: {1}".format( out_trade_no, e) LOG.exception(em) return {"error": [{"code": 500, "msg": "{0}".format(em)}]}
def add_discount(id, money, valid_date, user_context, description=None): try: if not id or not money or not valid_date or not user_context: em = "invalid argement with add discount" LOG.exception(em) return False, 400 # must be admin role if not user_context.get("user").get("admin"): em = "user <{0}> is not admin. role must be admin.".format( user_context.get("user").get("user").get("name")) LOG.exception(em) return False, 400 # check discount id is it esxit discount = Discount.query.filter( Discount.discount_id == id).first() if discount: em = "discount id discount_id already exist. id: <0>".format( id) LOG.exception(em) return False, 409 # add discount id add_discount = Discount(uid=str(uuid.uuid1()), discount_id=id, money=money, gen_time=int(time.time()), valid_date=int( ConvertTime2Int(valid_date)), is_used=False, description=description) db_session.add(add_discount) db_session.commit() LOG.debug( "add discount id Successful. discount id: <{0}>".format(id)) return True, 200 except Exception as e: db_session.rollback() em = "add discount error. discount id: <{0}>. msg: <{1}>".format( id, e) LOG.exception(em) return False, 500
def use_discount(id, user_context): try: now_time = int(time.time()) # check discount id is it exsit discount = Discount.query.filter( Discount.discount_id == id).first() if not discount: em = "can not found discount id. id: <0>".format(id) LOG.exception(em) return False, 410 # check discount id is is exceed if discount.valid_date or discount.is_used: if discount.valid_date <= now_time or discount.is_used: em = "discount id is not valid. id: <{0}>".format( discount.discount_id) LOG.exception(em) return False, 400 user_id = user_context.get("user").get("user").get("id") # update user's money user = User.query.filter(User.user_id == user_id).first() if not user: em = "can not found user. user id: <{0}>".format(user_id) LOG.exception(em) # try to add user data = { "tenant_id": user_context.get("tenant_id"), "_context_project_name": user_context.get("user").get("project").get("name"), "user_id": user_context.get("user").get("user").get("id"), "_context_user_name": user_context.get("user").get("user").get("id") } ret = UserOperate.add_user(data) if not ret.get("success"): return False, 500 user = ret.get("success")[0].get("data") user.money += discount.money # update discount tables's status # Discount.query.filter(Discount.discount_id == id).update({Discount.is_used: True}) # add Recharge Log recharge = RechargeLog( uid=str(uuid.uuid1()), user_id=user_id, user_name=user_context.get("user").get("user").get("name"), money=discount.money, log_time=int(time.time()), out_trade_no=discount.discount_id, trade_status="success", recharge_way="discount_id") db_session.add(recharge) db_session.flush() # update discount id Discount.query.filter(Discount.discount_id == id).update({ Discount.is_used: True, Discount.used_time: now_time }) db_session.commit() LOG.debug("discount id used by user id: <{0}>") # update user's exceed time ExprOrders.sync_user_exceed_time(user_id=user_id) return True, 200 except Exception as e: db_session.rollback() em = "use discount id error. id: <{0}> user id<{1}>".format( id, user_id) LOG.exception(em) return False, 500
def sync_user_exceed_time(user_id=None): """数据同步,根据用户当前订单及余额,更新用户超时时间""" try: now_time = int(time.time()) if not user_id: all_users = User.query.filter().all() else: all_users = User.query.filter(User.user_id == user_id).all() for user in all_users: total_used = 0 min_used = 0 orders = Orders.query.filter( and_(Orders.user_id == user.user_id, Orders.status == None, Orders.resource_type)).all() # 如果没有订单,余额大于0,更新过期时间为空,未过期 if not orders and float(user.money) > 0: User.query.filter(User.user_id == user.user_id).update({ User.is_exceed: False, User.exceed_time: None, }) db_session.flush() for order in orders: order.end_time = now_time order.order_type = options.order_immed price = Price.query.filter( Price.price_type == order.resource_type).first() if not price: em = "can not found price type. price type: <{0}>".format( order) LOG.exception(em) return False used = ExprOrders.order_used_money(order, price, immed=True) if used: total_used += used order.end_time = None if total_used > float(user.money): if not user.is_exceed: User.query.filter(User.user_id == user.user_id).update( { User.is_exceed: True, User.exceed_time: now_time, }) db_session.flush() else: for order in orders: price = Price.query.filter( Price.price_type == order.resource_type).first() used = ExprOrders.order_used_money(order, price, immed=False) if used: min_used += used if total_used > 0 and min_used > 0: new_money = float(user.money) - total_used valid_use_sec = new_money / min_used * 60 user_exceed_time = valid_use_sec + now_time User.query.filter(User.user_id == user.user_id).update( { User.exceed_time: user_exceed_time, User.is_exceed: False }) db_session.flush() em = "update <{0}> user's exceed time to <{1}>".format( user.user_id, user_exceed_time) LOG.debug(em) print "==================finish one. user_id: <{0}>==============================".format( user.user_id) db_session.commit() except ExprOrders as e: db_session.rollback() print "error. user_id==========>>>, ", user.user_id print e
def on_finish(self): if self.get_status() == 500: db_session.rollback() else: db_session.remove()