diff --git a/repository/test.data/report.list.cron.go b/repository/test.data/report.list.cron.go index c3afba4..568e2fa 100644 --- a/repository/test.data/report.list.cron.go +++ b/repository/test.data/report.list.cron.go @@ -302,10 +302,38 @@ func CronABList() { //} passProbability = decimal.NewFromFloat(passQuantity).Div(decimal.NewFromFloat(tcSfd.TcSfd05)). Round(4).Mul(decimal.NewFromFloat(100)).String() + var abList *model.ABList + global.PostGreSQL.Where("pbi = ? AND outsourcing_pbi = ?", + tcSfc.TcSfc01, tcSfd.TcSfd13).Find(&abList) global.PostGreSQL.Transaction(func(tx *gorm.DB) error { - var abList *model.ABList - tx.Where("pbi = ? AND outsourcing_pbi = ?", - tcSfc.TcSfc01, tcSfd.TcSfd13).Find(&abList) + var abListStocks []*model.ABListStock + global.PostGreSQL.Where("ab_list_id = ?", abList.ID).Find(&abListStocks) + var addABListStocks []*model.ABListStock + for k, v := range orderDateStockMap { + var isHandled bool + for _, abListStock := range abListStocks { + if abListStock.ReturnDate == k { + isHandled = true + } + } + if !isHandled { + addABListStocks = append(addABListStocks, &model.ABListStock{ + ABListID: abList.ID, + ReturnDate: k, + PassQuantity: v[0], + FailQuantity: v[1], + }) + } + } + var deleteABListStocks []*model.ABListStock + for _, abListStock := range abListStocks { + if _, ok := orderDateStockMap[abListStock.ReturnDate]; !ok { + deleteABListStocks = append(deleteABListStocks, abListStock) + } + } + global.PostGreSQL.Create(&addABListStocks) + global.PostGreSQL.Delete(&deleteABListStocks) + tx.Model(&abList).Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Updates(map[string]interface{}{ "status": status, @@ -318,32 +346,14 @@ func CronABList() { "online_quantity": tcSfd.TcSfd05 - returnQuantity, "pass_probability": passProbability + "%", }) - tx.Where("ab_list_id = ?", abList.ID).Delete(&model.ABListStock{}) - for k, v := range orderDateStockMap { - tx.Create(&model.ABListStock{ - ABListID: abList.ID, - ReturnDate: k, - PassQuantity: v[0], - FailQuantity: v[1], - }) - } + //tx.Where("ab_list_id = ?", abList.ID).Delete(&model.ABListStock{}) //for k, v := range orderDateStockMap { - // var abListStock *model.ABListStock - // if errors.Is(global.PostGreSQL.Where("ab_list_id = ? AND return_date = ?", abList.ID, k). - // First(&abListStock).Error, gorm.ErrRecordNotFound) { - // global.PostGreSQL.Create(&model.ABListStock{ - // ABListID: abList.ID, - // ReturnDate: k, - // PassQuantity: v[0], - // FailQuantity: v[1], - // }) - // } else { - // global.PostGreSQL.Model(&abListStock).Where("ab_list_id = ? AND return_date = ?", abList.ID, k). - // Updates(map[string]interface{}{ - // "pass_quantity": v[0], - // "fail_quantity": v[1], - // }) - // } + // tx.Create(&model.ABListStock{ + // ABListID: abList.ID, + // ReturnDate: k, + // PassQuantity: v[0], + // FailQuantity: v[1], + // }) //} return nil }) @@ -546,6 +556,34 @@ func CronBPList() { global.PostGreSQL.Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Find(&bpList) global.PostGreSQL.Transaction(func(tx *gorm.DB) error { + var bpListStocks []*model.BPListStock + global.PostGreSQL.Where("bp_list_id = ?", bpList.ID).Find(&bpListStocks) + var addBPListStocks []*model.BPListStock + for k, v := range orderDateStockMap { + var isHandled bool + for _, bpListStock := range bpListStocks { + if bpListStock.ReturnDate == k { + isHandled = true + } + } + if !isHandled { + addBPListStocks = append(addBPListStocks, &model.BPListStock{ + BPListID: bpList.ID, + ReturnDate: k, + Quantity: v[0], + DieQuantity: v[1], + }) + } + } + var deleteBPListStocks []*model.BPListStock + for _, bpListStock := range bpListStocks { + if _, ok := orderDateStockMap[bpListStock.ReturnDate]; !ok { + deleteBPListStocks = append(deleteBPListStocks, bpListStock) + } + } + global.PostGreSQL.Create(&addBPListStocks) + global.PostGreSQL.Delete(&deleteBPListStocks) + global.PostGreSQL.Model(&bpList).Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Updates(map[string]interface{}{ "status": status, @@ -553,24 +591,24 @@ func CronBPList() { "return_quantity": returnQuantity, "die_quantity": dieQuantity, }) - for k, v := range orderDateStockMap { - var bpListStock *model.BPListStock - if errors.Is(global.PostGreSQL.Where("bp_list_id = ? AND return_date = ?", bpList.ID, k). - First(&bpListStock).Error, gorm.ErrRecordNotFound) { - global.PostGreSQL.Create(&model.BPListStock{ - BPListID: bpList.ID, - ReturnDate: k, - Quantity: v[0], - DieQuantity: v[1], - }) - } else { - global.PostGreSQL.Model(&bpListStock).Where("bp_list_id = ? AND return_date = ?", bpList.ID, k). - Updates(map[string]interface{}{ - "quantity": v[0], - "die_quantity": v[1], - }) - } - } + //for k, v := range orderDateStockMap { + // var bpListStock *model.BPListStock + // if errors.Is(global.PostGreSQL.Where("bp_list_id = ? AND return_date = ?", bpList.ID, k). + // First(&bpListStock).Error, gorm.ErrRecordNotFound) { + // global.PostGreSQL.Create(&model.BPListStock{ + // BPListID: bpList.ID, + // ReturnDate: k, + // Quantity: v[0], + // DieQuantity: v[1], + // }) + // } else { + // global.PostGreSQL.Model(&bpListStock).Where("bp_list_id = ? AND return_date = ?", bpList.ID, k). + // Updates(map[string]interface{}{ + // "quantity": v[0], + // "die_quantity": v[1], + // }) + // } + //} return nil }) } @@ -733,6 +771,34 @@ func CronCPList() { global.PostGreSQL.Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Find(&cpList) global.PostGreSQL.Transaction(func(tx *gorm.DB) error { + var cpListStocks []*model.CPListStock + global.PostGreSQL.Where("cp_list_id = ?", cpList.ID).Find(&cpListStocks) + var addCPListStocks []*model.CPListStock + for k, v := range orderDateStockMap { + var isHandled bool + for _, cpListStock := range cpListStocks { + if cpListStock.ReturnDate == k { + isHandled = true + } + } + if !isHandled { + addCPListStocks = append(addCPListStocks, &model.CPListStock{ + CPListID: cpList.ID, + ReturnDate: k, + Quantity: v[0], + DieQuantity: v[1], + }) + } + } + var deleteCPListStocks []*model.CPListStock + for _, cpListStock := range cpListStocks { + if _, ok := orderDateStockMap[cpListStock.ReturnDate]; !ok { + deleteCPListStocks = append(deleteCPListStocks, cpListStock) + } + } + global.PostGreSQL.Create(&addCPListStocks) + global.PostGreSQL.Delete(&deleteCPListStocks) + global.PostGreSQL.Model(&cpList).Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Updates(map[string]interface{}{ "status": status, @@ -740,24 +806,24 @@ func CronCPList() { "return_quantity": returnQuantity, "die_quantity": dieQuantity, }) - for k, v := range orderDateStockMap { - var cpListStock *model.CPListStock - if errors.Is(global.PostGreSQL.Where("cp_list_id = ? AND return_date = ?", cpList.ID, k). - First(&cpListStock).Error, gorm.ErrRecordNotFound) { - global.PostGreSQL.Create(&model.CPListStock{ - CPListID: cpList.ID, - ReturnDate: k, - Quantity: v[0], - DieQuantity: v[1], - }) - } else { - global.PostGreSQL.Model(&cpListStock).Where("cp_list_id = ? AND return_date = ?", cpList.ID, k). - Updates(map[string]interface{}{ - "quantity": v[0], - "die_quantity": v[1], - }) - } - } + //for k, v := range orderDateStockMap { + // var cpListStock *model.CPListStock + // if errors.Is(global.PostGreSQL.Where("cp_list_id = ? AND return_date = ?", cpList.ID, k). + // First(&cpListStock).Error, gorm.ErrRecordNotFound) { + // global.PostGreSQL.Create(&model.CPListStock{ + // CPListID: cpList.ID, + // ReturnDate: k, + // Quantity: v[0], + // DieQuantity: v[1], + // }) + // } else { + // global.PostGreSQL.Model(&cpListStock).Where("cp_list_id = ? AND return_date = ?", cpList.ID, k). + // Updates(map[string]interface{}{ + // "quantity": v[0], + // "die_quantity": v[1], + // }) + // } + //} return nil }) } @@ -1031,6 +1097,34 @@ func CronFTList() { global.PostGreSQL.Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Find(&ftList) global.PostGreSQL.Transaction(func(tx *gorm.DB) error { + var ftListStocks []*model.FTListStock + global.PostGreSQL.Where("ft_list_id = ?", ftList.ID).Find(&ftListStocks) + var addFTListStocks []*model.FTListStock + for k, v := range orderDateStockMap { + var isHandled bool + for _, ftListStock := range ftListStocks { + if ftListStock.ReturnDate == k { + isHandled = true + } + } + if !isHandled { + addFTListStocks = append(addFTListStocks, &model.FTListStock{ + FTListID: ftList.ID, + ReturnDate: k, + PassQuantity: v[0], + FailQuantity: v[1], + }) + } + } + var deleteFTListStocks []*model.FTListStock + for _, ftListStock := range ftListStocks { + if _, ok := orderDateStockMap[ftListStock.ReturnDate]; !ok { + deleteFTListStocks = append(deleteFTListStocks, ftListStock) + } + } + global.PostGreSQL.Create(&addFTListStocks) + global.PostGreSQL.Delete(&deleteFTListStocks) + global.PostGreSQL.Model(&ftList).Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Updates(map[string]interface{}{ "wafer_product": waferProduct, @@ -1041,24 +1135,24 @@ func CronFTList() { "fail_quantity": failQuantity, "test_program_version": tcSfd.TcImbFile.TcImb04, }) - for k, v := range orderDateStockMap { - var ftListStock *model.FTListStock - if errors.Is(global.PostGreSQL.Where("ft_list_id = ? AND return_date = ?", ftList.ID, k). - First(&ftListStock).Error, gorm.ErrRecordNotFound) { - global.PostGreSQL.Create(&model.FTListStock{ - FTListID: ftList.ID, - ReturnDate: k, - PassQuantity: v[0], - FailQuantity: v[1], - }) - } else { - global.PostGreSQL.Model(&ftListStock).Where("ft_list_id = ? AND return_date = ?", ftList.ID, k). - Updates(map[string]interface{}{ - "pass_quantity": v[0], - "fail_quantity": v[1], - }) - } - } + //for k, v := range orderDateStockMap { + // var ftListStock *model.FTListStock + // if errors.Is(global.PostGreSQL.Where("ft_list_id = ? AND return_date = ?", ftList.ID, k). + // First(&ftListStock).Error, gorm.ErrRecordNotFound) { + // global.PostGreSQL.Create(&model.FTListStock{ + // FTListID: ftList.ID, + // ReturnDate: k, + // PassQuantity: v[0], + // FailQuantity: v[1], + // }) + // } else { + // global.PostGreSQL.Model(&ftListStock).Where("ft_list_id = ? AND return_date = ?", ftList.ID, k). + // Updates(map[string]interface{}{ + // "pass_quantity": v[0], + // "fail_quantity": v[1], + // }) + // } + //} return nil }) } @@ -1345,6 +1439,34 @@ func CronTRList() { global.PostGreSQL.Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Find(&trList) global.PostGreSQL.Transaction(func(tx *gorm.DB) error { + var trListStocks []*model.TRListStock + global.PostGreSQL.Where("tr_list_id = ?", trList.ID).Find(&trListStocks) + var addTRListStocks []*model.TRListStock + for k, v := range orderDateStockMap { + var isHandled bool + for _, trListStock := range trListStocks { + if trListStock.ReturnDate == k { + isHandled = true + } + } + if !isHandled { + addTRListStocks = append(addTRListStocks, &model.TRListStock{ + TRListID: trList.ID, + ReturnDate: k, + PassQuantity: v[0], + FailQuantity: v[1], + }) + } + } + var deleteTRListStocks []*model.TRListStock + for _, trListStock := range trListStocks { + if _, ok := orderDateStockMap[trListStock.ReturnDate]; !ok { + deleteTRListStocks = append(deleteTRListStocks, trListStock) + } + } + global.PostGreSQL.Create(&addTRListStocks) + global.PostGreSQL.Delete(&deleteTRListStocks) + global.PostGreSQL.Model(&trList).Where("pbi = ? AND outsourcing_pbi = ?", tcSfc.TcSfc01, tcSfd.TcSfd13).Updates(map[string]interface{}{ "status": status, @@ -1352,24 +1474,24 @@ func CronTRList() { "pass_quantity": passQuantity, "fail_quantity": failQuantity, }) - for k, v := range orderDateStockMap { - var trListStock *model.TRListStock - if errors.Is(global.PostGreSQL.Where("tr_list_id = ? AND return_date = ?", trList.ID, k). - First(&trListStock).Error, gorm.ErrRecordNotFound) { - global.PostGreSQL.Create(&model.TRListStock{ - TRListID: trList.ID, - ReturnDate: k, - PassQuantity: v[0], - FailQuantity: v[1], - }) - } else { - global.PostGreSQL.Model(&trListStock).Where("tr_list_id = ? AND return_date = ?", trList.ID, k). - Updates(map[string]interface{}{ - "pass_quantity": v[0], - "fail_quantity": v[1], - }) - } - } + //for k, v := range orderDateStockMap { + // var trListStock *model.TRListStock + // if errors.Is(global.PostGreSQL.Where("tr_list_id = ? AND return_date = ?", trList.ID, k). + // First(&trListStock).Error, gorm.ErrRecordNotFound) { + // global.PostGreSQL.Create(&model.TRListStock{ + // TRListID: trList.ID, + // ReturnDate: k, + // PassQuantity: v[0], + // FailQuantity: v[1], + // }) + // } else { + // global.PostGreSQL.Model(&trListStock).Where("tr_list_id = ? AND return_date = ?", trList.ID, k). + // Updates(map[string]interface{}{ + // "pass_quantity": v[0], + // "fail_quantity": v[1], + // }) + // } + //} return nil }) }