Exemple #1
0
class ApiSystem(Resource):
    def __init__(self):
        self.data = get_request_json()
        self.username = redis.get_username(request.headers.get('X-Token'))
        self.acim = ApiCompanyInfoManager()
        self.asim = ApiSystemInfoManager()
        self.aiim = ApiIntfInfoManager()
        self.apsrm = ApiProjectSystemRelationManager()

    @timer
    def post(self, action):
        if action == 'add':
            return self.add_system()

        elif action == 'edit':
            return self.edit_system()

        elif action == 'delete':
            return self.delete_system()

        elif action == 'queryByCompanyId':
            return self.query_by_company_id()

        elif action == 'getGitBranchNamesBySystemId':
            return self.get_git_branch_names_by_system_id()

        else:
            return make_response({
                "code":
                "100",
                "desc":
                "url错误,不存在的接口动作<{action}>".format(action=action)
            })

    @master_check
    def add_system(self):
        try:
            company_id = self.data.pop('companyId')
            system_name = self.data.pop('systemName')
            git_ssh_url = self.data.pop('gitSshURL')
            simple_desc = self.data.pop('simpleDesc', None)
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        system_name = str(system_name).strip()
        git_ssh_url = str(git_ssh_url).strip()

        if not self.acim.get_company(id=company_id):
            return make_response({
                "code": "201",
                "desc": "公司id\"{}\"不存在".format(company_id)
            })

        if self.asim.get_system(system_name=system_name,
                                api_company_id=company_id):
            return make_response({
                "code":
                "201",
                "desc":
                "公司下存在相同工程名称\"{}\", 无法新增".format(system_name)
            })

        self.asim.insert_system(system_name=system_name,
                                simple_desc=simple_desc,
                                api_company_id=company_id,
                                git_url=git_ssh_url,
                                creator=self.username)
        return make_response({
            "code": "000",
            "desc": "工程\"{}\"增加成功".format(system_name)
        })

    @master_check
    def edit_system(self):
        try:
            system_id = self.data.pop('systemId')
            system_name = self.data.pop('systemName')
            git_ssh_url = self.data.pop('gitSshURL')
            simple_desc = self.data.pop('simpleDesc', None)
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        system_obj = self.asim.get_system(id=system_id)
        if not system_obj:
            return make_response({
                "code":
                "202",
                "desc":
                "工程id\"{}\"不存在, 请刷新后重试".format(system_id)
            })

        exist_system_obj = self.asim.get_system(
            system_name=system_name, api_company_id=system_obj.api_company_id)
        if exist_system_obj and exist_system_obj.id != int(system_id):
            return make_response({
                "code":
                "201",
                "desc":
                "公司下存在相同工程名称\"{}\", 无法修改".format(system_name)
            })

        self.asim.update_system(system_id,
                                system_name=system_name,
                                git_url=git_ssh_url,
                                simple_desc=simple_desc,
                                last_modifier=self.username)
        return make_response({
            "code": "000",
            "desc": "工程\"{}\"修改成功".format(system_name)
        })

    @master_check
    def delete_system(self):
        try:
            system_id = self.data.pop('systemId')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        if not self.asim.get_system(id=system_id):
            return make_response({
                "code":
                "202",
                "desc":
                "工程id\"{}\"不存在, 请刷新后重试".format(system_id)
            })

        intf_objs = self.aiim.get_intfs(api_system_id=system_id)
        if intf_objs:
            return make_response({
                "code":
                "300",
                "desc":
                "工程下已配置{}个接口,无法直接删除".format(len(intf_objs))
            })

        self.asim.delete_system(system_id)
        return make_response({"code": "000", "desc": "工程删除成功"})

    # @login_check
    def query_by_company_id(self):
        try:
            company_id = self.data.pop('companyId')
            project_id = self.data.pop('projectId', None)
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        all_system_objs = self.asim.get_systems(api_company_id=company_id)
        all_system_list = []
        for obj in all_system_objs:
            all_system_list.append({
                'systemId': obj.id,
                'label': obj.system_name,
                'gitUrl': obj.git_url
            })

        include_system_list = []
        system_list = []
        if project_id:
            relation_objs = self.apsrm.get_relations(api_project_id=project_id)
            for system_dic in all_system_list:
                is_include = False
                for relation_obj in relation_objs:
                    if relation_obj.api_system_id == system_dic['systemId']:
                        include_system_list.append({
                            'systemId':
                            system_dic['systemId'],
                            'label':
                            system_dic['label'],
                        })
                        is_include = True
                        break
                if not is_include:
                    system_list.append({
                        'systemId': system_dic['systemId'],
                        'label': system_dic['label'],
                    })
            return make_response({
                "code": "000",
                "systemList": system_list,
                "includeSystemList": include_system_list
            })
        else:
            return make_response({
                "code": "000",
                "allSystemList": all_system_list
            })

    def get_git_branch_names_by_system_id(self):
        try:
            git_url = self.data.pop('gitUrl')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        git_userspace_name = git_url.split(":")[1].strip('.git')

        try:
            git = GitlabAPI()
            git_branches_list = git.get_project_branches(git_userspace_name)
            return make_response({
                "code": "000",
                "data": {
                    "gitUrl": git_url,
                    "gitBranchList": git_branches_list
                }
            })
        except:
            return make_response({"code": "999", "desc": "系统内部错误"})
