Ejemplo n.º 1
0
// 创建订单的所有逻辑都在这里
func (s *OrderService) CreateOrder(order *model.Order) (*model.Order, error) {

	// 这个步骤很重要,判断是否订单已经存在了;如果存在了,需要换一个订单号再试;
	if newtn := makeSureOrderTNUnique(order.TrackNumber); newtn > 0 {
		order.TrackNumber = newtn
	}

	for _, detail := range order.Details {
		detail.OrderTrackNumber = order.TrackNumber
	}

	var needUpdateBallance = false // 需要写入Person的累计欠款

	// If order delivery method is `takeaway`, chagne order status to `delivering` and
	// update account ballance; In other situation change status to `toprint`.
	if order.DeliveryMethod == "TakeAway" {
		order.Status = "delivering"
		needUpdateBallance = true
	} else {
		order.Status = "toprint"
	}

	// organize some data
	_processOrderCustomerPrice(order)
	_calculateOrder(order)

	// create order detail into db;
	if order.Details != nil && len(order.Details) > 0 {
		if err := orderdao.CreateOrderDetail(order.Details); err != nil {
			return nil, err
		}
	}

	// create order into db
	// fmt.Println("======================================")
	// fmt.Println("order.CreateTimeis ", order.CreateTime)
	// fmt.Println("======================================")
	if err := orderdao.CreateOrder(order); err != nil {
		return nil, err
	}

	_update_order_accumulated(order) // debug option

	// update account ballance
	if needUpdateBallance {
		Account.UpdateAccountBalance(order.CustomerId, -order.SumOrderPrice(),
			"Create Order", order.TrackNumber)

		// last step: update stocks. 修改累计欠款的时候就修改库存;
		_reduceProductStocks(order.Details)
	}

	return order, nil
}
Ejemplo n.º 2
0
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
}