def personal_extra_work_view(request):
    try:
        extra_work = Extra_Work.objects.filter(work_number=request.user.username).order_by('id').reverse()[0]
    except:
        return json.success({'states': '', 'registerDay': '', 'passPlace': ''})
    else:
        if extra_work.status == 0:
            return json.success({'states': 0, 'registerDay': extra_work.extra_work_time})
        elif extra_work.status == 1:
            return json.success(
                {'states': 1, 'passPlace': extra_work.extra_area, 'registerDay': extra_work.extra_work_time})
        else:
            return json.success(
                {'states': 2, 'whyReject': extra_work.refuse_reason, 'registerDay': extra_work.extra_work_time})
def try_login(request):
    if request.method == 'POST':
        try:
            req = simplejson.loads(request.body)
            username = req['username']
            password = req['password']
            user = authenticate(username=username, password=password)
        except:
            return json.post_error
        else:
            if user is not None:
                if user.is_active:
                    try:
                        if req['longtime']:
                            request.session.set_expiry(datetime.now() + timedelta(days=365))
                    except:
                        time = req['time']
                        if time == False:
                            request.session.set_expiry(0)
                        else:
                            request.session.set_expiry(691200)
                    login(request, user)
                    login_return = {}
                    login_return.update(initialization(request))
                    return json.success(login_return)
                else:
                    return json.what_error('username&password error')
            else:
                return json.what_error('username&password error')
    else:
        return json.no_post
def experience_view(request):
    ex = Experience.objects.all().order_by('id').reverse()
    exs = []
    for e in ex:
        exs.append(
            {'name': e.name, 'id': e.id, 'number': e.who_do, 'time': e.time, 'record': e.record, 'title': e.title})
    return json.success({'experience': exs})
def work_order_view(request, area, operator):
    try:
        if operator == '投诉':
            work_order = Work_Situation.objects.filter(work_area=area, situation_order=3).exclude(status=1)
        else:
            work_order = Work_Situation.objects.exclude(status=1).filter(work_area=area, operator=operator,
                                                                         situation_order__in=[2, 1])
        work_returns = []
        for x in work_order:
            work_return = {}
            work_return.update(
                {'id': x.id, 'netAccount': x.account_number, 'repairStatus': x.status,
                 'userPhone': x.telephone_number, 'userHouse': x.dormitory_number, 'userPlace': x.work_area,
                 'repairIntro': x.introduction, 'addTime': x.add_time, 'taskProperty': x.situation_order,
                 'lastChangeTime': x.last_change_time, 'netInfo': x.operator})
            work_returns.append(work_return)
    except:
        return json.error
    else:
        works = []
        a = Check_In.objects.filter(check_in_area=area, check=0)
        for x in a:
            works.append({'work_number': x.work_number, 'name': x.user.last_name, 'phone': x.user.personal.phone_number,
                          'area': area, 'work_phone': x.user.personal.work_phone})
        return json.success({'work_order': work_returns, 'works': works})
def check_out(request):
    try:
        req = simplejson.loads(request.body)
        check = Check_In.objects.filter(work_number=request.user.username).order_by('id').reverse()[0]
        if check.taken_toolkit == True:
            check.cable = req['cable']
            check.port_module = req['port_module']
            check.switch = req['switch']
            check.crimping_Tool = req['crimping_Tool']
            check.crystal_Head = req['crystal_Head']
            check.measuring_line = req['measuring_line']
            check.screwdriver = req['screwdriver']

            if req['hunt']:
                check.hunt = req['hunt']
            if req['key']:
                check.key = req['key']
            if req['detailed_description']:
                check.detailed_description = req['detailed_description']
    except:
        return json.post_error
    else:
        check.check = 1
        check.save()
        return json.success({'check': False, 'place': ''})
def is_login(request):
    csrf_token = get_token(request)
    login_return = {'csrf_token': csrf_token}
    if request.user.is_authenticated():
        login_return.update({'is_login': True})
        login_return.update(initialization(request))
        return json.success(login_return)
    else:
        login_return.update({'is_login': False})
        return json.failure(login_return)
