order.go 11.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
package domain

import (
	"fmt"
	"time"
)

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

// 订单状态
const (
	ORDER_STATUS_UNSHIPPED = iota + 1 // 待发货
	ORDER_STATUS_TRANSIT              // 待收货(运输中)
	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 int `json:"isDisable"`
	// 订单编号
	OrderNo string `json:"orderNo"`
	// 订单详情
	OrderDetail *OrderDetail `json:"orderDetail"`
	// 订单分红信息
	OrderDividend *Dividend `json:"orderDividend"`
	// 订单分红支付状态
	OrderDividendStatus int `json:"orderDividendStatus"`
	// 订单货品
	OrderGoods []*Goods `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 // 更新订单
	Checkout(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.(int)
	}
	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.([]*Goods)
	}
	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)
	}
	if createAt, ok := data["createAt"]; ok {
		order.CreateAt = createAt.(time.Time)
	}
	if updateAt, ok := data["updateAt"]; ok {
		order.UpdateAt = updateAt.(time.Time)
	}
	if deleteAt, ok := data["deleteAt"]; ok {
		order.DeleteAt = deleteAt.(time.Time)
	}
	return nil
}

// 发货
func (status *UnShippedStatus) Checkout(order *Order) error {
	currentTime := time.Now()
	order.DeliveryTime = currentTime
	if order.OrderType == ORDER_TYPE_INTENTION {
		order.OrderStatus = ORDER_STATUS_TRANSIT
		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("待发货订单不能收货")
}

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

func (status *TransitStatus) Close(order *Order) error {
	panic("implement me")
}

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

// 待收货订单不能重复发货
func (status *TransitStatus) Checkout(order *Order) 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
}

// 收货状态
type ReceivedStatus struct{}

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

// 发货
func (status *ReceivedStatus) Checkout(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 {
	panic("implement me")
}

// 退货状态
type ReturnedStatus struct{}

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

// 已退货订单不能再次发货
func (status *ReturnedStatus) Checkout(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 {
	panic("implement me")
}

// 取消状态
type CanceledStatus struct{}

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

func (status *CanceledStatus) Checkout(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 {
	panic("implement me")
}

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

func (status *CompletedStatus) Update(order *Order, data map[string]interface{}) error {

	return nil
}

func (status *CompletedStatus) Checkout(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("已完成订单不能收货")
}

// 订单仓储
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) Checkout() error {
	return order.CurrentStatus.Checkout(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)
}