
from apps.hrm.models import Employee
from apps.hrm.services import HrmService
from apps.utils.sms import send_sms
from apps.utils.tools import get_info_from_id
from apps.vm.models import Visit, Visitor, Vpeople
from rest_framework.exceptions import ParseError


def sync_to_visitor(ep: Employee):
    # 个人信息完善时同步到visitor表
    Visitor.objects.filter(employee=ep).update(name=ep.name, phone=ep.phone, id_number=ep.id_number, photo=ep.photo)
    if not Visitor.objects.filter(employee=ep, create_by=ep.user).exists():
        Visitor.objects.create(employee=ep, name=ep.name, phone=ep.phone,
                               id_number=ep.id_number, photo=ep.photo, create_by=ep.user)


def bind_visit(ticket, transition, new_ticket_data: dict):
    visit = Visit.objects.get(id=new_ticket_data['visit'])
    if visit.ticket and visit.ticket.id!=ticket.id:
        raise ParseError('重复创建工单')
    if visit.level == 10 and (not Vpeople.objects.filter(visit=visit).exists()):
        raise ParseError('请至少选择一位访客')
    ticket_data = ticket.ticket_data
    ticket_data.update({
        'purpose': visit.purpose,
        'belong_dept': visit.belong_dept.id
    })
    ticket.ticket_data = ticket_data
    ticket.belong_dept = visit.belong_dept
    ticket.create_by = visit.create_by
    ticket.save()
    if visit.ticket is None:
        visit.ticket = ticket
        visit.state = Visit.V_AUDIT
        visit.save()


def get_receptionist(state, ticket, new_ticket_data, handler):
    visit = Visit.objects.filter(ticket=ticket).first()
    if visit:
        return [visit.receptionist.id]


def visit_audit_end(ticket):
    visit = Visit.objects.get(ticket=ticket)
    # 更新企业访客人员库
    for i in Vpeople.objects.filter(visit=visit):
        visitor = i.visitor
        # if visitor.id_number and visitor.photo:
        ep = Employee.objects.get_queryset(all=True).filter(id_number=visitor.id_number).first()
        ep2 = Employee.objects.get_queryset(all=True).filter(phone=visitor.phone).first()
        if ep:
            pass
        elif ep2:
            ep = ep2
        else:
            ep = Employee()
        ep.id_number = visitor.id_number
        ep.gender = get_info_from_id(visitor.id_number).get('gender', '男')
        ep.name = visitor.name
        ep.phone = visitor.phone
        ep.photo = visitor.photo
        eptype = 'visitor'
        if visit.purpose == 50:
            eptype = 'driver'
        ep.type = eptype
        ep.belong_dept = visit.receptionist.belong_dept if visit.receptionist else visit.belong_dept
        ep.is_deleted = False
        ep.save()
        if ep.user:
            user = ep.user
            user.type = eptype
            user.save()
        visitor.employee = ep
        visitor.visit = visit  # 更新当前所属访客项目
        visitor.save()
        # 同步至大华人员库并下发人脸
        HrmService.sync_dahua_employee(ep, '', visit.visit_time, visit.leave_time)
    visit.state = Visit.V_ENTER
    visit.save()
    # 发送通知?
    phone = visit.create_by.phone if visit.create_by else None
    if phone:
        send_sms(phone=phone, template_code=1004, template_param={'name': '访客项目'})


def visit_driver_end(ticket):
    visit = Visit.objects.get(ticket=ticket)
    # 更新企业司机人员库
    for i in Vpeople.objects.filter(visit=visit):
        visitor = i.visitor
        # 尝试找到人员
        ep = Employee.objects.get_queryset(all=True).filter(id_number=visitor.id_number).first()
        ep2 = Employee.objects.get_queryset(all=True).filter(phone=visitor.phone).first()
        if ep:
            pass
        elif ep2:
            ep = ep2
        else:
            ep = Employee()
        ep.id_number = visitor.id_number
        ep.gender = get_info_from_id(visitor.id_number).get('gender', '男')
        ep.name = visitor.name
        ep.phone = visitor.phone
        ep.photo = visitor.photo
        ep.type = 'driver'
        ep.belong_dept = visit.belong_dept
        ep.save()
        if ep.user:
            user = ep.user
            user.type = 'driver'
            user.save()
        visitor.employee = ep
        visitor.visit = visit  # 更新当前所属访客项目
        visitor.save()
        # 同步至大华人员库并下发人脸
        HrmService.sync_dahua_employee(ep, '', visit.visit_time, visit.leave_time)
    visit.state = Visit.V_ENTER
    visit.save()
    # 发送通知?
    phone = visit.create_by.phone if visit.create_by else None
    if phone:
        send_sms(phone=phone, template_code=1004, template_param={'name': '拉送货入厂'})