def view_today(request):
    req = simplejson.loads(request.body)
    view = []
    work = Work_Situation.objects.filter(work_area=req['area']).exclude(status=1)
    for x in work:
        view.append({'id': x.id, 'netAccount': x.account_number, 'repairStatus': x.status,
                     'userPhone': x.telephone_number, 'userHouse': x.dormitory_number, 'userPlace': x.work_area,
                     'repairIntro': x.introduction, 'addTime': x.add_time, 'taskProperty': x.situation_order,
                     'lastChangeTime': x.last_change_time, 'netInfo': x.operator})
    return json.success({'today': view})
def change_announcement(request):
    try:
        req = simplejson.loads(request.body)
        a = Announcement.objects.all()[0]
        a.notice_content = req['content']
    except:
        return json.post_error
    else:
        a.save()
        return json.success({'content': a.notice_content})
def check_in(request):
    wd = User.objects.get(username=request.user.username).personal.week_day
    try:
        ex = Extra_Work.objects.filter(work_number=request.user.username).order_by('id').reverse()[0]
        if (ex.status != 1 or ex.extra_work_time != str(datetime.now().date()) + ':16.30') and wd != int(
                datetime.today().weekday()):
            if wd == 7:
                pass
            else:
                return json.what_error('not check in time')
    except:
        if wd != int(datetime.today().weekday()):
            if wd == 7:
                pass
            else:
                return json.what_error('not check in time')
    try:
        if Check_In.objects.filter(work_number=request.user.username).order_by('id').reverse()[0].check == 0:
            return json.what_error('had been check')
    except:
        pass
    try:
        req = simplejson.loads(request.body)
        toolkit = req['toolkit']
        if toolkit == True:
            check = Check_In.objects.create(work_number=request.user.username, check_in_area=req['area'],
                                            cable=req['cable'], port_module=req['port_module'],
                                            crimping_Tool=req['crimping_Tool'],
                                            screwdriver=req['screwdriver'], crystal_Head=req['crystal_Head'],
                                            switch=req['switch'], taken_toolkit=True,
                                            measuring_line=req['measuring_line'],
                                            user=User.objects.get(username=request.user.username))
            if req['key']:
                check.key = req['key']
            else:
                check.key = False
            if req['hunt']:
                check.hunt = req['hunt']
            else:
                check.hunt = False
            if req['detailed_description']:
                check.detailed_description = req['detailed_description']
        else:
            check = Check_In.objects.create(work_number=request.user.username, check_in_area=req['area'],
                                            taken_toolkit=False, detailed_description='',
                                            cable=False, crimping_Tool=False, switch=False,
                                            crystal_Head=False, measuring_line=False, port_module=False,
                                            key=False, screwdriver=False, check=0,
                                            user=User.objects.get(username=request.user.username))
    except:
        return json.post_error
    else:
        check.save()
        return json.success({'check': True, 'place': check.check_in_area})
def extra_work_view(request):
    try:
        extra_work = Extra_Work.objects.filter(status=0)
        extra_work_return = []
        for x in extra_work:
            extra_work_return.append(
                {'id': x.id, 'add_time': x.add_time, 'status': 0, 'extra_work_time': x.extra_work_time,
                 'name': x.user.last_name, 'work_number': x.user.username})
    except:
        return json.error
    else:
        return json.success({'extra_work': extra_work_return})
def experience_add(request):
    try:
        req = simplejson.loads(request.body)
        who = Personal.objects.get(work_number=request.user.username)
        ex = Experience.objects.create(name=who.name, who_do=request.user.username, record=req['experience'],
                                       title=req['title'])
    except:
        return json.error
    else:
        ex.save()
        return json.success(
            {'experience': {'name': ex.name, 'id': ex.id, 'number': ex.who_do, 'time': ex.time, 'record': ex.record,
                            'title': ex.title}})
