func LoadDetails(orders []*model.Order) error { if orders != nil { for _, o := range orders { details, err := orderdao.GetOrderDetails(o.TrackNumber) if err != nil { return err } o.Details = details } } return nil }
// load all suborders, with all details. set to order func LoadSubOrders(order *model.Order) ([]*model.Order, error) { // now := time.Now() suborders, err := orderdao.ListSubOrders(order.TrackNumber) if err != nil { return nil, err } // load all details. cascaded. for _, o := range suborders { details, err := orderdao.GetOrderDetails(o.TrackNumber) if err != nil { return nil, err } o.Details = details } // fmt.Println() return suborders, nil }
func _processingUpdateOrderDetails(order *model.Order) error { if nil == order { return nil // || order.Details == nil || len(order.Details) <= 0 } var createGroup = []*model.OrderDetail{} var updateGroup = []*model.OrderDetail{} var deleteGroup = []*model.OrderDetail{} // 1. load all details; details, err := orderdao.GetOrderDetails(order.TrackNumber) if err != nil { return err } if details == nil || len(details) == 0 { if order.Details != nil { createGroup = order.Details } } else { // normal case, create, update and delete; var deleteWhoIsFalse = make([]bool, len(details)) // make(map[int]bool, len(details)) if order.Details != nil { // find who should create and who need update. for _, d := range order.Details { // Liner find match in details. var find = false for idx2, d2 := range details { // find the matched item. if d.ProductId == d2.ProductId && d.Color == d2.Color && d.Size == d2.Size { // assign id into it; update operation need id. d.Id = d2.Id // if any value changed; If quantity changed to 0, delete it; if d.Quantity != d2.Quantity || d.SellingPrice != d2.SellingPrice || d.Note != d2.Note { if d.Quantity > 0 { updateGroup = append(updateGroup, d) } } // if quantity equails 0, mark as delete; find = true if d.Quantity == 0 { deleteWhoIsFalse[idx2] = false // nothing, just remind it. } else { deleteWhoIsFalse[idx2] = true } break } } if !find { createGroup = append(createGroup, d) // if not found, create this. } } } // -------------------------------------------------------------------------------- fmt.Println(">>>> details and order details:") if nil != details { for _, d := range details { fmt.Println("\tdetails: ", d.OrderTrackNumber, d.Color, d.Size, " = ", d.Quantity, d.SellingPrice) } } if nil != order.Details { for _, d := range order.Details { fmt.Println("\torder details: ", d.OrderTrackNumber, d.Color, d.Size, " = ", d.Quantity, d.SellingPrice) } } fmt.Println(">>>> who is false?") for idx, b := range deleteWhoIsFalse { fmt.Println("\t >> who is false: ", idx, b) } // who will be deleted? for idx, b := range deleteWhoIsFalse { // } i := 0; i < len(details); i++ { if !b { deleteGroup = append(deleteGroup, details[idx]) } } } var debugdetails = false if debugdetails { fmt.Println("\n\n\n--------------------------------------------------------------------------------") fmt.Println("Order Detail Create Group:") if nil != createGroup { for _, d := range createGroup { fmt.Println("\tcreate: ", d.OrderTrackNumber, d.Color, d.Size, " = ", d.Quantity, d.SellingPrice) } } fmt.Println("Order Detail Update Group:") if nil != updateGroup { for _, d := range updateGroup { fmt.Println("\tupdate: ", d.OrderTrackNumber, d.Color, d.Size, " = ", d.Quantity, d.SellingPrice) } } fmt.Println("Order Detail Delete Group:") if nil != deleteGroup { for _, d := range deleteGroup { fmt.Println("\tdelete: ", d.OrderTrackNumber, d.Color, d.Size, " = ", d.Quantity, d.SellingPrice) } } fmt.Println("==========================================================================\n\nDebug Done!") } // final process: create, update, and delete if createGroup != nil { if err := orderdao.CreateOrderDetail(createGroup); err != nil { return err } } if updateGroup != nil { if err := orderdao.BatchUpdateOrderDetail(updateGroup); err != nil { return err } } if deleteGroup != nil { if err := orderdao.DeleteOrderDetails(deleteGroup); err != nil { return err } } return nil }