def insertAndUpdateMusic(filename, file_type):
    session = SessionClass()
    music = session.query(Music).filter(Music.music_name == filename).first()
    if music == None:
        cur_max_human_no_music = session.query(Music).filter(Music.music_group == file_type).order_by(Music.music_human_no.desc()).first()
        music_human_no = str(int(cur_max_human_no_music.music_human_no) + 1).zfill(3)

        music_new = Music()
        music_new.music_name = filename
        music_new.music_human_no = music_human_no
        music_new.music_group = file_type
        music_new.music_human_no_and_group = music_human_no + file_type
        music_new.music_insert_time = getTimeStamp()
        try:
            session.add(music_new)
            session.flush()
            music_human_no_and_group = music_new.music_human_no_and_group
            session.commit()
            session.close()
            return music_human_no_and_group
        except:
            session.rollback()
            return None
    else:

        music_human_no_and_group = music.music_human_no_and_group
        session.commit()
        session.close()
        return music_human_no_and_group
def insertPatient(patientInfo,device_mac):
    session = SessionClass()
    device = session.query(Device).filter(Device.device_mac == device_mac).first()
    patient = session.query(Patient).filter(Patient.patient_age == patientInfo['age'],
                                               Patient.patient_gender == patientInfo['gender'],
                                               Patient.patient_name == patientInfo['name'],
                                               Patient.device_id == device.device_id).first()
    doctor_id = insertDoctor(patientInfo['doctor'], device_mac)
    if doctor_id != None:
        if patient==None:
            patient_new=Patient()
            patient_new.patient_name=patientInfo['name']
            patient_new.patient_gender = patientInfo['gender']
            patient_new.patient_age = patientInfo['age']
            patient_new.patient_self_reported = patientInfo.get('report', None)
            patient_new.patient_medical_history = patientInfo.get('history', None)
            patient_new.patient_examination = patientInfo.get('inspect', None)
            patient_new.patient_device_category = patientInfo['cate']
            #patient_new.patient_doctor_category1 = patientInfo['dcate1']
            patient_new.patient_doctor_category1 = patientInfo['cate']
            patient_new.patient_doctor_category2 = patientInfo.get('dcate2',None)
            patient_new.device_id=device.device_id
            patient_new.doctor_id=doctor_id
            try:
                session.add(patient_new)
                session.flush()
                patient_new_id=patient_new.patient_id
                session.commit()
                session.close()
                return patient_new_id
            except:
                session.rollback()
                return None
        else:
            if 'report' in patientInfo:
                patient.patient_self_reported = patientInfo['report']
            if 'history' in patientInfo:
                patient.patient_medical_history = patientInfo['history']
            if 'inspect' in patientInfo:
                patient.patient_examination = patientInfo['inspect']
            if 'dcate1' in patientInfo:
                patient.patient_examination = patientInfo['dcate1']
            if 'dcate2' in patientInfo:
                patient.patient_examination = patientInfo['dcate2']
            patient_id=patient.patient_id
            session.commit()
            session.close()
            return patient_id
    else:
        session.close()
        return None
def insertDevice(deviceInfo):
    session = SessionClass()
    device=session.query(Device).filter(Device.device_mac==deviceInfo['device_mac']).first()
    if device == None:
        device_new = Device()
        device_new.device_mac = deviceInfo['device_mac']
        device_new.device_img = deviceInfo.get('device_img', None)
        device_new.device_name = deviceInfo.get('device_name', None)
        device_new.device_department = deviceInfo.get('device_department', None)
        try:
            session.add(device_new)
            session.flush()
            device_new_id = device_new.device_id
            session.commit()
            session.close()
            return device_new_id
        except:
            session.rollback()
            return None
    else:
        session.close()
        return device.device_id
def insertDoctor(doctor_name,device_mac):
    session = SessionClass()
    device = session.query(Device).filter(Device.device_mac == device_mac).first()
    doctor = session.query(Doctor).filter(Doctor.device_id == device.device_id,Doctor.doctor_name==doctor_name).first()
    if doctor==None:
        doctor_new=Doctor()
        doctor_new.doctor_name=doctor_name
        doctor_new.device_id=device.device_id
        try:
            session.add(doctor_new)
            session.flush()
            doctor_new_id=doctor_new.doctor_id
            session.commit()
            session.close()
            #device=session.query(Doctor).filter(Doctor.doctor_id==doctor_new_id).first()
            return doctor_new_id
        except:
            session.rollback()
            return None
    else:
        session.close()
        return doctor.doctor_id