def create_ext_table_info(): data = request.json data["sync_column"] = ",".join(data["sync_column"]) data["order_column"] = ",".join(data["order_column"]) service.create_ext_table_info(data) return jsonify_with_data(APIError.OK, data={})
def get_ext_table_infos(): total, ext_table_infos = service.get_ext_table_infos(request.args) return jsonify_with_data(APIError.OK, data={ "total": total, "items": ext_table_infos })
def create_ext_datasource_con(): data = request.json try: service.create_ext_datasource_con(data) except ExtDatasourceConExists as e: return jsonify_with_error(APIError.CONFLICT, str(e)) return jsonify_with_data(APIError.OK, data={})
def login(): data = request.json try: token = service.login(data["username"], data["password"]) return jsonify_with_data(APIError.OK, data={"token": token}) except LoginFailed as e: return jsonify_with_error(APIError.UNAUTHORIZED)
def get_ext_datasource_con(): try: ext_datasource_con = service.get_ext_datasource_con( request.args["source_id"]) except ExtDatasourceConNotExist as e: return jsonify_with_error(APIError.NOTFOUND, str(e)) return jsonify_with_data(APIError.OK, data=ext_datasource_con)
def copy_ext_table_info(): data = request.json try: service.copy_ext_table_info(data) except (ExtDatasourceNotExist, ErpNotMatch, TableNotExist) as e: return jsonify_with_error(APIError.NOTFOUND, str(e)) return jsonify_with_data(APIError.OK, data={})
def create_sql(): data = request.json sql = data.get('sql') source_id = data.get('source_id') if not all([source_id, sql]): return jsonify_with_error(APIError.VALIDATE_ERROR, '参数不全') data_source = ext_check_table.get_datasource_by_source_id( source_id.upper()) if data_source is None: return jsonify_with_error(APIError.NOTFOUND, "source_id 有误, 请检查source_id是否正确") db_name = data_source.get('db_name') database = db_name.get('database') if not all([db_name, database]): return jsonify_with_error(APIError.BAD_REQUEST, "数据库信息不全,请检查") data_source['database'] = database error = ext_check_table.connect_test(**data_source) if error: return jsonify_with_error(APIError.BAD_REQUEST, "对方数据库连接失败!") result = ext_check_table.execute_sql(sql) if result == "error": return jsonify_with_error(APIError.VALIDATE_ERROR, 'sql错误!') for i in result: table_head = [k for k in i.keys()] return jsonify_with_data(APIError.OK, data={ 'table_head': table_head, 'result': result })
def batch_modify_ext_table_info(): data = request.json total, success = service.batch_ext_table_info(data) if total != success: message = "total:%s, success:%s" % (total, success) return jsonify_with_error(APIError.PARTIALLY_SUCCESS, message) return jsonify_with_data(APIError.OK, data={})
def get_ext_clean_info_target_tables(source_id): """ 获取某个source_id下所有生效的的目标表的表名称 :param source_id: :return: """ data = services.get_ext_clean_info_template_target_tables(source_id) return jsonify_with_data(APIError.OK, data={"tables": data})
def get_ext_clean_info_target_table(source_id): """ 获得还未添加的目标表,用于新增单个目标表的下拉选项 :param source_id: :return: """ tables = services.get_ext_clean_info_target_table(source_id) return jsonify_with_data(APIError.OK, data={"tables": tables})
def modify_ext_datasource_con(id): data = request.json try: service.modify_ext_datasource_con(id, data) except ExtDatasourceConNotExist as e: return jsonify_with_error(APIError.NOTFOUND, str(e)) except ExtDatasourceConExists as e: return jsonify_with_error(APIError.CONFLICT, str(e)) return jsonify_with_data(APIError.OK, data={})
def get_ext_check(source_id, target_table): date = request.args.get("date") if date is None: date = arrow.now().shift(days=-1).format('YYYY-MM-DD') source_id = source_id.upper() target_table = target_table.lower() # 查看本地数据库是否有数 my_data = ExtCheckNum.query.filter_by(source_id=source_id, date=date, target_table=target_table).first() if my_data and my_data.num > 500: return jsonify_with_data(APIError.OK, data={'num': my_data.num}) if (source_id in ['59YYYYYYYYYYYYY', '70YYYYYYYYYYYYY', '73YYYYYYYYYYYYY' ]) and target_table == 'goodsflow': num = ext_check_table.ext_serial(source_id, date) ext_check_table.create_check_num(source_id, target_table, num, date) return jsonify_with_data(APIError.OK, data={'num': num}) # 测试数据库是否能够正常连接,无法连接就返回错误信息 data_source = ext_check_table.get_datasource_by_source_id(source_id) if data_source is None: return jsonify_with_error(APIError.NOTFOUND, "source_id 有误, 请检查source_id是否正确") db_name = data_source.get('db_name') database = db_name.get('database') if not all([db_name, database]): return jsonify_with_error(APIError.BAD_REQUEST, "数据库信息不全,请检查") data_source['database'] = database error = ext_check_table.connect_test(**data_source) if error: return jsonify_with_error(APIError.BAD_REQUEST, "对方数据库连接失败!") num = ext_check_table.get_target_num(source_id, target_table, date) if not num: return jsonify_with_error(APIError.NOTFOUND, "sql not found") num = list(num[0].values())[0] ext_check_table.create_check_num(source_id, target_table, num, date) return jsonify_with_data(APIError.OK, data={'num': num})
def ext_save_sql(): data = request.json sql = data.get('sql') target_table = data.get('target_table') source_id = data.get('source_id') if not all([sql, source_id]): return jsonify_with_error(APIError.VALIDATE_ERROR, '参数有误') ext_check_table.create_test_query(source_id, sql, target_table) return jsonify_with_data(APIError.OK, data={'result': '{}保存成功'.format(source_id)})
def get_ext_clean_info_table(source_id): """ 获取该source_id对应下的配置为抓取的表,用于选择原始表的下拉选项 :param source_id: :return: """ try: tables = services.get_ext_clean_info_table(source_id) except ExtTableInfoNotFound as e: return jsonify_with_error(APIError.BAD_REQUEST, str(e)) return jsonify_with_data(APIError.OK, data={"tables": tables})
def modify_ext_table_info(id): data = request.json if data.get("sync_column") is not None: data["sync_column"] = ",".join(data["sync_column"]) if data.get("order_column") is not None: data["order_column"] = ",".join(data["order_column"]) try: service.modify_ext_table_info(id, data) except ExtTableInfoNotExist as e: return jsonify_with_error(APIError.NOTFOUND, str(e)) return jsonify_with_data(APIError.OK, data={})
def copy_ext_clean_info(): """ 将某个source_id的所有目标表配置copy复制到另一个source_id所有目标表,用于同个erp类型 :return: """ data = request.get_json() try: services.copy_ext_clean_info(data) except (ExtDatasourceNotExist, TableNotExist) as e: return jsonify_with_error(APIError.NOTFOUND, str(e)) return jsonify_with_data(APIError.OK, data={})
def delete_ext_clean_info(id): """ 逻辑删除单个目标表, 该目标表原先的配置不清除 :param id: :return: """ try: services.delete_ext_clean_info(id) except ExtCleanInfoNotFound as e: return jsonify_with_error(APIError.BAD_REQUEST, str(e)) return jsonify_with_data(APIError.OK, data={})
def create_ext_clean_info(): """ 新建单个目标表 :return: """ data = request.get_json() try: services.create_ext_clean_info(data) except ExtDatasourceNotExist as e: return jsonify_with_error(APIError.BAD_REQUEST, str(e)) return jsonify_with_data(APIError.OK, data={})
def get_ext_clean_info_target(): """ 获取source_id下的某个目标表的信息 :return: """ source_id = request.args.get("source_id") target = request.args.get("target") try: data = services.get_ext_clean_info_target(source_id, target) except (ExtCleanInfoParameterError, ExtCleanInfoNotFound) as e: return jsonify_with_error(APIError.VALIDATE_ERROR, str(e)) return jsonify_with_data(APIError.OK, data={"target": data})
def copy_ext_clean_info_table(): """ 将某个source_id的单个目标表配置copy复制到另一个source_id单个目标表,用于同个erp类型 :return: """ data = request.get_json() try: services.copy_ext_clean_info_table(data) except (ExtDatasourceNotExist, TableNotExist, ExtCleanInfoParameterError) as e: return jsonify_with_error(APIError.VALIDATE_ERROR, str(e)) return jsonify_with_data(APIError.OK, data={})
def modifly_ext_clean_info(id): """ 修改单个目标表 :param id: :return: """ data = request.get_json() try: services.modifly_ext_clean_info(id, data) except (ExtCleanInfoNotFound, ExtCleanInfoTableNotFound, ExtCleanInfoColumnNotFound) as e: return jsonify_with_error(APIError.BAD_REQUEST, str(e)) return jsonify_with_data(APIError.OK, data={})
def get_ext_clean_infos(): """ 获得source_id下所有的配置的目标表 :return: """ source_id = request.args.get("source_id") page = int(request.args.get("page", 1)) per_page = int(request.args.get("per_page", PER_PAGE)) try: total, items = services.get_ext_clean_infos(source_id, page, per_page) except (ExtCleanInfoParameterError, ExtDatasourceNotExist) as e: return jsonify_with_error(APIError.BAD_REQUEST, str(e)) return jsonify_with_data(APIError.OK, data={ "total": total, "items": items })
def get_ext_table_info(id): try: ext_table_info = service.get_ext_table_info(id) except ExtTableInfoNotExist as e: return jsonify_with_error(APIError.NOTFOUND, str(e)) return jsonify_with_data(APIError.OK, data=ext_table_info)