diff --git a/initialize/migrate/database/02139_invite_gift_days.down.sql b/initialize/migrate/database/02139_invite_gift_days.down.sql new file mode 100644 index 0000000..3203bf9 --- /dev/null +++ b/initialize/migrate/database/02139_invite_gift_days.down.sql @@ -0,0 +1,4 @@ +DELETE +FROM `system` +WHERE `category` = 'invite' + AND `key` = 'GiftDays'; diff --git a/initialize/migrate/database/02139_invite_gift_days.up.sql b/initialize/migrate/database/02139_invite_gift_days.up.sql new file mode 100644 index 0000000..a33752c --- /dev/null +++ b/initialize/migrate/database/02139_invite_gift_days.up.sql @@ -0,0 +1,14 @@ +INSERT INTO `system` (`category`, `key`, `value`, `type`, `desc`, `created_at`, `updated_at`) +SELECT 'invite', + 'GiftDays', + '3', + 'int', + 'Invite gift days', + NOW(3), + NOW(3) +WHERE NOT EXISTS ( + SELECT 1 + FROM `system` + WHERE `category` = 'invite' + AND `key` = 'GiftDays' +); diff --git a/internal/logic/admin/system/updateInviteConfigLogic.go b/internal/logic/admin/system/updateInviteConfigLogic.go index 8e2201f..9a96bcf 100644 --- a/internal/logic/admin/system/updateInviteConfigLogic.go +++ b/internal/logic/admin/system/updateInviteConfigLogic.go @@ -43,11 +43,26 @@ func (l *UpdateInviteConfigLogic) UpdateInviteConfig(req *types.InviteConfig) er fieldName := t.Field(i).Name // Get the field value to string fieldValue := tool.ConvertValueToString(v.Field(i)) - // Update the invite config - err = db.Model(&system.System{}).Where("`category` = 'invite' and `key` = ?", fieldName).Update("value", fieldValue).Error + // Update existing row; if missing (RowsAffected=0), create it to avoid silent config loss. + updateResult := db.Model(&system.System{}). + Where("`category` = 'invite' and `key` = ?", fieldName). + Update("value", fieldValue) + err = updateResult.Error if err != nil { break } + if updateResult.RowsAffected == 0 { + err = db.Create(&system.System{ + Category: "invite", + Key: fieldName, + Value: fieldValue, + Type: getSystemFieldType(v.Field(i)), + Desc: "invite config: " + fieldName, + }).Error + if err != nil { + break + } + } } if err != nil { return err @@ -62,3 +77,24 @@ func (l *UpdateInviteConfigLogic) UpdateInviteConfig(req *types.InviteConfig) er initialize.Invite(l.svcCtx) return nil } + +func getSystemFieldType(field reflect.Value) string { + kind := field.Kind() + if kind == reflect.Ptr && !field.IsNil() { + kind = field.Elem().Kind() + } + switch kind { + case reflect.Bool: + return "bool" + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return "int" + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return "uint" + case reflect.Float32, reflect.Float64: + return "float" + case reflect.String: + return "string" + default: + return "string" + } +} diff --git a/ppanel.json b/ppanel.json index 2b9464f..b08bf4c 100644 --- a/ppanel.json +++ b/ppanel.json @@ -5043,13 +5043,18 @@ "only_first_purchase": { "type": "boolean", "format": "boolean" + }, + "gift_days": { + "type": "integer", + "format": "int64" } }, "title": "GetInviteConfigResponse", "required": [ "forced_invite", "referral_percentage", - "only_first_purchase" + "only_first_purchase", + "gift_days" ] }, "GetListByPageRequest": { @@ -6383,13 +6388,18 @@ "only_first_purchase": { "type": "boolean", "format": "boolean" + }, + "gift_days": { + "type": "integer", + "format": "int64" } }, "title": "UpdateInviteConfigRequest", "required": [ "forced_invite", "referral_percentage", - "only_first_purchase" + "only_first_purchase", + "gift_days" ] }, "UpdateNodeConfigRequest": {