order.go 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
package domain

import (
	"fmt"
	"time"
)

// 订单类型
const (
	ORDER_TYPE_ACTUAL    = iota + 1 //实际订单
	ORDER_TYPE_INTENTION            // 意向订单
)

// 订单状态
const (
	ORDER_STATUS_UNSHIPPED     = iota + 1 // 待发货
	ORDER_STATUS_SHIPPING                 // 已发货
	ORDER_STATUS_PART_SHIPPING            // 部分发货
	ORDER_STATUS_RECEIVED                 // 已收货
	ORDER_STATUS_RETURN                   // 退换货
	ORDER_STATUS_CANCELED                 // 已取消
	ORDER_STATUS_COMPLETED                // 已完成
	ORDER_STATUS_CLOSED                   // 已关闭
)

// 订单实体
type Order struct {
	// 订单id
	OrderId int64 `json:"orderId"`
	// 买家信息
	Buyer *BuyerInfo `json:"buyer"`
	// 公司Id
	CompanyId int64 `json:"companyId"`
	// 合伙人id
	PartnerId int64 `json:"partnerId"`
	// 发货单号
	DeliveryCode string `json:"deliveryCode"`
	// 发货时间
	DeliveryTime time.Time `json:"deliveryTime"`
	// 是否关闭订单标志
	IsDisable bool `json:"isDisable"`
	// 订单编号
	OrderNo string `json:"orderNo"`
	// 订单详情
	OrderDetail *OrderDetail `json:"orderDetail"`
	// 订单分红信息
	OrderDividend *Dividend `json:"orderDividend"`
	// 订单分红支付状态
	OrderDividendStatus int `json:"orderDividendStatus"`
	// 订单货品
	OrderGoods []*Good `json:"orderGoods"`
	// 订单数据来源
	OrderSource *OrderSource `json:"orderSource"`
	// 订单类型(1.实际订单;2.意向订单)
	OrderType int `json:"orderType"`
	// 订单状态(1.发货;2.收货;3.退货;4.取消;5.完成)
	OrderStatus int `json:"orderStatus"`
	// 订单总价
	TotalPrice float64 `json:"totalPrice"`
	// 订单区域信息
	RegionInfo *RegionInfo `json:"regionInfo"`
	// 备注
	Remarks string `json:"remarks"`
	// 创建时间
	CreateAt time.Time `json:"createAt"`
	// 更新时间
	UpdateAt time.Time `json:"updateAt"`
	// 软删
	DeleteAt time.Time `json:"deleteAt"`
	// 当前状态
	CurrentStatus OrderStatus `json:"-"`
}

// 订单状态机
type OrderStatus interface {
	Update(order *Order, data map[string]interface{}) error // 更新订单
	Shipping(order *Order) error                            // 发货(全部发货,部分发货)
	Return(order *Order) error                              // 退换货
	Cancel(order *Order) error                              // 取消订单
	Receive(order *Order) error                             // 收货
	Close(order *Order) error                               // 关闭订单
}

// 待发货状态
type UnShippedStatus struct{}

