
from apps.wf.models import Ticket
# TicketFlow, Transition, Workflow, CustomField, State, 
from apps.ofm.models import LendingSeal, VehicleUse, BorrowRecord, Publicity, MroomBooking, MroomSlot
from rest_framework.exceptions import ParseError


def seal_submit_validate(ins: LendingSeal):
    if ins.submit_time:
        raise ParseError('该日志已提交!')
    if ins.mtask and ins.mtask.state == LendingSeal.MTASK_STOP:
        raise ParseError('该任务已停止!')

def bind_mroombooking(ticket: Ticket, transition, new_ticket_data: dict):
    ins = MroomBooking.objects.get(id=new_ticket_data['t_id'])
    ticket_data = ticket.ticket_data
    ticket_data.update({
        't_model': 'mroombooking',
        't_id': ins.id,
    })
    ticket.ticket_data = ticket_data
    ticket.create_by = ins.create_by
    ticket.save()
    if ins.ticket is None:
        ins.ticket = ticket
        ins.save()

def mroombooking_reject(ticket: Ticket, transition, new_ticket_data: dict):
    ins = MroomBooking.objects.get(id=new_ticket_data['t_id'])
    MroomSlot.objects.filter(booking=ins).update(is_inuse=False)
            

def bind_lendingseal(ticket: Ticket, transition, new_ticket_data: dict):
    ticket_data = ticket.ticket_data
    ins = LendingSeal.objects.get(id=new_ticket_data['t_id'])
    ins.actual_return_date = None
    ticket_data.update({
        't_model': 'LendingSeal',
        't_id': ins.id,
        "is_lending": ins.is_lending
    })
    ticket.ticket_data = ticket_data
    ticket.create_by = ins.create_by
    ticket.save()
    if ins.ticket is None:
        ins.ticket = ticket
        ins.save()
    # 如果驳回到开始状态

def lending_save_ticket_data(ticket: Ticket, new_ticket_data: dict, **kwargs):
    try:
        obj = LendingSeal.objects.get(id=new_ticket_data['t_id'])
    except LendingSeal.DoesNotExist:
        raise ParseError("Publicity t_id 不存在")
    data_save = {k: v for k, v in new_ticket_data.items() if k not in ['t_model', 't_id']}

    for k, v in data_save.items():
        setattr(obj, k, v)
    obj.save()


def bind_vehicle(ticket: Ticket, transition, new_ticket_data: dict):
    ins = VehicleUse.objects.get(id=new_ticket_data['t_id'])
    ticket_data = ticket.ticket_data
    ticket_data.update({
        't_model': 'Vehicle',
        't_id': ins.id,
        'is_city': ins.is_city
    })
    ins.actual_km = None
    ticket.ticket_data = ticket_data
    ticket.create_by = ins.create_by
    ticket.save()
    if ins.ticket is None:
        ins.ticket = ticket
        ins.save()

def vehicle_save_ticket_data(ticket: Ticket, new_ticket_data: dict, **kwargs):
    try:
        obj = VehicleUse.objects.get(id=new_ticket_data['t_id'])
    except VehicleUse.DoesNotExist:
        raise ParseError("Publicity t_id 不存在")
    data_save = {k: v for k, v in new_ticket_data.items() if k not in ['t_model', 't_id']}

    for k, v in data_save.items():
        setattr(obj, k, v)
    obj.save()

def bind_file(ticket: Ticket, transition, new_ticket_data: dict):
    ins = BorrowRecord.objects.get(id=new_ticket_data['t_id'])
    ticket_data = ticket.ticket_data
    ticket_data.update({
        't_model': 'BorrowRecord',
        't_id': ins.id,
    })
    ins.return_date = None
    ticket.ticket_data = ticket_data
    ticket.create_by = ins.create_by
    ticket.save()
    if ins.ticket is None:
        ins.ticket = ticket
        ins.save()
        
def file_save_ticket_data(ticket: Ticket, new_ticket_data: dict, **kwargs):
    try:
        obj = BorrowRecord.objects.get(id=new_ticket_data['t_id'])
    except BorrowRecord.DoesNotExist:
        raise ParseError("Publicity t_id 不存在")
    data_save = {k: v for k, v in new_ticket_data.items() if k not in ['t_model', 't_id']}

    for k, v in data_save.items():
        setattr(obj, k, v)
    obj.save()

def bind_publicity(ticket: Ticket, transition, new_ticket_data: dict):
    ins = Publicity.objects.get(id=new_ticket_data['t_id'])
    ticket_data = ticket.ticket_data
    ticket_data.update({
        't_model': 'publicity',
        't_id': ins.id,
    })
    ins.dept_opinion = None
    ins.secret_period = None
    ins.dept_opinion_review = None
    ins.publicity_opinion = None
    ticket.ticket_data = ticket_data
    ticket.create_by = ins.create_by
    ticket.save()
    if ins.ticket is None:
        ins.ticket = ticket
        ins.save()

def save_ticket_data(ticket: Ticket, new_ticket_data: dict, **kwargs):
    try:
        obj = Publicity.objects.get(id=new_ticket_data['t_id'])
    except Publicity.DoesNotExist:
        raise ParseError("Publicity t_id 不存在")
    data_save = {k: v for k, v in new_ticket_data.items() if k not in ['t_model', 't_id']}

    for k, v in data_save.items():
        setattr(obj, k, v)
    obj.save()
    

