feat: 添加客户删除前的授权码和许可证检查,更新相关错误提示信息
This commit is contained in:
1 parent
5a763ac7b6
commit
e54dbcb1ae
8 files changed
+80
-23
No files matched your search
@@ -21,6 +21,7 @@ errors:
|
||||
"200003": "Customer name is required"
|
||||
"200004": "Invalid customer information format"
|
||||
"200005": "Failed to query customer list"
|
||||
"200006": "Customer has authorization codes, cannot delete. Please delete authorizations first"
|
||||
|
||||
# Authorization module (30xxxx)
|
||||
authorization:
|
||||
@@ -72,10 +73,10 @@ enums:
|
||||
expired: "Expired"
|
||||
|
||||
customer_level:
|
||||
"normal": "Normal"
|
||||
"vip": "VIP"
|
||||
"enterprise": "Enterprise"
|
||||
"strategic": "Strategic"
|
||||
"basic": "Basic Customer"
|
||||
"active": "Active Customer"
|
||||
"vip": "VIP Customer"
|
||||
"strategic": "Strategic Partner"
|
||||
|
||||
customer_status:
|
||||
"active": "Active"
|
||||
|
||||
@@ -21,6 +21,7 @@ errors:
|
||||
"200003": "顧客名は必須です"
|
||||
"200004": "無効な顧客情報形式"
|
||||
"200005": "顧客リストの照会に失敗しました"
|
||||
"200006": "顧客にまだ認可コードが存在します。削除する前に認可を削除してください"
|
||||
|
||||
# 認可モジュール (30xxxx)
|
||||
authorization:
|
||||
@@ -72,10 +73,10 @@ enums:
|
||||
expired: "期限切れ"
|
||||
|
||||
customer_level:
|
||||
"normal": "一般顧客"
|
||||
"basic": "基本顧客"
|
||||
"active": "アクティブ顧客"
|
||||
"vip": "VIP顧客"
|
||||
"enterprise": "企業顧客"
|
||||
"strategic": "戦略顧客"
|
||||
"strategic": "戦略的パートナー"
|
||||
|
||||
customer_status:
|
||||
"active": "アクティブ"
|
||||
|
||||
@@ -21,6 +21,7 @@ errors:
|
||||
"200003": "客户名称不能为空"
|
||||
"200004": "客户信息格式无效"
|
||||
"200005": "客户列表查询失败"
|
||||
"200006": "客户仍有授权,无法删除,请先删除授权"
|
||||
|
||||
# 授权模块 (30xxxx)
|
||||
authorization:
|
||||
@@ -72,10 +73,10 @@ enums:
|
||||
expired: "已过期"
|
||||
|
||||
customer_level:
|
||||
"normal": "普通客户"
|
||||
"basic": "基础客户"
|
||||
"active": "活跃客户"
|
||||
"vip": "VIP客户"
|
||||
"enterprise": "企业客户"
|
||||
"strategic": "战略客户"
|
||||
"strategic": "战略合作客户"
|
||||
|
||||
customer_status:
|
||||
"active": "激活"
|
||||
|
||||
@@ -293,7 +293,7 @@ func (h *CustomerHandler) UpdateCustomer(c *gin.Context) {
|
||||
|
||||
// DeleteCustomer 删除客户
|
||||
// @Summary 删除客户
|
||||
// @Description 软删除客户记录
|
||||
// @Description 直接删除客户记录,删除前会检查是否有关联的授权码或许可证,如有则提示先删除授权
|
||||
// @Tags 客户管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -303,6 +303,7 @@ func (h *CustomerHandler) UpdateCustomer(c *gin.Context) {
|
||||
// @Failure 400 {object} models.ErrorResponse "请求参数无效"
|
||||
// @Failure 401 {object} models.ErrorResponse "未认证"
|
||||
// @Failure 404 {object} models.ErrorResponse "客户不存在"
|
||||
// @Failure 409 {object} models.ErrorResponse "客户仍有授权,无法删除"
|
||||
// @Failure 500 {object} models.ErrorResponse "服务器内部错误"
|
||||
// @Router /api/customers/{id} [delete]
|
||||
func (h *CustomerHandler) DeleteCustomer(c *gin.Context) {
|
||||
|
||||
@@ -189,9 +189,9 @@ func (r *customerRepository) UpdateCustomer(ctx context.Context, customer *model
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteCustomer 删除客户
|
||||
// DeleteCustomer 删除客户(物理删除)
|
||||
func (r *customerRepository) DeleteCustomer(ctx context.Context, id string) error {
|
||||
result := r.db.Where("id = ?", id).Delete(&models.Customer{})
|
||||
result := r.db.Unscoped().Where("id = ?", id).Delete(&models.Customer{})
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("failed to delete customer: %w", result.Error)
|
||||
}
|
||||
@@ -201,6 +201,26 @@ func (r *customerRepository) DeleteCustomer(ctx context.Context, id string) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckCustomerHasAuthorizationCodes 检查客户是否有关联的授权码
|
||||
func (r *customerRepository) CheckCustomerHasAuthorizationCodes(ctx context.Context, customerID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&models.AuthorizationCode{}).Where("customer_id = ?", customerID).Count(&count).Error
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to check authorization codes: %w", err)
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// CheckCustomerHasLicenses 检查客户是否有关联的许可证
|
||||
func (r *customerRepository) CheckCustomerHasLicenses(ctx context.Context, customerID string) (bool, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&models.License{}).Where("customer_id = ?", customerID).Count(&count).Error
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to check licenses: %w", err)
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
// GetCustomerCount 获取客户总数(用于统计)
|
||||
func (r *customerRepository) GetCustomerCount(ctx context.Context, filters map[string]interface{}) (int64, error) {
|
||||
var count int64
|
||||
|
||||
@@ -20,11 +20,17 @@ type CustomerRepository interface {
|
||||
// UpdateCustomer 更新客户信息
|
||||
UpdateCustomer(ctx context.Context, customer *models.Customer) error
|
||||
|
||||
// DeleteCustomer 删除客户
|
||||
// DeleteCustomer 删除客户(物理删除)
|
||||
DeleteCustomer(ctx context.Context, id string) error
|
||||
|
||||
// GetCustomerCount 获取客户总数(用于统计)
|
||||
GetCustomerCount(ctx context.Context, filters map[string]interface{}) (int64, error)
|
||||
|
||||
// CheckCustomerHasAuthorizationCodes 检查客户是否有关联的授权码
|
||||
CheckCustomerHasAuthorizationCodes(ctx context.Context, customerID string) (bool, error)
|
||||
|
||||
// CheckCustomerHasLicenses 检查客户是否有关联的许可证
|
||||
CheckCustomerHasLicenses(ctx context.Context, customerID string) (bool, error)
|
||||
}
|
||||
|
||||
// UserRepository 用户数据访问接口
|
||||
|
||||
@@ -88,7 +88,7 @@ func (s *customerService) fillDisplayFields(item *models.CustomerListItem, lang
|
||||
// CreateCustomer 创建客户
|
||||
func (s *customerService) CreateCustomer(ctx context.Context, req *models.CustomerCreateRequest) (*models.Customer, error) {
|
||||
lang := pkgcontext.GetLanguageFromContext(ctx)
|
||||
|
||||
|
||||
// 业务逻辑:参数验证
|
||||
if req == nil {
|
||||
return nil, i18n.NewI18nError("900001", lang)
|
||||
@@ -122,7 +122,7 @@ func (s *customerService) CreateCustomer(ctx context.Context, req *models.Custom
|
||||
if strings.Contains(err.Error(), "duplicate") || strings.Contains(err.Error(), "unique") {
|
||||
return nil, i18n.NewI18nError("200002", lang) // 客户已存在
|
||||
}
|
||||
|
||||
|
||||
// 数据库相关错误
|
||||
return nil, i18n.NewI18nError("900004", lang, err.Error())
|
||||
}
|
||||
@@ -136,7 +136,7 @@ func (s *customerService) CreateCustomer(ctx context.Context, req *models.Custom
|
||||
// UpdateCustomer 更新客户信息
|
||||
func (s *customerService) UpdateCustomer(ctx context.Context, id string, req *models.CustomerUpdateRequest) (*models.Customer, error) {
|
||||
lang := pkgcontext.GetLanguageFromContext(ctx)
|
||||
|
||||
|
||||
// 业务逻辑:参数验证
|
||||
if id == "" {
|
||||
return nil, i18n.NewI18nError("900001", lang)
|
||||
@@ -191,7 +191,7 @@ func (s *customerService) UpdateCustomer(ctx context.Context, id string, req *mo
|
||||
if req.Description != nil {
|
||||
existingCustomer.Description = req.Description
|
||||
}
|
||||
|
||||
|
||||
// 设置更新者
|
||||
existingCustomer.UpdatedBy = ¤tUserID
|
||||
|
||||
@@ -206,16 +206,43 @@ func (s *customerService) UpdateCustomer(ctx context.Context, id string, req *mo
|
||||
return existingCustomer, nil
|
||||
}
|
||||
|
||||
// DeleteCustomer 删除客户
|
||||
// DeleteCustomer 删除客户(物理删除,删除前检查是否有授权)
|
||||
func (s *customerService) DeleteCustomer(ctx context.Context, id string) error {
|
||||
lang := pkgcontext.GetLanguageFromContext(ctx)
|
||||
|
||||
|
||||
// 业务逻辑:参数验证
|
||||
if id == "" {
|
||||
return i18n.NewI18nError("900001", lang)
|
||||
}
|
||||
|
||||
// 委托给Repository层进行数据删除
|
||||
// 检查客户是否存在
|
||||
_, err := s.customerRepo.GetCustomerByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrCustomerNotFound) {
|
||||
return i18n.NewI18nError("200001", lang)
|
||||
}
|
||||
return i18n.NewI18nError("900004", lang, err.Error())
|
||||
}
|
||||
|
||||
// 检查是否有关联的授权码
|
||||
hasAuthCodes, err := s.customerRepo.CheckCustomerHasAuthorizationCodes(ctx, id)
|
||||
if err != nil {
|
||||
return i18n.NewI18nError("900004", lang, err.Error())
|
||||
}
|
||||
if hasAuthCodes {
|
||||
return i18n.NewI18nError("200006", lang)
|
||||
}
|
||||
|
||||
// 检查是否有关联的许可证
|
||||
hasLicenses, err := s.customerRepo.CheckCustomerHasLicenses(ctx, id)
|
||||
if err != nil {
|
||||
return i18n.NewI18nError("900004", lang, err.Error())
|
||||
}
|
||||
if hasLicenses {
|
||||
return i18n.NewI18nError("200006", lang)
|
||||
}
|
||||
|
||||
// 委托给Repository层进行物理删除
|
||||
if err := s.customerRepo.DeleteCustomer(ctx, id); err != nil {
|
||||
if errors.Is(err, repository.ErrCustomerNotFound) {
|
||||
return i18n.NewI18nError("200001", lang)
|
||||
@@ -229,7 +256,7 @@ func (s *customerService) DeleteCustomer(ctx context.Context, id string) error {
|
||||
// UpdateCustomerStatus 更新客户状态
|
||||
func (s *customerService) UpdateCustomerStatus(ctx context.Context, id string, req *models.CustomerStatusUpdateRequest) (*models.Customer, error) {
|
||||
lang := pkgcontext.GetLanguageFromContext(ctx)
|
||||
|
||||
|
||||
// 业务逻辑:参数验证
|
||||
if id == "" {
|
||||
return nil, i18n.NewI18nError("900001", lang)
|
||||
|
||||
@@ -108,7 +108,7 @@ func getHTTPStatusByCode(code string) int {
|
||||
return StatusForbidden
|
||||
case "900002", "200001", "300001": // 资源不存在
|
||||
return StatusNotFound
|
||||
case "900003", "200002", "300004": // 资源冲突
|
||||
case "900003", "200002", "200006", "300004": // 资源冲突
|
||||
return StatusConflict
|
||||
case "900004": // 服务器内部错误
|
||||
return StatusInternalServerError
|
||||
|
||||
Reference in new issue
Block a user