// 编辑待发货订单
func (status *UnShippedStatus) Update(order *Order, data map[string]interface{}) error {
	if orderId, ok := data["orderId"]; ok {
		order.OrderId = orderId.(int64)
	}
	if buyerName, ok := data["buyerName"]; ok {
		order.Buyer.BuyerName = buyerName.(string)
	}
	if contactInfo, ok := data["contactInfo"]; ok {
		order.Buyer.ContactInfo = contactInfo.(string)
	}
	if shippingAddress, ok := data["shippingAddress"]; ok {
		order.Buyer.ShippingAddress = shippingAddress.(string)
	}
	if companyId, ok := data["companyId"]; ok {
		order.CompanyId = companyId.(int64)
	}
	if partnerId, ok := data["partnerId"]; ok {
		order.PartnerId = partnerId.(int64)
	}
	if deliveryCode, ok := data["deliveryCode"]; ok {
		order.DeliveryCode = deliveryCode.(string)
	}
	if isDisable, ok := data["isDisable"]; ok {
		order.IsDisable = isDisable.(bool)
	}
	if orderNo, ok := data["orderNo"]; ok {
		order.OrderNo = orderNo.(string)
	}
	if orderDetailId, ok := data["orderDetailId"]; ok {
		order.OrderDetail.OrderDetailId = orderDetailId.(int64)
	}
	if orderNo, ok := data["orderNo"]; ok {
		order.OrderDetail.OrderNo = orderNo.(string)
	}
	if orderNum, ok := data["orderNum"]; ok {
		order.OrderDetail.OrderNum = orderNum.(int)
	}
	if totalPrice, ok := data["totalPrice"]; ok {
		order.OrderDetail.TotalPrice = totalPrice.(float64)
	}
	if adjustedNum, ok := data["adjustedNum"]; ok {
		order.OrderDetail.AdjustedNum = adjustedNum.(int)
	}
	if adjustedTotalPrice, ok := data["adjustedTotalPrice"]; ok {
		order.OrderDetail.AdjustedTotalPrice = adjustedTotalPrice.(float64)
	}
	if planBonus, ok := data["planBonus"]; ok {
		order.OrderDividend.PlanBonus = planBonus.(float64)
	}
	if useBonus, ok := data["useBonus"]; ok {
		order.OrderDividend.UseBonus = useBonus.(float64)
	}
	if bonusHas, ok := data["bonusHas"]; ok {
		order.OrderDividend.BonusHas = bonusHas.(float64)
	}
	if bonusNot, ok := data["bonusNot"]; ok {
		order.OrderDividend.BonusNot = bonusNot.(float64)
	}
	if bonusExpense, ok := data["bonusExpense"]; ok {
		order.OrderDividend.BonusExpense = bonusExpense.(float64)
	}
	if bonusStatus, ok := data["bonusStatus"]; ok {
		order.OrderDividend.BonusStatus = bonusStatus.(int)
	}
	if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
		order.OrderDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
	}
	if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
		order.OrderDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
	}
	if salesmanBonus, ok := data["salesmanBonus"]; ok {
		order.OrderDividend.SalesmanBonus = salesmanBonus.(float64)
	}
	if orderDividendStatus, ok := data["orderDividendStatus"]; ok {
		order.OrderDividendStatus = orderDividendStatus.(int)
	}
	if orderGoods, ok := data["orderGoods"]; ok {
		order.OrderGoods = orderGoods.([]*Good)
	}
	if dataSourceId, ok := data["dataSourceId"]; ok {
		order.OrderSource.DataSourceId = dataSourceId.(int64)
	}
	if platform, ok := data["platform"]; ok {
		order.OrderSource.Platform = platform.(string)
	}
	if createAt, ok := data["createAt"]; ok {
		order.OrderSource.CreateAt = createAt.(time.Time)
	}
	if orderType, ok := data["orderType"]; ok {
		order.OrderType = orderType.(int)
	}
	if orderStatus, ok := data["orderStatus"]; ok {
		order.OrderStatus = orderStatus.(int)
	}
	if totalPrice, ok := data["totalPrice"]; ok {
		order.TotalPrice = totalPrice.(float64)
	}
	if regionName, ok := data["regionName"]; ok {
		order.RegionInfo.RegionName = regionName.(string)
	}
	if remarks, ok := data["remarks"]; ok {
		order.Remarks = remarks.(string)
	}
	order.UpdateAt = time.Now()

	order.ComputeDividend()
	return nil
}

// 发货
func (status *UnShippedStatus) Shipping(order *Order) error {
	currentTime := time.Now()
	order.DeliveryTime = currentTime
	if order.OrderType == ORDER_TYPE_INTENTION {
		order.OrderStatus = ORDER_STATUS_SHIPPING
		order.CurrentStatus = &TransitStatus{}
	}
	return nil
}

// 待发货订单不能退货
func (status *UnShippedStatus) Return(order *Order) error {
	return fmt.Errorf("待发货订单不能退货")
}

// 取消订单
func (status *UnShippedStatus) Cancel(order *Order) error {
	order.CurrentStatus = &CanceledStatus{}
	return nil
}

// 待发货订单不能收货
func (status *UnShippedStatus) Receive(order *Order) error {
	return fmt.Errorf("待发货订单不能收货")
}

// 待收货订单不能关闭
func (status *UnShippedStatus) Close(order *Order) error {
	return fmt.Errorf("待收货订单不能关闭")
}

// 待收货状态
type TransitStatus struct{}

// 待收货订单不能再次发货
func (status *TransitStatus) Shipping(order *Order) error {
	return fmt.Errorf("待收货订单不能重复发货")
}

// 待收货订单不允许编辑
func (status *TransitStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已发货订单不允许编辑")
}

// 退货
func (status *TransitStatus) Return(order *Order) error {
	order.CurrentStatus = &ReturnedStatus{}
	return nil
}

// 待收货订单不能取消
func (status *TransitStatus) Cancel(order *Order) error {
	return fmt.Errorf("待收货订单不能取消")
}

// 收货
func (status *TransitStatus) Receive(order *Order) error {
	order.CurrentStatus = &ReceivedStatus{}
	return nil
}

// 待收货订单不能关闭
func (status *TransitStatus) Close(order *Order) error {
	return fmt.Errorf("待收货订单不能关闭")
}

// 收货状态
type ReceivedStatus struct{}

// 已收货订单不能编辑
func (status *ReceivedStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已收货订单不能编辑")
}

// 已收货订单不能再次收货
func (status *ReceivedStatus) Shipping(order *Order) error {
	return fmt.Errorf("已收货订单不能再次发货")
}