Exemple #2
0
class ApiCompany(Resource):
    def __init__(self):
        self.data = get_request_json()
        self.username = redis.get_username(request.headers.get('X-Token'))
        self.acim = ApiCompanyInfoManager()
        self.asim = ApiSystemInfoManager()
        self.all_sub_objs = None

    @timer
    def post(self, action):
        if action == 'add':
            return self.add_company()

        elif action == 'edit':
            return self.edit_company()

        elif action == 'delete':
            return self.delete_company()

        elif action == 'list':
            return self.company_list()

        elif action == 'subtree':
            return self.subtree()

        elif action == 'projectSubtree':
            return self.project_subtree()

        elif action == 'productLineSubtree':
            return self.product_line_subtree()

        elif action == 'intfCaseSubtree':
            return self.intf_case_subtree()

        elif action == 'getFilterConditions':
            return make_response({
                "code": "000",
                "conditions": subtree_filter_conditions
            })

        elif action == 'subtreeFilter':
            return self.api_subtree_filter()

        elif action == 'mainSubtreeFilter':
            return self.api_main_subtree_filter()

        else:
            return make_response({
                "code":
                "100",
                "desc":
                "url错误,不存在的接口动作<{action}>".format(action=action)
            })

    @master_check
    def add_company(self):
        try:
            company_name = self.data.pop('companyName')
            simple_desc = self.data.pop('simpleDesc', None)
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        company_name = str(company_name).strip()

        # 判断公司名是否已存在,存在无法添加
        if self.acim.get_company(company_name=company_name):
            return make_response({
                "code": "201",
                "desc": "公司名称\"{}\"已存在".format(company_name)
            })

        self.acim.insert_company(company_name=company_name,
                                 simple_desc=simple_desc,
                                 creator=self.username)
        return make_response({
            "code": "000",
            "desc": "公司\"{}\"增加成功".format(company_name)
        })

    @master_check
    def edit_company(self):
        try:
            company_id = self.data.pop('companyId')
            company_name = self.data.pop('companyName')
            simple_desc = self.data.pop('simpleDesc', None)
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        if not self.acim.get_company(id=company_id):
            return make_response({
                "code":
                "202",
                "desc":
                "公司id\"{}\"不存在, 请刷新后重试".format(company_id)
            })
        elif self.acim.get_company(company_name=company_name):
            return make_response({
                "code":
                "201",
                "desc":
                "公司名称\"{}\"已存在, 无法修改".format(company_name)
            })

        self.acim.update_company(company_id,
                                 company_name=company_name,
                                 simple_desc=simple_desc,
                                 last_modifier=self.username)
        return make_response({
            "code": "000",
            "desc": "公司\"{}\"修改成功".format(company_name)
        })

    @login_check
    def company_list(self):
        res_list = []
        objs = self.acim.get_companies()
        for obj in objs:
            res_list.append({
                'companyId': obj.id,
                'companyName': obj.company_name,
                'simpleDesc': obj.simple_desc,
                'creator': obj.creator,
                'last_modifier': obj.last_modifier
            })
        return make_response({"code": "000", "companyList": res_list})

    @master_check
    def delete_company(self):
        try:
            company_id = self.data.pop('companyId')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        if not self.acim.get_company(id=company_id):
            return make_response({
                "code":
                "202",
                "desc":
                "公司id\"{}\"不存在, 请刷新后重试".format(company_id)
            })

        system_objs = self.asim.get_systems(api_company_id=company_id)
        if system_objs:
            return make_response({
                "code":
                "300",
                "desc":
                "公司下已配置{}个工程,无法直接删除公司".format(len(system_objs))
            })

        self.acim.delete_company(company_id)
        return make_response({"code": "000", "desc": "公司删除成功"})

    @login_check
    def subtree(self):
        """根据公司id查询配置在该公司下的系统-接口"""
        try:
            company_id = self.data.pop('companyId')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        subtree = []
        index_id = 1

        result_list = self.acim.query_api_subtree(company_id)
        # logger.info('result_list:{}'.format(json.dumps(result_list)))

        system_id_exist_list = []
        intf_id_exist_list = []
        for row in result_list:
            if row[0] not in system_id_exist_list and row[2] is None:
                system_id_exist_list.append(row[0])
                subtree.append({
                    'id': index_id,
                    'label': row[1],
                    'systemId': row[0],
                    'gitSshURL': row[4],
                    'children': []
                })
                index_id += 1
            elif row[0] not in system_id_exist_list and row[
                    2] not in intf_id_exist_list:
                system_id_exist_list.append(row[0])
                intf_id_exist_list.append(row[2])
                subtree.append({
                    'id':
                    index_id,
                    'label':
                    row[1],
                    'systemId':
                    row[0],
                    'gitSshURL':
                    row[4],
                    'children': [{
                        'id': index_id + 1,
                        'label': row[3],
                        'intfId': row[2]
                    }]
                })
                index_id += 2
            elif row[0] in system_id_exist_list and row[
                    2] not in intf_id_exist_list:
                intf_id_exist_list.append(row[2])
                for system in subtree:
                    if row[0] == system['systemId']:
                        system['children'].append({
                            'id': index_id,
                            'label': row[3],
                            'intfId': row[2]
                        })
                        break
                index_id += 1

        return make_response({"code": "000", "data": subtree})

    @login_check
    def project_subtree(self):
        """根据公司id查询配置在该公司下的项目-系统-接口-用例"""
        try:
            company_id = self.data.pop('companyId')
            recent_days = int(self.data.pop('recentDays', 0))
        except (KeyError, ValueError):
            return make_response({"code": "100", "desc": "入参校验失败"})

        if recent_days:
            today_date = datetime.date(datetime.now())
            start_day = today_date + timedelta(days=-int(recent_days))
            result_list = self.acim.query_api_project_subtree(
                company_id, start_day=start_day)
        else:
            result_list = self.acim.query_api_project_subtree(company_id)

        # logger.info('result_list:{}'.format(json.dumps(result_list)))
        patch_result_list = self.acim.query_api_project_subtree_patch(
            company_id)
        subtree = result_list_to_subtree(result_list, patch_result_list)

        return make_response({"code": "000", "data": subtree})

    @login_check
    def product_line_subtree_old(self):
        """根据公司id查询配置在该公司下的产品线-全链路用例"""
        try:
            company_id = self.data.pop('companyId')
        except KeyError:
            return make_response({"code": "100", "desc": CODE_DESC_MAP["100"]})

        subtree = []
        index_id = 0

        result_list = self.acim.query_api_product_line_subtree(company_id)
        logger.debug(result_list)

        result_dic = db_result_to_map(result_list)
        logger.debug(result_dic)
        for p_k, p_dic in result_dic.items():
            p_name = p_dic.pop('name')
            index_id += 1
            p_tree = {
                'id': index_id,
                'label': p_name,
                'productLineId': p_k,
                'children': []
            }
            for t_k, t_dic in p_dic.items():
                t_name = t_dic.pop('name')
                index_id += 1
                t_tree = {
                    'id': index_id,
                    'label': t_name,
                    'testcaseId': t_k,
                    'children': []
                }
                p_tree['children'].append(t_tree)
            subtree.append(p_tree)

        return make_response({"code": "000", "data": subtree})

    @login_check
    def product_line_subtree(self):
        """根据公司id查询配置在该公司下的产品线-目录-目录-...-全链路用例"""
        try:
            company_id = self.data.pop('companyId')
            with_sub = self.data.pop('withSub', None)
            tag_id_list = self.data.pop('tagIdList', None)
            without_testcase = self.data.pop('withoutTestcase', None)
        except KeyError:
            return make_response({"code": "100", "desc": CODE_DESC_MAP["100"]})

        if not (isinstance(tag_id_list, list) and len(tag_id_list) in [1, 2]):
            tag_id_list = None

        self.all_sub_objs = ApiTestcaseSubManager.get_testcase_subs()

        subtree = []
        index = 0

        pl_objs = ApiProductLineManager.get_product_lines(
            api_company_id=company_id)
        for pl_obj in pl_objs:
            index += 1
            top_tree = {
                'id': index,
                'label': pl_obj.product_line_name,
                'productLineId': pl_obj.id,
                'children': []
            }
            index = get_under_node(pl_obj.id, top_tree['children'], index,
                                   with_sub, tag_id_list, without_testcase,
                                   self.all_sub_objs)
            subtree.append(top_tree)

        if tag_id_list:
            # 过滤去除没有用例的节点
            remove_no_case_node(subtree)

        return make_response({"code": "000", "data": subtree})

    @login_check
    def intf_case_subtree(self):
        """根据公司id查询配置在该公司下的工程-接口-用例"""
        try:
            company_id = self.data.pop('companyId')
        except KeyError:
            return make_response({"code": "100", "desc": CODE_DESC_MAP["100"]})

        subtree = []
        index_id = 0

        result_list = self.acim.query_api_intf_case_subtree(company_id)

        result_dic = db_result_to_map(result_list)
        for s_k, s_dic in result_dic.items():
            p_name = s_dic.pop('name')
            index_id += 1
            s_tree = {
                'id': index_id,
                'label': p_name,
                'value': s_k,
                'children': []
            }
            for i_k, i_dic in s_dic.items():
                t_name = i_dic.pop('name')
                index_id += 1
                i_tree = {'id': index_id, 'label': t_name, 'value': i_k}
                for t_k, t_dic in i_dic.items():
                    t_name = t_dic.pop('name')
                    index_id += 1
                    t_tree = {'id': index_id, 'label': t_name, 'value': t_k}
                    if 'children' not in i_tree:
                        i_tree['children'] = []
                    i_tree['children'].append(t_tree)
                s_tree['children'].append(i_tree)
            subtree.append(s_tree)

        return make_response({"code": "000", "data": subtree})

    @login_check
    def api_subtree_filter(self):
        try:
            company_id = self.data.pop('companyId')
            filter_ = self.data.pop('filter')
            keyword = self.data.pop('keyword').strip()
            if not keyword:
                raise ValueError
            if filter_ not in subtree_filter_conditions:
                raise ValueError
        except (KeyError, ValueError):
            return make_response({"code": "100", "desc": CODE_DESC_MAP["100"]})

        subtree = subtree_filter_by_keyword(company_id, filter_, keyword)

        return make_response({"code": "000", "data": subtree})

    @login_check
    def api_main_subtree_filter(self):
        try:
            company_id = self.data.pop('companyId')
            filter_ = self.data.pop('filter')
            keyword = self.data.pop('keyword').strip()
            if not keyword:
                raise ValueError
            if filter_ not in subtree_filter_conditions:
                raise ValueError
        except (KeyError, ValueError):
            return make_response({"code": "100", "desc": CODE_DESC_MAP["100"]})

        subtree = main_subtree_filter_by_keyword(company_id, filter_, keyword)

        return make_response({"code": "000", "data": subtree})
