package test_data import ( "github.com/shopspring/decimal" "gorm.io/gorm" "sort" "testData/global" "testData/model" "testData/request" ) func WaferList(r *request.WaferList) ([]*model.WaferList, int64) { if r.Page > 0 { r.Page-- } var sql string if r.Status != "" { sql += "status = `" + r.Status + "`" } if r.OrderDate != "" { if sql == "" { sql += "order_date = '" + r.OrderDate + "'" } else { sql += " AND order_date = '" + r.OrderDate + "'" } } if r.Product != "" { if sql == "" { sql += "product = '" + r.Product + "'" } else { sql += " AND product = '" + r.Product + "'" } } if r.PBI != "" { if sql == "" { sql += "pbi = '" + r.PBI + "'" } else { sql += " AND pbi = '" + r.PBI + "'" } } if r.Factory != "" { if sql == "" { sql += "factory = '" + r.Factory + "'" } else { sql += " AND factory = '" + r.Factory + "'" } } if r.WaferSize != "" { if sql == "" { sql += "wafer_size = '" + r.WaferSize + "'" } else { sql += " AND wafer_size = '" + r.WaferSize + "'" } } if r.Quantity != "" { if sql == "" { sql += "quantity = '" + r.Quantity + "'" } else { sql += " AND quantity = '" + r.Quantity + "'" } } if r.OnlineQuantity != "" { if sql == "" { sql += "online_quantity = '" + r.OnlineQuantity + "'" } else { sql += " AND online_quantity = '" + r.OnlineQuantity + "'" } } if r.ReturnQuantity != "" { if sql == "" { sql += "return_quantity = '" + r.ReturnQuantity + "'" } else { sql += " AND return_quantity = '" + r.ReturnQuantity + "'" } } var waferList []*model.WaferList var total int64 global.PostGreSQL.Where(sql).Order("order_date desc").Find(&waferList).Count(&total). Offset(r.Page*r.PageSize).Limit(r.PageSize).Preload("WaferStock", func(db *gorm.DB) *gorm.DB { return db.Order("return_date") }).Find(&waferList) return waferList, total } func ABList(r *request.ABList) ([]*model.ABList, int64) { var sql string if r.Status != "" { sql += "status = '" + r.Status + "'" } if r.OrderDate != "" { if sql != "" { sql += " AND order_date = " + r.OrderDate + "" } else { sql += "order_date = DATE '" + r.OrderDate + "'" } } if r.PBI != "" { if sql != "" { sql += " AND pbi = '" + r.PBI + "'" } else { sql += "pbi = '" + r.PBI + "'" } } if r.Product != "" { if sql != "" { sql += " AND product = '" + r.Product + "'" } else { sql += "product = '" + r.Product + "'" } } if r.WaferProduct != "" { if sql != "" { sql += " AND wafer_product = " + r.WaferProduct + "" } else { sql += "wafer_product = " + r.WaferProduct + "" } } if r.Package != "" { if sql != "" { sql += " AND package = '" + r.Package + "'" } else { sql += "package = '" + r.Package + "'" } } if r.Factory != "" { if sql != "" { sql += " AND factory LIKE '%" + r.Factory + "%'" } else { sql += "factory LIKE '%" + r.Factory + "%'" } } if r.Lot != "" { if sql != "" { sql += " AND lot = '" + r.Lot + "'" } else { sql += "lot = '" + r.Lot + "'" } } if r.Seal != "" { if sql != "" { sql += " AND seal = '" + r.Seal + "'" } else { sql += "seal = '" + r.Seal + "'" } } if r.WaferID != "" { if sql != "" { sql += " AND wafer_id = " + r.WaferID + "" } else { sql += "wafer_id = " + r.WaferID + "" } } if r.Quantity != "" { if sql != "" { sql += " AND quantity = " + r.Quantity + "" } else { sql += "quantity = " + r.Quantity + "" } } if r.OnlineQuantity != "" { if sql != "" { sql += " AND online_quantity = " + r.OnlineQuantity + "" } else { sql += "online_quantity = " + r.OnlineQuantity + "" } } if r.PassProbability != "" { if sql != "" { sql += " AND pass_probability = " + r.PassProbability + "" } else { sql += "pass_probability = " + r.PassProbability + "" } } if r.StockOutQuantity != "" { if sql != "" { sql += " AND stock_out_quantity = " + r.StockOutQuantity + "" } else { sql += "stock_out_quantity = " + r.StockOutQuantity + "" } } if r.StockInQuantity != "" { if sql != "" { sql += " AND stock_in_quantity = " + r.StockInQuantity + "" } else { sql += "stock_in_quantity = " + r.StockInQuantity + "" } } if r.PassQuantity != "" { if sql != "" { sql += " AND pass_quantity = " + r.PassQuantity + "" } else { sql += "pass_quantity = " + r.PassQuantity + "" } } if r.FailQuantity != "" { if sql != "" { sql += " AND fail_quantity = " + r.FailQuantity + "" } else { sql += "fail_quantity = " + r.FailQuantity + "" } } var abList []*model.ABList var total int64 if r.Page > 0 { r.Page-- } global.PostGreSQL.Where(sql).Order("order_date desc").Find(&abList).Count(&total).Offset(r.Page * r.PageSize).Limit(r.PageSize). Preload("ABListStock").Find(&abList) return abList, total } func BPList(r *request.BPList) ([]*model.BPList, int64) { var sql string if r.Status != "" { sql += "status = '" + r.Status + "'" } if r.OrderDate != "" { if sql != "" { sql += " AND order_date = " + r.OrderDate + "" } else { sql += "order_date = DATE '" + r.OrderDate + "'" } } if r.PBI != "" { if sql != "" { sql += " AND pbi = '" + r.PBI + "'" } else { sql += "pbi = '" + r.PBI + "'" } } if r.Product != "" { if sql != "" { sql += " AND product = '" + r.Product + "'" } else { sql += "product = '" + r.Product + "'" } } if r.Package != "" { if sql != "" { sql += " AND package = '" + r.Package + "'" } else { sql += "package = '" + r.Package + "'" } } if r.Factory != "" { if sql != "" { sql += " AND factory LIKE '%" + r.Factory + "%'" } else { sql += "factory LIKE '%" + r.Factory + "%'" } } if r.Lot != "" { if sql != "" { sql += " AND lot = '" + r.Lot + "'" } else { sql += "lot = '" + r.Lot + "'" } } if r.WaferID != "" { if sql != "" { sql += " AND wafer_id = " + r.WaferID + "" } else { sql += "wafer_id = " + r.WaferID + "" } } if r.Quantity != "" { if sql != "" { sql += " AND quantity = " + r.Quantity + "" } else { sql += "quantity = " + r.Quantity + "" } } if r.OnlineQuantity != "" { if sql != "" { sql += " AND online_quantity = " + r.OnlineQuantity + "" } else { sql += "online_quantity = " + r.OnlineQuantity + "" } } if r.ReturnQuantity != "" { if sql != "" { sql += " AND return_quantity = " + r.ReturnQuantity + "" } else { sql += "return_quantity = " + r.ReturnQuantity + "" } } if r.DieQuantity != "" { if sql != "" { sql += " AND die_quantity = " + r.DieQuantity + "" } else { sql += "die_quantity = " + r.DieQuantity + "" } } var bpList []*model.BPList var total int64 if r.Page > 0 { r.Page-- } global.PostGreSQL.Where(sql).Order("order_date desc").Find(&bpList).Count(&total). Offset(r.Page * r.PageSize).Limit(r.PageSize). Preload("BPListStock").Find(&bpList) return bpList, total } func CPList(r *request.CPList) ([]*model.CPShowList, int64) { var sql string if r.Status != "" { sql += "status = '" + r.Status + "'" } if r.OrderDate != "" { if sql == "" { sql += "order_date = '" + r.OrderDate + "'" } else { sql += " AND order_date = '" + r.OrderDate + "'" } } if r.PBI != "" { if sql == "" { sql += "pbi = '" + r.PBI + "'" } else { sql += " AND pbi = '" + r.PBI + "'" } } if r.Product != "" { if sql == "" { sql += "product = '" + r.Product + "'" } else { sql += " AND product = '" + r.Product + "'" } } if r.Package != "" { if sql == "" { sql += "package = '" + r.Package + "'" } else { sql += " AND package = '" + r.Package + "'" } } if r.Factory != "" { if sql == "" { sql += "factory = '" + r.Factory + "'" } else { sql += " AND factory = '" + r.Factory + "'" } } if r.Lot != "" { if sql == "" { sql += "lot = '" + r.Lot + "'" } else { sql += " AND lot = '" + r.Lot + "'" } } if r.WaferID != "" { if sql == "" { sql += "wafer_id = '" + r.WaferID + "'" } else { sql += " AND wafer_id = '" + r.WaferID + "'" } } if r.TestProgram != "" { if sql == "" { sql += "test_program = '" + r.TestProgram + "'" } else { sql += " AND test_program = '" + r.TestProgram + "'" } } if r.TestProgramVersion != "" { if sql == "" { sql += "test_program_version = '" + r.TestProgramVersion + "'" } else { sql += " AND test_program_version = '" + r.TestProgramVersion + "'" } } if r.Quantity != "" { if sql == "" { sql += "quantity = " + r.Quantity } else { sql += " AND quantity = '" + r.Quantity } } if r.ReturnQuantity != "" { if sql == "" { sql += "return_quantity = " + r.ReturnQuantity } else { sql += " AND return_quantity = '" + r.ReturnQuantity } } if r.DieQuantity != "" { if sql == "" { sql += "die_quantity = " + r.DieQuantity } else { sql += " AND die_quantity = '" + r.DieQuantity } } if r.Page > 0 { r.Page-- } var cpLists []*model.CPList var total int64 global.PostGreSQL.Where(sql).Order("order_date desc").Find(&cpLists).Count(&total). Offset(r.Page * r.PageSize).Limit(r.PageSize). Preload("CPListStock").Find(&cpLists) var cpShowLists []*model.CPShowList for _, cpList := range cpLists { cpShowList := model.CPShowList{ Status: cpList.Status, OrderDate: cpList.OrderDate, PBI: cpList.PBI, OutsourcingPBI: cpList.OutsourcingPBI, Product: cpList.Product, Package: cpList.Package, Lot: cpList.Lot, Factory: cpList.Factory, WaferID: cpList.WaferID, TestProgram: cpList.TestProgram, TestProgramVersion: cpList.TestProgramVersion, Quantity: cpList.Quantity, OnlineQuantity: cpList.OnlineQuantity, ReturnQuantity: cpList.ReturnQuantity, DieQuantity: cpList.DieQuantity, CPListStock: cpList.CPListStock, SoftBinFailMap: make(map[string]float64), HardBinFailMap: make(map[string]float64), } var reports []*model.Report global.PostGreSQL.Where("pbi = ? AND product = ? AND lot = ? AND step LIKE ?", cpList.PBI, cpList.Product, cpList.Lot, "CP"). Order("pbi"). Preload("BinFail", func(db *gorm.DB) *gorm.DB { return db.Where("report_site_id = ?", "0") }).Preload("Site", func(db *gorm.DB) *gorm.DB { return db.Preload("BinFail") }).Find(&reports) for _, report := range reports { var finalReports []*model.FinalReport global.PostGreSQL.Where("pbi = ? AND product = ? AND lot = ? AND step LIKE ?", report.PBI, report.Product, report.Lot, "CP").Find(&finalReports) var reportTestQuantity, reportPassQuantity, reportPassProbability string for _, finalReport := range finalReports { reportTestQuantityDecimal, _ := decimal.NewFromString(reportTestQuantity) reportPassQuantityDecimal, _ := decimal.NewFromString(reportPassQuantity) finalReportTestQuantityDecimal, _ := decimal.NewFromString(finalReport.ReportTestQuantity) finalReportPassQuantityDecimal, _ := decimal.NewFromString(finalReport.ReportPassQuantity) reportTestQuantity = reportTestQuantityDecimal.Add(finalReportTestQuantityDecimal).String() reportPassQuantity = reportPassQuantityDecimal.Add(finalReportPassQuantityDecimal).String() } reportTestQuantityDecimal, _ := decimal.NewFromString(reportTestQuantity) reportPassQuantityDecimal, _ := decimal.NewFromString(reportPassQuantity) if !reportTestQuantityDecimal.IsZero() { reportPassProbability = reportPassQuantityDecimal.Div(reportTestQuantityDecimal). Mul(decimal.NewFromInt(100).Round(2)).String() + "%" } firstPassQuantity, _ := decimal.NewFromString(report.FirstPassQuantity) listFirstPassQuantity, _ := decimal.NewFromString(cpShowList.PassQuantity) testQuantity, _ := decimal.NewFromString(report.TestQuantity) passQuantity, _ := decimal.NewFromString(report.PassQuantity) listTestQuantity, _ := decimal.NewFromString(cpShowList.TestQuantity) listPassQuantity, _ := decimal.NewFromString(cpShowList.PassQuantity) cpShowList.TestQuantity = testQuantity.Add(listTestQuantity).String() cpShowList.PassQuantity = passQuantity.Add(listPassQuantity).String() cpShowList.PassProbability = passQuantity.Add(listPassQuantity).Div(testQuantity.Add(listTestQuantity)). Round(4).Mul(decimal.NewFromInt(100)).String() + "%" cpShowList.FirstPassQuantity = firstPassQuantity.Add(listFirstPassQuantity).String() cpShowList.FirstPassProbability = firstPassQuantity.Add(listFirstPassQuantity).Div(testQuantity.Add(listTestQuantity)). Round(4).Mul(decimal.NewFromInt(100)).String() + "%" softBinFailMap, hardBinFailMap := make(map[string]float64), make(map[string]float64) for _, binFail := range report.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() cpShowList.SoftBinFailMap[binFail.SoftBin] += quantity cpShowList.HardBinFailMap[binFail.HardBin] += quantity softBinFailMap[binFail.SoftBin] += quantity hardBinFailMap[binFail.HardBin] += quantity } for _, site := range report.Site { for _, reportSite := range cpShowList.Site { if reportSite.Site == site.Site { siteTestQuantity, _ := decimal.NewFromString(site.TestQuantity) sitePassQuantity, _ := decimal.NewFromString(site.PassQuantity) reportSiteTestQuantity, _ := decimal.NewFromString(reportSite.TestQuantity) reportSitePassQuantity, _ := decimal.NewFromString(reportSite.PassQuantity) reportSite.TestQuantity = siteTestQuantity.Add(reportSiteTestQuantity).String() reportSite.PassQuantity = sitePassQuantity.Add(reportSitePassQuantity).String() reportSite.PassProbability = sitePassQuantity.Add(reportSitePassQuantity).Div(siteTestQuantity.Add(reportSiteTestQuantity)). Round(4).Mul(decimal.NewFromInt(100)).String() + "%" for _, binFail := range site.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() reportSite.SoftBinFailMap[binFail.SoftBin] += quantity reportSite.HardBinFailMap[binFail.HardBin] += quantity } } } } softBinFail, hardBinFail := make(map[string]float64), make(map[string]float64) for _, binFail := range report.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() softBinFail[binFail.SoftBin] += quantity hardBinFail[binFail.HardBin] += quantity } var reportListSites []*model.ReportListSites for _, site := range report.Site { siteSoftBinFail, siteHardBinFail := make(map[string]float64), make(map[string]float64) for _, binFail := range site.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() siteSoftBinFail[binFail.SoftBin] += quantity siteHardBinFail[binFail.HardBin] += quantity } reportListSites = append(reportListSites, &model.ReportListSites{ ID: site.ID, ReportID: site.ReportID, Site: site.Site, TestQuantity: site.TestQuantity, PassQuantity: site.PassQuantity, PassProbability: site.PassProbability, SoftBinFailMap: siteSoftBinFail, HardBinFailMap: siteHardBinFail, }) } cpShowList = model.CPShowList{ Status: cpShowList.Status, OrderDate: cpShowList.OrderDate, PBI: cpShowList.PBI, OutsourcingPBI: cpShowList.OutsourcingPBI, Product: cpShowList.Product, Package: cpShowList.Package, Lot: cpShowList.Lot, Factory: cpShowList.Factory, WaferID: cpShowList.WaferID, TestMachine: report.TestMachine, TestProgram: cpShowList.TestProgram, TestProgramVersion: cpShowList.TestProgramVersion, Quantity: cpShowList.Quantity, OnlineQuantity: cpShowList.OnlineQuantity, ReturnQuantity: cpShowList.ReturnQuantity, DieQuantity: cpShowList.DieQuantity, CPListStock: cpShowList.CPListStock, TestQuantity: report.TestQuantity, FirstPassQuantity: report.FirstPassQuantity, FirstPassProbability: report.FirstPassProbability, PassQuantity: report.PassQuantity, PassProbability: report.PassProbability, ReportTestQuantity: reportTestQuantity, ReportPassQuantity: reportPassQuantity, ReportPassProbability: reportPassProbability, TestTime: report.TestTime, StackingMaterials: report.StackingMaterials, SoftBinFailMap: softBinFail, HardBinFailMap: hardBinFail, Site: reportListSites, } } for _, report := range reports { softBinFailMap, hardBinFailMap := make(map[string]float64), make(map[string]float64) for _, binFail := range report.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() softBinFailMap[binFail.SoftBin] += quantity hardBinFailMap[binFail.HardBin] += quantity } var reportListSites []*model.ReportListSites for _, site := range report.Site { siteSoftBinFailMap, siteHardBinFailMap := make(map[string]float64), make(map[string]float64) for _, binFail := range site.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() siteSoftBinFailMap[binFail.SoftBin] += quantity siteHardBinFailMap[binFail.HardBin] += quantity } var siteSoftBinFail, siteHardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range siteSoftBinFailMap { siteSoftBinFail = append(siteSoftBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, }) } sort.Slice(siteSoftBinFail, func(i, j int) bool { return siteSoftBinFail[i].Quantity > siteSoftBinFail[j].Quantity }) for hardBin, hardBinQuantity := range siteHardBinFailMap { siteHardBinFail = append(siteHardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, }) } sort.Slice(siteHardBinFail, func(i, j int) bool { return siteHardBinFail[i].Quantity > siteHardBinFail[j].Quantity }) reportListSites = append(reportListSites, &model.ReportListSites{ ID: site.ID, ReportID: site.ReportID, Site: site.Site, TestQuantity: site.TestQuantity, PassQuantity: site.PassQuantity, PassProbability: site.PassProbability, SoftBinFailMap: siteSoftBinFailMap, HardBinFailMap: siteHardBinFailMap, SoftBinFail: siteSoftBinFail, HardBinFail: siteHardBinFail, }) } testQuantityDecimal, _ := decimal.NewFromString(report.TestQuantity) var softBinFail, hardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range softBinFailMap { softBinQuantityDecimal := decimal.NewFromFloat(softBinQuantity) softBinFail = append(softBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, Proportion: softBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(softBinFail, func(i, j int) bool { return softBinFail[i].Quantity > softBinFail[j].Quantity }) for hardBin, hardBinQuantity := range hardBinFailMap { hardBinQuantityDecimal := decimal.NewFromFloat(hardBinQuantity) hardBinFail = append(hardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, Proportion: hardBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(hardBinFail, func(i, j int) bool { return hardBinFail[i].Quantity > hardBinFail[j].Quantity }) cpShowList.WaferDetails = append(cpShowList.WaferDetails, model.ReportList{ OrderDate: cpShowList.OrderDate, PBI: cpShowList.PBI, Product: cpShowList.Product, Lot: cpShowList.Lot, SubBatch: report.SubBatch, WaferID: report.WaferID, Factory: cpShowList.Factory, TestMachine: report.TestMachine, TestProgram: cpShowList.TestProgram, Seal: report.Seal, TestQuantity: report.TestQuantity, FirstPassQuantity: report.FirstPassQuantity, FirstPassProbability: report.FirstPassProbability, PassQuantity: report.PassQuantity, PassProbability: report.PassProbability, TestTime: report.TestTime, Step: report.Step, StackingMaterials: report.StackingMaterials, SoftBinFailMap: softBinFailMap, HardBinFailMap: hardBinFailMap, SoftBinFail: softBinFail, HardBinFail: hardBinFail, Site: reportListSites, }) } sort.Slice(cpShowList.WaferDetails, func(i, j int) bool { iDecimal, _ := decimal.NewFromString(cpShowList.WaferDetails[i].PassProbability[:len(cpShowList.WaferDetails[i].PassProbability)-1]) jDecimal, _ := decimal.NewFromString(cpShowList.WaferDetails[j].PassProbability[:len(cpShowList.WaferDetails[j].PassProbability)-1]) return jDecimal.LessThan(iDecimal) }) var testQuantityDiffLabel, passQuantityDiffLabel, returnProbabilityLabel bool testQuantityDecimal, _ := decimal.NewFromString(cpShowList.TestQuantity) reportTestQuantityDecimal, _ := decimal.NewFromString(cpShowList.ReportTestQuantity) firstPassQuantityDecimal, _ := decimal.NewFromString(cpShowList.FirstPassQuantity) passQuantityDecimal, _ := decimal.NewFromString(cpShowList.PassQuantity) reportPassQuantityDecimal, _ := decimal.NewFromString(cpShowList.ReportPassQuantity) if !testQuantityDecimal.IsZero() { diff := reportTestQuantityDecimal.Sub(testQuantityDecimal).Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)) if reportTestQuantityDecimal.Sub(testQuantityDecimal).LessThan(decimal.NewFromInt(0)) || decimal.NewFromInt(5).LessThanOrEqual(diff) { testQuantityDiffLabel = true } cpShowList.TestQuantityDiff = diff.String() + "%" cpShowList.TestQuantityDiffLabel = testQuantityDiffLabel probability := passQuantityDecimal.Sub(firstPassQuantityDecimal).Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)) if probability.LessThan(decimal.NewFromInt(0)) || decimal.NewFromInt(5).LessThanOrEqual(probability) { returnProbabilityLabel = true } cpShowList.ReturnProbability = probability.String() + "%" cpShowList.ReturnProbabilityLabel = returnProbabilityLabel } if !reportPassQuantityDecimal.IsZero() { diff := passQuantityDecimal.Sub(reportPassQuantityDecimal).Div(reportPassQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)) if passQuantityDecimal.Sub(reportPassQuantityDecimal).LessThan(decimal.NewFromInt(0)) || decimal.NewFromInt(5).LessThanOrEqual(diff) { passQuantityDiffLabel = true } cpShowList.PassQuantityDiff = diff.String() + "%" cpShowList.PassQuantityDiffLabel = passQuantityDiffLabel } var softBinFail, hardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range cpShowList.SoftBinFailMap { softBinQuantityDecimal := decimal.NewFromFloat(softBinQuantity) softBinFail = append(softBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, Proportion: softBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(softBinFail, func(i, j int) bool { return softBinFail[i].Quantity > softBinFail[j].Quantity }) for hardBin, hardBinQuantity := range cpShowList.HardBinFailMap { hardBinQuantityDecimal := decimal.NewFromFloat(hardBinQuantity) hardBinFail = append(hardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, Proportion: hardBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(hardBinFail, func(i, j int) bool { return hardBinFail[i].Quantity > hardBinFail[j].Quantity }) for _, site := range cpShowList.Site { var siteSoftBinFail, siteHardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range site.SoftBinFailMap { siteSoftBinFail = append(siteSoftBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, }) } sort.Slice(siteSoftBinFail, func(i, j int) bool { return siteSoftBinFail[i].Quantity > siteSoftBinFail[j].Quantity }) for hardBin, hardBinQuantity := range site.HardBinFailMap { siteHardBinFail = append(siteHardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, }) } sort.Slice(siteHardBinFail, func(i, j int) bool { return siteHardBinFail[i].Quantity > siteHardBinFail[j].Quantity }) site.SoftBinFail = siteSoftBinFail site.HardBinFail = siteHardBinFail } sort.Slice(cpShowList.Site, func(i, j int) bool { iDecimal, _ := decimal.NewFromString(cpShowList.Site[i].PassProbability[:len(cpShowList.Site[i].PassProbability)-1]) jDecimal, _ := decimal.NewFromString(cpShowList.Site[j].PassProbability[:len(cpShowList.Site[j].PassProbability)-1]) return jDecimal.LessThan(iDecimal) }) cpShowList.SoftBinFail = softBinFail cpShowList.HardBinFail = hardBinFail cpShowLists = append(cpShowLists, &cpShowList) } return cpShowLists, total } func FTList(r *request.FTList) ([]*model.FTShowList, int64) { var sql string if r.Status != "" { sql += "status = '" + r.Status + "'" } if r.OrderDate != "" { if sql == "" { sql += "order_date = '" + r.OrderDate + "'" } else { sql += " AND order_date = '" + r.OrderDate + "'" } } if r.PBI != "" { if sql == "" { sql += "pbi = '" + r.PBI + "'" } else { sql += " AND pbi = '" + r.PBI + "'" } } if r.Product != "" { if sql == "" { sql += "product = '" + r.Product + "'" } else { sql += " AND product = '" + r.Product + "'" } } if r.Package != "" { if sql == "" { sql += "package = '" + r.Package + "'" } else { sql += " AND package = '" + r.Package + "'" } } if r.Factory != "" { if sql == "" { sql += "factory = '" + r.Factory + "'" } else { sql += " AND factory = '" + r.Factory + "'" } } if r.Lot != "" { if sql == "" { sql += "lot = '" + r.Lot + "'" } else { sql += " AND lot = '" + r.Lot + "'" } } if r.Seal != "" { if sql == "" { sql += "seal = '" + r.Seal + "'" } else { sql += " AND seal = '" + r.Seal + "'" } } if r.TestProgram != "" { if sql == "" { sql += "test_program = '" + r.TestProgram + "'" } else { sql += " AND test_program = '" + r.TestProgram + "'" } } if r.TestProgramVersion != "" { if sql == "" { sql += "test_program_version = '" + r.TestProgramVersion + "'" } else { sql += " AND test_program_version = '" + r.TestProgramVersion + "'" } } if r.Quantity != "" { if sql == "" { sql += "quantity = " + r.Quantity } else { sql += " AND quantity = " + r.Quantity } } if r.ReturnQuantity != "" { if sql == "" { sql += "return_quantity = " + r.ReturnQuantity } else { sql += " AND return_quantity = " + r.ReturnQuantity } } if r.FTPassQuantity != "" { if sql == "" { sql += "pass_quantity = " + r.FTPassQuantity } else { sql += " AND pass_quantity = " + r.FTPassQuantity } } if r.FTPassQuantity != "" { if sql == "" { sql += "fail_quantity = " + r.FTPassQuantity } else { sql += " AND fail_quantity = " + r.FTPassQuantity } } if r.Page > 0 { r.Page-- } var ftLists []*model.FTList var total int64 global.PostGreSQL.Where(sql).Order("order_date desc").Find(&ftLists).Count(&total). Offset(r.Page * r.PageSize).Limit(r.PageSize). Preload("FTListStock").Find(&ftLists) var ftShowLists []*model.FTShowList for _, ftList := range ftLists { ftShowList := model.FTShowList{ Status: ftList.Status, OrderDate: ftList.OrderDate, PBI: ftList.PBI, OutsourcingPBI: ftList.OutsourcingPBI, Product: ftList.Product, WaferProduct: ftList.WaferProduct, Package: ftList.Package, Lot: ftList.Lot, Factory: ftList.Factory, Seal: ftList.Seal, TestProgram: ftList.TestProgram, TestProgramVersion: ftList.TestProgramVersion, Quantity: ftList.Quantity, OnlineQuantity: ftList.OnlineQuantity, ReturnQuantity: ftList.ReturnQuantity, FTPassQuantity: ftList.PassQuantity, FTFailQuantity: ftList.FailQuantity, FTListStock: ftList.FTListStock, SoftBinFailMap: make(map[string]float64), HardBinFailMap: make(map[string]float64), } var reports []*model.Report global.PostGreSQL.Where("pbi = ? AND product = ? AND lot = ? AND step LIKE ?", ftList.PBI, ftList.Product, ftList.Lot, "FT"). Order("pbi"). Preload("BinFail", func(db *gorm.DB) *gorm.DB { return db.Where("report_site_id = ?", "0") }).Preload("Site", func(db *gorm.DB) *gorm.DB { return db.Preload("BinFail") }).Find(&reports) for _, report := range reports { var finalReports []*model.FinalReport global.PostGreSQL.Where("pbi = ? AND product = ? AND lot = ? AND step LIKE ?", report.PBI, report.Product, report.Lot, "FT").Find(&finalReports) var reportTestQuantity, reportPassQuantity, reportPassProbability string for _, finalReport := range finalReports { reportTestQuantityDecimal, _ := decimal.NewFromString(reportTestQuantity) reportPassQuantityDecimal, _ := decimal.NewFromString(reportPassQuantity) finalReportTestQuantityDecimal, _ := decimal.NewFromString(finalReport.ReportTestQuantity) finalReportPassQuantityDecimal, _ := decimal.NewFromString(finalReport.ReportPassQuantity) reportTestQuantity = reportTestQuantityDecimal.Add(finalReportTestQuantityDecimal).String() reportPassQuantity = reportPassQuantityDecimal.Add(finalReportPassQuantityDecimal).String() } reportTestQuantityDecimal, _ := decimal.NewFromString(reportTestQuantity) reportPassQuantityDecimal, _ := decimal.NewFromString(reportPassQuantity) if !reportTestQuantityDecimal.IsZero() { reportPassProbability = reportPassQuantityDecimal.Div(reportTestQuantityDecimal). Mul(decimal.NewFromInt(100).Round(2)).String() + "%" } firstPassQuantity, _ := decimal.NewFromString(report.FirstPassQuantity) listFirstPassQuantity, _ := decimal.NewFromString(ftShowList.PassQuantity) testQuantity, _ := decimal.NewFromString(report.TestQuantity) passQuantity, _ := decimal.NewFromString(report.PassQuantity) listTestQuantity, _ := decimal.NewFromString(ftShowList.TestQuantity) listPassQuantity, _ := decimal.NewFromString(ftShowList.PassQuantity) ftShowList.TestQuantity = testQuantity.Add(listTestQuantity).String() ftShowList.PassQuantity = passQuantity.Add(listPassQuantity).String() ftShowList.PassProbability = passQuantity.Add(listPassQuantity).Div(testQuantity.Add(listTestQuantity)). Round(4).Mul(decimal.NewFromInt(100)).String() + "%" ftShowList.FirstPassQuantity = firstPassQuantity.Add(listFirstPassQuantity).String() ftShowList.FirstPassProbability = firstPassQuantity.Add(listFirstPassQuantity).Div(testQuantity.Add(listTestQuantity)). Round(4).Mul(decimal.NewFromInt(100)).String() + "%" softBinFailMap, hardBinFailMap := make(map[string]float64), make(map[string]float64) for _, binFail := range report.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() ftShowList.SoftBinFailMap[binFail.SoftBin] += quantity ftShowList.HardBinFailMap[binFail.HardBin] += quantity softBinFailMap[binFail.SoftBin] += quantity hardBinFailMap[binFail.HardBin] += quantity } for _, site := range report.Site { for _, reportSite := range ftShowList.Site { if reportSite.Site == site.Site { siteTestQuantity, _ := decimal.NewFromString(site.TestQuantity) sitePassQuantity, _ := decimal.NewFromString(site.PassQuantity) reportSiteTestQuantity, _ := decimal.NewFromString(reportSite.TestQuantity) reportSitePassQuantity, _ := decimal.NewFromString(reportSite.PassQuantity) reportSite.TestQuantity = siteTestQuantity.Add(reportSiteTestQuantity).String() reportSite.PassQuantity = sitePassQuantity.Add(reportSitePassQuantity).String() reportSite.PassProbability = sitePassQuantity.Add(reportSitePassQuantity).Div(siteTestQuantity.Add(reportSiteTestQuantity)). Round(4).Mul(decimal.NewFromInt(100)).String() + "%" for _, binFail := range site.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() reportSite.SoftBinFailMap[binFail.SoftBin] += quantity reportSite.HardBinFailMap[binFail.HardBin] += quantity } } } } softBinFail, hardBinFail := make(map[string]float64), make(map[string]float64) for _, binFail := range report.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() softBinFail[binFail.SoftBin] += quantity hardBinFail[binFail.HardBin] += quantity } var reportListSites []*model.ReportListSites for _, site := range report.Site { siteSoftBinFail, siteHardBinFail := make(map[string]float64), make(map[string]float64) for _, binFail := range site.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() siteSoftBinFail[binFail.SoftBin] += quantity siteHardBinFail[binFail.HardBin] += quantity } reportListSites = append(reportListSites, &model.ReportListSites{ ID: site.ID, ReportID: site.ReportID, Site: site.Site, TestQuantity: site.TestQuantity, PassQuantity: site.PassQuantity, PassProbability: site.PassProbability, SoftBinFailMap: siteSoftBinFail, HardBinFailMap: siteHardBinFail, }) } ftShowList = model.FTShowList{ Status: ftShowList.Status, OrderDate: ftShowList.OrderDate, PBI: ftShowList.PBI, OutsourcingPBI: ftShowList.OutsourcingPBI, Product: ftShowList.Product, Package: ftShowList.Package, Factory: ftShowList.Factory, Lot: ftShowList.Lot, Seal: ftShowList.Seal, TestProgram: ftShowList.TestProgram, TestProgramVersion: ftShowList.TestProgramVersion, Quantity: ftShowList.Quantity, OnlineQuantity: ftShowList.OnlineQuantity, ReturnQuantity: ftShowList.ReturnQuantity, FTPassQuantity: ftShowList.FTPassQuantity, FTFailQuantity: ftShowList.FTFailQuantity, FTListStock: ftShowList.FTListStock, TestMachine: report.TestMachine, TestQuantity: report.TestQuantity, FirstPassQuantity: report.FirstPassQuantity, FirstPassProbability: report.FirstPassProbability, PassQuantity: report.PassQuantity, PassProbability: report.PassProbability, ReportTestQuantity: reportTestQuantity, ReportPassQuantity: reportPassQuantity, ReportPassProbability: reportPassProbability, TestTime: report.TestTime, StackingMaterials: report.StackingMaterials, SoftBinFailMap: softBinFail, HardBinFailMap: hardBinFail, Site: reportListSites, } } for _, report := range reports { softBinFailMap, hardBinFailMap := make(map[string]float64), make(map[string]float64) for _, binFail := range report.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() softBinFailMap[binFail.SoftBin] += quantity hardBinFailMap[binFail.HardBin] += quantity } var reportListSites []*model.ReportListSites for _, site := range report.Site { siteSoftBinFailMap, siteHardBinFailMap := make(map[string]float64), make(map[string]float64) for _, binFail := range site.BinFail { quantityDecimal, _ := decimal.NewFromString(binFail.Quantity) quantity, _ := quantityDecimal.Float64() siteSoftBinFailMap[binFail.SoftBin] += quantity siteHardBinFailMap[binFail.HardBin] += quantity } var siteSoftBinFail, siteHardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range siteSoftBinFailMap { siteSoftBinFail = append(siteSoftBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, }) } sort.Slice(siteSoftBinFail, func(i, j int) bool { return siteSoftBinFail[i].Quantity > siteSoftBinFail[j].Quantity }) for hardBin, hardBinQuantity := range siteHardBinFailMap { siteHardBinFail = append(siteHardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, }) } sort.Slice(siteHardBinFail, func(i, j int) bool { return siteHardBinFail[i].Quantity > siteHardBinFail[j].Quantity }) reportListSites = append(reportListSites, &model.ReportListSites{ ID: site.ID, ReportID: site.ReportID, Site: site.Site, TestQuantity: site.TestQuantity, PassQuantity: site.PassQuantity, PassProbability: site.PassProbability, SoftBinFailMap: siteSoftBinFailMap, HardBinFailMap: siteHardBinFailMap, SoftBinFail: siteSoftBinFail, HardBinFail: siteHardBinFail, }) } testQuantityDecimal, _ := decimal.NewFromString(report.TestQuantity) var softBinFail, hardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range softBinFailMap { softBinQuantityDecimal := decimal.NewFromFloat(softBinQuantity) softBinFail = append(softBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, Proportion: softBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(softBinFail, func(i, j int) bool { return softBinFail[i].Quantity > softBinFail[j].Quantity }) for hardBin, hardBinQuantity := range hardBinFailMap { hardBinQuantityDecimal := decimal.NewFromFloat(hardBinQuantity) hardBinFail = append(hardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, Proportion: hardBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(hardBinFail, func(i, j int) bool { return hardBinFail[i].Quantity > hardBinFail[j].Quantity }) ftShowList.SubBatchDetails = append(ftShowList.SubBatchDetails, model.ReportList{ OrderDate: ftShowList.OrderDate, PBI: ftShowList.PBI, Product: ftShowList.Product, Lot: ftShowList.Lot, SubBatch: report.SubBatch, WaferID: report.WaferID, Factory: ftShowList.Factory, TestMachine: report.TestMachine, TestProgram: ftShowList.TestProgram, Seal: report.Seal, TestQuantity: report.TestQuantity, FirstPassQuantity: report.FirstPassQuantity, FirstPassProbability: report.FirstPassProbability, PassQuantity: report.PassQuantity, PassProbability: report.PassProbability, TestTime: report.TestTime, Step: report.Step, StackingMaterials: report.StackingMaterials, SoftBinFailMap: softBinFailMap, HardBinFailMap: hardBinFailMap, SoftBinFail: softBinFail, HardBinFail: hardBinFail, Site: reportListSites, }) } sort.Slice(ftShowList.SubBatchDetails, func(i, j int) bool { iDecimal, _ := decimal.NewFromString(ftShowList.SubBatchDetails[i].PassProbability[:len(ftShowList.SubBatchDetails[i].PassProbability)-1]) jDecimal, _ := decimal.NewFromString(ftShowList.SubBatchDetails[j].PassProbability[:len(ftShowList.SubBatchDetails[j].PassProbability)-1]) return jDecimal.LessThan(iDecimal) }) var testQuantityDiffLabel, passQuantityDiffLabel, returnProbabilityLabel bool testQuantityDecimal, _ := decimal.NewFromString(ftShowList.TestQuantity) reportTestQuantityDecimal, _ := decimal.NewFromString(ftShowList.ReportTestQuantity) firstPassQuantityDecimal, _ := decimal.NewFromString(ftShowList.FirstPassQuantity) passQuantityDecimal, _ := decimal.NewFromString(ftShowList.PassQuantity) reportPassQuantityDecimal, _ := decimal.NewFromString(ftShowList.ReportPassQuantity) if !testQuantityDecimal.IsZero() { diff := reportTestQuantityDecimal.Sub(testQuantityDecimal).Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)) if reportTestQuantityDecimal.Sub(testQuantityDecimal).LessThan(decimal.NewFromInt(0)) || decimal.NewFromInt(5).LessThanOrEqual(diff) { testQuantityDiffLabel = true } ftShowList.TestQuantityDiff = diff.String() + "%" ftShowList.TestQuantityDiffLabel = testQuantityDiffLabel probability := passQuantityDecimal.Sub(firstPassQuantityDecimal).Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)) if probability.LessThan(decimal.NewFromInt(0)) || decimal.NewFromInt(5).LessThanOrEqual(probability) { returnProbabilityLabel = true } ftShowList.ReturnProbability = probability.String() + "%" ftShowList.ReturnProbabilityLabel = returnProbabilityLabel } if !reportPassQuantityDecimal.IsZero() { diff := passQuantityDecimal.Sub(reportPassQuantityDecimal).Div(reportPassQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)) if passQuantityDecimal.Sub(reportPassQuantityDecimal).LessThan(decimal.NewFromInt(0)) || decimal.NewFromInt(5).LessThanOrEqual(diff) { passQuantityDiffLabel = true } ftShowList.PassQuantityDiff = diff.String() + "%" ftShowList.PassQuantityDiffLabel = passQuantityDiffLabel } var softBinFail, hardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range ftShowList.SoftBinFailMap { softBinQuantityDecimal := decimal.NewFromFloat(softBinQuantity) softBinFail = append(softBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, Proportion: softBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(softBinFail, func(i, j int) bool { return softBinFail[i].Quantity > softBinFail[j].Quantity }) for hardBin, hardBinQuantity := range ftShowList.HardBinFailMap { hardBinQuantityDecimal := decimal.NewFromFloat(hardBinQuantity) hardBinFail = append(hardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, Proportion: hardBinQuantityDecimal.Div(testQuantityDecimal).Round(4). Mul(decimal.NewFromInt(100)).String() + "%", }) } sort.Slice(hardBinFail, func(i, j int) bool { return hardBinFail[i].Quantity > hardBinFail[j].Quantity }) for _, site := range ftShowList.Site { var siteSoftBinFail, siteHardBinFail []*model.ReportBinFail for softBin, softBinQuantity := range site.SoftBinFailMap { siteSoftBinFail = append(siteSoftBinFail, &model.ReportBinFail{ Bin: softBin, Quantity: softBinQuantity, }) } sort.Slice(siteSoftBinFail, func(i, j int) bool { return siteSoftBinFail[i].Quantity > siteSoftBinFail[j].Quantity }) for hardBin, hardBinQuantity := range site.HardBinFailMap { siteHardBinFail = append(siteHardBinFail, &model.ReportBinFail{ Bin: hardBin, Quantity: hardBinQuantity, }) } sort.Slice(siteHardBinFail, func(i, j int) bool { return siteHardBinFail[i].Quantity > siteHardBinFail[j].Quantity }) site.SoftBinFail = siteSoftBinFail site.HardBinFail = siteHardBinFail } sort.Slice(ftShowList.Site, func(i, j int) bool { iDecimal, _ := decimal.NewFromString(ftShowList.Site[i].PassProbability[:len(ftShowList.Site[i].PassProbability)-1]) jDecimal, _ := decimal.NewFromString(ftShowList.Site[j].PassProbability[:len(ftShowList.Site[j].PassProbability)-1]) return jDecimal.LessThan(iDecimal) }) ftShowList.SoftBinFail = softBinFail ftShowList.HardBinFail = hardBinFail ftShowLists = append(ftShowLists, &ftShowList) } return ftShowLists, total } func TRList(r *request.TRList) ([]*model.TRList, int64) { var sql string if r.Status != "" { sql += "status = '" + r.Status + "'" } if r.OrderDate != "" { if sql != "" { sql += " AND order_date = " + r.OrderDate + "" } else { sql += "order_date = DATE '" + r.OrderDate + "'" } } if r.PBI != "" { if sql != "" { sql += " AND pbi = '" + r.PBI + "'" } else { sql += "pbi = '" + r.PBI + "'" } } if r.Product != "" { if sql != "" { sql += " AND product = '" + r.Product + "'" } else { sql += "product = '" + r.Product + "'" } } if r.WaferProduct != "" { if sql != "" { sql += " AND wafer_product = " + r.WaferProduct + "" } else { sql += "wafer_product = " + r.WaferProduct + "" } } if r.Package != "" { if sql != "" { sql += " AND package = '" + r.Package + "'" } else { sql += "package = '" + r.Package + "'" } } if r.Factory != "" { if sql != "" { sql += " AND factory LIKE '%" + r.Factory + "%'" } else { sql += "factory LIKE '%" + r.Factory + "%'" } } if r.Lot != "" { if sql != "" { sql += " AND lot = '" + r.Lot + "'" } else { sql += "lot = '" + r.Lot + "'" } } if r.Seal != "" { if sql != "" { sql += " AND seal = '" + r.Seal + "'" } else { sql += "seal = '" + r.Seal + "'" } } if r.Quantity != "" { if sql != "" { sql += " AND quantity = " + r.Quantity + "" } else { sql += "quantity = " + r.Quantity + "" } } if r.OnlineQuantity != "" { if sql != "" { sql += " AND online_quantity = " + r.OnlineQuantity + "" } else { sql += "online_quantity = " + r.OnlineQuantity + "" } } if r.PassQuantity != "" { if sql != "" { sql += " AND pass_quantity = " + r.PassQuantity + "" } else { sql += "pass_quantity = " + r.PassQuantity + "" } } if r.FailQuantity != "" { if sql != "" { sql += " AND fail_quantity = " + r.FailQuantity + "" } else { sql += "fail_quantity = " + r.FailQuantity + "" } } var trList []*model.TRList var total int64 if r.Page > 0 { r.Page-- } global.PostGreSQL.Where(sql).Order("order_date desc").Find(&trList).Count(&total). Offset(r.Page * r.PageSize).Limit(r.PageSize). Preload("TRListStock").Find(&trList) return trList, total }