# encoding: UTF-8
from flask import Blueprint, request

from ...common.apiResponse import ApiResponse

from .controller.createDataInfoController import CreateDataInfoController
from .controller.createDataDetailController import CreateDataDetailController
from .controller.createDataDictController import CreateDataDictController
from .controller.createDataResultController import CreateDataResultController
from .controller.commonController import CommonController
from .controller.scrapyController import ScrapyController
from .controller.userController import UserController

api = Blueprint('api', __name__)


@api.route('/add/info/data', methods=['POST'])
def add_info_data():
    """新增造数请求参数数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_info_id |  true  | query |  int  | 基本信息的id |
        | create_data_detail_id |  true  | query |  int  | 详细信息的id |
        | className |  true  | query |  string  | 类名 |
        | methodName |  true  | query |  string  | 方法名 |
        | request_parameter |  true  | query |  string  | 请求参数 |

        ### 请求
        ```json
        {"create_data_info_id":"","create_data_detail_id":102222,"className":"","methodName":"","request_parameter":{}}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":true,"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    access_token = request.headers['accesstoken']
    controller = CreateDataInfoController(req_json, access_token)
    ret = controller.create_info_data()
    if ret:
        return ApiResponse.build_failure(5000, data=ret)
    return ApiResponse.build_success(20000, data=True)


@api.route('/edit/info/data', methods=['POST'])
def edit_info_data():
    """编辑造数请求参数数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_info_id |  true  | query |  int  | 基本信息的id |
        | create_data_detail_id |  true  | query |  int  | 详细信息的id |
        | className |  true  | query |  string  | 类名 |
        | methodName |  true  | query |  string  | 方法名 |
        | request_parameter |  true  | query |  string  | 请求参数 |

        ### 请求
        ```json
        {"create_data_info_id":"","create_data_detail_id":102222,"className":"","methodName":"","request_parameter":{}}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x ", "data":true,"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    access_token = request.headers['accesstoken']
    controller = CreateDataInfoController(req_json, access_token)
    ret = controller.update_info_data()
    if ret:
        return ApiResponse.build_failure(5000, data=ret)
    return ApiResponse.build_success(20000, data=True)


@api.route('/delete/detail/data', methods=['POST'])
def delete_detail_data():
    """删除造数详情数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_detail_id |  true  | query |  int  | 详细信息的id |

        ### 请求
        ```json
        {"create_data_detail_id":102222}
        ```
        ### 响应
        ```json
        {"code": x, "message": "删除成功", "data":true,"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    access_token = request.headers['accesstoken']
    controller = CreateDataDetailController(req_json, access_token)
    ret = controller.delete_detail_data()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/create/data/detail/list', methods=['POST'])
def get_keywors_detail_list():
    """查询造数关键字基本信息列表页数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | pageNo |  true  | query |  int  | 基本信息的id |
        | pageSize |  true  | query |  int  | 详细信息的id |
        | detail_name |  true  | query |  string  | 基本描述信息 |
        | team_name |  true  | query |  string  | 业务线 |
        | file_name |  true  | query |  string  | 业务对应业务线名称 |
        | module_name |  true  | query |  string  | 模块名称 |

        ### 请求
        ```json
        {"pageNo":1,"pageSize":10,"dict_value":"","team_name":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":list[{},{}],"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDetailController(req_json, accesstoken)
    ret = controller.get_detail_query_advance()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/create/data/detail/advance', methods=['POST'])
def get_keywors_detail_advance():
    """查询造数关键字基本信息详情页数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_detail_id |  true  | query |  int  | 基本信息的id |

        ### 请求
        ```json
        {"create_data_detail_id":123}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":{},"success":True}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDetailController(req_json, accesstoken)
    ret = controller.get_detail_advance()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/keyword/run', methods=['POST'])
def run_keywords():
    """执行造数
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_info_id |  true  | query |  string  | 基本信息的id |
        | create_data_detail_id |  true  | query |  string  | 详细信息的id |
        | dict_key |  true  | query |  string  | 组名 |
        | get_tag |  true  | query |  string  | 标签，备注说明 |
        | request_parameter |  true  | query |  string  | 请求参数 |

        ### 请求
        ```json
        {"create_data_info_id":"12","create_data_detail_id":"13","dict_key":"StudentEvent","get_tag":"get_student_classroom","request_parameter":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "success":}
        ```
        @@@
    """
    req_json = request.get_json()
    access_token = request.headers['accesstoken']
    controller = CreateDataDetailController(req_json, access_token)
    data = controller.run_keyword_data()
    if data.get("message") == "造数成功":
        return ApiResponse.build_success(20000, message="造数成功", data=data.get("data"))
    return ApiResponse.build_failure(40012, msg=data.get("message"))


@api.route('/create/data/result/list', methods=['POST'])
def get_keywors_result_list():
    """查询造数关键字造数结果列表页数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | pageNo |  true  | query |  int  | 页码 |
        | pageSize |  true  | query |  int  | 页码规格 |
        | detail_name |  true  | query |  string  | 详细描述信息 |
        | team_name |  true  | query |  string  | 业务线 |
        | get_tag |  true  | query |  string  | 标签，备注 |

        ### 请求
        ```json
        {"pageNo":1,"pageSize":10,"detail_name":"","team_name":"","get_tag":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":list[{},{}],"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataResultController(req_json, accesstoken)
    ret = controller.get_result_query_list()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/create/data/result/advance', methods=['POST'])
def get_keywors_result_advance():
    """查询造数结果的详情信息
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_result_id |  true  | query |  int  | 基本信息的id |

        ### 请求
        ```json
        {"create_data_result_id":123}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":{},"success":True}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataResultController(req_json, accesstoken)
    ret = controller.get_result_advance()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/add/dict/data', methods=['POST'])
def edit_dict_data():
    """编辑造数关键字进行的映射
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_dict_data_id |  true  | query |  int  | 基本信息的id |
        | data_type |  true  | query |  int  | 详细信息的id |
        | dict_key |  true  | query |  string  | 映射的key |
        | dict_value |  true  | query |  string  | 字段映射对应的名称 |
        | team_name |  true  | query |  string  | 组名 |
        | created_time |  true  | query |  string  | 创建时间 |
        | modified_time |  true  | query |  string  | 修改时间 |

        ### 请求
        ```json
        {"create_dict_data_id":"e757b5cd-cec5-11ed-ae4a-c8b29bebcb51","team_name":"用户","data_type":3,"dict_key":"common","dict_value":"公共","created_time":"202303301440","modified_time":"202304111341"}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":list[{},{}],"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDictController(req_json, accesstoken)
    ret = controller.update_dict_data()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/dict/data/page', methods=['POST'])
def get_dict_data_list():
    """查询造数关键字配置列表
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | pageNo |  true  | query |  int  | 基本信息的id |
        | pageSize |  true  | query |  int  | 详细信息的id |
        | dict_value |  true  | query |  string  | 配置值 |
        | team_name |  true  | query |  string  | 业务线 |

        ### 请求
        ```json
        {"pageNo":1,"pageSize":10,"detail_name":"","team_name":"","file_name":"","module_name":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":list[{},{}],"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDictController(req_json, accesstoken)
    ret = controller.get_dict_list_data()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/get/pull/git', methods=['POST'])
def get_new_git_info():
    """获取git上的最新代码到本地
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | fileName |  true  | query |  string  | 传入需要抓取的文件名称 |
        | team |  true  | query |  string  | 业务线名称 |
        | username |  true  | query |  string  | git地址用户名 |
        | password |  true  | query |  string  | git地址密码 |

        ### 请求
        ```json
        {"team":"USER","fileName":"","username":"","password":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "success":}
        ```
        @@@
    """
    req_json = request.get_json()
    # accesstoken = request.headers['accesstoken']
    accesstoken = "22233"
    controller = ScrapyController(req_json, accesstoken)
    ret = controller.get_git_pull()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/get/eureka', methods=['POST'])
def get_eureka_ip():
    """更新eureka上的ip地址
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | team |  true  | query |  string  | 业务线名称 |

        ### 请求
        ```json
        {"team":"USER"}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = ScrapyController(req_json, accesstoken)
    ret = controller.get_eureka_ip()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/create/data/detail/scrapy', methods=['POST'])
def scrapy_entery_db():
    """查找出全部的可用方法与描述进行录库
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | fileName |  true  | query |  string  | 传入需要抓取的文件名称 |
        | team |  true  | query |  string  | 业务线名称 |
        | username |  true  | query |  string  | git地址用户名 |
        | password |  true  | query |  string  | git地址密码 |

        ### 请求
        ```json
        {"team":"USER","fileName":"","username":"","password":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "success":}
        ```
        @@@
    """
    req_json = request.get_json()
    # accesstoken = request.headers['accesstoken']
    accesstoken = "123"
    controller = ScrapyController(req_json, accesstoken)
    ret = controller.scrapy_enter_db()
    if ret != "抓取成功":
        return ApiResponse.build_failure(40012, msg=ret)
    return ApiResponse.build_success(20000, message=ret)


@api.route('/get/team/name', methods=['POST'])
def get_team_name():
    """获取各个业务线的数据字典,用于查询
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | data_type |  true  | query |  int  | 类型 |
        | team_name |  true  | query |  string  | 组名 |

        ### 请求
        ```json
        {"data_type":2,"team_name":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":"","success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDictController(req_json, accesstoken)
    ret = controller.get_team_name()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/create/data/detail/delete', methods=['POST'])
def delete_data_detail():
    """删除造数关键字基本信息
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_detail_id |  true  | query |  int  | 基本信息的id |

        ### 请求
        ```json
        {"create_data_detail_id":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":true,"success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDetailController(req_json, accesstoken)
    ret = controller.delete_detail_data()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/create/data/info/add', methods=['POST'])
def add_keywors_data():
    """增加造数关键字的请求参数
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_info_id |  true  | query |  string  | 基本信息的id |
        | create_data_detail_id |  true  | query |  string  | 详细信息的id |
        | className |  true  | query |  string  | 类名 |
        | methodName |  true  | query |  string  | 方法名 |
        | request_parameter |  true  | query |  string  | 请求参数 |

        ### 请求
        ```json
        {"create_data_info_id":"12","create_data_detail_id":"13","className":"StudentEvent","methodName":"get_student_classroom","request_parameter":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataInfoController(req_json, accesstoken)
    err_msg = controller.create_info_data()
    if err_msg:
        return ApiResponse.build_failure(40012, msg=err_msg)
    return ApiResponse.build_success(20000)


@api.route('/create/data/info/list', methods=['POST'])
def get_keywors_data_list():
    """查询造数关键字请求列表页数据
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_info_id |  true  | query |  string  | 基本信息的id |
        | create_data_detail_id |  true  | query |  string  | 详细信息的id |
        | className |  true  | query |  string  | 类名 |
        | methodName |  true  | query |  string  | 方法名 |
        | request_parameter |  true  | query |  string  | 请求参数 |

        ### 请求
        ```json
        {"create_data_info_id":"12","create_data_detail_id":"13","className":"StudentEvent","methodName":"get_student_classroom","request_parameter":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataInfoController(req_json, accesstoken)
    ret = controller.create_info_data()
    return ApiResponse.build_success(20000, data=ret)


@api.route('/Login', methods=['POST'])
def get_user_login():
    """判断登录，根据登录信息获取邮箱
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | username |  true  | query |  string  | 用户名 |
        | password |  true  | query |  string  | 密码 |

        ### 请求
        ```json
        {"username":2,"password":""}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":"","success":}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = UserController(req_json, accesstoken)
    ret = controller.get_user_email()
    if ret.get("code") != 20000:
        return ApiResponse.build_failure(40012, msg=ret.get("message"))
    return ApiResponse.build_success(20000, data=ret.get("data"))


@api.route('/create/data/detail/approve', methods=['POST'])
def approve_detail_info():
    """对关键字进行审核
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        | create_data_detail_id |  true  | query |  int  | 基本信息的id |

        ### 请求
        ```json
        {"create_data_detail_id":123}
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":{},"success":True}
        ```
        @@@
    """
    req_json = request.get_json()
    accesstoken = request.headers['accesstoken']
    controller = CreateDataDetailController(req_json, accesstoken)
    ret = controller.approve_detail_status()
    if ret.get("code") != 20000:
        return ApiResponse.build_failure(40012, msg=ret.get("message"))
    return ApiResponse.build_success(20000, data=ret)


@api.route('/update/month/use/number', methods=['GET'])
def update_month_use_number():
    """每月使用次数更新，定时任务
        @@@
        ### 参数
        |  参数名 | 是否必传 | 请求类型 |参数类型 |  参数描述 |
        |-------|---------|--------|--------|----------|
        ### 请求
        ```json
        ```
        ### 响应
        ```json
        {"code": x, "message": "x", "data":{},"success":True}
        ```
        @@@
    """
    controller = CommonController()
    ret = controller.daily_task_update_month_number()
    if ret.get("code") != 20000:
        return ApiResponse.build_failure(40012, msg=ret.get("message"))
    return ApiResponse.build_success(20000, data=ret)
