|
|
|
package order
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
|
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 订单详情
|
|
|
|
func Detail(header *protocol.RequestHeader, request *protocol.OrderDetailRequest) (rsp *protocol.OrderDetailResponse, err error) {
|
|
|
|
var (
|
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
|
OrderResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
|
|
|
|
OrderDao, _ = factory.CreateOrderBaseDao(transactionContext)
|
|
|
|
order *domain.OrderBase
|
|
|
|
)
|
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
transactionContext.RollbackTransaction()
|
|
|
|
}()
|
|
|
|
|
|
|
|
rsp = &protocol.OrderDetailResponse{}
|
|
|
|
if order, err = OrderResponsitory.FindOne(utils.ObjectJsonToMap(request)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rsp.Order = protocol.OrderDetailVO{
|
|
|
|
Id: order.Id,
|
|
|
|
OrderNo: order.OrderCode,
|
|
|
|
DeliveryNo: order.DeliveryCode,
|
|
|
|
OrderStatus: orderStatus(order),
|
|
|
|
CreateTime: order.CreateTime.Unix() * 1000,
|
|
|
|
UpdateTime: order.UpdateTime.Unix() * 1000,
|
|
|
|
OrderDistrict: map[string]interface{}{"id": order.RegionInfo.RegionId, "name": order.RegionInfo.RegionName},
|
|
|
|
Customer: protocol.Customer{Uname: order.Buyer.BuyerName, Phone: order.Buyer.ContactInfo},
|
|
|
|
Product: orderProducts(order),
|
|
|
|
Total: orderTotalStatic(order),
|
|
|
|
}
|
|
|
|
if header.UserId == order.PartnerId && order.UpdateTime.After(order.LastViewTime) {
|
|
|
|
if err = OrderDao.UpdateLastViewTime(order.Id, time.Now()); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func orderStatus(order *domain.OrderBase) int {
|
|
|
|
var hasBonus = false
|
|
|
|
for i := range order.OrderGood {
|
|
|
|
good := order.OrderGood[i]
|
|
|
|
if good.BonusStatus == domain.BonusPaid {
|
|
|
|
hasBonus = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasBonus {
|
|
|
|
if order.UseOrderCount > 0 && order.UseOrderCount < order.PlanOrderCount {
|
|
|
|
return 3 // 已支付退货
|
|
|
|
}
|
|
|
|
return 2 // 已支付分红
|
|
|
|
}
|
|
|
|
if !hasBonus {
|
|
|
|
if order.UseOrderCount > 0 && order.UseOrderCount < order.PlanOrderCount {
|
|
|
|
return 4 // 待支付退货
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
func orderProducts(order *domain.OrderBase) interface{} {
|
|
|
|
var products []map[string]interface{}
|
|
|
|
for i := range order.OrderGood {
|
|
|
|
good := order.OrderGood[i]
|
|
|
|
item := make(map[string]interface{})
|
|
|
|
item["productName"] = good.GoodName
|
|
|
|
item["orderCount"] = good.PlanGoodNumber
|
|
|
|
item["orderAmount"] = good.PlanAmount
|
|
|
|
item["dividendPercent"] = good.PartnerBonusPercent
|
|
|
|
item["dividendReceivable"] = good.PartnerBonusHas
|
|
|
|
item["dividendUnReceive"] = good.PartnerBonusNot
|
|
|
|
item["dividendExpend"] = good.PartnerBonusExpense
|
|
|
|
if len(good.Remark) > 0 {
|
|
|
|
item["orderUpdateReason"] = good.Remark
|
|
|
|
}
|
|
|
|
item["dividendStatus"] = good.Status()
|
|
|
|
if good.Status() > 2 {
|
|
|
|
item["countAdjust"] = good.UseGoodNumber
|
|
|
|
item["amountAdjust"] = good.UseAmount
|
|
|
|
}
|
|
|
|
products = append(products, item)
|
|
|
|
}
|
|
|
|
return products
|
|
|
|
}
|
|
|
|
func orderTotalStatic(order *domain.OrderBase) interface{} {
|
|
|
|
item := make(map[string]interface{})
|
|
|
|
item["totalCount"] = order.PlanOrderCount
|
|
|
|
item["totalAmount"] = order.PlanOrderAmount
|
|
|
|
item["totalDividendReceivable"] = order.OrderTotalBonus()
|
|
|
|
item["totalReceived"] = order.OrderBonusReceive()
|
|
|
|
item["totalUnReceive"] = order.OrderBonusWait()
|
|
|
|
item["totalExpend"] = order.OrderBonusOutstanding()
|
|
|
|
if order.UseOrderCount > 0 {
|
|
|
|
item["totalCountAdjust"] = order.UseOrderCount
|
|
|
|
item["totalAmountAdjust"] = order.UseOrderAmount
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// 订单统计
|
|
|
|
func Statistics(header *protocol.RequestHeader, request *protocol.OrderStatisticsRequest) (rsp *protocol.OrderStatisticsResponse, err error) {
|
|
|
|
var (
|
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
|
OrderDao, _ = factory.CreateOrderBaseDao(transactionContext)
|
|
|
|
)
|
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
transactionContext.RollbackTransaction()
|
|
|
|
}()
|
|
|
|
rsp = &protocol.OrderStatisticsResponse{Statistics: protocol.OrderStatics{}}
|
|
|
|
//if rsp.Statistics.TodayIntentionQuantity, rsp.Statistics.TodayIntentionMoney, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
// BeginTime: utils.GetDayBegin().Unix() * 1000,
|
|
|
|
// EndTime: utils.GetDayEnd().Unix() * 1000,
|
|
|
|
// OrderType: domain.OrderIntention,
|
|
|
|
// PartnerId: header.UserId,
|
|
|
|
//}); err != nil {
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
if rsp.Statistics.TodayRealQuantity, rsp.Statistics.TodayRealMoney, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
BeginTime: utils.GetDayBegin().Unix() * 1000,
|
|
|
|
EndTime: utils.GetDayEnd().Unix() * 1000,
|
|
|
|
OrderType: domain.OrderReal,
|
|
|
|
PartnerId: header.UserId,
|
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if rsp.Statistics.CumulativeQuantity, rsp.Statistics.CumulativeMoney, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
EndTime: time.Now().Unix() * 1000,
|
|
|
|
OrderType: domain.OrderReal,
|
|
|
|
PartnerId: header.UserId,
|
|
|
|
}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 订单列表
|
|
|
|
func List(header *protocol.RequestHeader, request *protocol.OrderListRequest) (rsp *protocol.OrderListResponse, err error) {
|
|
|
|
var (
|
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
|
OrderResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
|
|
|
|
OrderDao, _ = factory.CreateOrderBaseDao(transactionContext)
|
|
|
|
orders []*domain.OrderBase
|
|
|
|
)
|
|
|
|
|
|
|
|
rsp = &protocol.OrderListResponse{
|
|
|
|
List: make([]*protocol.OrderListItem, 0),
|
|
|
|
Statistics: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
transactionContext.RollbackTransaction()
|
|
|
|
}()
|
|
|
|
queryOption := &domain.OrderQueryOption{
|
|
|
|
PartnerId: header.UserId,
|
|
|
|
Limit: request.PageSize,
|
|
|
|
Offset: request.PageSize * request.PageIndex,
|
|
|
|
SortByUpdateTime: domain.DESC,
|
|
|
|
IsDisable: "0",
|
|
|
|
}
|
|
|
|
if request.StartTime > 0 {
|
|
|
|
queryOption.BeginTime = time.Unix(request.StartTime/1000, 0)
|
|
|
|
}
|
|
|
|
if request.EndTime > 0 {
|
|
|
|
queryOption.EndTime = time.Unix(request.EndTime/1000, 0)
|
|
|
|
}
|
|
|
|
queryOption.OrderType = request.OrderType
|
|
|
|
_, orders, _ = OrderResponsitory.Find(utils.ObjectJsonToMap(queryOption))
|
|
|
|
//统计数据
|
|
|
|
if request.PageIndex == 0 {
|
|
|
|
var (
|
|
|
|
partShipmentQuantity, allShipmentQuantity int
|
|
|
|
cumulativeQuantity int
|
|
|
|
)
|
|
|
|
//部分发货的订单数量
|
|
|
|
//partShipmentQuantity, _, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
// EndTime: request.EndTime,
|
|
|
|
// BeginTime: request.StartTime,
|
|
|
|
// OrderType: domain.OrderReal,
|
|
|
|
// PartnerId: header.UserId,
|
|
|
|
// OrderStatus: domain.OrderStatusDeliverSome,
|
|
|
|
//})
|
|
|
|
//全部发货的订单数量
|
|
|
|
allShipmentQuantity, _, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
EndTime: request.EndTime,
|
|
|
|
BeginTime: request.StartTime,
|
|
|
|
OrderType: domain.OrderReal,
|
|
|
|
PartnerId: header.UserId,
|
|
|
|
//OrderStatus: domain.OrderStatusDeliverAll,
|
|
|
|
})
|
|
|
|
//累计实发订单
|
|
|
|
cumulativeQuantity, _, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
EndTime: time.Now().Unix() * 1000,
|
|
|
|
//BeginTime: request.StartTime,
|
|
|
|
OrderType: domain.OrderReal,
|
|
|
|
PartnerId: header.UserId,
|
|
|
|
})
|
|
|
|
//累计意向订单
|
|
|
|
//intentionQuantity, _, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
|
|
|
|
// EndTime: time.Now().Unix() * 1000,
|
|
|
|
// //BeginTime: request.StartTime,
|
|
|
|
// OrderType: domain.OrderIntention,
|
|
|
|
// PartnerId: header.UserId,
|
|
|
|
//})
|
|
|
|
//订单数量
|
|
|
|
//rsp.Statistics["partShipmentQuantity"] = partShipmentQuantity
|
|
|
|
//rsp.Statistics["allShipmentQuantity"] = allShipmentQuantity
|
|
|
|
rsp.Statistics["orderQuantity"] = partShipmentQuantity + allShipmentQuantity //所有订单 = 部分发货 + 已经发货
|
|
|
|
rsp.Statistics["cumulativeQuantity"] = cumulativeQuantity //实发订单 = 部分发货 + 全部发货
|
|
|
|
//rsp.Statistics["intentionQuantity"] = intentionQuantity
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orders) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range orders {
|
|
|
|
rsp.List = append(rsp.List, DomainOrderToOrderListItem(orders[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func DomainOrderToOrderListItem(order *domain.OrderBase) *protocol.OrderListItem {
|
|
|
|
return &protocol.OrderListItem{
|
|
|
|
Id: order.Id,
|
|
|
|
OrderType: order.OrderType,
|
|
|
|
OrderNo: order.OrderCode,
|
|
|
|
DeliveryNo: order.DeliveryCode,
|
|
|
|
OrderAmount: order.OrderAmount(),
|
|
|
|
UpdateTime: order.UpdateTime.Unix() * 1000,
|
|
|
|
MyDividend: order.OrderTotalBonus(),
|
|
|
|
IsRead: order.IsRead(),
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|