def bookaddcopy():
    from model.model import Book, PBook
    from dboperation.dboperation import queryAccordingToID, add, addnum
    book = {}
    if request.method == 'POST':
        bid = request.form['bid']
        oldbid = request.form['oldbid']
        sta = request.form['sta']
        print(bid, sta)
        temp_ans = queryAccordingToID(detail='book', par=bid)
        if not temp_ans:
            ans = queryAccordingToID(detail='book', par=oldbid)
            loc = ans.Loc
            per = ans.Per
            isbn = ans.ISBN
            result = queryAccordingToID(detail='pbook', par=ans.ISBN)
            bname = result.Bname
            author = result.Author
            pub = result.Pub
            pyear = result.Pyear
            book = Book(BID=bid, ISBN=isbn, Loc=loc, Sta=sta, Per=per)
            pbook = PBook(ISBN=isbn, Bname=bname, Author=author, Pub=pub, Pyear=pyear, num=1, Per=per)
            addnum(isbn)
            add(book)
            ans_data = {
                'bid': bid,
                'status': 'ok'
            }
            return jsonify(ans_data)
    ans_data = {
        'bid': '',
        'status': 'fail'
    }
    return jsonify(ans_data)
def borrowReconds(rid='', bid='', botime='', rbtime1='', rbtime2=''):
    from dboperation.dboperation import queryborrowRecords, queryAccordingToID
    import datetime
    result = queryborrowRecords(rid=rid, bid=bid, botime=botime, rbtime1=rbtime1, rbtime2=rbtime2)
    print('borrowReconds函数下的result')
    print('rid:' + rid)
    ans = []
    # num = 0
    print("result:")
    # print(type(result))
    for rec in result:
        # print(type(rec))
        print("进入result!:")
        # num = num + 1
        # print("num:", num)
        rec = dict(rec.items())
        temp1 = queryAccordingToID('book', rec['BID'])
        temp2 = queryAccordingToID('pbook', temp1.ISBN)
        rec['ISBN'] = temp2.ISBN
        rec['Bname'] = temp2.Bname
        if not rec['Rbtime2']:
            rec['Condi'] = '未归还'
            if rec['Rbtime1'] > datetime.datetime.now():
                rec['Condi'] = '已超期'
            rec['Rbtime2'] = rec['Condi']
        else:
            if rec['Penalty']:
                rec['Condi'] = '罚金' + str(rec['Penalty'])
        ans.append(copy.deepcopy(rec))
        # print(type(rec))
    print("================================================================================")
    for rec in ans:
        print(rec)
    print("================================================================================")
    return ans
def bookQuery(detail, bid='', isbn='', bname=''):
    from dboperation.dboperation import queryAccordingToID, accordingToISBN, accordingTobname
    book = {}
    books = []
    if detail == 'bid':
        result = queryAccordingToID('book', bid)
        if not result:
            return books
        ans = queryAccordingToID('pbook', result.ISBN)
        book['BID'] = result.BID
        book['ISBN'] = result.ISBN
        book['Loc'] = result.Loc
        book['Sta'] = result.Sta
        book['Bname'] = ans.Bname
        book['Author'] = ans.Author
        book['Pub'] = ans.Pub
        book['Pyear'] = ans.Pyear
        # book['Num'] = num
        book['Per'] = ans.Per
        books.append(copy.deepcopy(book))
    elif detail == 'isbn':
        ans = queryAccordingToID('pbook', isbn)
        if ans:
            book['Bname'] = ans.Bname
            book['Author'] = ans.Author
            book['Pub'] = ans.Pub
            book['Pyear'] = ans.Pyear
            book['ISBN'] = ans.ISBN
            #           接下来是要写查询book的内容
            result = accordingToISBN(isbn)
            for rec in result:
                # num = num + 1
                print(rec)
                # print("num:",num)
                book['BID'] = rec.BID
                book['Loc'] = rec.Loc
                book['Sta'] = rec.Sta
                book['Per'] = rec.Per
                # book['Num'] = num
                print(book)
                books.append(copy.deepcopy(book))
            print('*' * 40)
            print(books)
    elif detail == 'bname':
        result = accordingTobname(bname)
        for rec in result:
            book['Bname'] = rec.Bname
            book['Author'] = rec.Author
            book['Pub'] = rec.Pub
            book['Pyear'] = rec.Pyear
            book['ISBN'] = rec.ISBN
            ans = accordingToISBN(rec.ISBN)
            for eve in ans:
                book['BID'] = eve.BID
                book['Loc'] = eve.Loc
                book['Sta'] = eve.Sta
                book['Per'] = eve.Per
                print(book)
                books.append(copy.deepcopy(book))
    return books