Exemple #3
0
class ApiIntf(Resource):
    def __init__(self):
        self.data = get_request_json()
        self.username = redis.get_username(request.headers.get('X-Token'))
        self.acim = ApiCompanyInfoManager()
        self.asim = ApiSystemInfoManager()
        self.aiim = ApiIntfInfoManager()
        self.atim = ApiTestcaseInfoManager()
        self.apirm = ApiProjectIntfRelationManager()
        self.aidrm = ApiIntfDefaultRequestManager()

    @timer
    def post(self, action):
        if action == 'add':
            return self.add_intf()

        elif action == 'edit':
            return self.edit_intf()

        elif action == 'delete':
            return self.delete_intf()

        elif action == 'detail':
            return self.intf_detail()

        elif action == 'queryBySystemId':
            return self.query_by_system_id()

        else:
            return make_response({"code": "100", "desc": "url错误,不存在的接口动作<{action}>".format(action=action)})

    @developer_check
    def add_intf(self):
        try:
            system_id = self.data.pop('systemId')
            intf_desc = self.data.pop('intfNameInChinese')
            intf_type = self.data.pop('type')
            intf_info = self.data.pop('info')
            request_dic = self.data.pop('request', {})
            request_detail_dic = self.data.pop('requestDetail', [])
            intf_relation = self.data.pop('intfRelation')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        intf_desc = str(intf_desc).strip()

        if not self.asim.get_system(id=system_id):
            return make_response({"code": "201", "desc": "工程id\"{}\"不存在".format(system_id)})

        if intf_type != 'MQ':
            if intf_type == 'HTTP':
                intf_info['apiUrl'] = intf_info['apiUrl'].strip()
                intf_name = intf_info['apiUrl']
            elif intf_type == 'DUBBO':
                intf_info['dubboService'] = intf_info['dubboService'].strip()
                intf_info['dubboMethod'] = intf_info['dubboMethod'].strip()
                intf_name = '{0}.{1}'.format(intf_info['dubboService'], intf_info['dubboMethod'])

            company_id = self.asim.get_system(id=system_id).api_company_id
            system_id_list = [row.id for row in self.asim.get_systems(api_company_id=company_id)]
            intf_name_list = [row.intf_name for row in self.aiim.get_intfs_in_system_id_list(system_id_list)]
            if intf_name in intf_name_list:
                company_name = self.acim.get_company(id=company_id).company_name
                return make_response({"code": "201", "desc": "\"{0}\"公司下存在相同接口\"{1}\", 请使用已存在的接口".format(
                    company_name, intf_name)})

        else:
            intf_info['topic'] = intf_info['topic'].strip()
            intf_info['tag'] = intf_info['tag'].strip()
            intf_name = '{0}.{1}'.format(intf_info['topic'], intf_info['tag'])
            obj = self.aiim.get_intf(intf_name=intf_name, api_system_id=system_id)
            if obj:
                return make_response({"code": "201", "desc": "工程下存在相同MQ接口\"{}\", 请使用已存在的MQ接口".format(intf_name)})

        # 增加依赖接口列表属性
        if intf_relation:
            intf_relation = [i[1] for i in intf_relation]
            self.aiim.insert_intf(intf_name=intf_name, intf_desc=intf_desc, api_system_id=system_id,
                                  intf_type=intf_type, intf_info=json_dumps(intf_info), creator=self.username,
                                  intf_relation=json_dumps(intf_relation))
        else:
            self.aiim.insert_intf(intf_name=intf_name, intf_desc=intf_desc, api_system_id=system_id,
                                  intf_type=intf_type, intf_info=json_dumps(intf_info), creator=self.username)
        intf_obj = self.aiim.get_intf(intf_name=intf_name, intf_desc=intf_desc, api_system_id=system_id,
                                      intf_type=intf_type, intf_info=json_dumps(intf_info), creator=self.username)
        self.aidrm.insert_request(api_intf_id=intf_obj.id, request=json_dumps(request_dic),
                                  request_detail=json_dumps(request_detail_dic))
        return make_response({"code": "000", "desc": "接口\"{}\"增加成功".format(intf_name)})

    @developer_with_limit_check
    def edit_intf(self):
        try:
            intf_id = self.data.pop('intfId')
            intf_desc = self.data.pop('intfNameInChinese')
            intf_type = self.data.pop('type')
            intf_info = self.data.pop('info')
            request_dic = self.data.pop('request', {})
            request_detail_dic = self.data.pop('requestDetail', [])
            intf_relation = self.data.pop('intfRelation')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        intf_obj = self.aiim.get_intf(id=intf_id)
        if not intf_obj:
            return make_response({"code": "202", "desc": "接口id\"{}\"不存在, 请刷新后重试".format(intf_id)})

        if intf_type != 'MQ':
            header_variables = []
            if intf_type == 'HTTP':
                intf_info['apiUrl'] = intf_info['apiUrl'].strip()
                intf_name = intf_info['apiUrl']
                header = intf_info['headers']
                variable_regexp = r"\$([\w_]+)"
                header_variables = re.findall(variable_regexp, header)
            elif intf_type == 'DUBBO':
                intf_info['dubboService'] = intf_info['dubboService'].strip()
                intf_info['dubboMethod'] = intf_info['dubboMethod'].strip()
                intf_name = '{0}.{1}'.format(intf_info['dubboService'], intf_info['dubboMethod'])

            company_id = self.asim.get_system(id=intf_obj.api_system_id).api_company_id
            system_id_list = [row.id for row in self.asim.get_systems(api_company_id=company_id)]
            for obj in self.aiim.get_intfs_in_system_id_list(system_id_list):
                if obj.intf_name == intf_name and int(obj.id) != int(intf_id):
                    company_name = self.acim.get_company(id=company_id).company_name
                    return make_response({"code": "201", "desc": "\"{0}\"公司下存在相同接口\"{1}\", 无法将当前接口修改为\"{1}\"".format(
                        company_name, intf_name)})

        else:
            intf_info['topic'] = intf_info['topic'].strip()
            intf_info['tag'] = intf_info['tag'].strip()
            intf_name = '{0}.{1}'.format(intf_info['topic'], intf_info['tag'])

            obj = self.aiim.get_intf(intf_name=intf_name, api_system_id=intf_obj.api_system_id)
            if obj and obj.id != intf_id:
                return make_response({"code": "201", "desc": "工程下存在相同MQ接口\"{}\", 请使用已存在的MQ接口".format(intf_name)})

        if intf_relation:
            intf_relation = [i[1] for i in intf_relation]
            self.aiim.update_intf(intf_id, intf_name=intf_name, intf_desc=intf_desc, intf_type=intf_type,
                                  intf_info=json_dumps(intf_info), last_modifier=self.username,
                                  intf_relation=json_dumps(intf_relation))
        else:
            self.aiim.update_intf(intf_id, intf_name=intf_name, intf_desc=intf_desc, intf_type=intf_type,
                                  intf_info=json_dumps(intf_info), last_modifier=self.username)
        self.aidrm.update_request_by_intf_id(intf_id, request=json_dumps(request_dic),
                                             request_detail=json_dumps(request_detail_dic))

        # 保存接口headers中的公共变量到接口下的所有用例
        if intf_type == 'HTTP' and header_variables:
            to_add_pv_id_list = []
            pv_objs = ApiPublicVariableInfoManager.get_variables(api_company_id=company_id)
            for pv_obj in pv_objs:
                for header_variable in header_variables:
                    if header_variable == pv_obj.variable_name:
                        to_add_pv_id_list.append(pv_obj.id)
                        break
            # 如果存在需添加的公共变量id
            if to_add_pv_id_list:
                tc_objs = ApiTestcaseInfoManager.get_testcases(api_intf_id=intf_id)
                for tc_obj in tc_objs:
                    try:
                        pv_id_list = json_loads(tc_obj.include)[0]['public_variables']
                    except (json.decoder.JSONDecodeError, IndexError, KeyError):
                        pv_id_list = []
                    merge_pv_id_list = pv_id_list + to_add_pv_id_list
                    merge_pv_id_list = list(set(merge_pv_id_list))
                    if set(merge_pv_id_list) != set(pv_id_list):
                        include = json_dumps([{"public_variables": merge_pv_id_list}])
                        ApiTestcaseInfoManager.update_testcase(id_=tc_obj.id, include=include)
                ts_objs = ApiTestcaseSubManager.get_testcase_subs(api_intf_id=intf_id)
                for ts_obj in ts_objs:
                    try:
                        pv_id_list = json_loads(ts_obj.include)[0]['public_variables']
                    except (json.decoder.JSONDecodeError, IndexError, KeyError):
                        pv_id_list = []
                    merge_pv_id_list = pv_id_list + to_add_pv_id_list
                    merge_pv_id_list = list(set(merge_pv_id_list))
                    if set(merge_pv_id_list) != set(pv_id_list):
                        include = json_dumps([{"public_variables": merge_pv_id_list}])
                        ApiTestcaseSubManager.update_testcase_sub(id_=ts_obj.id, include=include)

        return make_response({"code": "000", "desc": "接口\"{}\"修改成功".format(intf_name)})

    @developer_with_limit_check
    def delete_intf(self):
        try:
            intf_id = self.data.pop('intfId')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        if not self.aiim.get_intf(id=intf_id):
            return make_response({"code": "202", "desc": "接口id\"{}\"不存在, 请刷新后重试".format(intf_id)})

        testcase_objs = self.atim.get_testcases(api_intf_id=intf_id)
        if testcase_objs:
            return make_response({"code": "300", "desc": "接口下已编写{}个用例,无法直接删除".format(len(testcase_objs))})

        self.aiim.delete_intf(intf_id)
        self.aidrm.delete_request_by_intf_id(intf_id)
        relation_objs = self.apirm.get_relations(api_intf_id=intf_id)
        for relation_obj in relation_objs:
            self.apirm.delete_relation(relation_obj.id)
        return make_response({"code": "000", "desc": "接口删除成功"})

    @login_check
    def intf_detail(self):
        try:
            intf_id = self.data.pop('intfId')
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        intf_obj = self.aiim.get_intf(id=intf_id)
        if not intf_obj:
            return make_response({"code": "202", "desc": "接口id\"{}\"不存在, 请刷新后重试".format(intf_id)})

        try:
            info = json.loads(intf_obj.intf_info)
        except json.decoder.JSONDecodeError:
            return make_response({"code": "301", "desc": "intf_info字段解析异常"})

        intf_r_obj = self.aidrm.get_request(api_intf_id=intf_id)
        if intf_r_obj:
            request_dic = json.loads(intf_r_obj.request)
            request_detail_dic = json.loads(intf_r_obj.request_detail)
        else:
            request_dic = {}
            request_detail_dic = []

        # 处理依赖列表数据
        if intf_obj.intf_relation:
            relation_intf_objs = self.aiim.get_intfs_in_id_list(json.loads(intf_obj.intf_relation))
            intf_relation = [[i.api_system_id, i.id] for i in relation_intf_objs]
        else:
            intf_relation = []
        data = {
            'intfChineseName': intf_obj.intf_desc,
            'type': intf_obj.intf_type,
            'systemId': intf_obj.api_system_id,
            'info': info,
            'request': request_dic,
            'requestDetail': request_detail_dic,
            'intfRelation': intf_relation
        }
        return make_response({"code": "000", "data": data})

    @login_check
    def query_by_system_id(self):
        try:
            system_id = self.data.pop('systemId')
            project_id = self.data.pop('projectId', None)
        except KeyError:
            return make_response({"code": "100", "desc": "入参校验失败"})

        all_intf_objs = self.aiim.get_intfs(api_system_id=system_id)
        # print(len(all_intf_objs))
        all_intf_list = []
        for obj in all_intf_objs:
            all_intf_list.append(
                {
                    'intfId': obj.id,
                    'label': obj.intf_name,
                }
            )

        include_intf_list = []
        intf_list = []
        if project_id:
            relation_objs = self.apirm.get_relations(api_project_id=project_id)
            for intf_dic in all_intf_list:
                is_include = False
                for relation_obj in relation_objs:
                    if relation_obj.api_intf_id == intf_dic['intfId']:
                        include_intf_list.append(
                            {
                                'intfId': intf_dic['intfId'],
                                'label': intf_dic['label'],
                            }
                        )
                        is_include = True
                        break
                if not is_include:
                    intf_list.append(
                        {
                            'intfId': intf_dic['intfId'],
                            'label': intf_dic['label'],
                        }
                    )
            return make_response({"code": "000", "intfList": intf_list, "includeIntfList": include_intf_list})
        else:
            return make_response({"code": "000", "intfList": all_intf_list, "includeIntfList": include_intf_list})