def leader():
    #前置處理
    
    #設定應存放資料表
    global newSQLlistname
    newSQLlistname=datetime.datetime.now().strftime('%Y-%m-%d')
    
    #確定應存放資料表存在與否,決定做選取或是新增
    try:
        SQL.newlist(newSQLlistname)
        print "創新資料表:"+newSQLlistname
    except:
        SQL.clear(newSQLlistname)        
        print "重製資料表:"+newSQLlistname
        
    #更新開啟(重製)程式時間
    global startday
    startday = datetime.datetime.now().strftime('%d')
    global starttime
    starttime = datetime.datetime.now().strftime('%H:%M:%S')
    
    #清除主測表中過時(非今日)資訊
    '''
    print str(fetchdata[0][1].day)
    print str(datetime.datetime.now().day)
    '''
    fetchdata = SQL.catchdbfetch(1)
    if  SQL.catchidnum()!=0:
        if  fetchdata[0][1].day!=datetime.datetime.now().day:
            SQL.clear('detect')
            print '清除過時資訊'
def savepic(savepic,savepicid,pnum):#(儲存影像,影像編號,儲存路徑)
    #若圖片人數為0則代表進入循環末端處理
    if pnum != 0:
    #如為正常儲存程序(非進入循環末端處理)時,為保持固定秒數儲存的功能,進入判定是否應新儲存圖像
        
        #更新現在時間
        nowtime = datetime.datetime.now()
        datename = datetime.datetime.now().strftime('%Y-%m-%d')
        
        #確認間隔時間
        saveintervaltime =  9
        f=SQL.catchdbfetch(SQL.catchidnum())
        
        
        #讀取上次儲存時間
        global lastsavecount
        global piccount
        if SQL.catchidnum()==0:
            print '第一筆資料'
        #若上次儲存時間至今已超過"圖像儲存間隔時間",則進入接續進入儲存程序,不滿條件則跳出
        elif (piccount-lastsavecount)<saveintervaltime :
            if int(pnum) > int(f[0][3]):
                '''
                print piccount
                print lastsavecount
                print '現在日期'+str(pnum)
                print 'id: '+str(SQL.catchidnum())
                print '上次人數'+str(f[0][3])
                print '時間名子'+str(datename)
                '''
                print '-'
                SQL.updatef(datename,pnum,SQL.catchidnum())
            return
                
            
        #更新上次儲存時間
        lastsavecount = piccount

    #合併儲存路徑
    savedir = config.savedir+config.savename+' ('+str(savepicid)+').'+config.savetype

    #儲存圖像至本機端
    pichost.save(savepic,savedir)

    #儲存圖像至SQL
    SQL.insertpythondetect_frompic(savedir,savepicid,pnum)
    
    #開啟倒數機制(循環末端處理)
    global countdownhandle
    countdownhandle=1
