# coding= utf-8
import time
import sys
import os
import json
from xlsxwriter.workbook import Workbook

# --------------------------------------------------------------
count = eval(sys.argv[1])

with open("/usr/local/pxls.txt", "r") as yaml_f:
    pathall = yaml_f.read()

if not pathall:
    exit(0)

list_jspath = pathall.split(";")

endxlsx_path = "/usr/local/Woutput.xlsx"
workbook = Workbook(endxlsx_path)
gxlsx_sheet_currow = {}
gxlsx_sheet_tblobj = {}

waitreporttime = time.time()
itemtime = waitreporttime

total = 0
NGtotal = 0
modeltotal = 0
row=0
col=0
statistic=[0,0,0,0,0,0,0,0,0,0,0,0,0]
unqualified=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
worksheet = workbook.add_worksheet("table")
for jscat in list_jspath:
    if jscat=='':
        continue
    with open(jscat, "r",encoding='utf-8') as jsonfile:
        jsonvalue = jsonfile.read()
    list_json = json.loads(jsonvalue)

    worksheet.write(row, 1, "空膜")
    worksheet.write(row, 2, "空胶塞")
    worksheet.write(row, 3, "印刷反")
    worksheet.write(row, 4, "无锥头")
    worksheet.write(row, 5, "无手柄")
    worksheet.write(row, 6, "产品未到位")
    worksheet.write(row, 7, "活塞未到位")
    worksheet.write(row, 8, "异物")
    worksheet.write(row, 9, "缺针")
    worksheet.write(row, 10, "多针")
    worksheet.write(row, 11, "毛发(划伤)")
    worksheet.write(row, 12, "黑点")
    worksheet.write(row, 13, "挤伤")
    row+=1
    totalresult = list_json["result"]
    total+=totalresult["检测数"]
    NGtotal+=totalresult["总数"]
    modelresult = list_json["record"]
    modelnum = 1
    ran = [2,0,3,1]
    for i in ran:
        for j in range(0,count):
            if i==0 or i==1:
                if j==0:
                    continue
            model = "%s"%i+"_%s"%j
            modelData = modelresult[model]
            statistic[0] = modelData["空膜"]
            statistic[1] = modelData["空胶塞"]
            statistic[2] = modelData["印刷反"]
            statistic[3] = modelData["无锥头"]
            statistic[4] = modelData["无手柄"]
            statistic[5] = modelData["产品未到位"]
            statistic[6] = modelData["活塞未到位"]
            statistic[7] = modelData["异物"]
            statistic[8] = modelData["缺针"]
            statistic[9] = modelData["多针"]
            statistic[10] = modelData["毛发(划伤)"]
            statistic[11] = modelData["黑点"]
            statistic[12] = modelData["挤伤"]
            modelNGtotal = modelData["总数"]
            modeltotal = modelData["检测数"]
            col = 0
            modelname = "模腔%s" %modelnum
            worksheet.write(row, 0, modelname)
            for col in range(0, 13):
                worksheet.write(row, col + 1, statistic[col])
            if modeltotal != 0:
                unqualified[modelnum-1] = str(round((modelNGtotal / modeltotal) * 100, 2)) + "%"
            else:
                unqualified[modelnum-1] = 0
            row += 1
            modelnum += 1

    result = list_json["Allresult"]
    worksheet.write(row, 0, "NG次数")
    worksheet.write(row, 1, result["空膜"])
    worksheet.write(row, 2, result["空胶塞"])
    worksheet.write(row, 3, result["印刷反"])
    worksheet.write(row, 4, result["无锥头"])
    worksheet.write(row, 5, result["无手柄"])
    worksheet.write(row, 6, result["产品未到位"])
    worksheet.write(row, 7, result["活塞未到位"])
    worksheet.write(row, 8, result["异物"])
    worksheet.write(row, 9, result["缺针"])
    worksheet.write(row, 10, result["多针"])
    worksheet.write(row, 11, result["毛发(划伤)"])
    worksheet.write(row, 12, result["黑点"])
    worksheet.write(row, 13, result["挤伤"])

    row+=2
    worksheet.write(row, 0, "线体")
    worksheet.write(row, 1, "时间")
    worksheet.write(row, 2, "模数")
    worksheet.write(row, 3, "合格率")
    worksheet.write(row, 4, "不良率")
    for i in range(1,4*count-1):
        modelname = "模腔%s" %i
        worksheet.write(row, i+4, modelname)

    row+=1
    get1=jscat.rfind("Datas/")
    get2=jscat.rfind("/WGResult")
    projectname = jscat[get1+6:get2]
    get1 = jscat.rfind("WGResult/")
    get2 = jscat.rfind(".txt")
    DateTime = jscat[get1+9:get2]
    Date = ""
    if DateTime.find("day")!=-1:
        Date=DateTime.replace("day","白班")
    elif DateTime.find("night")!=-1:
        Date=DateTime.replace("night","夜班")
    elif DateTime.find("mid")!=-1:
        Date=DateTime.replace("mid","中班")
    worksheet.write(row, 0, projectname)
    worksheet.write(row, 1, Date)
    worksheet.write(row, 2, total)
    if total!=0:
        Precent = 1-(NGtotal/total)
        unPrecent = 1-Precent
    else:
        Precent = 0
        unPrecent = 0
    BPrecent=str(round(Precent*100,2))+"%"
    BunPrecent=str(round(unPrecent*100,2))+"%"
    worksheet.write(row, 3, BPrecent)
    worksheet.write(row, 4, BunPrecent)
    col = 5
    for col in range(5,4*count+3):
        worksheet.write(row, col, unqualified[col-5])
    total=0
    NGtotal=0
    row += 2
    jsonfile.close()

workbook.close()