// 退货
func (status *ReceivedStatus) Return(order *Order) error {
	order.CurrentStatus = &ReturnedStatus{}
	return nil
}

// 已取消订单不能再次取消
func (status *ReceivedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已收货订单不能取消")
}

// 已收货订单不能重复收货
func (status *ReceivedStatus) Receive(order *Order) error {
	return fmt.Errorf("已收货订单不能重复收货")
}

// 关闭订单
func (status *ReceivedStatus) Close(order *Order) error {
	order.IsDisable = true
	order.CurrentStatus = &ClosedStatus{}
	return nil
}

// 退货状态
type ReturnedStatus struct{}

// 已退货订单不能编辑
func (status *ReturnedStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已退货订单不允许编辑")
}

// 已收货订单不能再次收货
func (status *ReturnedStatus) Shipping(order *Order) error {
	return fmt.Errorf("已收货订单不能再次发货")
}

// 已退货订单不能再次发货
func (status *ReturnedStatus) Return(order *Order) error {
	return fmt.Errorf("已退货订单不允许再次退货")
}

// 已退货订单不能取消
func (status *ReturnedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已退货订单不能取消")
}

// 已退货订单不能重复守护
func (status *ReturnedStatus) Receive(order *Order) error {
	return fmt.Errorf("已退货订单不能收货")
}

// 关闭已退货订单
func (status *ReturnedStatus) Close(order *Order) error {
	order.IsDisable = true
	order.CurrentStatus = &ClosedStatus{}
	return nil
}

// 取消状态
type CanceledStatus struct{}

// 已取消订单不能编辑
func (status *CanceledStatus) Update(order *Order, dat map[string]interface{}) error {
	return fmt.Errorf("已取消订单不能编辑")
}

// 已取消订单不能发货
func (status *CanceledStatus) Shipping(order *Order) error {
	return fmt.Errorf("已取消订单不能再次发货")
}

// 已取消的订单不能退货
func (status *CanceledStatus) Return(order *Order) error {
	return fmt.Errorf("已取消订单不能退货")
}

// 已取消定案不能再次取消
func (status *CanceledStatus) Cancel(order *Order) error {
	return fmt.Errorf("已取消订单不能再次取消")
}

// 已取消订单不能收货
func (status *CanceledStatus) Receive(order *Order) error {
	return fmt.Errorf("已取消订单不能进行收货")
}

// 关闭订单
func (status *CanceledStatus) Close(order *Order) error {
	order.IsDisable = true
	order.CurrentStatus = &ClosedStatus{}
	return nil
}

// 已完成状态
type CompletedStatus struct{}

// 编辑已完成订单分红
func (status *CompletedStatus) Update(order *Order, data map[string]interface{}) error {
	if planBonus, ok := data["planBonus"]; ok {
		order.OrderDividend.PlanBonus = planBonus.(float64)
	}
	if useBonus, ok := data["useBonus"]; ok {
		order.OrderDividend.UseBonus = useBonus.(float64)
	}
	if bonusHas, ok := data["bonusHas"]; ok {
		order.OrderDividend.BonusHas = bonusHas.(float64)
	}
	if bonusNot, ok := data["bonusNot"]; ok {
		order.OrderDividend.BonusNot = bonusNot.(float64)
	}
	if bonusExpense, ok := data["bonusExpense"]; ok {
		order.OrderDividend.BonusExpense = bonusExpense.(float64)
	}
	if bonusStatus, ok := data["bonusStatus"]; ok {
		order.OrderDividend.BonusStatus = bonusStatus.(int)
	}
	if partnerBonusPercent, ok := data["partnerBonusPercent"]; ok {
		order.OrderDividend.PartnerBonusPercent = partnerBonusPercent.(float64)
	}
	if salesmanBonusPercent, ok := data["salesmanBonusPercent"]; ok {
		order.OrderDividend.SalesmanBonusPercent = salesmanBonusPercent.(float64)
	}
	if salesmanBonus, ok := data["salesmanBonus"]; ok {
		order.OrderDividend.SalesmanBonus = salesmanBonus.(float64)
	}
	if orderDividendStatus, ok := data["orderDividendStatus"]; ok {
		order.OrderDividendStatus = orderDividendStatus.(int)
	}
	return nil
}

// 已完成的订单不能再次发货
func (status *CompletedStatus) Shipping(order *Order) error {
	return fmt.Errorf("已完成订单不能发货")
}

// 已完成订单不能退货
func (status *CompletedStatus) Return(order *Order) error {
	return fmt.Errorf("已完成订单不能退货")
}

// 已完成订单不能取消
func (status *CompletedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已完成订单不能取消")
}

// 已完成订单不能收货
func (status *CompletedStatus) Receive(order *Order) error {
	return fmt.Errorf("已完成订单不能收货")
}

