实现多商户履约平台基础

This commit is contained in:
yml2213
2026-07-30 12:02:32 +08:00
parent dde6f2c269
commit 83429456a6
41 changed files with 5487 additions and 504 deletions
+36 -8
View File
@@ -20,6 +20,7 @@ func NewOrderService(db *gorm.DB) *OrderService {
}
type OrderListQuery struct {
MerchantID uint
Page int
Size int
Status string
@@ -27,6 +28,7 @@ type OrderListQuery struct {
}
type CreateOrderInput struct {
MerchantID uint
SkinID uint
DistributorID uint
BuyerName string
@@ -43,6 +45,9 @@ func (s *OrderService) List(q OrderListQuery) ([]model.Order, int64, error) {
q.Size = 20
}
tx := s.db.Model(&model.Order{})
if q.MerchantID != 0 {
tx = tx.Where("merchant_id = ?", q.MerchantID)
}
if q.Status != "" {
tx = tx.Where("status = ?", q.Status)
}
@@ -63,7 +68,10 @@ func (s *OrderService) List(q OrderListQuery) ([]model.Order, int64, error) {
func (s *OrderService) Create(in CreateOrderInput) (*model.Order, error) {
var skin model.Skin
if err := s.db.First(&skin, in.SkinID).Error; err != nil {
if in.MerchantID == 0 {
return nil, errors.New("商户不能为空")
}
if err := s.db.Where("id = ? AND merchant_id = ?", in.SkinID, in.MerchantID).First(&skin).Error; err != nil {
return nil, errors.New("皮肤不存在")
}
if skin.Status != 1 {
@@ -72,6 +80,15 @@ func (s *OrderService) Create(in CreateOrderInput) (*model.Order, error) {
if skin.Stock == 0 {
return nil, errors.New("库存不足")
}
var memberCount int64
if err := s.db.Model(&model.MerchantMember{}).
Where("merchant_id = ? AND user_id = ? AND status = ?", in.MerchantID, in.DistributorID, 1).
Count(&memberCount).Error; err != nil {
return nil, err
}
if memberCount == 0 {
return nil, errors.New("分销商不属于当前商户")
}
status := model.OrderStatusPending
switch in.Status {
@@ -89,6 +106,7 @@ func (s *OrderService) Create(in CreateOrderInput) (*model.Order, error) {
}
order := &model.Order{
MerchantID: in.MerchantID,
OrderNo: generateOrderNo(),
SkinID: in.SkinID,
DistributorID: in.DistributorID,
@@ -121,7 +139,7 @@ func (s *OrderService) Create(in CreateOrderInput) (*model.Order, error) {
return order, nil
}
func (s *OrderService) UpdateStatus(id uint, status string) error {
func (s *OrderService) UpdateStatus(merchantID, id uint, status string) error {
allowed := map[string]bool{
model.OrderStatusPending: true,
model.OrderStatusPaid: true,
@@ -133,7 +151,7 @@ func (s *OrderService) UpdateStatus(id uint, status string) error {
if !allowed[status] {
return errors.New("无效的订单状态")
}
res := s.db.Model(&model.Order{}).Where("id = ?", id).Update("status", status)
res := s.db.Model(&model.Order{}).Where("id = ? AND merchant_id = ?", id, merchantID).Update("status", status)
if res.Error != nil {
return res.Error
}
@@ -381,6 +399,7 @@ func (s *OrderService) appendShipLog(order *model.Order, in ShipNotifyInput, res
payload = string(b)
}
log := &model.ShipLog{
MerchantID: order.MerchantID,
OrderNo: order.OrderNo,
OrderID: order.ID,
ShipStatus: in.ShipStatus,
@@ -394,6 +413,7 @@ func (s *OrderService) appendShipLog(order *model.Order, in ShipNotifyInput, res
}
type ShipLogListQuery struct {
MerchantID uint
Page int
Size int
OrderNo string
@@ -408,6 +428,9 @@ func (s *OrderService) ListShipLogs(q ShipLogListQuery) ([]model.ShipLog, int64,
q.Size = 20
}
tx := s.db.Model(&model.ShipLog{})
if q.MerchantID != 0 {
tx = tx.Where("merchant_id = ?", q.MerchantID)
}
if q.OrderNo != "" {
tx = tx.Where("order_no LIKE ?", "%"+q.OrderNo+"%")
}
@@ -432,16 +455,21 @@ type DashboardStats struct {
PendingOrderCount int64 `json:"pending_order_count"`
}
func (s *OrderService) Dashboard() (*DashboardStats, error) {
func (s *OrderService) Dashboard(merchantID uint) (*DashboardStats, error) {
stats := &DashboardStats{}
s.db.Model(&model.Skin{}).Count(&stats.SkinCount)
s.db.Model(&model.User{}).Where("role = ?", model.RoleDistributor).Count(&stats.DistributorCount)
s.db.Model(&model.Order{}).Count(&stats.OrderCount)
s.db.Model(&model.Order{}).Where("status = ?", model.OrderStatusPending).Count(&stats.PendingOrderCount)
s.db.Model(&model.Skin{}).Where("merchant_id = ?", merchantID).Count(&stats.SkinCount)
s.db.Model(&model.User{}).
Joins("JOIN merchant_members ON merchant_members.user_id = users.id").
Where("merchant_members.merchant_id = ? AND users.role = ?", merchantID, model.RoleDistributor).
Count(&stats.DistributorCount)
s.db.Model(&model.Order{}).Where("merchant_id = ?", merchantID).Count(&stats.OrderCount)
s.db.Model(&model.Order{}).Where("merchant_id = ? AND status = ?", merchantID, model.OrderStatusPending).Count(&stats.PendingOrderCount)
s.db.Model(&model.Order{}).
Where("merchant_id = ?", merchantID).
Where("status IN ?", []string{model.OrderStatusPaid, model.OrderStatusDelivered}).
Select("COALESCE(SUM(amount),0)").Scan(&stats.TotalSales)
s.db.Model(&model.Order{}).
Where("merchant_id = ?", merchantID).
Where("status IN ?", []string{model.OrderStatusPaid, model.OrderStatusDelivered}).
Select("COALESCE(SUM(commission_amt),0)").Scan(&stats.TotalCommission)
return stats, nil