考勤數據處理工具

以前是用易語言寫過,後來用python改寫了,但是是腳本運行模式沒有界面,再用wxPython改寫一遍。

#coding:utf-8
import os
import os.path
import random

import datetime
import pymysql
import wx
import os

class MyFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,title="考勤數據處理小助手",size=(300,300))
        
        panel = wx.Panel(self)
        
        self.bt_addinfo = wx.Button(panel,label="導入基礎數據",pos=(90,50))
        self.bt_addinfo.Bind(wx.EVT_BUTTON,self.OnclickAddInfo)
        
        self.bt_addpara = wx.Button(panel,label="導入人員參數",pos=(90,100))
        self.bt_addpara.Bind(wx.EVT_BUTTON,self.OnclickAddPara)
        
        self.bt_export = wx.Button(panel,label="導出結果數據",pos=(90,150))
        self.bt_export.Bind(wx.EVT_BUTTON,self.OnclickExport)
        
    def OnclickAddInfo(self,event):
        file_wildcard = "All files(*.*)|*.*" 
        dlg = wx.FileDialog(self,  "Save paint as ...",os.getcwd(),style = wx.FD_OPEN,wildcard = file_wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            #wx.MessageBox(filename)
            
            conn = pymysql.connect(host='127.0.0.1',user='root', passwd='123456',db='mysql')

            cur =conn.cursor()

            h_orig = open(filename,"r")

            sql = "insert into kaoqin_in_real_info values(%s,%s,%s,%s,%s)"
            updatesql = "update kaoqin_in_real_info set do_time=%s,do_alltime=%s where clr_date=%s and id=%s and status=%s" 

            for line in h_orig:
                line=line.strip('\n')
                #計算分割數
                i_num = line.count("    ")
                #print(i_num)

                id = line.split("    ")[0].strip()
                do_alltime = line.split("    ")[1]
                clr_date = do_alltime.split(" ")[0].replace('-','')
                do_alltime = do_alltime.replace('-','/')
                do_time = do_alltime.split(" ")[1]
                if ( int(do_time.split(":")[0]) > 15 ):
                    status = 2
                else:
                    status = 1

                find_sql = "select do_time from kaoqin_in_real_info where clr_date='" + clr_date +"' and id ='" +id  + "' and status='" + str(status) + "'"

                i_num = cur.execute(find_sql)
                #print(i_num)
                if (i_num == 0):
                    #新數據入庫
                    params=(clr_date,id,status,do_time,do_alltime)
                    reCount = cur.execute(sql,params)
                else:
                    #舊數據判斷是否更新
                    result = cur.fetchone()
                    if ( (status == 2 and result[0] > do_time)  or (status == 1 and result[0] < do_time) ):
                        params=(do_time,do_alltime,clr_date,id,status)
                        reCount = cur.execute(updatesql,params)
    
    
            h_orig.close()
            conn.commit()
            cur.close()
            conn.close()
            wx.MessageBox("基礎數據導入成功")
        dlg.Destroy()
        
    def OnclickAddPara(self,event):
        file_wildcard = "All files(*.*)|*.*" 
        dlg = wx.FileDialog(self,  "Save paint as ...",os.getcwd(),style = wx.FD_OPEN,wildcard = file_wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            
            conn = pymysql.connect(host='127.0.0.1',user='root', passwd='123456',db='mysql')

            cur =conn.cursor()

            h_orig = open(filename,"r")

            sql = "insert into kaoqin_person_para values(%s,%s,%s,%s,%s,%s,%s)"
            deletesql = "delete from kaoqin_person_para"

            cur.execute(deletesql)

            for line in h_orig:
                line=line.strip('\n')
                #計算分割數
                i_num = line.count("    ")
                #print(i_num)

                in_id = line.split("    ")[0].strip()
                out_id = line.split("    ")[1].strip()
                name = line.split("    ")[2].strip().encode('gb2312')
                begin_date = line.split("    ")[3].strip()
                end_date = line.split("    ")[4].strip()
                xiabanka_begin = line.split("    ")[5].strip()
                xiabanka_end = line.split("    ")[6].strip()
                
                params=(in_id,out_id,name,begin_date,end_date,xiabanka_begin,xiabanka_end)
                cur.execute(sql,params)
    
            h_orig.close()
            conn.commit()
            cur.close()
            conn.close()
            
            wx.MessageBox("參數數據導入成功")
        dlg.Destroy()
        
    def OnclickExport(self,event):
        file_wildcard = "All files(*.*)|*.*" 
        dlg = wx.FileDialog(self,  "Save paint as ...",os.getcwd(),style = wx.FD_SAVE,wildcard = file_wildcard)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            
            conn = pymysql.connect(host='127.0.0.1',user='root', passwd='123456',db='mysql')
            cur =conn.cursor()
            h_result = open(filename,"w")

            fetch_sql = "select * from kaoqin_person_para";

            cur.execute(fetch_sql)
            results = cur.fetchall()

            for row in results:
                id = row[0]
                name = row[2]
                begin_time = row[3]
                end_time = row[4]

                xiabanka_begin = int(row[5])
                xiabanka_end = int(row[6])
                if (xiabanka_begin < 18):
                    xiabanka_begin = 21
                if (xiabanka_begin < 18):
                    xiabanka_begin = 22
   
                datestart = datetime.datetime.strptime(begin_time,'%Y%m%d')
                dateend = datetime.datetime.strptime(end_time,'%Y%m%d')
                while datestart <= dateend:
                    clr_date = datestart.strftime("%Y%m%d")
                    #print("now is do ",id,clr_date)
                    for status in range(1,3):
                        find_sql = "select * from kaoqin_in_real_info where clr_date='" + clr_date+"' and id='"+ id +"' and status='"+ str(status) +"'"
                        #print(find_sql)
                        i_num = cur.execute(find_sql)
                        if (i_num == 0):
                            #print("not find ",clr_date,id,status)
                            if (status == 1):
                                dakatime_h = str(random.randint(8,9))
                                if (dakatime_h == '9'):
                                    dakatime_h = '8'
                                    dakatime_f = random.randint(20,50)
                                else:
                                    dakatime_f = random.randint(40,59)
                            else:
                                dakatime_h = str(random.randint(xiabanka_begin,xiabanka_end))
                                dakatime_f = random.randint(1,59)

                            dakatime_m = random.randint(1,59)
                            all_time = clr_date[0:4]  + "/" + clr_date[4:6]  + "/" + clr_date[6:8] + " " +  dakatime_h +":" + '%02d' % dakatime_f + ":" + '%02d' % dakatime_m

                            writeline = id + "," + all_time + "," + clr_date[0:4]  + "/" + clr_date[4:6]  + "/" + clr_date[6:8] + ",foolall\n"
                            h_result.write(writeline)
                        else:
                            data = cur.fetchone()
                            #print("have find ",clr_date,id,status)
                            real_all_time = data[4].split(" ")[1]
                            #shangwu daka jilu
                            if (status == 1):
                                if (real_all_time[0:2] != '08'):
                                    dakatime_f = random.randint(50,59)
                                    change_all_time = '08' + ":" + str(dakatime_f) + ":" +real_all_time[6:8]
                                    real_all_time = data[4].split(" ")[0] + " " + change_all_time
                                    writeline = id + "," + real_all_time + "," + clr_date[0:4]  + "/" + clr_date[4:6]  + "/" + clr_date[6:8] +  ",foolmoring\n"
                                else:
                                    writeline = id + "," + data[4] + "," + clr_date[0:4]  + "/" + clr_date[4:6]  + "/" + clr_date[6:8] +  ",real\n"
                            else:       
                                writeline = id + "," + data[4] + "," + clr_date[0:4]  + "/" + clr_date[4:6]  + "/" + clr_date[6:8] +  ",real\n"
                            h_result.write(writeline)

                    datestart+=datetime.timedelta(days=1)

            h_result.close()
            conn.commit()
            cur.close()
            conn.close()
            wx.MessageBox("結果數據導出成功")
        dlg.Destroy()
      

  
if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()


    

######################################################

#生成exe不帶cmd窗口需要帶上-w參數  

pyinstaller -w xxxxxx.py


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章