// 关闭已完成订单
func (status *CompletedStatus) Close(order *Order) error {
	order.IsDisable = true
	order.CurrentStatus = &ClosedStatus{}
	return nil
}

// 关闭状态
type ClosedStatus struct{}

// 已关闭订单不能进行编辑
func (status *ClosedStatus) Update(order *Order, data map[string]interface{}) error {
	return fmt.Errorf("已关闭订单不能进行编辑")
}

// 已关闭订单不能发货
func (status *ClosedStatus) Shipping(order *Order) error {
	return fmt.Errorf("已关闭订单不能发货")
}

// 已关闭订单不能退货
func (status *ClosedStatus) Return(order *Order) error {
	return fmt.Errorf("已关闭订单不能退货")
}

// 已关闭订单不能取消
func (status *ClosedStatus) Cancel(order *Order) error {
	return fmt.Errorf("已关闭订单不能取消")
}

// 已关闭订单不能收货
func (status *ClosedStatus) Receive(order *Order) error {
	return fmt.Errorf("已关闭订单不能收货")
}

// 已关闭订单不能再次关闭
func (status *ClosedStatus) Close(order *Order) error {
	return fmt.Errorf("已关闭订单不能再次关闭")
}

// 订单仓储
type OrderRepository interface {
	Save(order *Order) (*Order, error)
	Remove(order *Order) (*Order, error)
	FindOne(queryOptions map[string]interface{}) (*Order, error)
	Find(queryOptions map[string]interface{}) (int64, []*Order, error)
}

func (order *Order) Identify() interface{} {
	if order.OrderId == 0 {
		return nil
	}
	return order.OrderId
}

// 更新订单
func (order *Order) Update(data map[string]interface{}) error {
	return order.CurrentStatus.Update(order, data)
}

// 发货
func (order *Order) Shipping() error {
	return order.CurrentStatus.Shipping(order)
}

// 退货
func (order *Order) Return() error {
	return order.CurrentStatus.Return(order)
}

// 收货
func (order *Order) Receive() error {
	return order.CurrentStatus.Receive(order)
}

// 取消订单
func (order *Order) Cancel() error {
	return order.CurrentStatus.Cancel(order)
}

// 关闭订单
func (order *Order) Close() error {
	return order.CurrentStatus.Close(order)
}

func (order *Order) RefreshOrderDetail() {

}
func (order *Order) ComputeDividend() {
	order.computeGoodsDividend()
	order.computeOrderDividend()
}
func (order *Order) computeOrderDividend() {
	var totalPlanBonus float64
	// 调整后分红
	var totalUseBonus float64
	// 已收分红
	var totalBonusHas float64
	// 未收分红
	var totalBonusNot float64
	// 分红支出
	var totalBonusExpense float64
	for _, v := range order.OrderGoods {
		totalPlanBonus += v.GoodDividend.PlanBonus
		totalUseBonus += v.GoodDividend.UseBonus
		totalBonusHas += v.GoodDividend.BonusHas
		totalBonusNot += v.GoodDividend.BonusNot
		totalBonusExpense += v.GoodDividend.BonusExpense
	}
	order.OrderDividend.PlanBonus = totalPlanBonus
	order.OrderDividend.UseBonus = totalUseBonus
	order.OrderDividend.BonusHas = totalBonusHas
	order.OrderDividend.BonusNot = totalBonusNot
	order.OrderDividend.BonusExpense = totalBonusExpense
}
func (order *Order) computeGoodsDividend() {
	for i := range order.OrderGoods {
		good := order.OrderGoods[i]
		bonus := good.GoodTotalPrice * float64(good.GoodNum) * (order.OrderDividend.PartnerBonusPercent / 100)
		if good.GoodDividend == nil {
			good.GoodDividend = &Dividend{
				PlanBonus:    bonus,
				UseBonus:     -1,
				BonusHas:     0,
				BonusNot:     bonus,
				BonusExpense: 0,
				BonusStatus:  Dividend_Unpaid,
			}
		} else {
			/* 计算预计分红、调整后分红 */
			if bonus != good.GoodDividend.PlanBonus {
				if order.OrderStatus == ORDER_STATUS_UNSHIPPED {
					good.GoodDividend.PlanBonus = bonus
					good.GoodDividend.BonusNot = bonus
				} else {
					expense := good.GoodDividend.PlanBonus - bonus
					if expense <= 0 {
						expense = 0
					}
					good.GoodDividend.UseBonus = bonus
					good.GoodDividend.BonusNot = bonus
					good.GoodDividend.BonusExpense = expense
				}
			}
			/*计算未支付分红*/
			if good.GoodDividend.BonusHas > 0 {
				good.GoodDividend.BonusNot = good.GoodDividend.PlanBonus - good.GoodDividend.BonusHas
			}
		}
	}
}