#encoding:utf-8
#!/usr/local/bin/python2.7
from __future__ import division
import MySQLdb as mdb
import re

def myAdd(x,y):
    return x+y

def myMin(x,y):
    return x-y

def preZjq(cur,scr,w,d,l,a):
    print \"======总进球预测======\"
    sql_wd = \"select zjq,count(*) from results where win between %s and %s and dog between %s and %s group by zjq order by count(*)\" % (myMin(w,a),myAdd(w,a),myMin(d,a),myAdd(d,a))
    sql_dl = \"select zjq,count(*) from results where dog between %s and %s and los between %s and %s group by zjq order by count(*)\" % (myMin(d,a),myAdd(d,a),myMin(l,a),myAdd(l,a))
    sql_wl = \"select zjq,count(*) from results where win between %s and %s or los between %s and %s group by zjq order by count(*)\" % (myMin(w,a),myAdd(w,a),myMin(l,a),myAdd(l,a))
    temp_wd=[\"win&dog\"+\"\\n\"]
    temp_wd.append((\"场次:%s \\n\") % str(scr))
    temp_wd.append((\"win:%s  dog:%s  los:%s  acc:%s\"+\"\\n\") % (str(w),str(d),str(l),str(a)))
    temp_dl=[\"win&dog\"+\"\\n\"]
    temp_dl.append((\"场次:%s \\n\") % str(scr))
    temp_dl.append((\"win:%s  dog:%s  los:%s  acc:%s\"+\"\\n\") % (str(w),str(d),str(l),str(a)))
    temp_wl=[\"win&dog\"+\"\\n\"]
    temp_wl.append((\"场次:%s \\n\") % str(scr))
    temp_wl.append((\"win:%s  dog:%s  los:%s  acc:%s\"+\"\\n\") % (str(w),str(d),str(l),str(a)))

    f = open(\"/root/bet/prediction.txt\",\'a\')

    sum_wd=0
    #wd_alldata:前4个进球数以及比例
    wd_alldata=[]
    #将SQL的所有结果保存到文件中
    cur.execute(sql_wd)
    for item in cur.fetchall():
        temp_wd_str1=str(item[0])+\"球,\"
        temp_wd.append(temp_wd_str1)
        temp1_wd=str(item[1])
        sum_wd+=item[1]
        temp_wd_str2 = re.search(\"\\d{1,3}\",temp1_wd).group()
        temp_wd.append(temp_wd_str2+\'\\n\')
    temp_wd.append(\"总共:\"+str(sum_wd)+\"场\")
    temp_wd.append(\'\\n\\n\\n\')
    cur.execute(sql_wd)
    for d in cur.fetchall():
        wd_alldata.append(str(d[0]))
        tmp4=str(d[1])
        temp_srt3 = re.search(\"\\d{1,3}\",tmp4).group()
        wd_alldata.append(temp_srt3)
    for item in temp_wd:
        f.writelines(str(item))
    l = len(wd_alldata)
    print (\"场次: %s \\n主赔+平陪-->\"+\"一共 \"+str(sum_wd)+\"场\") % str(scr)
    if l==2:
        temp_wd_alldata = wd_alldata[-2:]
        print str((temp_wd_alldata[0])+\"球:\")+str(format(int(temp_wd_alldata[1])/sum_wd,\".2%\"))
    elif l==4:
        temp_wd_alldata = wd_alldata[-4:]
        print str((temp_wd_alldata[0])+\"球:\")+str(format(int(temp_wd_alldata[1])/sum_wd,\".2%\"))
        print str((temp_wd_alldata[2])+\"球:\")+str(format(int(temp_wd_alldata[3])/sum_wd,\".2%\"))
    elif l>=6:
        temp_wd_alldata = wd_alldata[-6:]
        print str((temp_wd_alldata[0])+\"球:\")+str(format(int(temp_wd_alldata[1])/sum_wd,\".2%\"))
        print str((temp_wd_alldata[2])+\"球:\")+str(format(int(temp_wd_alldata[3])/sum_wd,\".2%\"))
        print str((temp_wd_alldata[4])+\"球:\")+str(format(int(temp_wd_alldata[5])/sum_wd,\".2%\"))

    #平负
    sum_dl=0
    #dl_alldata:前4个进球数以及比例
    dl_alldata=[]
    #将SQL的所有结果保存到文件中
    cur.execute(sql_dl)
    for item in cur.fetchall():
        temp_dl_str1=str(item[0])+\"球,\"
        temp_dl.append(temp_dl_str1)
        temp1_dl=str(item[1])
        sum_dl+=item[1]
        temp_dl_str2 = re.search(\"\\d{1,3}\",temp1_dl).group()
        temp_dl.append(temp_dl_str2+\'\\n\')
    temp_dl.append(\"总共:\"+str(sum_dl)+\"场\")
    temp_dl.append(\'\\n\\n\\n\')
    cur.execute(sql_dl)
    for d in cur.fetchall():
        dl_alldata.append(str(d[0]))
        tmp4=str(d[1])
        temp_srt3 = re.search(\"\\d{1,3}\",tmp4).group()
        dl_alldata.append(temp_srt3)
    for item in temp_dl:
        f.writelines(str(item))
    l = len(dl_alldata)
    print (\"\\n平赔+负陪-->\"+\"一共 \"+str(sum_dl)+\"场\")
    if l==2:
        temp_dl_alldata = dl_alldata[-2:]
        print str((temp_dl_alldata[0])+\"球:\")+str(format(int(temp_dl_alldata[1])/sum_dl,\".2%\"))
    elif l==4:
        temp_dl_alldata = dl_alldata[-4:]
        print str((temp_dl_alldata[0])+\"球:\")+str(format(int(temp_dl_alldata[1])/sum_dl,\".2%\"))
        print str((temp_dl_alldata[2])+\"球:\")+str(format(int(temp_dl_alldata[3])/sum_dl,\".2%\"))
    elif l>=6:
        temp_dl_alldata = dl_alldata[-6:]
        print str((temp_dl_alldata[0])+\"球:\")+str(format(int(temp_dl_alldata[1])/sum_dl,\".2%\"))
        print str((temp_dl_alldata[2])+\"球:\")+str(format(int(temp_dl_alldata[3])/sum_dl,\".2%\"))
        print str((temp_dl_alldata[4])+\"球:\")+str(format(int(temp_dl_alldata[5])/sum_dl,\".2%\"))

    #胜负
    sum_wl=0
    #wl_alldata:前4个进球数以及比例
    wl_alldata=[]
    #将SQL的所有结果保存到文件中
    cur.execute(sql_wl)
    for item in cur.fetchall():
        temp_wl_str1=str(item[0])+\"球,\"
        temp_wl.append(temp_wl_str1)
        temp1_wl=str(item[1])
        sum_wl+=item[1]
        temp_wl_str2 = re.search(\"\\d{1,3}\",temp1_wl).group()
        temp_wl.append(temp_wl_str2+\'\\n\')
    temp_wl.append(\"总共:\"+str(sum_wl)+\"场\")
    temp_wl.append(\'\\n\\n\\n\')
    cur.execute(sql_wl)
    for d in cur.fetchall():
        wl_alldata.append(str(d[0]))
        tmp4=str(d[1])
        temp_srt3 = re.search(\"\\d{1,3}\",tmp4).group()
        wl_alldata.append(temp_srt3)
    for item in temp_wl:
        f.writelines(str(item))
    l = len(wl_alldata)
    print (\"\\n主赔+负陪-->\"+\"一共 \"+str(sum_wl)+\"场\")
    if l==2:
        temp_wl_alldata = wl_alldata[-2:]
        print str((temp_wl_alldata[0])+\"球:\")+str(format(int(temp_wl_alldata[1])/sum_wl,\".2%\"))
    elif l==4:
        temp_wl_alldata = wl_alldata[-4:]
        print str((temp_wl_alldata[0])+\"球:\")+str(format(int(temp_wl_alldata[1])/sum_wl,\".2%\"))
        print str((temp_wl_alldata[2])+\"球:\")+str(format(int(temp_wl_alldata[3])/sum_wl,\".2%\"))
    elif l>=6:
        temp_wl_alldata = wl_alldata[-6:]
        print str((temp_wl_alldata[0])+\"球:\")+str(format(int(temp_wl_alldata[1])/sum_wl,\".2%\"))
        print str((temp_wl_alldata[2])+\"球:\")+str(format(int(temp_wl_alldata[3])/sum_wl,\".2%\"))
        print str((temp_wl_alldata[4])+\"球:\")+str(format(int(temp_wl_alldata[5])/sum_wl,\".2%\"))
    print \"============\"
    f.close()

def preSpf(cur,scr,w,d,l,a):
    print \"======胜平负预测======\"
    sql_spf_wd = \"select spf,count(*) from results where win between %s and %s and dog between %s and %s group by spf order by count(*)\" % (myMin(w,a),myAdd(w,a),myMin(d,a),myAdd(d,a))
    sql_spf_dl = \"select spf,count(*) from results where dog between %s and %s and los between %s and %s group by spf order by count(*)\" % (myMin(d,a),myAdd(d,a),myMin(l,a),myAdd(l,a))
    sql_spf_wl = \"select spf,count(*) from results where win between %s and %s or los between %s and %s group by spf order by count(*)\" % (myMin(w,a),myAdd(w,a),myMin(l,a),myAdd(l,a))

    temp_spf_wd = []
    temp_spf_dl = []
    temp_spf_wl = []
    temp = []
    #添加场次信息
    temp.append((\"场次:%s \\n\") % str(scr))
    #添加赔率信息
    temp.append((\"win:%s  dog:%s  los:%s  acc:%s\"+\"\\n\") % (str(w),str(d),str(l),str(a)))

    #写入到文件Precdiction.txt
    f = open(\"/root/bet/prediction.txt\",\'a\')

    #1.对于sql_spf_wd的输出进行处理
    cur.execute(sql_spf_wd)
    #统计胜平负场次总和
    sum_spf_wd=0
    #将SQL的所有结果保存到文件中
    for item in cur.fetchall():
        temp_spf_wd.append(str(item[0]))
        temp_spf_wd.append(str(item[1]))
        sum_spf_wd+=(item[1])
    #收集胜平负的场次,并求百分比
    print \"胜平-->共\"+str(sum_spf_wd)+\"场\"
    #统计胜平负的比例
    cur.execute(sql_spf_wd)
    l = len(temp_spf_wd)
    print (\"场次: %s \\n主赔+平陪-->\"+\"一共 \"+str(sum_spf_wd)+\"场\") % str(scr)
    if l==2:
        temp_spf_wd = temp_spf_wd[-2:]
        print str((temp_spf_wd[0])+\":\")+str(format(int(temp_spf_wd[1])/sum_spf_wd,\".2%\"))
    elif l==4:
        temp_spf_wd = temp_spf_wd[-4:]
        print str((temp_spf_wd[0])+\":\")+str(format(int(temp_spf_wd[1])/sum_spf_wd,\".2%\"))
        print str((temp_spf_wd[2])+\":\")+str(format(int(temp_spf_wd[3])/sum_spf_wd,\".2%\"))
    elif l>=6:
        temp_spf_wd = temp_spf_wd[-6:]
        print str((temp_spf_wd[0])+\":\")+str(format(int(temp_spf_wd[1])/sum_spf_wd,\".2%\"))
        print str((temp_spf_wd[2])+\":\")+str(format(int(temp_spf_wd[3])/sum_spf_wd,\".2%\"))
        print str((temp_spf_wd[4])+\":\")+str(format(int(temp_spf_wd[5])/sum_spf_wd,\".2%\"))

    #2.对于sql_spf_dl的输出进行处理
    cur.execute(sql_spf_dl)
    #统计胜平负场次总和
    sum_spf_dl=0
    #将SQL的所有结果保存到文件中
    for item in cur.fetchall():
        temp_spf_dl.append(str(item[0]))
        temp_spf_dl.append(str(item[1]))
        sum_spf_dl+=(item[1])
    #统计胜平负的比例
    cur.execute(sql_spf_dl)
    l = len(temp_spf_dl)
    print (\"\\n平陪+负赔-->\"+\"一共 \"+str(sum_spf_dl)+\"场\")
    if l==2:
        temp_spf_dl = temp_spf_dl[-2:]
        print str((temp_spf_dl[0])+\":\")+str(format(int(temp_spf_dl[1])/sum_spf_dl,\".2%\"))
    elif l==4:
        temp_spf_dl = temp_spf_dl[-4:]
        print str((temp_spf_dl[0])+\":\")+str(format(int(temp_spf_dl[1])/sum_spf_dl,\".2%\"))
        print str((temp_spf_dl[2])+\":\")+str(format(int(temp_spf_dl[3])/sum_spf_dl,\".2%\"))
    elif l>=6:
        temp_spf_dl = temp_spf_dl[-6:]
        print str((temp_spf_dl[0])+\":\")+str(format(int(temp_spf_dl[1])/sum_spf_dl,\".2%\"))
        print str((temp_spf_dl[2])+\":\")+str(format(int(temp_spf_dl[3])/sum_spf_dl,\".2%\"))
        print str((temp_spf_dl[4])+\":\")+str(format(int(temp_spf_dl[5])/sum_spf_dl,\".2%\"))

    #3.对于sql_spf_wl的输出进行处理
    cur.execute(sql_spf_wl)
    #统计胜平负场次总和
    sum_spf_wl=0
    #将SQL的所有结果保存到文件中
    for item in cur.fetchall():
        temp_spf_wl.append(str(item[0]))
        temp_spf_wl.append(str(item[1]))
        sum_spf_wl+=(item[1])
    #统计胜平负的比例
    cur.execute(sql_spf_wl)
    l = len(temp_spf_wl)
    print (\"\\n主陪+负赔-->\"+\"一共 \"+str(sum_spf_wl)+\"场\")
    if l==2:
        temp_spf_wl = temp_spf_wl[-2:]
        print str((temp_spf_wl[0])+\":\")+str(format(int(temp_spf_wl[1])/sum_spf_wl,\".2%\"))
    elif l==4:
        temp_spf_wl = temp_spf_wl[-4:]
        print str((temp_spf_wl[0])+\":\")+str(format(int(temp_spf_wl[1])/sum_spf_wl,\".2%\"))
        print str((temp_spf_wl[2])+\":\")+str(format(int(temp_spf_wl[3])/sum_spf_wl,\".2%\"))
    elif l>=6:
        temp_spf_wl = temp_spf_wl[-6:]
        print str((temp_spf_wl[0])+\":\")+str(format(int(temp_spf_wl[1])/sum_spf_wl,\".2%\"))
        print str((temp_spf_wl[2])+\":\")+str(format(int(temp_spf_wl[3])/sum_spf_wl,\".2%\"))
        print str((temp_spf_wl[4])+\":\")+str(format(int(temp_spf_wl[5])/sum_spf_wl,\".2%\"))
    print \"============\"
    f.close()

#=========================================
#比分预测   
def preRes(cur,scr,w,d,l,a):
    sql_res_wd = \"select res,count(*) from results where win between %s and %s and dog between %s and %s group by res order by count(*)\" % (myMin(w,a),myAdd(w,a),myMin(d,a),myAdd(d,a))
    sql_res_dl = \"select res,count(*) from results where dog between %s and %s and los between %s and %s group by res order by count(*)\" % (myMin(d,a),myAdd(d,a),myMin(l,a),myAdd(l,a))
    sql_res_wl = \"select res,count(*) from results where win between %s and %s or los between %s and %s group by res order by count(*)\" % (myMin(w,a),myAdd(w,a),myMin(l,a),myAdd(l,a))

    #统计比分的场次之和
    sum_res_wd = 0
    sum_res_dl = 0
    sum_res_wl = 0

    #暂存保存到文件的信息
    temp_res_wd = []
    temp_res_dl = []
    temp_res_wl = []
    #添加场次信息
    temp_res_wd.append((\"场次:%s \\n\") % str(scr))
    #添加赔率信息
    temp_res_wd.append((\"win:%s  dog:%s  los:%s  acc:%s\"+\"\\n\") % (str(w),str(d),str(l),str(a)))
    #对于总共收集的场次
    sum_res = 0
    #对于比分以及场次进行收集
    all_res_data=[]

    #写入到文件Precdiction.txt
    f = open(\"/root/bet/prediction.txt\",\'a\')

    print \"======比分预测======\"

    #1.对于sql_res_wd的输出进行处理
    cur.execute(sql_res_wd)
    #将SQL的所有结果保存到文件中
    for item in cur.fetchall():
        temp_res_wd.append(str(item[0]))
        temp_res_wd.append(str(item[1]))
        sum_res_wd+=item[1]
    print \"\\n胜平预测-->共\"+str(sum_res_wd)+\"场\"
    #统计比分比例
    cur.execute(sql_res_wd)
    l = len(temp_res_wd)
    if l==4:
        temp_res_wd2 = temp_res_wd[-2:]
        print str((temp_res_wd2[0])+\"==》\")+str(format(int(temp_res_wd2[1])/sum_res_wd,\".2%\"))
    elif l==6:
        temp_res_wd2 = temp_res_wd[-4:]
        print str((temp_res_wd2[0])+\"==》\")+str(format(int(temp_res_wd2[1])/sum_res_wd,\".2%\"))
        print str((temp_res_wd2[2])+\"==》\")+str(format(int(temp_res_wd2[3])/sum_res_wd,\".2%\"))
    elif l>6:
        temp_res_wd2 = temp_res_wd[-8:]
        print str((temp_res_wd2[0])+\"==》\")+str(format(int(temp_res_wd2[1])/sum_res_wd,\".2%\"))
        print str((temp_res_wd2[2])+\"==》\")+str(format(int(temp_res_wd2[3])/sum_res_wd,\".2%\"))
        print str((temp_res_wd2[4])+\"==》\")+str(format(int(temp_res_wd2[5])/sum_res_wd,\".2%\"))
        print str((temp_res_wd2[6])+\"==》\")+str(format(int(temp_res_wd2[7])/sum_res_wd,\".2%\"))

    #2.对于sql_res_dl的输出进行处理
    cur.execute(sql_res_dl)
    #将SQL的所有结果保存到文件中
    for item in cur.fetchall():
        temp_res_dl.append(str(item[0]))
        temp_res_dl.append(str(item[1]))
        sum_res_dl+=item[1]
    print \"\\n平负预测-->共\"+str(sum_res_dl)+\"场\"
    #统计比分比例
    cur.execute(sql_res_dl)
    l = len(temp_res_dl)
    if l==4:
        temp_res_dl2 = temp_res_dl[-2:]
        print str((temp_res_dl2[0])+\"==》\")+str(format(int(temp_res_dl2[1])/sum_res_dl,\".2%\"))
    elif l==6:
        temp_res_dl2 = temp_res_dl[-4:]
        print str((temp_res_dl2[0])+\"==》\")+str(format(int(temp_res_dl2[1])/sum_res_dl,\".2%\"))
        print str((temp_res_dl2[2])+\"==》\")+str(format(int(temp_res_dl2[3])/sum_res_dl,\".2%\"))
    elif l>6:
        temp_res_dl2 = temp_res_dl[-8:]
        print str((temp_res_dl2[0])+\"==》\")+str(format(int(temp_res_dl2[1])/sum_res_dl,\".2%\"))
        print str((temp_res_dl2[2])+\"==》\")+str(format(int(temp_res_dl2[3])/sum_res_dl,\".2%\"))
        print str((temp_res_dl2[4])+\"==》\")+str(format(int(temp_res_dl2[5])/sum_res_dl,\".2%\"))
        print str((temp_res_dl2[6])+\"==》\")+str(format(int(temp_res_dl2[7])/sum_res_dl,\".2%\"))

    #3.对于sql_res_wl的输出进行处理
    cur.execute(sql_res_wl)
    #将SQL的所有结果保存到文件中
    for item in cur.fetchall():
        temp_res_wl.append(str(item[0]))
        temp_res_wl.append(str(item[1]))
        sum_res_wl+=item[1]
    print \"\\n胜负预测-->共\"+str(sum_res_wl)+\"场\"
    #统计比分比例
    cur.execute(sql_res_wl)
    l = len(temp_res_wl)
    if l==4:
        temp_res_wl2 = temp_res_wl[-2:]
        print str((temp_res_wl2[0])+\"==》\")+str(format(int(temp_res_wl2[1])/sum_res_wl,\".2%\"))
    elif l==6:
        temp_res_wl2 = temp_res_wl[-4:]
        print str((temp_res_wl2[0])+\"==》\")+str(format(int(temp_res_wl2[1])/sum_res_wl,\".2%\"))
        print str((temp_res_wl2[2])+\"==》\")+str(format(int(temp_res_wl2[3])/sum_res_wl,\".2%\"))
    elif l>6:
        temp_res_wl2 = temp_res_wl[-12:]
        print str((temp_res_wl2[0])+\"==》\")+str(format(int(temp_res_wl2[1])/sum_res_wl,\".2%\"))
        print str((temp_res_wl2[2])+\"==》\")+str(format(int(temp_res_wl2[3])/sum_res_wl,\".2%\"))
        print str((temp_res_wl2[4])+\"==》\")+str(format(int(temp_res_wl2[5])/sum_res_wl,\".2%\"))
        print str((temp_res_wl2[6])+\"==》\")+str(format(int(temp_res_wl2[7])/sum_res_wl,\".2%\"))
        print str((temp_res_wl2[8])+\"==》\")+str(format(int(temp_res_wl2[9])/sum_res_wl,\".2%\"))
        print str((temp_res_wl2[10])+\"==》\")+str(format(int(temp_res_wl2[11])/sum_res_wl,\".2%\"))
    #======END OF THE FUNCTION======
    f.close()

def preAll(cur,scr,w,d,l,a):
    preSpf(cur,scr,w,d,l,a)
    preZjq(cur,scr,w,d,l,a)
    preRes(cur,scr,w,d,l,a)