# coding=utf-8
# create by oldman at 17/1/11
from functools import wraps

from api.models import ServiceRegister
from rpc.tool.log_tool import info_logger


def lock_serviceregister(fn):
    """
    美购申请在被处理的时候锁定该美购状态
    :param fn:
    :return:
    """
    @wraps(fn)
    def wrapper(ctx, serviceregister_id, is_pass, is_operator_pass, reason):
        serviceregister_s = ServiceRegister.objects.get(pk=serviceregister_id)
        try:
            if serviceregister_s.is_lock:
                raise ValueError(u'美购状态错误')
            else:
                serviceregister_s.is_lock = True
                serviceregister_s.save()
            fn(ctx, serviceregister_id, is_pass, is_operator_pass, reason)
            serviceregister_e = ServiceRegister.objects.get(pk=serviceregister_id)
            serviceregister_e.is_lock = False
            serviceregister_e.save()
        except ValueError:
            raise
        except:
            serviceregister_s.is_lock = False
            serviceregister_s.save()
            info_logger.info(__import__('traceback').format_exc())
            raise

    return wrapper