def person_today(request):
    persons = []
    s = datetime.strptime(str(datetime.now().date()), '%Y-%m-%d')
    e = s + timedelta(hours=23, minutes=59, seconds=59)
    history = History.objects.filter(time__range=(s,e))
    work = []
    for x in history:
        if request.user.username == x.who_do:
            work.append(x.to_id)
    work = set(work)
    work = Work_Situation.objects.filter(id__in=work)
    for x in work:
        person = {}
        person.update({'id': x.id, 'netAccount': x.account_number, 'repairStatus': x.status,
                       'userPhone': x.telephone_number, 'userHouse': x.dormitory_number, 'userPlace': x.work_area,
                       'repairIntro': x.introduction, 'addTime': x.add_time, 'taskProperty': x.situation_order,
                       'lastChangeTime': x.last_change_time, 'netInfo': x.operator, 'history': []})
        for y in History.objects.filter(to_id=x.id):
            person['history'].append(
                {'id': y.id, 'time': y.time, 'record': y.record, 'who_do': y.who_do, 'name': y.name})
        persons.append(person)
    return json.success({'persons': persons})
def extra_work_add(request):
    try:
        if Extra_Work.objects.filter(work_number=request.user.username).order_by('id').reverse()[0].status == 0:
            return json.what_error('had extra_work')
    except:
        pass
    try:
        req = simplejson.loads(request.body)
        if datetime.now().date() == datetime.strptime(req['date'], '%Y-%m-%d').date() and timedelta(
                hours=int(datetime.now().hour), minutes=int(datetime.now().minute)) > timedelta(minutes=00,
                                                                                                hours=16) or datetime.now().date() + timedelta(
            days=1) < datetime.strptime(req['date'], '%Y-%m-%d').date() or datetime.now().date() > datetime.strptime(
            req['date'], '%Y-%m-%d').date():
            return json.what_error('time error')
        else:
            new_extra_work = Extra_Work.objects.create(user=User.objects.get(username=request.user.username),
                                                       extra_work_time=req['date'] + ':16.30',
                                                       work_number=request.user.username, status=0, extra_area=''
                                                       , refuse_reason='')
    except:
        return json.post_error
    else:
        new_extra_work.save()
        return json.success({'states': 0, 'registerDay': new_extra_work.extra_work_time})
