# encoding: UTF-8
from ....common.sqlSession import SqlSession

from ..service.createDataNumberService import CreateNumberService
from ..service.createDataDetailService import CreateDetailService
from ..service.createDataInfoService import CreateInfoService
from ..controller import commonController


class CreateDataNumberController(object):
    def __init__(self, req_json, token):
        self.session = SqlSession()
        self.create_data_detail_id = req_json.get('create_data_detail_id')
        self.method_name = req_json.get('method_name')
        self.token = token
        self.del_flag = 0
        self.req_json = req_json

    def get_number_data(self, create_data_detail_id):
        """
        根据id查询对应的次数信息
        :return:
        """
        create_number_service = CreateNumberService()
        get_query_info = create_number_service.get_service_number_by_id(session=self.session,
                                                                        create_data_detail_id=create_data_detail_id)

    def create_result_data(self, add_data):
        """
        对造数结果数据进行统计
        :return:
        """
        create_number_service = CreateNumberService()
        create_data_info_id = create_number_service.create_service_number_data(session=self.session,
                                                                               create_info=add_data)
        return create_data_info_id

    def update_number_data(self, is_right):
        """
        更新造数使用次数表
        """
        create_number_service = CreateNumberService()
        try:
            get_db_data_number, success = create_number_service.get_service_number_by_id(session=self.session,
                                                                                         create_data_detail_id=self.create_data_detail_id)
            if success:
                if get_db_data_number:
                    total_number = get_db_data_number.total_number
                    error_number = get_db_data_number.error_number
                    right_number = get_db_data_number.right_number
                    month_number = get_db_data_number.month_number

                    update_total_number = total_number + 1
                    update_data = {"total_number": update_total_number}
                    if is_right:
                        update_right_number = right_number + 1
                        update_month_number = month_number + 1
                        update_data["right_number"] = update_right_number
                        update_data["month_number"] = update_month_number
                    else:
                        update_error_number = error_number + 1
                        update_data["error_number"] = update_error_number

                    create_number_service = CreateNumberService()
                    update_data_info_id = create_number_service.update_service_number_data(
                        create_data_detail_id=self.create_data_detail_id, session=self.session,
                        update_info=update_data)
                    return "更新数据成功"
                else:
                    get_uuid = commonController.get_uuid()
                    get_time = commonController.get_time()
                    if is_right:
                        right_number = 1
                        error_number = 0
                        month_number = 1
                    else:
                        right_number = 0
                        error_number = 1
                        month_number = 0
                    add_data = {
                        'id': get_uuid,
                        'create_data_detail_id': self.create_data_detail_id,
                        'method_name': self.method_name,
                        'total_number': 1,
                        'error_number': error_number,
                        'right_number': right_number,
                        'month_number': month_number,
                        'del_flag': self.del_flag,
                        'created_time': get_time,
                        'modified_time': get_time,
                    }
                    self.create_result_data(add_data=add_data)
            else:
                return "更新数据失败"
        except:
            return "更新数据失败"

    def do_history_data(self):
        """
        处理历史数据
        :return:
        """
        create_detail_service = CreateDetailService()
        create_number_service = CreateNumberService()
        create_data_number_id = commonController.get_uuid()
        created_time = commonController.get_time()
        modified_time = commonController.get_time()
        get_data_detail = create_detail_service.get_all_detail_list_no_filters_page(session=self.session)
        list_update_data = []
        for detail_info in get_data_detail:
            get_create_number = create_number_service.get_service_number_by_id(session=self.session,
                                                                               create_data_detail_id=detail_info.create_data_detail_id)
            if get_create_number:
                list_update_data.append(detail_info.create_data_detail_id)
                create_number_service.update_service_number_data(session=self.session,
                                                                 update_info={"method_name": detail_info.method_name},
                                                                 create_data_detail_id=detail_info.create_data_detail_id)
            else:
                add_data = {
                    "id": create_data_number_id,
                    "create_data_detail_id": detail_info.create_data_detail_id,
                    "method_name": detail_info.method_name,
                    "total_number": 0,
                    "error_number": 0,
                    "right_number": 0,
                    "del_flag": 0,
                    "created_time": created_time,
                    "modified_time": modified_time,
                }
                create_number_service.create_service_number_data(session=self.session, create_info=add_data)

                list_update_data.append(detail_info.create_data_detail_id)

        return list_update_data
