feat: Add customer order management module including repository, service, and configuration.
This commit is contained in:
1 parent
560adafe27
commit
a1f411639d
2 files changed
+33
-4
No files matched your search
@@ -139,12 +139,17 @@ func (r *cuOrderRepository) Delete(id string) error {
|
|||||||
|
|
||||||
func (r *cuOrderRepository) CheckTrialOrderExists(cuUserID string, currentMonth string) (bool, error) {
|
func (r *cuOrderRepository) CheckTrialOrderExists(cuUserID string, currentMonth string) (bool, error) {
|
||||||
var count int64
|
var count int64
|
||||||
startOfMonth := currentMonth + "-01"
|
startOfMonth := currentMonth + "-01 00:00:00"
|
||||||
endOfMonth := currentMonth + "-31"
|
endOfMonth := currentMonth + "-31 23:59:59"
|
||||||
|
|
||||||
err := r.db.Model(&models.CuOrder{}).
|
err := r.db.Model(&models.CuOrder{}).
|
||||||
Where("cu_user_id = ? AND package_id = ? AND status = ? AND created_at >= ? AND created_at <= ? AND deleted_at IS NULL",
|
Where("cu_user_id = ? AND (status = 'paid' OR status = 'pending') AND created_at >= ? AND created_at <= ? AND deleted_at IS NULL",
|
||||||
cuUserID, "trial", "paid", startOfMonth, endOfMonth).
|
cuUserID, startOfMonth, endOfMonth).
|
||||||
|
// We need to join with package to check package type because package_id might be a UUID, not 'trial'.
|
||||||
|
// But if the order already recorded the type somewhere or if we just assume 'trial' is the package ID?
|
||||||
|
// Actually, let's just use a subquery or join to verify package type is trial.
|
||||||
|
Joins("JOIN packages ON packages.id = cu_orders.package_id").
|
||||||
|
Where("packages.type = ?", string(models.PackageTypeTrial)).
|
||||||
Count(&count).Error
|
Count(&count).Error
|
||||||
|
|
||||||
return count > 0, err
|
return count > 0, err
|
||||||
|
|||||||
@@ -106,6 +106,30 @@ func (s *cuOrderService) CreatePendingOrder(ctx context.Context, cuUserID, custo
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 对于试用版,进行特殊检查
|
||||||
|
if pkgEntity.Type == string(models.PackageTypeTrial) {
|
||||||
|
// 试用版不支持批量购买
|
||||||
|
if req.LicenseCount != 1 {
|
||||||
|
return nil, i18n.NewI18nError("600004", lang) // 试用版不支持批量购买
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查购买时间限制
|
||||||
|
now := time.Now()
|
||||||
|
if now.Day() > 25 {
|
||||||
|
return nil, i18n.NewI18nError("600003", lang) // 试用版购买时间限制
|
||||||
|
}
|
||||||
|
|
||||||
|
// 每个用户每月只能有一个试用版(包括pending的)
|
||||||
|
currentMonth := now.Format("2006-01")
|
||||||
|
exists, err := s.repo.CheckTrialOrderExists(cuUserID, currentMonth)
|
||||||
|
if err != nil {
|
||||||
|
return nil, i18n.NewI18nError("900004", lang, err.Error())
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
return nil, i18n.NewI18nError("600005", lang) // 当月已购买试用版
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 再次校验数量(防御性校验)
|
// 再次校验数量(防御性校验)
|
||||||
if req.LicenseCount > maxDevices {
|
if req.LicenseCount > maxDevices {
|
||||||
return nil, i18n.NewI18nError("601005", lang)
|
return nil, i18n.NewI18nError("601005", lang)
|
||||||
|
|||||||
Reference in new issue
Block a user