def inquire(request):
    req = simplejson.loads(request.body)
    inquire_return = []
    model = req['model']
    if model == 1:
        d = datetime.strptime(req['date'], '%Y-%m-%d')
        day = Check_In.objects.filter(check_in_time__gte=d.date()).exclude(
            check_in_time__gte=(d + timedelta(days=1)).date())
        for x in day:
            inquire_return.append({'id': x.user.username, 'name': x.user.last_name, 'check': x.check,
                                   'check_in_time': x.check_in_time, 'check_area': x.check_in_area,
                                   'check_out_time': x.check_out_time, 'taken_toolkit': x.taken_toolkit,
                                   'toolkit': {'detailed_description': x.detailed_description, 'cable': x.cable,
                                               'crimping_Tool': x.crimping_Tool, 'switch': x.switch,
                                               'crystal_Head': x.crystal_Head, 'key': x.key,
                                               'measuring_line': x.measuring_line, 'port_module': x.port_module}})
        return json.success({'inquire': inquire_return})
    elif model == 2:
        s = datetime.strptime(req['start_date'], '%Y-%m-%d')
        e = datetime.strptime(req['end_date'], '%Y-%m-%d') + timedelta(hours=23, minutes=59, seconds=59)
        day = History.objects.filter(time__range=(s, e))
        work = []
        for x in day:
            work.append(x.to_id)
        work = set(work)
        work = Work_Situation.objects.filter(id__in=work)
        done = 0
        undone = 0
        reported = 0
        tomorrow = 0
        for x in work:
            if x.status == 0:
                undone += 1
            elif x.status == 1:
                done += 1
            elif x.status == 2:
                reported += 1
            elif x.status == 3:
                tomorrow += 1
        return json.success({'done': done, 'undone': undone, 'reported': reported, 'tomorrow': tomorrow})
    elif model == 3:
        work = Work_Situation.objects.filter(telephone_number=req['telephone_number'])
        for x in work:
            inquire_returns = {}
            inquire_returns.update(
                {'id': x.id, 'netAccount': x.account_number, 'repairStatus': x.status,
                 'userPhone': x.telephone_number, 'userHouse': x.dormitory_number, 'userPlace': x.work_area,
                 'repairIntro': x.introduction, 'addTime': x.add_time, 'taskProperty': x.situation_order,
                 'lastChangeTime': x.last_change_time, 'netInfo': x.operator, 'history': [], 'person': []})
            for y in x.who_do.all():
                inquire_returns['person'].append({'id': y.username, 'name': y.last_name})
            for z in History.objects.filter(to_id=x.id):
                inquire_returns['history'].append(
                    {'id': z.id, 'work_number': z.who_do, 'time': z.time, 'name': z.name,
                     'record': z.record})
            inquire_return.append(inquire_returns)
        return json.success({'return': inquire_return})
    elif model == 4:
        s = datetime.strptime(req['start_date'], '%Y-%m-%d')
        e = datetime.strptime(req['end_date'], '%Y-%m-%d') + timedelta(hours=23, minutes=59, seconds=59)
        history = History.objects.filter(time__range=(s, e))
        work = []
        for x in history:
            if request.user.username == x.who_do:
                work.append(x.to_id)
        work = set(work)
        work = Work_Situation.objects.filter(id__in=work)
        for a in work:
            inquire_returns = {}
            inquire_returns.update(
                {'id': a.id, 'netAccount': a.account_number, 'repairStatus': a.status,
                 'userPhone': a.telephone_number, 'userHouse': a.dormitory_number, 'userPlace': a.work_area,
                 'repairIntro': a.introduction, 'addTime': a.add_time, 'taskProperty': a.situation_order,
                 'lastChangeTime': a.last_change_time, 'netInfo': a.operator, 'history': [],
                 'person': []})
            for y in a.who_do.all():
                inquire_returns['person'].append({'id': y.username, 'name': y.last_name})
            for z in History.objects.filter(to_id=a.id):
                inquire_returns['history'].append(
                    {'id': z.id, 'work_number': z.who_do, 'time': z.time, 'name': z.name,
                     'record': z.record})
            inquire_return.append(inquire_returns)
        return json.success({'return': inquire_return})
    elif model == 5:
        s = datetime.strptime(req['start_date'], '%Y-%m-%d')
        e = datetime.strptime(req['end_date'], '%Y-%m-%d') + timedelta(hours=23, minutes=59, seconds=59)
        day = History.objects.filter(time__range=(s, e))
        work = []
        for x in day:
            work.append(x.to_id)
        work = set(work)
        work = Work_Situation.objects.filter(id__in=work)
        if req['status'] == 4:
            for x in work:
                inquire_returns = {}
                inquire_returns.update(
                    {'id': x.id, 'netAccount': x.account_number, 'repairStatus': x.status,
                     'userPhone': x.telephone_number, 'userHouse': x.dormitory_number, 'userPlace': x.work_area,
                     'repairIntro': x.introduction, 'addTime': x.add_time, 'taskProperty': x.situation_order,
                     'lastChangeTime': x.last_change_time, 'netInfo': x.operator, 'history': [], 'person': []})
                for y in x.who_do.all():
                    inquire_returns['person'].append({'id': y.username, 'name': y.last_name})
                for z in History.objects.filter(to_id=x.id):
                    inquire_returns['history'].append(
                        {'id': z.id, 'work_number': z.who_do, 'time': z.time, 'name': z.name,
                         'record': z.record})
                inquire_return.append(inquire_returns)
        else:
            for x in work:
                inquire_returns = {}
                if x.status == req['status']:
                    inquire_returns.update(
                        {'id': x.id, 'netAccount': x.account_number, 'repairStatus': x.status,
                         'userPhone': x.telephone_number, 'userHouse': x.dormitory_number, 'userPlace': x.work_area,
                         'repairIntro': x.introduction, 'addTime': x.add_time, 'taskProperty': x.situation_order,
                         'lastChangeTime': x.last_change_time, 'netInfo': x.operator, 'history': [], 'person': []})
                    for y in x.who_do.all():
                        inquire_returns['person'].append({'id': y.username, 'name': y.last_name})
                    for z in History.objects.filter(to_id=x.id):
                        inquire_returns['history'].append(
                            {'id': z.id, 'work_number': z.who_do, 'time': z.time, 'name': z.name,
                             'record': z.record})
                    inquire_return.append(inquire_returns)
        return json.success({'return': inquire_return})