Commit 37c3a38b authored by tarak.li's avatar tarak.li

优化代码,场景测试

parent aedddc03
......@@ -184,19 +184,25 @@ def add_account_host_bind(handler, args):
operator = handler.get_current_user()
# 1. 判断账户是否已经存在了
sql = 'SELECT id FROM tp_remote_account_host_bind WHERE account="{}"'.format(args['account'])
sql = 'SELECT id FROM tp_remote_account_host_bind WHERE account="{}" '.format(args['account'])
db_ret = db.query(sql)
if db_ret is not None and len(db_ret) > 0:
return TPE_EXISTS, 0
# 2. 判断host_id 是否存在
# todo 只允许同资质绑定
sql = 'SELECT id FROM tp_remote_host WHERE id="{}"'.format(args['host_id'])
db_ret = db.query(sql)
if not db_ret:
return TPE_NOT_EXISTS, 0
sql = 'INSERT INTO `tp_remote_account_host_bind` (biz_id,mch_no, comp_id, host_id, host_assigned, account_source, username, password, login_status, mch_name, create_tiem, create_by, update_time, update_by) VALUES ' \
# 3. 是否已经存在不同资质公司
sql = 'SELECT comp_id FROM tp_remote_account_host_bind WHERE host_id={}'.format(args['host_id'])
db_ret = db.query(sql)
comp_id = args.get('comp_id')
if db_ret and (comp_id,) not in db_ret:
return TPE_FAILED, 0
sql = 'INSERT INTO `tp_remote_account_host_bind` (biz_id,mch_no, comp_id, host_id, host_assigned, account_source, account, password, login_status, mch_name, create_tiem, create_by, update_time, update_by) VALUES ' \
'({biz_id},"{mch_no}", {comp_id}, {host_id}, {host_assigned}, {account_source}, "{account}", "{password}", {login_status},"{mch_name}", "{create_time}", "{create_by}", "{update_time}", "{update_by}");' \
''.format(mch_no=args.get("mch_no", ""), comp_id=args.get("comp_id", 0), host_id=args.get("host_id", 0),
host_assigned=args.get("host_assigned", 0), account_source=args.get("account_source", 1),
......@@ -229,6 +235,9 @@ def update_account_host_bind(handler, args):
sql = 'SELECT comp_id FROM tp_remote_account_host_bind WHERE host_id="{}"'.format(args['host_id'])
db_ret = db.query(sql)
if not db_ret:
return TPE_NOT_EXISTS
# 不允许修改公司资质
if db_ret[0][0] != args['comp_id']:
return TPE_FAILED
......@@ -243,11 +252,11 @@ def update_account_host_bind(handler, args):
id = db_ret[0][0]
sql = 'UPDATE `tp_remote_account_host_bind` SET ' \
'`mch_no`="{mch_no}", `comp_id`={comp_id}, `host_id`="{host_id}", ' \
'`host_assigned`={host_assigned}, `account_source`="{account_source}", `username`="{username}", `password`="{password}", ' \
'`host_assigned`={host_assigned}, `account_source`="{account_source}", `account`="{account}", `password`="{password}", ' \
'`login_status`="{login_status}", `mch_name`="{mch_name}", `update_time`="{update_time}", `update_by`="{update_by}" WHERE `id`={id};' \
.format(mch_no=args.get("mch_no", ""), comp_id=args.get("comp_id", 0), host_id=args.get("host_id", 0),
host_assigned=args.get("host_assigned", 0), account_source=args.get("account_source", 1),
username=args.get("username", ""), password=args.get("password", ""),
account=args.get("account", ""), password=args.get("password", ""),
login_status=args.get("login_status", 0), mch_name=args.get("mch_name", ""),
update_time=_time_now, update_by=operator['id'], id=id)
......@@ -268,10 +277,10 @@ def get_session_info(host_id, mch_no, ip):
if host_id:
sql = 'SELECT username FROM tp_remote_host WHERE id="{}"'.format(host_id)
elif ip:
sql = """select a.username
sql = """select a.username,a.id
from tp_remote_host a where ip = "{}" """.format(ip)
elif mch_no:
sql = """select a.username
sql = """select a.username,a.id
from tp_remote_host a left join tp_remote_account_host_bind b on a.id = b.host_id
where b.mch_no = "" """.format(mch_no)
else:
......@@ -280,11 +289,12 @@ def get_session_info(host_id, mch_no, ip):
db_ret = db.query(sql)
username = db_ret[0][0]
host_id = host_id or db_ret[0][1]
sql = 'SELECT id FROM tp_acc WHERE host_id="{}" and username = "{}"'.format(host_id, username)
db_ret = db.query(sql)
acc_id = db_ret[0][0]
return TPE_OK, acc_id
return TPE_OK, acc_id, host_id
def get_host_ip(host_id):
......@@ -295,15 +305,56 @@ def get_host_ip(host_id):
return ip
def query(table: str, fields: list, where: str = None):
def query(table: str, fields: list, where: dict):
s = SQL(get_db())
s.select_from(table, fields, alt_name='a')
if where:
s.where('a.{}'.format(where))
_where = ' and '.join(
['`{key}`={value}'.format(key=k, value='"{}"'.format(v) if isinstance(v, str) else v) for k, v in
where.items()])
s.where(_where)
err = s.query()
if err != TPE_OK:
return None
return {}
return s.recorder
def query_one(table: str, fields: list, where: dict):
s = SQL(get_db())
s.select_from(table, fields, alt_name='a')
if where:
_where = ' and '.join(
['`{key}`={value}'.format(key=k, value='"{}"'.format(v) if isinstance(v, str) else v) for k, v in
where.items()])
s.where(_where)
err = s.query()
if err != TPE_OK:
return {}
if s.recorder.__len__() > 0:
return s.recorder[0]
else:
return {}
def update(table: str, fields: dict, where: dict):
db = get_db()
_set = ','.join(['`{key}`={value}'.format(key=k, value='"{}"'.format(v) if isinstance(v, str) else v) for k, v in
fields.items()])
_where = ' and '.join(
['`{key}`={value}'.format(key=k, value='"{}"'.format(v) if isinstance(v, str) else v) for k, v in
where.items()])
sql = 'UPDATE `{table}` SET {set} WHERE {where};'.format(table=table, set=_set, where=_where)
print(sql)
db_ret = db.exec(sql)
if not db_ret:
return TPE_DATABASE
return TPE_OK
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment