优化删除
This commit is contained in:
@@ -26,7 +26,6 @@ const (
|
||||
|
||||
var (
|
||||
ErrQrCodeNotFound = errors.New("二维码不存在")
|
||||
ErrQrCodeCannotDelete = errors.New("已使用的二维码不能删除")
|
||||
ErrQrCodeCannotReenable = errors.New("已发放的二维码不能改回待用")
|
||||
)
|
||||
|
||||
@@ -43,6 +42,11 @@ type BatchCreateQrCodeRequest struct {
|
||||
Items []CreateQrCodeRequest `json:"items" binding:"required,min=1,max=20"`
|
||||
}
|
||||
|
||||
// BatchDeleteQrCodeRequest 批量删除二维码请求
|
||||
type BatchDeleteQrCodeRequest struct {
|
||||
IDs []uint64 `json:"ids" binding:"required,min=1,max=100"`
|
||||
}
|
||||
|
||||
// UpdateQrCodeRequest 更新二维码请求
|
||||
type UpdateQrCodeRequest struct {
|
||||
ImageURL *string `json:"image_url"`
|
||||
@@ -313,7 +317,7 @@ func qrcodeWasIssued(qrcode model.ChatQrCode) bool {
|
||||
return qrcode.Status == QrCodeStatusUsed || qrcode.ConversationID != nil || qrcode.UsedAt != nil
|
||||
}
|
||||
|
||||
// DeleteQrCode 删除二维码(仅未使用的可删除)
|
||||
// DeleteQrCode 删除二维码
|
||||
func (r *Repository) DeleteQrCode(ctx context.Context, id uint64) error {
|
||||
var qrcode model.ChatQrCode
|
||||
if err := r.db.WithContext(ctx).First(&qrcode, id).Error; err != nil {
|
||||
@@ -323,12 +327,20 @@ func (r *Repository) DeleteQrCode(ctx context.Context, id uint64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 已使用的不能删除
|
||||
if qrcode.Status == QrCodeStatusUsed {
|
||||
return ErrQrCodeCannotDelete
|
||||
return r.db.WithContext(ctx).Delete(&qrcode).Error
|
||||
}
|
||||
|
||||
// BatchDeleteQrCodes 批量删除二维码
|
||||
func (r *Repository) BatchDeleteQrCodes(ctx context.Context, ids []uint64) (int64, error) {
|
||||
if len(ids) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).Delete(&qrcode).Error
|
||||
result := r.db.WithContext(ctx).Where("id IN ?", ids).Delete(&model.ChatQrCode{})
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
}
|
||||
return result.RowsAffected, nil
|
||||
}
|
||||
|
||||
// fetchUnusedQrCode 获取一个未使用且未过期的二维码(带行锁)
|
||||
|
||||
Reference in New Issue
Block a user