951 lines
33 KiB
Go
951 lines
33 KiB
Go
package test_data
|
|
|
|
import (
|
|
"gitee.com/golang-module/carbon/v2"
|
|
"github.com/shopspring/decimal"
|
|
"gorm.io/gorm"
|
|
"math"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"testData/global"
|
|
"testData/model"
|
|
"testData/request"
|
|
)
|
|
|
|
func PackagePassProbabilityLine(r *request.PackagePassProbabilityLine) ([]*model.Line, int, int) {
|
|
var sql string
|
|
if r.OrderDateStart != "" && r.OrderDateEnd != "" {
|
|
sql += "tc_sfc02 BETWEEN DATE '" + r.OrderDateStart + "' AND DATE '" + r.OrderDateEnd + "'"
|
|
}
|
|
//else if r.OrderDateStart != "" && r.OrderDateEnd == "" {
|
|
// sql += "tc_sfc02 >= DATE '" + r.OrderDateStart + "'"
|
|
//} else if r.OrderDateStart == "" && r.OrderDateEnd != "" {
|
|
// sql += "tc_sfc02 <= DATE '" + r.OrderDateEnd + "'"
|
|
//}
|
|
var factories, x []string
|
|
if len(r.Factory) == 0 {
|
|
global.Oracle.Model(&model.TcSfcFile{}).Where("tc_sfc08 LIKE ?", "%AB%").Where(sql).
|
|
Group("tc_sfc04").Select("tc_sfc04").Find(&factories)
|
|
} else {
|
|
global.Oracle.Model(&model.PmcFile{}).Where("pmc03 IN ?", r.Factory).Select("pmc01").Find(&factories)
|
|
}
|
|
productMap := make(map[string]int)
|
|
for _, product := range r.Product {
|
|
productMap[product] = 1
|
|
}
|
|
lotMap := make(map[string]int)
|
|
for _, lot := range r.Lot {
|
|
lotMap[lot] = 1
|
|
}
|
|
specificationMap := make(map[string]int)
|
|
for _, specification := range r.Package {
|
|
specificationMap[specification] = 1
|
|
}
|
|
if r.DateSize == "天" {
|
|
days := int(carbon.Parse(r.OrderDateStart).DiffAbsInDays(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < days; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddDays(i).Format("Y-m-d"))
|
|
}
|
|
} else if r.DateSize == "周" {
|
|
weeks := int(carbon.Parse(r.OrderDateStart).DiffAbsInWeeks(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < weeks; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddWeeks(i).Format("Y-m-d"))
|
|
}
|
|
} else if r.DateSize == "月" {
|
|
months := int(carbon.Parse(r.OrderDateStart).DiffAbsInMonths(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < months; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddMonths(i).Format("Y-m"))
|
|
}
|
|
} else if r.DateSize == "年" {
|
|
years := int(carbon.Parse(r.OrderDateStart).DiffAbsInYears(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < years; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddYears(i).Format("Y"))
|
|
}
|
|
}
|
|
maxValue := math.MinInt
|
|
minValue := math.MaxInt
|
|
var line []*model.Line
|
|
for _, factory := range factories {
|
|
var pmc *model.PmcFile
|
|
global.Oracle.Where("pmc01 = ?", factory).Find(&pmc)
|
|
var tcSfcFile []*model.TcSfcFile
|
|
global.Oracle.Where("tc_sfc08 LIKE ? AND tc_sfc04 = ?", "%AB%", factory).Where(sql).
|
|
Preload("TcSfdFile", func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("ImaFile").Preload("RvaFile", func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("IddFile")
|
|
})
|
|
}).Preload("TcSfeFile", func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("ImaFile")
|
|
}).Order("tc_sfc02").Find(&tcSfcFile)
|
|
quantityMap := make(map[string][]float64)
|
|
var y []string
|
|
for _, tcSfc := range tcSfcFile {
|
|
for _, tcSfd := range tcSfc.TcSfdFile {
|
|
if _, ok := productMap[tcSfd.ImaFile.Ima02]; !ok && len(productMap) > 0 {
|
|
continue
|
|
}
|
|
if _, ok := specificationMap[tcSfd.ImaFile.Ima021]; !ok && len(specificationMap) > 0 {
|
|
continue
|
|
}
|
|
var stockInQuantity, failQuantity float64
|
|
//for _, rva := range tcSfd.RvaFile {
|
|
for _, idd := range tcSfd.RvaFile.IddFile {
|
|
if _, ok := lotMap[idd.Idd04]; !ok && len(lotMap) > 0 {
|
|
continue
|
|
}
|
|
stockInQuantity += idd.Idd13
|
|
if idd.Idd06 == "BIN99" {
|
|
failQuantity += idd.Idd13
|
|
}
|
|
}
|
|
//}
|
|
var s string
|
|
if r.DateSize == "天" {
|
|
s = carbon.Parse(r.OrderDateStart).AddDays(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInDays(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y-m-d")
|
|
} else if r.DateSize == "周" {
|
|
s = carbon.Parse(r.OrderDateStart).AddWeeks(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInWeeks(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y-m-d")
|
|
} else if r.DateSize == "月" {
|
|
s = carbon.Parse(r.OrderDateStart).AddMonths(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInMonths(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y-m")
|
|
} else if r.DateSize == "年" {
|
|
s = carbon.Parse(r.OrderDateStart).AddYears(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInYears(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y")
|
|
}
|
|
if _, ok := quantityMap[s]; !ok {
|
|
quantityMap[s] = []float64{stockInQuantity - failQuantity, stockInQuantity}
|
|
} else {
|
|
quantityMap[s][0] += stockInQuantity - failQuantity
|
|
quantityMap[s][1] += stockInQuantity
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, xDate := range x {
|
|
if _, ok := quantityMap[xDate]; ok {
|
|
if quantityMap[xDate][1] == 0 {
|
|
y = append(y, "")
|
|
} else {
|
|
passProbability := decimal.NewFromFloat(quantityMap[xDate][0]).Div(decimal.NewFromFloat(quantityMap[xDate][1])).
|
|
Round(4).Mul(decimal.NewFromFloat(100))
|
|
|
|
passProbabilityInt, _ := strconv.Atoi(passProbability.Round(0).String())
|
|
if passProbabilityInt > maxValue {
|
|
maxValue = passProbabilityInt + 1
|
|
}
|
|
if passProbabilityInt < minValue {
|
|
minValue = passProbabilityInt - 1
|
|
}
|
|
y = append(y, passProbability.String())
|
|
}
|
|
} else {
|
|
y = append(y, "")
|
|
}
|
|
}
|
|
line = append(line, &model.Line{
|
|
Name: pmc.Pmc03,
|
|
X: x,
|
|
Y: y,
|
|
})
|
|
}
|
|
return line, maxValue, minValue
|
|
}
|
|
|
|
func OrderNumberHistogram(r *request.PackagePassProbabilityLine) []*model.Histogram {
|
|
var fTHistogram []*model.Histogram
|
|
var sql string
|
|
if r.OrderDateStart != "" && r.OrderDateEnd != "" {
|
|
sql += "tc_sfc02 BETWEEN DATE '" + r.OrderDateStart + "' AND DATE '" + r.OrderDateEnd + "'"
|
|
}
|
|
//else if r.OrderDateStart != "" && r.OrderDateEnd == "" {
|
|
// sql += "tc_sfc02 >= DATE '" + r.OrderDateStart + "'"
|
|
//} else if r.OrderDateStart == "" && r.OrderDateEnd != "" {
|
|
// sql += "tc_sfc02 <= DATE '" + r.OrderDateEnd + "'"
|
|
//}
|
|
var factories, x []string
|
|
if len(r.Factory) == 0 {
|
|
global.Oracle.Model(&model.TcSfcFile{}).Where("tc_sfc08 LIKE ?", "%AB%").Where(sql).
|
|
Group("tc_sfc04").Select("tc_sfc04").Find(&factories)
|
|
} else {
|
|
factories = r.Factory
|
|
}
|
|
productMap := make(map[string]int)
|
|
for _, product := range r.Product {
|
|
productMap[product] = 1
|
|
}
|
|
lotMap := make(map[string]int)
|
|
for _, lot := range r.Lot {
|
|
lotMap[lot] = 1
|
|
}
|
|
specificationMap := make(map[string]int)
|
|
for _, specification := range r.Package {
|
|
specificationMap[specification] = 1
|
|
}
|
|
if r.DateSize == "天" {
|
|
days := int(carbon.Parse(r.OrderDateStart).DiffAbsInDays(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < days; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddDays(i).Format("Y-m-d"))
|
|
}
|
|
} else if r.DateSize == "周" {
|
|
weeks := int(carbon.Parse(r.OrderDateStart).DiffAbsInWeeks(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < weeks; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddWeeks(i).Format("Y-m-d"))
|
|
}
|
|
} else if r.DateSize == "月" {
|
|
months := int(carbon.Parse(r.OrderDateStart).DiffAbsInMonths(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < months; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddMonths(i).Format("m"))
|
|
}
|
|
} else if r.DateSize == "年" {
|
|
years := int(carbon.Parse(r.OrderDateStart).DiffAbsInYears(carbon.Parse(r.OrderDateEnd))) + 1
|
|
for i := 0; i < years; i++ {
|
|
x = append(x, carbon.Parse(r.OrderDateStart).AddYears(i).Format("Y"))
|
|
}
|
|
}
|
|
for _, factory := range factories {
|
|
var pmc *model.PmcFile
|
|
global.Oracle.Where("pmc01 = ?", factory).Find(&pmc)
|
|
var tcSfcFile []*model.TcSfcFile
|
|
global.Oracle.Where("tc_sfc08 LIKE ? AND tc_sfc04 = ?", "%AB%", factory).Where(sql).
|
|
Preload("TcSfdFile", func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("ImaFile").Preload("RvaFile", func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("IddFile")
|
|
})
|
|
}).Preload("TcSfeFile", func(db *gorm.DB) *gorm.DB {
|
|
return db.Preload("ImaFile")
|
|
}).Order("tc_sfc02").Find(&tcSfcFile)
|
|
quantityMap := make(map[string]float64)
|
|
var y []string
|
|
for _, tcSfc := range tcSfcFile {
|
|
for _, tcSfd := range tcSfc.TcSfdFile {
|
|
if _, ok := productMap[tcSfd.ImaFile.Ima02]; !ok && len(productMap) > 0 {
|
|
continue
|
|
}
|
|
if _, ok := specificationMap[tcSfd.ImaFile.Ima021]; !ok && len(specificationMap) > 0 {
|
|
continue
|
|
}
|
|
var stockInQuantity float64
|
|
//for _, rva := range tcSfd.RvaFile {
|
|
for _, idd := range tcSfd.RvaFile.IddFile {
|
|
if _, ok := lotMap[idd.Idd04]; !ok && len(lotMap) > 0 {
|
|
continue
|
|
}
|
|
stockInQuantity += idd.Idd13
|
|
}
|
|
//}
|
|
var s string
|
|
if r.DateSize == "天" {
|
|
s = carbon.Parse(r.OrderDateStart).AddDays(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInDays(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y-m-d")
|
|
} else if r.DateSize == "周" {
|
|
s = carbon.Parse(r.OrderDateStart).AddWeeks(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInWeeks(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y-m-d")
|
|
} else if r.DateSize == "月" {
|
|
s = carbon.Parse(r.OrderDateStart).AddMonths(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInMonths(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("m")
|
|
} else if r.DateSize == "年" {
|
|
s = carbon.Parse(r.OrderDateStart).AddYears(int(carbon.Parse(r.OrderDateStart).
|
|
DiffAbsInYears(carbon.CreateFromStdTime(tcSfc.TcSfc02)))).Format("Y")
|
|
}
|
|
if _, ok := quantityMap[s]; !ok {
|
|
quantityMap[s] = stockInQuantity
|
|
} else {
|
|
quantityMap[s] += stockInQuantity
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, xDate := range x {
|
|
if _, ok := quantityMap[xDate]; ok {
|
|
y = append(y, decimal.NewFromFloat(quantityMap[xDate]).String())
|
|
} else {
|
|
y = append(y, "0")
|
|
}
|
|
}
|
|
fTHistogram = append(fTHistogram, &model.Histogram{
|
|
Field: pmc.Pmc03,
|
|
//Field: factory,
|
|
X: x,
|
|
Y: y,
|
|
})
|
|
}
|
|
return fTHistogram
|
|
}
|
|
|
|
func PassProbabilityByFactory(r *request.PassProbabilityByFactory) ([]*model.Line, []*model.Histogram, int, int) {
|
|
var lines []*model.Line
|
|
var histogram []*model.Histogram
|
|
maxValue := math.MinInt
|
|
minValue := math.MaxInt
|
|
startDate := carbon.Now().SubYear().StartOfMonth().Format("Y-m-d")
|
|
endDate := carbon.Now().EndOfMonth().Format("Y-m-d")
|
|
var factorys []string
|
|
var sql string
|
|
if r.Factory != "" {
|
|
sql += "factory = '" + r.Factory + "'"
|
|
}
|
|
global.PostGreSQL.Model(&model.FinalReport{}).Where("step LIKE ? AND order_date BETWEEN ? AND ?", "%"+r.Type+"%", startDate, endDate).
|
|
Where(sql).Group("factory").Select("Factory").Find(&factorys)
|
|
for _, factory := range factorys {
|
|
var x, lineY, histogramY []string
|
|
var finalReports []*model.FinalReport
|
|
global.PostGreSQL.Where("step LIKE ? AND factory = ? AND order_date BETWEEN ? AND ?", "%"+r.Type+"%", factory, startDate, endDate).
|
|
Find(&finalReports)
|
|
factoryMap := make(map[string][]string)
|
|
for _, report := range finalReports {
|
|
if _, ok := factoryMap[report.OrderDate[:7]]; !ok {
|
|
factoryMap[report.OrderDate[:7]] = []string{report.ReportTestQuantity, report.ReportPassQuantity}
|
|
} else {
|
|
factoryMapTestQuantity, _ := decimal.NewFromString(factoryMap[report.OrderDate[:7]][0])
|
|
factoryMapPassQuantity, _ := decimal.NewFromString(factoryMap[report.OrderDate[:7]][1])
|
|
testQuantity, _ := decimal.NewFromString(report.ReportTestQuantity)
|
|
passQuantity, _ := decimal.NewFromString(report.ReportPassQuantity)
|
|
factoryMap[report.OrderDate[:7]] = []string{factoryMapTestQuantity.Add(testQuantity).String(),
|
|
factoryMapPassQuantity.Add(passQuantity).String()}
|
|
}
|
|
}
|
|
month := carbon.Now().SubYear().StartOfMonth().Format("Y-m")
|
|
for {
|
|
if month == carbon.Now().EndOfMonth().Format("Y-m") {
|
|
break
|
|
}
|
|
x = append(x, month)
|
|
if _, ok := factoryMap[month]; !ok {
|
|
lineY = append(lineY, "")
|
|
histogramY = append(histogramY, "")
|
|
} else {
|
|
testQuantity, _ := decimal.NewFromString(factoryMap[month][0])
|
|
passQuantity, _ := decimal.NewFromString(factoryMap[month][1])
|
|
if !testQuantity.IsZero() {
|
|
passProbability := passQuantity.Div(testQuantity).
|
|
Round(4).Mul(decimal.NewFromFloat(100))
|
|
passProbabilityInt, _ := strconv.Atoi(passProbability.Round(0).String())
|
|
if passProbabilityInt > maxValue {
|
|
maxValue = passProbabilityInt + 1
|
|
}
|
|
if passProbabilityInt < minValue {
|
|
minValue = passProbabilityInt - 1
|
|
}
|
|
lineY = append(lineY, passProbability.String())
|
|
} else {
|
|
lineY = append(lineY, "")
|
|
}
|
|
histogramY = append(histogramY, factoryMap[month][0])
|
|
|
|
}
|
|
month = carbon.Parse(month).AddMonth().Format("Y-m")
|
|
}
|
|
lines = append(lines, &model.Line{
|
|
Name: factory,
|
|
X: x,
|
|
Y: lineY,
|
|
})
|
|
histogram = append(histogram, &model.Histogram{
|
|
Field: factory,
|
|
X: x,
|
|
Y: histogramY,
|
|
})
|
|
}
|
|
return lines, histogram, maxValue, minValue
|
|
}
|
|
|
|
func FTPassProbabilityByProduct(r *request.PassProbabilityByProduct) (*model.Line, *model.Histogram, *model.Line, *model.Histogram) {
|
|
var line, binLine *model.Line
|
|
var histogram, binHistogram *model.Histogram
|
|
maxValue := math.MinInt
|
|
minValue := math.MaxInt
|
|
startDate := carbon.Now().SubYear().StartOfMonth().Format("Y-m-d")
|
|
endDate := carbon.Now().EndOfMonth().Format("Y-m-d")
|
|
var sql string
|
|
if r.Factory != "" {
|
|
sql += "factory = '" + r.Factory + "'"
|
|
}
|
|
var finalReports []*model.FinalReport
|
|
global.PostGreSQL.Where("step LIKE ? AND product = ? AND order_date BETWEEN ? AND ?",
|
|
"%"+r.Type+"%", r.Product, startDate, endDate).Where(sql).Find(&finalReports)
|
|
productMap := make(map[string][]string)
|
|
binMap := make(map[string]float64)
|
|
var x, lineY, histogramY []string
|
|
for _, report := range finalReports {
|
|
if _, ok := productMap[report.OrderDate[:7]]; !ok {
|
|
productMap[report.OrderDate[:7]] = []string{report.ReportTestQuantity, report.ReportPassQuantity}
|
|
} else {
|
|
factoryMapTestQuantity, _ := decimal.NewFromString(productMap[report.OrderDate[:7]][0])
|
|
factoryMapPassQuantity, _ := decimal.NewFromString(productMap[report.OrderDate[:7]][1])
|
|
testQuantity, _ := decimal.NewFromString(report.ReportTestQuantity)
|
|
passQuantity, _ := decimal.NewFromString(report.ReportPassQuantity)
|
|
productMap[report.OrderDate[:7]] = []string{factoryMapTestQuantity.Add(testQuantity).String(),
|
|
factoryMapPassQuantity.Add(passQuantity).String()}
|
|
}
|
|
//for _, binFail := range report.BinFail {
|
|
// if _, ok := binMap[binFail.HardBin]; !ok {
|
|
// binMap[binFail.HardBin] = 0
|
|
// }
|
|
// quantityDecimal, _ := decimal.NewFromString(binFail.Quantity)
|
|
// quantity, _ := quantityDecimal.Float64()
|
|
// binMap[binFail.HardBin] += quantity
|
|
//}
|
|
bin1Decimal, _ := decimal.NewFromString(report.Bin1)
|
|
bin1, _ := bin1Decimal.Float64()
|
|
binMap["1"] += bin1
|
|
bin2Decimal, _ := decimal.NewFromString(report.Bin2)
|
|
bin2, _ := bin2Decimal.Float64()
|
|
binMap["2"] += bin2
|
|
bin3Decimal, _ := decimal.NewFromString(report.Bin3)
|
|
bin3, _ := bin3Decimal.Float64()
|
|
binMap["3"] += bin3
|
|
bin4Decimal, _ := decimal.NewFromString(report.Bin4)
|
|
bin4, _ := bin4Decimal.Float64()
|
|
binMap["4"] += bin4
|
|
bin5Decimal, _ := decimal.NewFromString(report.Bin5)
|
|
bin5, _ := bin5Decimal.Float64()
|
|
binMap["5"] += bin5
|
|
bin6Decimal, _ := decimal.NewFromString(report.Bin6)
|
|
bin6, _ := bin6Decimal.Float64()
|
|
binMap["6"] += bin6
|
|
bin7Decimal, _ := decimal.NewFromString(report.Bin7)
|
|
bin7, _ := bin7Decimal.Float64()
|
|
binMap["7"] += bin7
|
|
bin8Decimal, _ := decimal.NewFromString(report.Bin8)
|
|
bin8, _ := bin8Decimal.Float64()
|
|
binMap["8"] += bin8
|
|
bin9Decimal, _ := decimal.NewFromString(report.Bin9)
|
|
bin9, _ := bin9Decimal.Float64()
|
|
binMap["9"] += bin9
|
|
bin10Decimal, _ := decimal.NewFromString(report.Bin10)
|
|
bin10, _ := bin10Decimal.Float64()
|
|
binMap["10"] += bin10
|
|
}
|
|
month := carbon.Now().SubYear().AddMonth().Format("Y-m")
|
|
var testStr, passStr string
|
|
for {
|
|
if month == carbon.Now().AddMonth().Format("Y-m") {
|
|
break
|
|
}
|
|
x = append(x, month)
|
|
if _, ok := productMap[month]; !ok {
|
|
lineY = append(lineY, "")
|
|
histogramY = append(histogramY, "")
|
|
} else {
|
|
testQuantity, _ := decimal.NewFromString(productMap[month][0])
|
|
passQuantity, _ := decimal.NewFromString(productMap[month][1])
|
|
testDecimal, _ := decimal.NewFromString(testStr)
|
|
passDecimal, _ := decimal.NewFromString(passStr)
|
|
testStr = testDecimal.Add(testQuantity).String()
|
|
passStr = passDecimal.Add(passQuantity).String()
|
|
passProbability := passQuantity.Div(testQuantity).
|
|
Round(4).Mul(decimal.NewFromFloat(100))
|
|
passProbabilityInt, _ := strconv.Atoi(passProbability.Round(0).String())
|
|
if passProbabilityInt > maxValue {
|
|
maxValue = passProbabilityInt + 1
|
|
}
|
|
if passProbabilityInt < minValue {
|
|
minValue = passProbabilityInt - 1
|
|
}
|
|
lineY = append(lineY, passProbability.String())
|
|
histogramY = append(histogramY, productMap[month][0])
|
|
}
|
|
month = carbon.Parse(month).AddMonth().Format("Y-m")
|
|
}
|
|
var binX, binHistogramY, binLineY []string
|
|
var binFail float64
|
|
for bin, quantity := range binMap {
|
|
if bin != "1" {
|
|
if quantity != 0 {
|
|
binX = append(binX, bin)
|
|
binFail += quantity
|
|
}
|
|
}
|
|
}
|
|
sort.Slice(binX, func(i, j int) bool {
|
|
return binMap[binX[i]] > binMap[binX[j]]
|
|
})
|
|
var accumulation float64
|
|
//testDecimal, _ := decimal.NewFromString(testStr)
|
|
//passDecimal, _ := decimal.NewFromString(passStr)
|
|
//failQuantity, _ := testDecimal.Sub(passDecimal).Float64()
|
|
failQuantity := binFail
|
|
for _, bin := range binX {
|
|
binHistogramY = append(binHistogramY, decimal.NewFromFloat(binMap[bin]).String())
|
|
accumulation += binMap[bin]
|
|
if !decimal.NewFromFloat(failQuantity).IsZero() {
|
|
binLineY = append(binLineY, decimal.NewFromFloat(accumulation).Div(decimal.NewFromFloat(failQuantity)).
|
|
Round(4).Mul(decimal.NewFromFloat(100)).String())
|
|
} else {
|
|
binLineY = append(binLineY, "0")
|
|
}
|
|
|
|
}
|
|
line = &model.Line{
|
|
Name: r.Product,
|
|
X: x,
|
|
Y: lineY,
|
|
}
|
|
histogram = &model.Histogram{
|
|
Field: r.Product,
|
|
X: x,
|
|
Y: histogramY,
|
|
}
|
|
binLine = &model.Line{
|
|
Name: r.Product,
|
|
X: binX,
|
|
Y: binLineY,
|
|
}
|
|
binHistogram = &model.Histogram{
|
|
Field: r.Product,
|
|
X: binX,
|
|
Y: binHistogramY,
|
|
}
|
|
return line, histogram, binLine, binHistogram
|
|
}
|
|
|
|
func PassProbabilityLine(r *request.TestPassProbabilityLine) {
|
|
|
|
}
|
|
|
|
func PassProbability(r *request.PassProbability) (interface{}, string) {
|
|
r.EndDate = carbon.Parse(r.EndDate).AddDay().Format("Y-m-d")
|
|
var reports []*model.Report
|
|
if len(r.Product) == 0 && len(r.Lot) == 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Order("created_at").Find(&reports)
|
|
} else if len(r.Product) != 0 && len(r.Lot) == 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Where("product IN ?", r.Product).Order("created_at").Find(&reports)
|
|
} else if len(r.Product) == 0 && len(r.Lot) != 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Where("lot IN ?", r.Lot).Order("created_at").Find(&reports)
|
|
} else if len(r.Product) != 0 && len(r.Lot) != 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Where("product IN ? AND lot IN ?", r.Product, r.Lot).Order("created_at").Find(&reports)
|
|
}
|
|
|
|
passProbabilityMap := make(map[string][]float64)
|
|
passProbabilityGroupMap := make(map[string]map[string][]float64)
|
|
|
|
startDate := carbon.Parse(r.StartDate)
|
|
endDate := carbon.Parse(r.EndDate)
|
|
//if len(r.X) == 1 && strings.Contains(r.X[0], "time") {
|
|
// if strings.Contains(r.X[0], "day") {
|
|
// for startDate.Lte(endDate) {
|
|
// passProbabilityMap[startDate.Format("Y-m-d")] = []float64{}
|
|
// startDate = startDate.AddDay()
|
|
// }
|
|
// } else if strings.Contains(r.X[0], "week") {
|
|
// for startDate.Lte(endDate) {
|
|
// passProbabilityMap[startDate.Format("Y-m-d")] = []float64{}
|
|
// startDate = startDate.AddWeek()
|
|
// }
|
|
// } else if strings.Contains(r.X[0], "month") {
|
|
// for startDate.Lte(endDate) {
|
|
// passProbabilityMap[startDate.Format("Y-m")] = []float64{}
|
|
// startDate = startDate.AddMonth()
|
|
// }
|
|
// }
|
|
//}
|
|
for _, report := range reports {
|
|
var s string
|
|
|
|
for _, x := range r.X {
|
|
if s != "" {
|
|
s += "-"
|
|
}
|
|
if x == "product" {
|
|
s += report.Product
|
|
} else if x == "lot" {
|
|
s += report.Lot
|
|
} else if x == "wafer_id" {
|
|
s += report.WaferID
|
|
} else if x == "sub_batch" {
|
|
s += report.SubBatch
|
|
} else if x == "factory" {
|
|
s += report.Factory
|
|
} else if x == "test_machine" {
|
|
s += report.TestMachine
|
|
} else if x == "test_program" {
|
|
s += report.TestProgram
|
|
} else if strings.Contains(x, "time") {
|
|
if strings.Contains(x, "day") {
|
|
s += carbon.Parse(report.OrderDate).Format("Y-m-d")
|
|
} else if strings.Contains(x, "week") {
|
|
weekDate := carbon.Parse(report.OrderDate)
|
|
weekDate = startDate.AddWeeks(int(weekDate.DiffAbsInWeeks(startDate)))
|
|
s += weekDate.Format("Y-m-d")
|
|
} else if strings.Contains(x, "month") {
|
|
s += carbon.Parse(report.OrderDate).Format("Y-m")
|
|
}
|
|
}
|
|
}
|
|
if _, ok := passProbabilityMap[s]; !ok {
|
|
passProbabilityMap[s] = []float64{}
|
|
}
|
|
testQuantityDecimal, _ := decimal.NewFromString(report.TestQuantity)
|
|
passQuantityDecimal, _ := decimal.NewFromString(report.PassQuantity)
|
|
if !testQuantityDecimal.IsZero() {
|
|
passProbability, _ := passQuantityDecimal.Div(testQuantityDecimal).Round(4).Mul(decimal.NewFromInt(100)).Float64()
|
|
passProbabilityMap[s] = append(passProbabilityMap[s], passProbability)
|
|
for _, group := range r.Group {
|
|
if group == "factory" {
|
|
if _, ok := passProbabilityGroupMap[report.Factory]; !ok {
|
|
passProbabilityGroupMap[report.Factory] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.Factory][s] = append(passProbabilityGroupMap[report.Factory][s], passProbability)
|
|
} else if group == "product" {
|
|
if _, ok := passProbabilityGroupMap[report.Product]; !ok {
|
|
passProbabilityGroupMap[report.Product] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.Product][s] = append(passProbabilityGroupMap[report.Product][s], passProbability)
|
|
} else if group == "test_program" {
|
|
if _, ok := passProbabilityGroupMap[report.TestProgram]; !ok {
|
|
passProbabilityGroupMap[report.TestProgram] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.TestProgram][s] = append(passProbabilityGroupMap[report.TestProgram][s], passProbability)
|
|
} else if group == "test_machine" {
|
|
if _, ok := passProbabilityGroupMap[report.TestMachine]; !ok {
|
|
passProbabilityGroupMap[report.TestMachine] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.TestMachine][s] = append(passProbabilityGroupMap[report.TestMachine][s], passProbability)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if r.ChartType == "line" {
|
|
var lines []*model.Line
|
|
var x, y []string
|
|
if len(r.X) == 1 && strings.Contains(r.X[0], "time") {
|
|
if strings.Contains(r.X[0], "day") {
|
|
for startDate.Lte(endDate) {
|
|
average := 0.0
|
|
k := startDate.Format("Y-m-d")
|
|
if _, ok := passProbabilityMap[k]; ok {
|
|
for _, passProbability := range passProbabilityMap[k] {
|
|
average += passProbability
|
|
}
|
|
}
|
|
if len(passProbabilityMap[k]) != 0 {
|
|
average /= float64(len(passProbabilityMap[k]))
|
|
}
|
|
x = append(x, k)
|
|
if average != 0.0 {
|
|
y = append(y, decimal.NewFromFloat(average).Round(2).String())
|
|
} else {
|
|
y = append(y, "")
|
|
}
|
|
startDate = startDate.AddDay()
|
|
}
|
|
} else if strings.Contains(r.X[0], "week") {
|
|
for startDate.Lte(endDate) {
|
|
average := 0.0
|
|
k := startDate.Format("Y-m-d")
|
|
if _, ok := passProbabilityMap[k]; ok {
|
|
for _, passProbability := range passProbabilityMap[k] {
|
|
average += passProbability
|
|
}
|
|
}
|
|
if len(passProbabilityMap[k]) != 0 {
|
|
average /= float64(len(passProbabilityMap[k]))
|
|
}
|
|
x = append(x, k)
|
|
if average != 0.0 {
|
|
y = append(y, decimal.NewFromFloat(average).Round(2).String())
|
|
} else {
|
|
y = append(y, "")
|
|
}
|
|
startDate = startDate.AddWeek()
|
|
}
|
|
} else if strings.Contains(r.X[0], "month") {
|
|
for startDate.Lte(endDate) {
|
|
average := 0.0
|
|
k := startDate.Format("Y-m")
|
|
if _, ok := passProbabilityMap[k]; ok {
|
|
for _, passProbability := range passProbabilityMap[k] {
|
|
average += passProbability
|
|
}
|
|
}
|
|
if len(passProbabilityMap[k]) != 0 {
|
|
average /= float64(len(passProbabilityMap[k]))
|
|
}
|
|
x = append(x, k)
|
|
if average != 0.0 {
|
|
y = append(y, decimal.NewFromFloat(average).Round(2).String())
|
|
} else {
|
|
y = append(y, "")
|
|
}
|
|
startDate = startDate.AddMonth()
|
|
}
|
|
}
|
|
} else {
|
|
for k, v := range passProbabilityMap {
|
|
average := 0.0
|
|
for _, passProbability := range v {
|
|
average += passProbability
|
|
}
|
|
if len(v) != 0 {
|
|
average /= float64(len(v))
|
|
}
|
|
x = append(x, k)
|
|
y = append(y, decimal.NewFromFloat(average).Round(2).String())
|
|
}
|
|
}
|
|
if len(r.Group) == 0 {
|
|
line := &model.Line{
|
|
Name: "良率 折线图",
|
|
X: x,
|
|
Y: y,
|
|
}
|
|
lines = append(lines, line)
|
|
}
|
|
for group, passProbabilityGroup := range passProbabilityGroupMap {
|
|
var groupY []string
|
|
for _, s := range x {
|
|
if _, ok := passProbabilityGroup[s]; !ok {
|
|
groupY = append(groupY, "")
|
|
} else {
|
|
averagea := 0.0
|
|
for _, passProbability := range passProbabilityGroup[s] {
|
|
averagea += passProbability
|
|
}
|
|
if len(passProbabilityGroup[s]) != 0 {
|
|
averagea /= float64(len(passProbabilityGroup[s]))
|
|
}
|
|
groupY = append(groupY, decimal.NewFromFloat(averagea).Round(2).String())
|
|
}
|
|
}
|
|
lineSeries := &model.Line{
|
|
Name: group,
|
|
X: x,
|
|
Y: groupY,
|
|
}
|
|
lines = append(lines, lineSeries)
|
|
}
|
|
return lines, "良率(%)"
|
|
} else if r.ChartType == "boxPlot" {
|
|
var boxPlots []*model.BoxPlot
|
|
var x []string
|
|
var values [][]float64
|
|
for k, v := range passProbabilityMap {
|
|
x = append(x, k)
|
|
values = append(values, v)
|
|
}
|
|
if len(r.Group) == 0 {
|
|
boxPlot := &model.BoxPlot{
|
|
Name: "良率 箱型图",
|
|
X: x,
|
|
Values: values,
|
|
}
|
|
boxPlots = append(boxPlots, boxPlot)
|
|
}
|
|
for group, passProbabilityGroup := range passProbabilityGroupMap {
|
|
var groupY [][]float64
|
|
for _, s := range x {
|
|
if _, ok := passProbabilityGroup[s]; !ok {
|
|
groupY = append(groupY, []float64{})
|
|
} else {
|
|
groupY = append(groupY, passProbabilityGroup[s])
|
|
}
|
|
}
|
|
boxPlotSeries := &model.BoxPlot{
|
|
Name: group,
|
|
X: x,
|
|
Values: groupY,
|
|
}
|
|
boxPlots = append(boxPlots, boxPlotSeries)
|
|
}
|
|
return boxPlots, "良率(%)"
|
|
}
|
|
return nil, ""
|
|
}
|
|
|
|
func BinProbability(r *request.BinProbability) (interface{}, string) {
|
|
var reports []*model.Report
|
|
if len(r.Product) == 0 && len(r.Lot) == 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Preload("BinFail", func(db *gorm.DB) *gorm.DB {
|
|
return db.Where("report_site_id = ?", 0)
|
|
}).Order("created_at").Find(&reports)
|
|
} else if len(r.Product) != 0 && len(r.Lot) == 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Where("product IN ?", r.Product).Preload("BinFail", func(db *gorm.DB) *gorm.DB {
|
|
return db.Where("report_site_id = ?", 0)
|
|
}).Order("created_at").Find(&reports)
|
|
} else if len(r.Product) == 0 && len(r.Lot) != 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Where("lot IN ?", r.Lot).Preload("BinFail", func(db *gorm.DB) *gorm.DB {
|
|
return db.Where("report_site_id = ?", 0)
|
|
}).Order("created_at").Find(&reports)
|
|
} else if len(r.Product) != 0 && len(r.Lot) != 0 {
|
|
global.PostGreSQL.Where("step LIKE ? AND DATE(created_at) BETWEEN DATE(?) AND DATE(?)", "%"+r.Step+"%", r.StartDate, r.EndDate).
|
|
Where("product IN ? AND lot IN ?", r.Product, r.Lot).Preload("BinFail", func(db *gorm.DB) *gorm.DB {
|
|
return db.Where("report_site_id = ?", 0)
|
|
}).Order("created_at").Find(&reports)
|
|
}
|
|
|
|
passProbabilityMap := make(map[string][]float64)
|
|
passProbabilityGroupMap := make(map[string]map[string][]float64)
|
|
for _, report := range reports {
|
|
var s string
|
|
for _, x := range r.X {
|
|
if s != "" {
|
|
s += "-"
|
|
}
|
|
if x == "product" {
|
|
s += report.Product
|
|
} else if x == "lot" {
|
|
s += report.Lot
|
|
} else if x == "wafer_id" {
|
|
s += report.WaferID
|
|
} else if x == "sub_batch" {
|
|
s += report.SubBatch
|
|
} else if x == "factory" {
|
|
s += report.Factory
|
|
} else if x == "test_machine" {
|
|
s += report.TestMachine
|
|
} else if x == "test_program" {
|
|
s += report.TestProgram
|
|
}
|
|
}
|
|
|
|
if _, ok := passProbabilityMap[s]; !ok {
|
|
passProbabilityMap[s] = []float64{}
|
|
}
|
|
testQuantityDecimal, _ := decimal.NewFromString(report.TestQuantity)
|
|
for _, binFail := range report.BinFail {
|
|
if r.BinType == "Hard Bin" {
|
|
if binFail.HardBin == r.Bin {
|
|
binDecimal, _ := decimal.NewFromString(binFail.Quantity)
|
|
if !testQuantityDecimal.IsZero() {
|
|
passProbability, _ := binDecimal.Div(testQuantityDecimal).Round(4).Mul(decimal.NewFromInt(100)).Float64()
|
|
passProbabilityMap[s] = append(passProbabilityMap[s], passProbability)
|
|
for _, group := range r.Group {
|
|
if group == "factory" {
|
|
if _, ok := passProbabilityGroupMap[report.Factory]; !ok {
|
|
passProbabilityGroupMap[report.Factory] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.Factory][s] = append(passProbabilityGroupMap[report.Factory][s], passProbability)
|
|
} else if group == "product" {
|
|
if _, ok := passProbabilityGroupMap[report.Product]; !ok {
|
|
passProbabilityGroupMap[report.Product] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.Product][s] = append(passProbabilityGroupMap[report.Product][s], passProbability)
|
|
} else if group == "test_program" {
|
|
if _, ok := passProbabilityGroupMap[report.TestProgram]; !ok {
|
|
passProbabilityGroupMap[report.TestProgram] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.TestProgram][s] = append(passProbabilityGroupMap[report.TestProgram][s], passProbability)
|
|
} else if group == "test_machine" {
|
|
if _, ok := passProbabilityGroupMap[report.TestMachine]; !ok {
|
|
passProbabilityGroupMap[report.TestMachine] = make(map[string][]float64)
|
|
}
|
|
passProbabilityGroupMap[report.TestMachine][s] = append(passProbabilityGroupMap[report.TestMachine][s], passProbability)
|
|
}
|
|
}
|
|
}
|
|
break
|
|
}
|
|
} else if r.BinType == "Soft Bin" {
|
|
if binFail.SoftBin == r.Bin {
|
|
binDecimal, _ := decimal.NewFromString(binFail.Quantity)
|
|
if !testQuantityDecimal.IsZero() {
|
|
passProbability, _ := binDecimal.Div(testQuantityDecimal).Round(4).Mul(decimal.NewFromInt(100)).Float64()
|
|
passProbabilityMap[s] = append(passProbabilityMap[s], passProbability)
|
|
for _, group := range r.Group {
|
|
if group == "factory" {
|
|
if _, ok := passProbabilityGroupMap[report.Factory]; !ok {
|
|
passProbabilityGroupMap[report.Factory] = make(map[string][]float64)
|
|
}
|
|
//if _, ok := passProbabilityGroupMap[report.Factory][s]; !ok {
|
|
// passProbabilityGroupMap[report.Factory][s] = []float64{}
|
|
//}
|
|
passProbabilityGroupMap[report.Factory][s] = append(passProbabilityGroupMap[report.Factory][s], passProbability)
|
|
}
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if r.ChartType == "line" {
|
|
var lines []*model.Line
|
|
var x, y []string
|
|
for k, v := range passProbabilityMap {
|
|
average := 0.0
|
|
for _, passProbability := range v {
|
|
average += passProbability
|
|
}
|
|
if len(v) != 0 {
|
|
average /= float64(len(v))
|
|
}
|
|
x = append(x, k)
|
|
y = append(y, decimal.NewFromFloat(average).Round(2).String())
|
|
}
|
|
if len(r.Group) == 0 {
|
|
line := &model.Line{
|
|
Name: "Bin 折线图",
|
|
X: x,
|
|
Y: y,
|
|
}
|
|
lines = append(lines, line)
|
|
}
|
|
for group, passProbabilityGroup := range passProbabilityGroupMap {
|
|
var groupY []string
|
|
for _, s := range x {
|
|
if _, ok := passProbabilityGroup[s]; !ok {
|
|
groupY = append(groupY, "")
|
|
} else {
|
|
averagea := 0.0
|
|
for _, passProbability := range passProbabilityGroup[s] {
|
|
averagea += passProbability
|
|
}
|
|
averagea /= float64(len(passProbabilityGroup[s]))
|
|
groupY = append(groupY, decimal.NewFromFloat(averagea).Round(2).String())
|
|
}
|
|
}
|
|
linea := &model.Line{
|
|
Name: group,
|
|
X: x,
|
|
Y: groupY,
|
|
}
|
|
lines = append(lines, linea)
|
|
}
|
|
return lines, "Bin(%)"
|
|
} else if r.ChartType == "boxPlot" {
|
|
var boxPlots []*model.BoxPlot
|
|
var x []string
|
|
var values [][]float64
|
|
for k, v := range passProbabilityMap {
|
|
x = append(x, k)
|
|
values = append(values, v)
|
|
}
|
|
if len(r.Group) == 0 {
|
|
boxPlot := &model.BoxPlot{
|
|
Name: "良率 箱型图",
|
|
X: x,
|
|
Values: values,
|
|
}
|
|
boxPlots = append(boxPlots, boxPlot)
|
|
}
|
|
for group, passProbabilityGroup := range passProbabilityGroupMap {
|
|
var groupY [][]float64
|
|
for _, s := range x {
|
|
if _, ok := passProbabilityGroup[s]; !ok {
|
|
groupY = append(groupY, []float64{})
|
|
} else {
|
|
groupY = append(groupY, passProbabilityGroup[s])
|
|
}
|
|
}
|
|
boxPlotSeries := &model.BoxPlot{
|
|
Name: group,
|
|
X: x,
|
|
Values: groupY,
|
|
}
|
|
boxPlots = append(boxPlots, boxPlotSeries)
|
|
}
|
|
return boxPlots, "Bin(%)"
|
|
}
|
|
return nil, ""
|
|
}
|