def bookupdate():
    from dboperation.dboperation import updateBookInfo, queryAccordingToID
    bookinfo = {}
    book = {}
    if request.method == 'POST':
        bookinfo['newbid'] = request.form['newbid']
        bookinfo['bid'] = request.form['bid']
        bookinfo['newisbn'] = request.form['newisbn']
        bookinfo['isbn'] = request.form['isbn']
        bookinfo['bname'] = request.form['bname']
        bookinfo['author'] = request.form['author']
        bookinfo['pub'] = request.form['pub']
        bookinfo['sta'] = request.form['sta']
        bookinfo['pyear'] = request.form['pyear']
        bookinfo['loc'] = request.form['loc']
        bookinfo['per'] = request.form['per']
        if updateBookInfo(info=bookinfo):
            result = queryAccordingToID('book', bookinfo['newbid'])
            ans = queryAccordingToID('pbook', result.ISBN)
            book['BID'] = result.BID
            book['ISBN'] = result.ISBN
            book['Loc'] = result.Loc
            book['Sta'] = result.Sta
            book['Bname'] = ans.Bname
            book['Author'] = ans.Author
            book['Pub'] = ans.Pub
            book['Pyear'] = ans.Pyear
            book['Per'] = ans.Per
            return render_template('bookupdate.html', result=book)
    return render_template('bookupdate.html', result=book)
def readerQuery(rid='', rname=''):
    from dboperation.dboperation import queryAccordingToID, getNote, accordingTorname
    readers = []
    reader = {}
    if rid != '':
        result = queryAccordingToID('reader', rid)
        ans = queryAccordingToID('r_sta', rid)
        note = ''
        if not result or not ans:
            print("这是在读者管理界面,没有找到对应RID")
            # reader = ''
        else:
            print("ans_remarik:", ans.Remark)
            note = getNote(ans.Remark)
            reader['RID'] = result.RID
            reader['Rname'] = result.Rname
            reader['Rtel'] = result.Rtel
            reader['Rem'] = result.Rem
            reader['condi'] = '学生'
            # reader['condi'] = '学生'
            if ans.Normal:
                reader['Normal'] = '正常'
            else:
                reader['Normal'] = '异常'
            reader['Remark'] = note
            readers.append(copy.deepcopy(reader))
        print("readers:", readers)
        return readers
    elif rname != '':
        res = accordingTorname(rname=rname)
        print("readers:", res)
        for rec in res:
            print("=" * 70)
            print(rec)
            result = queryAccordingToID('reader', rec.RID)
            ans = queryAccordingToID('r_sta', rec.RID)
            note=''
            if ans:
                note = getNote(ans.Remark)
            reader['RID'] = result.RID
            reader['Rname'] = result.Rname
            reader['Rtel'] = result.Rtel
            reader['Rem'] = result.Rem
            reader['condi'] = '学生'
            if ans.Normal:
                reader['Normal'] = '正常'
            else:
                reader['Normal'] = '异常'
            reader['Remark'] = note
            readers.append(copy.deepcopy(reader))
        print("readers:", readers)
        return readers
