import time
import pymysql as psql
import message_pb2 as message
import  mysqlhelper
class DBSeacherPickCmd():
    def __init__(self,ip,port,database,user,password):
        self.ip=ip
        self.port=port
        self.database=database
        self.user=user
        self.password=password
        self._close=False
        self._opendoor_callback=None
        self.db = mysqlhelper.MySqLHelper()
        # self.conn=psql.connect(host=ip,
        #                        port=port,
        #                        database=database,
        #                        charset="utf8",
        #                        user=user,
        #                        passwd=password)

    def close(self):
        self._close=True
        self.join()
        # self.conn.close()
    def search_pickcmd(self):
        sql = "select * from command_queue where statu=2 "
        return self.db.selectall(sql)

        # self.conn.begin()
        # cursor=self.conn.cursor()
        # SQL="select * from command_queue where statu=2 "    #寻找已到出口的取车指令
        # cursor.execute(SQL)
        # pick_cmds=cursor.fetchall()
        # self.conn.commit()
        # cursor.close()

        # return pick_cmds

    def delete_cmd(self,id):
        sql = "delete from command_queue where statu=2 and export_id=%s"
        return self.db.delete(sql,id)

        # self.conn.begin()
        # cursor=self.conn.cursor()
        # SQL="delete from command_queue where statu=2 and export_id=%d"%id
        # cursor.execute(SQL)
        # self.conn.commit()
        # cursor.close()

'''
    def run(self):
        while self._close==False:
            cmds=self.search_pickcmd()
            
            for cmd in cmds:
                if len(cmd)>=9:
                    table=message.pick_table()
                    table.car_number=cmd[0]
                    table.primary_key=cmd[1]
                    table.unit=cmd[2]
                    table.queue_id=cmd[3]
                    table.type=cmd[4]
                    table.statu=cmd[5]
                    table.export_id=cmd[8]
                    if not table.export_id==None:
                        self._opendoor_callback(table.export_id,table)

            time.sleep(1)

'''