def main(index,s,e,sel):
    tables = SQL.tables()
    #by Wangchunshan
    print "選擇資料表為:"+str(tables[index][0])
    leader(str(tables[index][0]))
    #set var
    ALLcount = 0
    addtime =""
    lesstime = ""
    s1=0
    s2=0
    s3=0
    s4=0
    addtimearray = []
    lesstimearray = []
    addtimearraypicname = []
    lesstimearraypicname = []
    firsttime = ""
    endtime =""
    detecttime=0#觀測時間
    SSTime = []
    SETime = []
    selecttimerange = sel   
    #選擇的時間範圍-開始
    '''
    SSTime[0]=selecttimerange[:2]
    SSTime[1]=selecttimerange[3:5]
    SSTime[2]=selecttimerange[6:8]
    SSTime[3]=selecttimerange[9:11]
    #選擇的時間範圍-結束
    SETime[0]=selecttimerange[12:14]
    SETime[1]=selecttimerange[15:17]
    SETime[2]=selecttimerange[18:20]
    SETime[3]=selecttimerange[21:23]
    '''
    
    fetchdata = SQL.catchdbfetch(1)
    #將文字轉為日期形式
    SSTime.append(int(selecttimerange[:2]))
    SSTime.append(int(selecttimerange[3:5]))
    SSTime.append(int(selecttimerange[6:8]))
    SSTime.append(int(selecttimerange[9:11]))
    SSTime_=datetime.datetime(fetchdata[0][1].year,fetchdata[0][1].month,fetchdata[0][1].day,SSTime[0],SSTime[1],SSTime[2],SSTime[3])
    
    SETime.append(int(selecttimerange[12:14]))
    SETime.append(int(selecttimerange[15:17]))
    SETime.append(int(selecttimerange[18:20]))
    SETime.append(int(selecttimerange[21:23]))
    SETime_=datetime.datetime(fetchdata[0][1].year,fetchdata[0][1].month,fetchdata[0][1].day,SETime[0],SETime[1],SETime[2],SETime[3])
    
    #print SSTime
    #print SETime
    #資料庫
    #
    # 
    for x in SQL.searchdocument(str(tables[index][0])):
        detecttime+=int(x[2])
    
    for x in range(SQL.catchidnum()):
        #By Wangchunshan
        #讀取單筆資料 [0][2] =>路徑 [0][1]=>時間 
        fetchdata = SQL.catchdbfetch(x+1)
        if fetchdata[0][1]> SSTime_ and fetchdata[0][1]<SETime_:    

            if 0xFF & cv2.waitKey(5) == 27:
                break
            #By Wangchunshan

        #!  #讀取此圖像中可判別的臉數
            count = fetchdata[0][3]
            print count

           
            #提示讀取圖像id與判別後數值
            #print '圖像id:'+str(fetchdata[0][0])+' ,圖片路徑:'+fetchdata[0][2]+' ,有'+str(count)+'張臉'
            
            #終端處理
        #!  #更新最新時間點圖像之臉數        
            count_after = count

            #確定人數增加時間點
            if count_after>s1  :
                
               #總觀看人數增加
               if (count_after-s1)==(count_after-s2) and (count_after-s1)==(count_after-s3):
                   ALLcount+=(count_after-s1)               
                   errorpop = ""  
               else:
                   errorpop = "\n***發現誤差值可能***\n照片編號:"+str(fetchdata[0][0])+"\n"    
               
               for p in range(count_after-s1):
                   addtimearray.append(fetchdata[0][1])           
                   addtimearraypicname.append(fetchdata[0][0])
               #紀錄增加時間點與增加人數
               addtime += errorpop+str(fetchdata[0][1])+",增加人數為:"+str(count_after-s1)+"\n"
               #紀錄開始時間
               if firsttime =="":
                    firsttime = fetchdata[0][1]
            
            #確定人數減少時間點
            if count_after<s1:            
               for p in range((count_after-s1)*-1):
                   lesstimearray.append(fetchdata[0][1])              
                   lesstimearraypicname.append(fetchdata[0][0])
               lesstime += str(fetchdata[0][1])+",減少人數為:"+str((count_after-s1)*-1)+"\n"
               endtime = fetchdata[0][1]
            s4 = s3
            s3 = s2
            s2 = s1
            s1 = count_after 
            #資料解析

    #輸出 "增加人數時間點" 與 "人數減少時間點"之情況   
    #print '\n\n\n增加人數時間點\n'+addtime+'\n\n\n人數減少時間點\n'+lesstime
    errortime = 0
    allwatch = 0
    #print len(addtimearray)
    #print len(lesstimearray)
    print '\n\n'
    print '計算完成,開始輸出結果'
    print '\n\n'
    for x in range(len(addtimearray)):        
        if len(addtimearray)>=x+1 and len(lesstimearray)>=x+1:
            print '第'+str(x+1)+'位觀看者\n從'+str(addtimearray[x])+'開始觀看,至'+str(lesstimearray[x])+'結束觀看'
            print '注視時間長度為:'+str((lesstimearray[x]-addtimearray[x]).seconds)+"秒"
            print '判別增加.照片編號為:'+str(addtimearraypicname[x])+',判別減少.照片編號為:'+str(lesstimearraypicname[x])
            print '\n\n'
            allwatch += int((lesstimearray[x]-addtimearray[x]).seconds)
                      
        else:
            errortime+=1
    if ALLcount==0:
        print "時間區隔中總觀看人次為零"
    else:
        
        #邏輯錯誤計算 預測情況:離開事件發生數大於進入事件發生數
        #print '錯誤次數'+str(errortime)+"\n"
        
        print "今日總偵測時間:"+str(detecttime)+"秒" 
        print "選擇時間區隔為:"+str(selecttimerange)+"\n時間區隔共有"+str((SETime_-SSTime_).seconds)+"秒" 
        print "時間區隔中總觀看人次:"+str(ALLcount)       
        print "時間區隔中總被觀看時間(每人次注視時間相加總):"+str(allwatch)+"秒"
        print "時間區隔中平均每人次觀看時間:"+str(allwatch/ALLcount)+"秒"        
        
        
        
        
    #s=raw_input('Want More Detailed?Y or N :')
    s="Y"
    if(s=="Y"):
        #main(index,s,e)
        s="x"
        
    
    
    print
    print
    print "          ---------------------------------------------------"
    print
    print
    print