def showBookInfo(bid):
    from dboperation.dboperation import updateBookInfo, queryAccordingToID
    book = {}
    result = queryAccordingToID('book', bid)
    ans = queryAccordingToID('pbook', result.ISBN)
    print("result:", result)
    print("ans: ", ans)
    book['BID'] = result.BID
    book['ISBN'] = result.ISBN
    book['Loc'] = result.Loc
    book['Sta'] = result.Sta
    book['Bname'] = ans.Bname
    book['Author'] = ans.Author
    book['Pub'] = ans.Pub
    book['Pyear'] = ans.Pyear
    book['Per'] = ans.Per
    return render_template('bookupdate.html', record=book)
def delreader(rid):
    from dboperation.dboperation import queryAccordingToID, accordingToISBN, delt, delnum
    if request.method == "GET":
        print("==============================in delreader===============================")
        print("rid", rid)
        res = queryAccordingToID(detail='reader', par=rid)
        ans = queryAccordingToID(detail='r_sta', par=res.RID)
        delt(ans)
        delt(res)
        ans_data = {
            'status': 'ok',
            'rid': rid,
        }
        return jsonify(ans_data)
    ans_data = {
        'status': 'fail',
        'rid': rid,
    }
    return jsonify(ans_data)
def delbook(bid):
    from dboperation.dboperation import queryAccordingToID, accordingToISBN, delt, delnum
    if request.method == "GET":
        print("==============================in delbook===============================")
        print("bid", bid)
        res = queryAccordingToID(detail='book', par=bid)
        isbn = res.ISBN
        ans = queryAccordingToID(detail='pbook', par=isbn)
        if ans.num == 0:
            delt(res)
            delt(ans)
        if ans.num != 0:
            delnum(isbn)
            delt(res)
            print("num!=0")
        ans_data = {
            'status': 'ok',
            'isbn': isbn,
        }
        return jsonify(ans_data)
def reserveRrecord():
    from dboperation.dboperation import queryreservationReconds, queryAccordingToID
    ans = []
    rec={}
    if request.method == 'POST':
        rid = request.form['rid']
        print('rid=',rid)
        if rid:
            result = queryreservationReconds(rid=rid)
            print(result)
            for record in result:
                print("进入result!:")
                # rec = dict(rec.items())
                temp1 = queryAccordingToID('book', record.BID)
                print('='*50)
                # print('rec[bid]=',rec['BID'])
                print('=' * 50)
                temp2 = queryAccordingToID('pbook', temp1.ISBN)
                rec['BID'] = record.BID
                rec['Bname'] = temp2.Bname
                rec['RID'] = rid
                if record.Status:
                    rec['Status'] = '预约满足,请自取'
                else:
                    rec['Status'] = '预约中'
                rec['Aptime1'] = record.Aptime1
                rec['Aptime2'] = record.Aptime2
                ans.append(copy.deepcopy(rec))
            print(ans)
            ans_data = {
                'reserveRrecord': ans,
                'status': 'ok'
            }
            return jsonify(ans_data)
    ans_data = {
        'reserveRrecord': '',
        'status': 'failed'
    }
    return jsonify(ans_data)
def bookadd():
    from model.model import Book, PBook
    from dboperation.dboperation import queryAccordingToID, add, addnum

    if request.method == 'POST':
        print("=====================in bookadd ======================================")
        print(request.form)
        bid = request.form['bid']
        isbn = request.form['isbn']
        bname = request.form['bname']
        author = request.form['author']
        pub = request.form['pub']
        sta = request.form['sta']
        pyear = request.form['pyear']
        loc = request.form['loc']
        print("bookadd:bid:", bid)
        per = request.form['per']
        print("bookadd:isbn:", isbn)
        res = queryAccordingToID('pbook', isbn)
        book = Book(BID=bid, ISBN=isbn, Loc=loc, Sta=sta, Per=per)
        pbook = PBook(ISBN=isbn, Bname=bname, Author=author, Pub=pub, Pyear=pyear, num=1, Per=per)
        if not res:
            add(pbook)
            add(book)
        else:
            addnum(isbn)
            add(book)
        print("bookadd:bid:", bid)
        ans = bookQuery(detail='bid', bid=bid)
        print("在这里")
        print(ans)
        ans_data = {
            'isbn': isbn,
            'status': 'ok'
        }
        return jsonify(ans_data)