import mysql.connector
import uuid
from mysql.connector import pooling

class GC_MYSQL:
    mydb  = ''
    host = ''
    user = ''
    passwd = ''
    database = ''
    connection_pool = ''
    
    def setConnectInfo( self, hostname, login_user, login_password, use_database = '' ):
        dbconfig = {
            "host": hostname,
            "user": login_user,
            "password": login_password,
            "database": use_database,
        }
        self.connection_pool = pooling.MySQLConnectionPool(pool_name="mypool", pool_size=5, **dbconfig)
    def connect( self ):
        try:
            self.mydb = self.connection_pool.get_connection()
        except Exception as e:
            print(e)
    
    def check_bind( self, user_id ):
        status = '';
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            
            sql = "SELECT lineToken FROM member WHERE lineToken = '" + user_id + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            
            if data:
                for meta in data:
                    if( str(meta[0]) == 'None' ):
                        status = False
                    else:
                        status = True
            else:
                status = False
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            status = 'error';
        dbcursor.close()
        self.mydb.close()
        return status
    
    def get_member_remaining( self, user_id ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT remaining FROM member WHERE lineToken = '" + user_id + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    
    def get_member_meta( self, user_id, meta_key = '' ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT " + meta_key + " FROM member WHERE lineToken = '" + user_id + "' OR account = '" + user_id + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    def get_win_lose_draw( self, msg ):
        if( msg == 'win' ):
            return '勝'
        elif( msg == 'lose' ):
            return '負'
        elif( msg == 'draw' ):
            return '和'
        else:
            return ''
    
    def get_game_bet_histry( self, user_id, game_id ):
        reply_msg = '━━━下注紀錄━━━\n';
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT b.date, b.seq, a.betOn, a.bet, b.resultA, b.resultB, b.resultC ,a.win FROM betResult as a JOIN gameResultTianjiu as b ON a.gameId = b.gameId  WHERE a.memberId IN ( SELECT memberId FROM member WHERE lineToken = '" + user_id + "' )  AND b.gameId = '" + game_id + "' AND a.status = 'end' order by a.created_at DESC"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            data = list(reversed(data))
            if data:
                tmp = '';
                row_1 = True;
                member_bet = 0;
                for meta in data:
                    if( tmp != str(meta[0]) + '#' + "{:03d}".format(meta[1]) ):
                        if( row_1 ):
                            row_1 = False
                        else:
                            reply_msg += '輸贏總額 | ' + str(member_bet) + '\n';
                            member_bet = 0;
                            reply_msg += '━━━━━━━━━━━━\n'
                        resultA = self.get_win_lose_draw( meta[4] )
                        resultB = self.get_win_lose_draw( meta[5] )
                        resultC = self.get_win_lose_draw( meta[6] )
                        tmp = str(meta[0]) + '#' + "{:03d}".format(meta[1])
                        reply_msg += '局號 | ' + tmp + '\n';
                        reply_msg += '開獎 | A' + resultA + ' B' + resultB + ' C' + resultC + '\n';
                    
                    if( meta[2] == 'banker' ):
                        betOn = '莊家'
                    else:
                        betOn = meta[2]
                        
                    reply_msg += '      下注項目 | ' + betOn + '  金額 | ' + str(meta[3]) + '\n';
                    if( meta[2] == 'banker' ):
                        reply_msg +=  '      上莊費 | 300\n'
                    win_money = meta[7]
                    if( str(win_money) == 'None' ):
                        win_money = '0'
                    member_bet += int(win_money)
                reply_msg += '輸贏總額 | ' + str(member_bet) + '\n';
            else:
                reply_msg += '查無紀錄'
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            reply_msg = 'error';
        return reply_msg
    
    def get_bet_histry( self, user_id ):
        reply_msg = '━━━下注紀錄━━━\n';
        try:
            gameIds = self.get_game_ids(3, user_id)
            if not gameIds:
                reply_msg += '查無紀錄'
                return reply_msg
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT b.date, b.seq, a.betOn, a.bet, b.resultA, b.resultB, b.resultC ,a.win FROM betResult as a JOIN gameResultTianjiu as b ON a.gameId = b.gameId  WHERE a.memberId IN ( SELECT memberId FROM member WHERE lineToken = '" + user_id + "' )  AND b.gameId IN " + gameIds + " order by a.created_at DESC"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            data = list(reversed(data))
            if data:
                tmp = '';
                row_1 = True;
                member_bet = 0;
                for meta in data:
                    if( tmp != str(meta[0]) + '#' + "{:03d}".format(meta[1]) ):
                        if( row_1 ):
                            row_1 = False
                        else:
                            reply_msg += '輸贏總額 | ' + str(member_bet) + '\n';
                            member_bet = 0;
                            reply_msg += '━━━━━━━━━━━━\n'
                        resultA = self.get_win_lose_draw( meta[4] )
                        resultB = self.get_win_lose_draw( meta[5] )
                        resultC = self.get_win_lose_draw( meta[6] )
                        tmp = str(meta[0]) + '#' + "{:03d}".format(meta[1])
                        reply_msg += '局號 | ' + tmp + '\n';
                        reply_msg += '開獎 | A' + resultA + ' B' + resultB + ' C' + resultC + '\n';
                    
                    if( meta[2] == 'banker' ):
                        betOn = '莊家'
                    else:
                        betOn = meta[2]
                        
                    reply_msg += '      下注項目 | ' + betOn + '  金額 | ' + str(meta[3]) + '\n';
                    if( meta[2] == 'banker' ):
                        reply_msg +=  '      上莊費 | 300\n'
                    win_money = meta[7]
                    if( str(win_money) == 'None' ):
                        win_money = '0'
                    member_bet += int(win_money)
                reply_msg += '輸贏總額 | ' + str(member_bet) + '\n';
            else:
                reply_msg += '查無紀錄'
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            reply_msg = 'error';
        return reply_msg
        
    def get_tip_histry( self, user_id ):
        reply_msg = '━━━打賞紀錄━━━\n';
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT tip,created_at FROM tip WHERE memberId IN ( SELECT memberId FROM member WHERE lineToken = '" + user_id + "' ) order by created_at DESC LIMIT 10 "
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            data = list(reversed(data))
            if data:
                for meta in data:
                    reply_msg += '打賞金額 | ' + str(meta[0]) + ' 打賞時間 | ' + str(meta[1]) + '\n';
                    
            else:
                reply_msg += '查無紀錄'
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            reply_msg = 'error';
        return reply_msg 
    
    def get_game_ids( self, count, user_id ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT gameId FROM `gameResultTianjiu` WHERE gameId IN ( SELECT gameId FROM betResult WHERE status='end' AND memberId IN ( SELECT memberId FROM member WHERE lineToken = '" + user_id + "' ) ) ORDER BY created_at DESC LIMIT " + str(count)
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            ids = '(';
            i = 0;
            if not data:
                return False
            for meta in data:
                i += 1
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    ids += '"' + meta[0] + '"'
                    if( i < len(data) ):
                        ids += ','
                    
            return ids + ')'
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    def get_game_meta( self, gameId, meta_key ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT " + meta_key + " FROM gameResultTianjiu WHERE gameId = '" + gameId + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
    def get_begin_game_id( self ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT gameId FROM gameResultTianjiu WHERE status = 'begin' order by created_at DESC"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    
    def get_banker( self, gameId ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT name FROM member WHERE memberId IN (SELECT banker FROM gameResultTianjiu WHERE gameId = '" + gameId + "')"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return '0'
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return '0'
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    def check_game_manager( self, user_id ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT master FROM member WHERE lineToken = '" + user_id + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    def get_begin_game_info( self, gameId ):
        reply_msg = '';
        try:
            banker_bet_total = 0;
            a_total = 0;
            b_total = 0;
            c_total = 0;
            bet_count = 0;
            banker_count = 0;
            member_bet_total = 0;
            banker_fee = 0;
            
            game_begin_flex_msg = {}
            game_begin_flex_msg['type'] = 'bubble'
            game_begin_flex_msg['body'] = {}
            game_begin_flex_msg['body']['type'] = 'box'
            game_begin_flex_msg['body']['layout'] = 'vertical'
            game_begin_flex_msg['body']['contents'] = []
            
            self.connect()
            dbcursor = self.mydb.cursor()
            
            sql = "SELECT date,seq FROM gameResultTianjiu WHERE gameId = '" + gameId + "'"
            
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            for meta in data:
                game_no = "局號："+ str(meta[0]) + '#' + "{:03d}".format(meta[1])
                if( int(meta[1]) % 2 == 0 ):
                    game_no += '-下巡'
                else:
                    game_no += '-上巡'
            
                game_begin_flex_msg['body']['contents'].append({
                    "type": "text",
                    "text": game_no,
                    "size": "sm",
                    "color": "#069142"
                })
                game_begin_flex_msg['body']['contents'].append({
                    "type": "text",
                    "text": "🎲牌局快訊🎲",
                    "size": "xxl",
                    "margin": "sm",
                    "align": "center"
                })
                game_begin_flex_msg['body']['contents'].append({
                    "type": "text",
                    "text": "有效下注玩家",
                    "align": "center",
                    "margin": "sm"
                })
                game_begin_flex_msg['body']['contents'].append({
                    "type": "separator"
                })
             
            
            bet_list = []
            a_list = [] 
            b_list = []
            c_list = []
            
            sql = "SELECT b.name,a.bet,a.betOn FROM betResult as a JOIN member as b ON a.memberId = b.memberId  WHERE a.gameId = '" + gameId + "' AND a.bet > 0 ORDER BY a.created_at"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            for meta in data:
                if( meta[2] == "banker" ):
                    bet_list.append({
                        "type": "text",
                        "text": "[" + meta[0] + "] " + '%d' % (meta[1]),
                        "margin": "sm",
                        "size": "sm"
                    })
                    banker_count += 1;
                    banker_bet_total = int(meta[1]);
                    
                    banker_fee = 300;
                if( meta[2] == "A" ):
                    a_list.append({
                        "type": "text",
                        "text": "[" + meta[0] + "] " + '%d' % (meta[1]),
                        "margin": "sm",
                        "size": "sm"
                    })
                    bet_count += 1;
                    a_total += int(meta[1]);
                    
                    
                if( meta[2] == "B" ):
                    b_list.append({
                        "type": "text",
                        "text": "[" + meta[0] + "] " + '%d' % (meta[1]),
                        "margin": "sm",
                        "size": "sm"
                    })
                    bet_count += 1;
                    b_total += int(meta[1]);
                    
                    
                if( meta[2] == "C" ):
                    c_list.append({
                        "type": "text",
                        "text": "[" + meta[0] + "] " + '%d' % (meta[1]),
                        "margin": "sm",
                        "size": "sm"
                    })
                    bet_count += 1;
                    c_total += int(meta[1]);
                    
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "莊",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_begin_flex_msg['body']['contents'] = game_begin_flex_msg['body']['contents'] + bet_list
            game_begin_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "A",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_begin_flex_msg['body']['contents'] = game_begin_flex_msg['body']['contents'] + a_list
            game_begin_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "B",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_begin_flex_msg['body']['contents'] = game_begin_flex_msg['body']['contents'] + b_list
            game_begin_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "C",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_begin_flex_msg['body']['contents'] = game_begin_flex_msg['body']['contents'] + c_list
            game_begin_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "莊金額",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "莊 | " + '%d' % (banker_bet_total),
                "margin": "sm",
                "size": "sm"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": '上莊費 | ' + str(banker_fee),
                "margin": "sm",
                "size": "sm"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": '押注總額 | ' + '%d' % (banker_bet_total + banker_fee),
                "margin": "sm",
                "size": "sm"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "押注 | " + str(banker_count),
                "margin": "sm",
                "size": "sm"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "下注總表",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "A門 | " + '%d' % (a_total),
                "margin": "sm",
                "size": "sm"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "B門 | " + '%d' % (b_total),
                "margin": "sm",
                "size": "sm"
            })
            
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "C門 | " + '%d' % (c_total),
                "margin": "sm",
                "size": "sm"
            })
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "押注總額 | " + '%d' % ( a_total + b_total + c_total ),
                "margin": "sm",
                "size": "sm"
            })
            game_begin_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "押注 | " + str(bet_count),
                "margin": "sm",
                "size": "sm"
            })
            
            dbcursor.close()
            self.mydb.close()
            
            
            return game_begin_flex_msg;
            #return reply_msg;
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            return False;
            
    def get_game_result( self, gameId ):
        reply_msg = '';
        try:
            banker_bet_total = 0;
            a_total = 0;
            b_total = 0;
            c_total = 0;
            bet_count = 0;
            banker_count = 0;
            member_bet_total = 0;
            
            A_is_win = '';
            B_is_win = '';
            C_is_win = '';
            
            game_result_flex_msg = {}
            game_result_flex_msg['type'] = 'bubble'
            game_result_flex_msg['body'] = {}
            game_result_flex_msg['body']['type'] = 'box'
            game_result_flex_msg['body']['layout'] = 'vertical'
            game_result_flex_msg['body']['contents'] = []
            
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT date,seq,resultA,resultB,resultC FROM gameResultTianjiu WHERE gameId = '" + gameId + "'"
            
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            for meta in data:
                game_no = "局號："+ str(meta[0]) + '#' + "{:03d}".format(meta[1])
                if( int(meta[1]) % 2 == 0 ):
                    game_no += '-下巡'
                else:
                    game_no += '-上巡'
                    
                game_result_flex_msg['body']['contents'].append({
                    "type": "text",
                    "text": game_no,
                    "size": "sm",
                    "color": "#069142"
                })
                game_result_flex_msg['body']['contents'].append({
                    "type": "text",
                    "text": "🎲開牌結果🎲",
                    "size": "xxl",
                    "margin": "sm",
                    "align": "center"
                })
                
                game_result_flex_msg['body']['contents'].append({
                    "type": "separator"
                })
            
                A_is_win = meta[2];
                B_is_win = meta[3];
                C_is_win = meta[4];
                
                
            a_msg = 'A';
            b_msg = 'B';
            c_msg = 'C';
            banker_msg = '莊';
            if( A_is_win == 'lose' ):
                a_msg += " 負";
            elif( A_is_win == 'draw' ):
                a_msg += " 和";
            else:
                a_msg += " 勝";
                
                
            if( B_is_win == 'lose' ):
                b_msg += " 負";
            elif( B_is_win == 'draw' ):
                b_msg += " 和";
            else:
                b_msg += " 勝"; 
                
            if( C_is_win == 'lose' ):
                c_msg += " 負";
            elif( C_is_win == 'draw' ):
                c_msg += " 和";
            else:
                c_msg += " 勝";
                
            bet_list = []
            a_list = [] 
            b_list = []
            c_list = []    
            
            sql = "SELECT b.name,a.win,a.betOn FROM betResult as a JOIN member as b ON a.memberId = b.memberId  WHERE a.gameId = '" + gameId + "' AND a.bet > 0 ORDER BY a.created_at"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            for meta in data:
                if( meta[2] == "banker" ):
                    bet_msg = "[" + meta[0] + "] " + '%d' % (meta[1])
                    banker_bet_total = int(meta[1]);
                    if( banker_bet_total > 0 ):
                        bet_msg += '🀄️';
                        
                    bet_list.append({
                        "type": "text",
                        "text": bet_msg,
                        "margin": "sm",
                        "size": "sm"
                    })
                    
                    banker_count += 1;
                    
                if( meta[2] == "A" ):
                    a_f_msg = "[" + meta[0] + "] " + '%d' % (meta[1])
                    if( A_is_win == 'win' ):
                        a_f_msg += '🀄️';
                        
                    a_list.append({
                        "type": "text",
                        "text": a_f_msg,
                        "margin": "sm",
                        "size": "sm"
                    })
                    
                    bet_count += 1;
                    a_total += int(meta[1]);
                   
                if( meta[2] == "B" ):
                    b_f_msg = "[" + meta[0] + "] " + '%d' % (meta[1])
                    if( B_is_win == 'win' ):
                        b_f_msg += '🀄️';
                        
                    b_list.append({
                        "type": "text",
                        "text": b_f_msg,
                        "margin": "sm",
                        "size": "sm"
                    })
                    bet_count += 1;
                    b_total += int(meta[1]);
                   
                if( meta[2] == "C" ):
                    c_f_msg = "[" + meta[0] + "] " + '%d' % (meta[1])
                    if( C_is_win == 'win' ):
                        c_f_msg += '🀄️';
                        
                    c_list.append({
                        "type": "text",
                        "text": c_f_msg,
                        "margin": "sm",
                        "size": "sm"
                    })
                
                    bet_count += 1;
                    c_total += int(meta[1]);
                    
            

            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "莊",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_result_flex_msg['body']['contents'] = game_result_flex_msg['body']['contents'] + bet_list
            game_result_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": a_msg,
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_result_flex_msg['body']['contents'] = game_result_flex_msg['body']['contents'] + a_list
            game_result_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": b_msg,
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_result_flex_msg['body']['contents'] = game_result_flex_msg['body']['contents'] + b_list
            game_result_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": c_msg,
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_result_flex_msg['body']['contents'] = game_result_flex_msg['body']['contents'] + c_list
            game_result_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "莊金額",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "莊 | " + '%d' % (banker_bet_total),
                "margin": "sm",
                "size": "sm"
            })
            
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": '莊家輸贏 | ' + '%d' % (banker_bet_total),
                "margin": "sm",
                "size": "sm"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "押注 | " + str(banker_count),
                "margin": "sm",
                "size": "sm"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "separator"
            })
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "輸嬴總表",
                "size": "md",
                "weight": "bold",
                "margin": "sm"
            })
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "A門 | " + '%d' % (a_total),
                "margin": "sm",
                "size": "sm"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "B門 | " + '%d' % (b_total),
                "margin": "sm",
                "size": "sm"
            })
            
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "C門 | " + '%d' % (c_total),
                "margin": "sm",
                "size": "sm"
            })
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "押注總額 | " + '%d' % ( a_total + b_total + c_total ),
                "margin": "sm",
                "size": "sm"
            })
            game_result_flex_msg['body']['contents'].append({
                "type": "text",
                "text": "押注 | " + str(bet_count),
                "margin": "sm",
                "size": "sm"
            })

            
            dbcursor.close()
            self.mydb.close()
            
            return game_result_flex_msg
            #return reply_msg;
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            return False;
           
    def get_bet_total( self, gameId ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT SUM(bet) FROM betResult WHERE gameId = '" + gameId + "' AND betOn != 'banker' "
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return 0
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
          
    def is_full_bet( self, gameId ):
        try:
            
            bet_total = self.get_bet_total( gameId );
            self.mydb.close() 
            bankerBet = self.get_game_meta( gameId, 'bankerBet' );
            self.mydb.close()
            can_bet_total = int(bankerBet) - int(bet_total);
            if( can_bet_total == 0 ):
                return True;
            else:
                return False;
            
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
    def is_banker( self, gameId, user_id ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT lineToken FROM member WHERE memberId IN (SELECT banker FROM gameResultTianjiu WHERE gameId = '" + gameId + "') "
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    if( meta[0] == user_id ):
                        return True;
                    else:
                        return False;
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            return False;
    
    def get_first_round_banker( self, seq, date ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT lineToken FROM member WHERE memberId IN (SELECT banker FROM gameResultTianjiu WHERE seq = '" + str(seq) + "' AND date ='" + date + "')"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
    def get_fist_game_id( self, seq, date ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT gameId FROM gameResultTianjiu WHERE seq = '" + str(seq) + "' AND date ='" + date + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
    def get_member_id_by_account( self, account ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT memberId FROM member WHERE account = '" + account + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return 'no_member'
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return 'no_member'
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
    def get_banker_lineToken( self, banker ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT lineToken FROM member WHERE memberId ='" + banker + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
    
    
    def get_game_id_by_date_seq( self, date, seq ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT gameId FROM gameResultTianjiu WHERE date = '" + str(date) + "' AND seq = '" + str(seq) + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return meta[0]
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
    def check_account( self, account ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT count(account) FROM member WHERE account = '" + account + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( meta[0] == 0 ):
                    return False
                else:
                    return True
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)
            
            
    def was_bind( self, account ):
        try:
            self.connect()
            dbcursor = self.mydb.cursor()
            sql = "SELECT lineToken FROM member WHERE account = '" + account + "'"
            dbcursor.execute(sql)
            data = dbcursor.fetchall()
            dbcursor.close()
            self.mydb.close()
            if not data:
                return False
            for meta in data:
                if( str(meta[0]) == 'None' ):
                    return False
                else:
                    return True
                
        except Exception as e:
            if self.mydb is not None:
                self.mydb.rollback()
            print(e)